SystemNotice.php 3.9 KB

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