SystemNotice.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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')
  92. ->join('system_notice_admin B', 'B.notice_type = A.type')
  93. ->where('A.status', 1)
  94. ->where('B.is_visit', 0)
  95. ->where('B.is_click', 0)
  96. ->where('B.admin_id', $adminId)
  97. ->field('count(B.id) total')
  98. ->group('A.id')
  99. ->select()
  100. ->toArray();
  101. if (!$list) return 0;
  102. return array_reduce($list, function ($initial, $res) {
  103. return $initial + $res['total'];
  104. }, 0);
  105. }
  106. public static function getAdminNotice($adminId)
  107. {
  108. $list = self::alias('A')
  109. ->join('system_notice_admin B', 'B.notice_type = A.type')
  110. ->where('A.status', 1)
  111. ->where('B.is_visit', 0)
  112. ->where('B.is_click', 0)
  113. ->where('B.admin_id', $adminId)
  114. ->field('A.id,A.type,A.title,A.icon,count(B.id) total,A.template,max(B.add_time) as last_time')
  115. ->group('A.id')
  116. ->having('total > 0')
  117. ->select()
  118. ->toArray();
  119. $noticeTypeList = [];
  120. array_walk($list, function (&$notice) use (&$noticeTypeList) {
  121. $notice['message'] = sprintf($notice['template'], $notice['total']);
  122. $noticeTypeList[] = $notice['type'];
  123. });
  124. if (count($noticeTypeList))
  125. self::systemNoticeAdminDb()->where('notice_type', 'IN', $noticeTypeList)->where('admin_id', $adminId)
  126. ->update(['is_visit' => 1, 'visit_time' => time()]);
  127. return $list;
  128. }
  129. }