NoticesService.php 7.6 KB

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