SystemNotice.php 4.2 KB

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