NoticeRuleRepository.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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. if(isset($request['notice_type'])){
  31. $where[] = ['notice_type', $request['notice_type']];
  32. }
  33. return $this->noticeRule
  34. ->where($where)
  35. ->orderBy('id','desc')
  36. ->paginate($perPage);
  37. }
  38. /**
  39. * 创建消息规则
  40. */
  41. public function create($request)
  42. {
  43. $noticeUsers = [];
  44. $post_type = '';
  45. if($request['notice_user_type'] == 1){
  46. if(!(isset($request['attribute']) && $request['attribute']) && !(isset($request['category']) && $request['category'])){
  47. return Response::create([
  48. 'message' => '请选择属性或分类',
  49. 'status_code' => 500
  50. ]);
  51. }
  52. if(isset($request['attribute']) && $request['attribute']){
  53. $noticeUsers['attribute'] = $request['attribute'];
  54. }
  55. if(isset($request['category']) && $request['category']){
  56. $noticeUsers['category'] = array_unique($request['category']);
  57. }
  58. }elseif($request['notice_user_type'] == 2){
  59. if(!(isset($request['uids']) && $request['uids'])){
  60. return Response::create([
  61. 'message' => '请输入用户uid',
  62. 'status_code' => 500
  63. ]);
  64. }
  65. $uids = $noticeGroups = explode('、', $request['uids']);
  66. $noticeUsers['uids'] = array_unique($uids);
  67. }
  68. if(isset($request['action_type']) && $request['action_type'] == 'post'){
  69. //查看内容的类型
  70. $post_type = $this->getPostType($request['action_id']);
  71. if(!$post_type){
  72. return Response::create([
  73. 'message' => '请输入有效内容id',
  74. 'status_code' => 500
  75. ]);
  76. }
  77. }
  78. $data = [
  79. 'notice_type' => $request['notice_type'],
  80. 'title' => $request['title'],
  81. 'content' => $request['content'],
  82. 'notice_user_type' => $request['notice_user_type'],
  83. 'notice_users' => json_encode($noticeUsers),
  84. 'action_type' => $request['action_type']??'',
  85. 'action_id' => $request['action_id']??0,
  86. 'post_type' => $post_type,
  87. 'cover' => $request['cover']??'',
  88. 'notice_status' => 0,
  89. 'send_time' => isset($request['send_time']) && $request['send_time']? $request['send_time']:null,
  90. 'send_count' => 0,
  91. ];
  92. DB::beginTransaction();
  93. try{
  94. $this->noticeRule->create($data);
  95. DB::commit();
  96. return Response::create();
  97. }catch (QueryException $exception){
  98. DB::rollBack();
  99. Log::debug('创建通知规则失败:'.$exception->getMessage());
  100. return Response::create([
  101. 'message' => '添加失败,请重试',
  102. 'error' => $exception->getMessage(),
  103. 'status_code' => 500
  104. ]);
  105. }
  106. }
  107. /**
  108. * 编辑消息规则
  109. */
  110. public function update($request)
  111. {
  112. $message = $this->messageRule->find($request['id']);
  113. if(!$message || $message->message_status != 0){
  114. return Response::create([
  115. 'message' => '只能编辑未发送消息',
  116. 'status_code' => 500
  117. ]);
  118. }
  119. $noticeGroups = explode(',', $request['notice_groups']);
  120. if(in_array(0, $noticeGroups)){
  121. $noticeGroups = 0;
  122. } else {
  123. if(array_diff($noticeGroups, [1,2])){
  124. return Response::create([
  125. 'message' => '通知群体参数有误',
  126. 'status_code' => 500
  127. ]);
  128. }
  129. $noticeGroups = implode(',', array_unique($noticeGroups));
  130. }
  131. $message->title = $request['title'];
  132. $message->notice_groups = $noticeGroups;
  133. $message->message_type = $request['message_type'];
  134. if(isset($request['cover'])){
  135. $message->cover = $request['cover'];
  136. }
  137. if(isset($request['activity_url'])){
  138. $message->activity_url = $request['activity_url'];
  139. }
  140. if(isset($request['message_show_type'])){
  141. $message->message_show_type = $request['message_show_type'];
  142. }
  143. if(isset($request['content'])){
  144. $message->content = $request['content'];
  145. }
  146. if(isset($request['send_time'])){
  147. if($request['send_time']){
  148. $message->send_time = $request['send_time'];
  149. }else{
  150. $message->send_time = null;
  151. }
  152. }
  153. if(isset($request['activity_time'])){
  154. $message->activity_time = $request['activity_time'];
  155. }
  156. DB::beginTransaction();
  157. try{
  158. $message->save();
  159. DB::commit();
  160. return Response::create();
  161. }catch (QueryException $exception){
  162. DB::rollBack();
  163. Log::debug('编辑消息规则:'.$exception->getMessage());
  164. return Response::create([
  165. 'message' => '编辑失败,请重试',
  166. 'error' => $exception->getMessage(),
  167. 'status_code' => 500
  168. ]);
  169. }
  170. }
  171. /**
  172. * 消息规则详情
  173. */
  174. public function detail($request)
  175. {
  176. return $this->messageRule->find($request['id']);
  177. }
  178. /**
  179. * 发送消息规则
  180. */
  181. public function send($request)
  182. {
  183. $message = $this->messageRule->find($request['id']);
  184. if(!$message || $message->message_status != 0){
  185. return Response::create([
  186. 'message' => '只能发送未发送消息',
  187. 'status_code' => 500
  188. ]);
  189. }
  190. $message->send_time = Carbon::now()->toDateTimeString();
  191. DB::beginTransaction();
  192. try{
  193. $message->save();
  194. DB::commit();
  195. return Response::create();
  196. }catch (QueryException $exception){
  197. DB::rollBack();
  198. Log::debug('发送消息规则:'.$exception->getMessage());
  199. return Response::create([
  200. 'message' => '发送失败,请重试',
  201. 'error' => $exception->getMessage(),
  202. 'status_code' => 500
  203. ]);
  204. }
  205. }
  206. /**
  207. * 隐藏消息规则
  208. */
  209. public function hide($request)
  210. {
  211. $message = $this->messageRule->find($request['id']);
  212. if(!$message || $message->message_status != 2){
  213. return Response::create([
  214. 'message' => '只能隐藏已发送消息',
  215. 'status_code' => 500
  216. ]);
  217. }
  218. $message->message_status = 3;
  219. DB::beginTransaction();
  220. try{
  221. $message->save();
  222. DB::commit();
  223. return Response::create();
  224. }catch (QueryException $exception){
  225. DB::rollBack();
  226. Log::debug('隐藏消息规则:'.$exception->getMessage());
  227. return Response::create([
  228. 'message' => '隐藏失败,请重试',
  229. 'error' => $exception->getMessage(),
  230. 'status_code' => 500
  231. ]);
  232. }
  233. }
  234. /**
  235. * 更新消息规则
  236. */
  237. public function updateStatus($data)
  238. {
  239. Log::debug('更新消息规则收到数据:'.json_encode($data));
  240. $message = $this->messageRule->find($data['id']);
  241. if(!$message || $message->message_status != 1){
  242. Log::error('更新消息规则状态失败:'.$data['id']);
  243. return false;
  244. }
  245. $message->message_status = 2;
  246. $message->sent_count = $data['num'];
  247. DB::beginTransaction();
  248. try{
  249. $message->save();
  250. DB::commit();
  251. return true;
  252. }catch (QueryException $exception){
  253. DB::rollBack();
  254. Log::error('更新消息规则状态:'.$data['id'].$exception->getMessage());
  255. return false;
  256. }
  257. }
  258. }