SysMsgLog.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * Author: 芸众商城 www.yunzshop.com
  5. * Date: 2017/4/12
  6. * Time: 下午1:38
  7. */
  8. namespace app\common\models\systemMsg;
  9. use app\common\helpers\Cache;
  10. use app\common\models\BaseModel;
  11. use app\common\services\SystemMsgService;
  12. /**
  13. * Class SysMsgLog
  14. * @package app\common\models\systemMsg
  15. */
  16. class SysMsgLog extends BaseModel
  17. {
  18. protected $table = 'yz_sys_msg_log';
  19. protected $guarded = [];
  20. protected $appends = ['belongs_to_type'];
  21. public function setRedirectParamAttribute($value)
  22. {
  23. $this->attributes['redirect_param'] = json_encode($value);
  24. }
  25. public function getRedirectParamAttribute($value)
  26. {
  27. return json_decode($value,true);
  28. }
  29. public function getRedirectUrlAttribute($value)
  30. {
  31. return SystemMsgService::$url[$value]?yzWebUrl(SystemMsgService::$url[$value],$this->redirect_param):$value;
  32. }
  33. public function getBelongsToTypeAttribute()
  34. {
  35. return collect(SystemMsgService::$msg_type)->where('id',$this->type_id)->first();
  36. }
  37. // /**
  38. // * 获取该消息所属的消息类型。
  39. // */
  40. // public function belongsToType()
  41. // {
  42. // return $this->belongsTo('app\common\models\systemMsg\SysMsgType','type_id');
  43. // }
  44. public static function getLogList($type_id = 0,$search = [])
  45. {
  46. $model = self::uniacid()->selectRaw('id,uniacid,type_id,title,content,redirect_url,redirect_param,is_read,created_at,read_at');
  47. if(!empty($type_id)){
  48. $model->where('type_id',$type_id);
  49. }
  50. if(isset($search['is_read']) && is_numeric($search['is_read']) && in_array($search['is_read'],[0,1]))
  51. {
  52. $model->where('is_read',$search['is_read']);
  53. }
  54. if(!empty($search['time'])){
  55. $range = [$search['time']['start'], $search['time']['end']];
  56. $model->whereBetween('created_at', $range);
  57. }
  58. return $model;
  59. }
  60. //获取总共未读的数量
  61. public static function getLogCount()
  62. {
  63. if(Cache::has('sys_msg_count')){
  64. $count = Cache::get('sys_msg_count');
  65. }else{
  66. $count = self::uniacid()->where('is_read',0)->count();
  67. Cache::put('sys_msg_count',$count,0.2);
  68. }
  69. return $count;
  70. }
  71. }