NoticesService.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. <?php
  2. /**
  3. * ${CARET}
  4. * @file:NoitceService.php
  5. * @Created by gnitif
  6. * @Date: 2023/3/27
  7. * @Time: 11:54
  8. */
  9. namespace Modules\System\Services\Notice;
  10. use Catch\Exceptions\FailedException;
  11. use Illuminate\Database\Eloquent\Model;
  12. use Illuminate\Support\Facades\Auth;
  13. use Illuminate\Support\Facades\DB;
  14. use Modules\Permissions\Models\Roles;
  15. use Modules\System\Models\Notices;
  16. use Modules\System\Models\UserNotice;
  17. use Modules\User\Models\User;
  18. class NoticesService
  19. {
  20. protected static function getModel()
  21. {
  22. return new Notices();
  23. }
  24. /**
  25. * 添加通知
  26. * name: addNotice
  27. * @param array $param
  28. * $param = [
  29. * 'title' => '测试', // 通知标题
  30. * 'notice_type_id' => 2, // 通知分类id
  31. * 'type' => '2', // 通知人群 1全部 2,指定人,3指定角色
  32. * 'notice_obj' => [['id' => 1,'name' => "超管"]] , // 通知对象
  33. * 'is_popup' => '1', // 是否是弹窗 1弹窗 0 普通
  34. * 'content' => '312312', // 通知内容
  35. * ];
  36. *
  37. * date 2023/03/29 14:25
  38. */
  39. public static function addNotice(array $param)
  40. {
  41. if ($param['type'] != 1 && (!isset($param['notice_obj']) || empty($param['notice_obj']))) {
  42. throw new FailedException('通知对象不能为空!');
  43. }
  44. if ($param['type'] == 3) {
  45. $roleIds = array_column($param['notice_obj'], 'id');
  46. $userIds = DB::table('user_has_roles')->whereIn('role_id', $roleIds)->pluck('user_id')->toArray();
  47. } else if ($param['type'] == 2) {
  48. $userIds = array_column($param['notice_obj'], 'id');
  49. } else {
  50. $userIds = User::pluck('id')->toArray();
  51. $param['notice_obj'] = [];
  52. }
  53. $param['user_ids'] = $userIds;
  54. return self::getModel()->storeBy($param);
  55. }
  56. public static function delete($id)
  57. {
  58. return self::getModel()->updateBy($id, ['is_deleted' => 1, 'deleted_at' => get_date()]);
  59. }
  60. /**
  61. * 获取通知详情
  62. * name: getDetail
  63. * @param $id
  64. * @return \Illuminate\Database\Eloquent\Collection|\Illuminate\Support\Collection|Notices[]
  65. * date 2023/03/29 15:12
  66. */
  67. public static function getDetail($id)
  68. {
  69. $info = Notices::leftJoin('notice_types', 'notice_types.id', 'notices.notice_type_id')
  70. ->select('notices.*', 'notice_types.name as notice_type_name')->where('notices.id', $id)->first();
  71. return $info;
  72. }
  73. /**
  74. * 更新
  75. * name: update
  76. * @param $id
  77. * @param array $param
  78. * date 2023/03/29 18:32
  79. */
  80. public static function update($id, array $param)
  81. {
  82. return self::getModel()->updateBy($id, $param);
  83. }
  84. /**
  85. * 管理员操作通知列表
  86. * name: list
  87. * @return mixed
  88. * date 2023/03/29 22:49
  89. */
  90. public static function list()
  91. {
  92. $list = self::getModel()->setBeforeGetList(function ($query) {
  93. return $query->where('is_deleted', 0)->orderBy('created_at','desc');
  94. })->getList();
  95. if (!$list->isEmpty()) {
  96. $cate = NoitceTypeService::list([], true);
  97. if ($cate->isEmpty()) {
  98. $cate = [];
  99. } else {
  100. $cate = array_column($cate->toArray(), null, 'id');
  101. }
  102. foreach ($list as $value) {
  103. $value->notice_type_txt = $cate[$value->notice_type_id]['name'] ?? "";
  104. $value->type_txt = $value->type == 1 ? "全部" : ($value->type == 2 ? "指定用户" : "指定角色");
  105. }
  106. }
  107. return $list;
  108. }
  109. /**
  110. * 我的通知
  111. * name: myNoticesList
  112. * date 2023/03/29 22:49
  113. */
  114. public static function myNoticesList($param = [])
  115. {
  116. $type = $param['type'] ?? "";
  117. $noticeTypeId = $param['notice_type_id'] ?? 0;
  118. $title = $param['title'] ?? "";
  119. $pageSize = $param['limit'] ?? 0;
  120. $pageSize = $pageSize < 1 ? 15 : $pageSize;
  121. $userId = Auth::guard(getGuardName())->id();
  122. $where = [
  123. ['user_notice.is_deleted', '=', 0],
  124. ['user_notice.user_id', '=', $userId],
  125. ['notices.is_deleted', '=', 0],
  126. ['user_notice.is_deleted', '=', 0],
  127. ];
  128. if ($type) {
  129. $where[] = ['notices.type', '=', $type];
  130. }
  131. if ($noticeTypeId) {
  132. $where[] = ['notices.notice_type_id', '=', $noticeTypeId];
  133. }
  134. if ($title) {
  135. $where[] = ['notices.title', 'like', "%" . $title . "%"];
  136. }
  137. $list = DB::table('user_notice')->leftJoin('notices', 'notices.id', "user_notice.notice_id")->where($where)->select('notices.id', 'notices.title', 'notices.is_popup', "user_notice.is_read", 'notices.created_at')
  138. ->orderBy('notices.created_at', 'desc')->orderBy('sort', 'desc')->paginate($pageSize);
  139. if (!$list->isEmpty()) {
  140. foreach ($list as $val) {
  141. $val->is_read_txt = $val->is_read == 1 ? "已读" : "未读";
  142. }
  143. }
  144. return $list;
  145. }
  146. /**
  147. * 设置已读
  148. * name: setRead
  149. * @param $id
  150. * date 2023/03/29 23:51
  151. */
  152. public static function setRead($id)
  153. {
  154. $userId = Auth::guard(getGuardName())->id();
  155. return UserNotice::where('user_id', $userId)->where('notice_id', $id)->update(['is_read' => 1, 'read_at' => get_date()]);
  156. }
  157. /**
  158. * 用户删除
  159. * name: userDel
  160. * @param $id
  161. * date 2023/03/29 23:55
  162. */
  163. public static function userDel($id)
  164. {
  165. $userId = Auth::guard(getGuardName())->id();
  166. return UserNotice::where('user_id', $userId)->where('notice_id', $id)->update(['is_deleted' => 1, 'deleted_at' => get_date()]);
  167. }
  168. /**
  169. * 阅读详情
  170. * name: detail
  171. * @param $id
  172. * @return Notices
  173. * date 2023/03/30 00:09
  174. */
  175. public static function detail($id)
  176. {
  177. return Notices::where('id', $id)->where('is_deleted', 0)->select('title', 'id', 'is_popup', 'content')->first();
  178. }
  179. /**
  180. * 获取指定对象选择项
  181. * name: objOption
  182. * @param $type
  183. * @param string $name
  184. * @return array
  185. * date 2023/03/30 10:23
  186. */
  187. public static function objOption($type, $name = ""): mixed
  188. {
  189. if ($type == 'user') {
  190. if ($name) {
  191. return User::where("username", 'like', "%" . $name . "%")->without(['roles','jobs'])->select('id','username as name')->get();
  192. }
  193. return User::select('id','username as name')->without(['roles','jobs'])->get();
  194. } else if ($type == "role") {
  195. if ($name) {
  196. return Roles::where("role_name", 'like', "%" . $name . "%")->select('id','role_name as name')->get();
  197. }
  198. return Roles::select('id','role_name as name')->get();
  199. }
  200. return [];
  201. }
  202. /**
  203. * 一条获取弹窗信息
  204. * name: getPopup
  205. * @return Model|UserNotice|object|null
  206. * date 2023/03/30 16:45
  207. */
  208. public static function getPopup()
  209. {
  210. $where = [
  211. ['user_notice.is_deleted', '=', 0],
  212. ['user_notice.user_id', '=', Auth::guard(getGuardName())->id()],
  213. ['notices.is_deleted', '=', 0],
  214. ['notices.is_popup', '=', 1],
  215. ['user_notice.is_read', '=', 0],
  216. ];
  217. $info = UserNotice::leftJoin('notices', 'notices.id', "user_notice.notice_id")->where($where)->select('notices.id', 'notices.title', 'notices.content')
  218. ->orderBy('notices.created_at', 'desc')->orderBy('sort', 'desc')->first();
  219. if (empty($info)){
  220. return [];
  221. }
  222. return $info;
  223. }
  224. }