SystemNotice.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. /**
  3. * @author: xaboy<365615158@qq.com>
  4. * @day: 2017/11/02
  5. */
  6. namespace app\admin\model\system;
  7. use crmeb\traits\ModelTrait;
  8. use crmeb\basic\BaseModel;
  9. use think\facade\Db;
  10. /**
  11. * 后台通知model
  12. * Class SystemNotice
  13. * @package app\admin\model\system
  14. */
  15. class SystemNotice extends BaseModel
  16. {
  17. /**
  18. * 数据表主键
  19. * @var string
  20. */
  21. protected $pk = 'id';
  22. /**
  23. * 模型名称
  24. * @var string
  25. */
  26. protected $name = 'system_notice';
  27. use ModelTrait;
  28. protected function setResultAttr($value)
  29. {
  30. return json_encode($value);
  31. }
  32. protected function setTableTitleAttr($value)
  33. {
  34. $list = [];
  35. if(!empty($value)){
  36. $group = explode(',',$value);
  37. $list = array_map(function($v){
  38. list($title,$key) = explode('-',$v);
  39. return compact('title','key');
  40. },$group);
  41. }
  42. return json_encode($list);
  43. }
  44. protected function getTableTitleAttr($value)
  45. {
  46. return json_decode($value,true);
  47. }
  48. protected function getResultAttr($value)
  49. {
  50. return json_decode($value,true);
  51. }
  52. protected function setPushAdminAttr($value)
  53. {
  54. $value = is_array($value) ? array_unique(array_filter($value)) : [];
  55. return implode(',',$value);
  56. }
  57. protected function getPushAdminAttr($value)
  58. {
  59. return array_filter(explode(',',$value));
  60. }
  61. public static function typeByAdminList($type,$field = 'id')
  62. {
  63. return self::where('type',$type)->field($field)->find();
  64. }
  65. public static function systemNoticeAdminDb()
  66. {
  67. return Db::name('SystemNoticeAdmin');
  68. }
  69. public static function adminMessage($notice_type,$admin_id,$link_id,array $table_data = [])
  70. {
  71. $table_data = json_encode($table_data);
  72. $add_time = time();
  73. return self::systemNoticeAdminDb()->insert(compact('notice_type','admin_id','link_id','table_data','add_time'));
  74. }
  75. public static function noticeMessage($noticeType,$linkId,array $tableData = [])
  76. {
  77. $noticeInfo = self::get(['type'=>$noticeType]);
  78. if(!$noticeInfo) return self::setErrorInfo('通知模板消息不存在!');
  79. $adminIds = array_merge(array_map(function($v){
  80. return $v['id'];
  81. },SystemAdmin::getTopAdmin('id')->toArray())?:[],self::typeByAdminList($noticeType,'push_admin')->push_admin?:[]);
  82. $adminIds = array_unique(array_filter($adminIds));
  83. if(!count($adminIds)) return self::setErrorInfo('没有有效的通知用户!');
  84. foreach ($adminIds as $id){
  85. self::adminMessage($noticeType,$id,$linkId,$tableData);
  86. }
  87. return true;
  88. }
  89. public static function getAdminNoticeTotal($adminId)
  90. {
  91. $list = self::alias('A')->join('__SYSTEM_NOTICE_ADMIN__ B','B.notice_type = A.type')
  92. ->where('A.status',1)->where('B.is_visit',0)->where('B.is_click',0)->where('B.admin_id',$adminId)
  93. ->field('count(B.id) total')->group('A.id')->select()->toArray();
  94. if(!$list) return 0;
  95. return array_reduce($list,function($initial,$res){
  96. return $initial+$res['total'];
  97. },0);
  98. }
  99. public static function getAdminNotice($adminId)
  100. {
  101. $list = self::alias('A')->join('__SYSTEM_NOTICE_ADMIN__ B','B.notice_type = A.type')
  102. ->where('A.status',1)->where('B.is_visit',0)->where('B.is_click',0)->where('B.admin_id',$adminId)
  103. ->field('A.id,A.type,A.title,A.icon,count(B.id) total,A.template,max(B.add_time) as last_time')
  104. ->group('A.id')->having('total > 0')->select()->toArray();
  105. $noticeTypeList = [];
  106. array_walk($list,function(&$notice) use(&$noticeTypeList){
  107. $notice['message'] = sprintf($notice['template'],$notice['total']);
  108. $noticeTypeList[] = $notice['type'];
  109. });
  110. if(count($noticeTypeList))
  111. self::systemNoticeAdminDb()->where('notice_type','IN',$noticeTypeList)->where('admin_id',$adminId)
  112. ->update(['is_visit'=>1,'visit_time'=>time()]);
  113. return $list;
  114. }
  115. }