NoticeRuleRepository.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. <?php
  2. namespace App\Repositories;
  3. use App\Models\NoticeRule;
  4. use App\Traits\PostTrait;
  5. use Illuminate\Database\QueryException;
  6. use Dingo\Api\Http\Response;
  7. use Illuminate\Support\Carbon;
  8. use Illuminate\Support\Facades\DB;
  9. use Illuminate\Support\Facades\Log;
  10. use Illuminate\Support\Facades\Redis;
  11. use Symfony\Component\HttpKernel\Exception\HttpException;
  12. use Tymon\JWTAuth\Facades\JWTAuth;
  13. class NoticeRuleRepository
  14. {
  15. use PostTrait;
  16. public function __construct(NoticeRule $noticeRule)
  17. {
  18. $this->noticeRule = $noticeRule;
  19. }
  20. /**
  21. * 通知列表
  22. */
  23. public function lists($request)
  24. {
  25. $perPage = isset($request['per_page']) ? $request['per_page'] : 20;
  26. $where = [];
  27. if(isset($request['title'])){
  28. $where[] = ['title', 'like', "%{$request['title']}%"];
  29. }
  30. $where[] = ['notice_type', 0];
  31. return $this->noticeRule
  32. ->where($where)
  33. ->orderBy('id','desc')
  34. ->paginate($perPage);
  35. }
  36. /**
  37. * 创建通知规则
  38. */
  39. public function create($request)
  40. {
  41. $noticeUsers = [];
  42. $post_type = '';
  43. if($request['notice_user_type'] == 1){
  44. if(!(isset($request['attribute']) && $request['attribute']) && !(isset($request['category']) && $request['category'])){
  45. return Response::create([
  46. 'message' => '请选择属性或分类',
  47. 'status_code' => 500
  48. ]);
  49. }
  50. if(isset($request['attribute']) && $request['attribute']){
  51. $noticeUsers['attribute'] = $request['attribute'];
  52. }
  53. if(isset($request['category']) && $request['category']){
  54. $noticeUsers['category'] = array_unique($request['category']);
  55. }
  56. }elseif($request['notice_user_type'] == 2){
  57. if(!(isset($request['uids']) && $request['uids'])){
  58. return Response::create([
  59. 'message' => '请输入用户uid',
  60. 'status_code' => 500
  61. ]);
  62. }
  63. $uids = $noticeGroups = explode('、', $request['uids']);
  64. $noticeUsers['uids'] = array_unique($uids);
  65. }
  66. if(isset($request['action_type']) && $request['action_type'] == 'post'){
  67. //查看内容的类型
  68. $post_type = $this->getPostType($request['action_id']);
  69. if(!$post_type){
  70. return Response::create([
  71. 'message' => '请输入有效内容id',
  72. 'status_code' => 500
  73. ]);
  74. }
  75. }
  76. $data = [
  77. 'notice_type' => 0,
  78. 'title' => $request['title'],
  79. 'content' => $request['content'],
  80. 'notice_user_type' => $request['notice_user_type'],
  81. 'notice_users' => json_encode($noticeUsers),
  82. 'action_type' => $request['action_type']??'',
  83. 'action_id' => $request['action_id']??0,
  84. 'post_type' => $post_type,
  85. 'cover' => $request['cover']??'',
  86. 'notice_status' => 0,
  87. 'send_time' => isset($request['send_time']) && $request['send_time']? $request['send_time']:null,
  88. 'send_count' => 0,
  89. ];
  90. DB::beginTransaction();
  91. try{
  92. $this->noticeRule->create($data);
  93. DB::commit();
  94. return Response::create();
  95. }catch (QueryException $exception){
  96. DB::rollBack();
  97. Log::debug('创建通知规则失败:'.$exception->getMessage());
  98. return Response::create([
  99. 'message' => '添加失败,请重试',
  100. 'error' => $exception->getMessage(),
  101. 'status_code' => 500
  102. ]);
  103. }
  104. }
  105. /**
  106. * 通知规则详情
  107. */
  108. public function detail($request)
  109. {
  110. return $this->noticeRule->find($request['id']);
  111. }
  112. /**
  113. * 发送通知规则
  114. */
  115. public function send($request)
  116. {
  117. $notice = $this->noticeRule->find($request['id']);
  118. if(!$notice || $notice->notice_status != 0){
  119. return Response::create([
  120. 'message' => '只能发送未发送通知',
  121. 'status_code' => 500
  122. ]);
  123. }
  124. $notice->send_time = Carbon::now()->toDateTimeString();
  125. DB::beginTransaction();
  126. try{
  127. $notice->save();
  128. DB::commit();
  129. return Response::create();
  130. }catch (QueryException $exception){
  131. DB::rollBack();
  132. Log::debug('发送通知规则失败:'.$exception->getMessage());
  133. return Response::create([
  134. 'message' => '发送失败,请重试',
  135. 'error' => $exception->getMessage(),
  136. 'status_code' => 500
  137. ]);
  138. }
  139. }
  140. /**
  141. * 删除通知
  142. */
  143. public function delete($request)
  144. {
  145. $notice = $this->noticeRule->find($request['id']);
  146. if(!$notice || $notice->notice_status != 0){
  147. return Response::create([
  148. 'message' => '只能删除未发送通知',
  149. 'status_code' => 500
  150. ]);
  151. }
  152. DB::beginTransaction();
  153. try{
  154. $notice->delete();
  155. DB::commit();
  156. return Response::create();
  157. }catch (QueryException $exception){
  158. DB::rollBack();
  159. Log::debug('删除通知规则失败:'.$exception->getMessage());
  160. return Response::create([
  161. 'message' => '删除失败,请重试',
  162. 'error' => $exception->getMessage(),
  163. 'status_code' => 500
  164. ]);
  165. }
  166. }
  167. /**
  168. * 更新通知规则
  169. */
  170. public function updateStatus($data)
  171. {
  172. Log::debug('更新通知规则收到数据:'.json_encode($data));
  173. $notice = $this->noticeRule->find($data['id']);
  174. if(!$notice || $notice->notice_status != 1){
  175. Log::error('更新通知规则状态失败:'.$data['id']);
  176. return false;
  177. }
  178. $notice->notice_status = 2;
  179. $notice->send_count = $data['num'];
  180. DB::beginTransaction();
  181. try{
  182. $notice->save();
  183. DB::commit();
  184. return true;
  185. }catch (QueryException $exception){
  186. DB::rollBack();
  187. Log::error('更新通知规则状态:'.$data['id'].$exception->getMessage());
  188. return false;
  189. }
  190. }
  191. }