SystemNotice.php 4.4 KB

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