|
@@ -0,0 +1,218 @@
|
|
|
+<?php
|
|
|
+
|
|
|
+namespace App\Repositories;
|
|
|
+
|
|
|
+use App\Models\NoticeRule;
|
|
|
+use App\Traits\PostTrait;
|
|
|
+use Illuminate\Database\QueryException;
|
|
|
+use Dingo\Api\Http\Response;
|
|
|
+use Illuminate\Support\Carbon;
|
|
|
+use Illuminate\Support\Facades\DB;
|
|
|
+use Illuminate\Support\Facades\Log;
|
|
|
+use Illuminate\Support\Facades\Redis;
|
|
|
+use Symfony\Component\HttpKernel\Exception\HttpException;
|
|
|
+use Tymon\JWTAuth\Facades\JWTAuth;
|
|
|
+
|
|
|
+class NoticeRuleRepository
|
|
|
+{
|
|
|
+ use PostTrait;
|
|
|
+ public function __construct(NoticeRule $noticeRule)
|
|
|
+ {
|
|
|
+ $this->noticeRule = $noticeRule;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 通知列表
|
|
|
+ */
|
|
|
+ public function lists($request)
|
|
|
+ {
|
|
|
+ $perPage = isset($request['per_page']) ? $request['per_page'] : 20;
|
|
|
+ $where = [];
|
|
|
+ if(isset($request['title'])){
|
|
|
+ $where[] = ['title', 'like', "%{$request['title']}%"];
|
|
|
+ }
|
|
|
+ $where[] = ['notice_type', 0];
|
|
|
+ return $this->noticeRule
|
|
|
+ ->where($where)
|
|
|
+ ->orderBy('id','desc')
|
|
|
+ ->paginate($perPage);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 创建通知规则
|
|
|
+ */
|
|
|
+ public function create($request)
|
|
|
+ {
|
|
|
+ $noticeUsers = [];
|
|
|
+ $post_type = '';
|
|
|
+ if($request['notice_user_type'] == 1){
|
|
|
+ if(!(isset($request['attribute']) && $request['attribute']) && !(isset($request['category']) && $request['category'])){
|
|
|
+ return Response::create([
|
|
|
+ 'message' => '请选择属性或分类',
|
|
|
+ 'status_code' => 500
|
|
|
+ ]);
|
|
|
+ }
|
|
|
+ if(isset($request['attribute']) && $request['attribute']){
|
|
|
+ $noticeUsers['attribute'] = $request['attribute'];
|
|
|
+ }
|
|
|
+
|
|
|
+ if(isset($request['category']) && $request['category']){
|
|
|
+ $noticeUsers['category'] = array_unique($request['category']);
|
|
|
+ }
|
|
|
+ }elseif($request['notice_user_type'] == 2){
|
|
|
+ if(!(isset($request['uids']) && $request['uids'])){
|
|
|
+ return Response::create([
|
|
|
+ 'message' => '请输入用户uid',
|
|
|
+ 'status_code' => 500
|
|
|
+ ]);
|
|
|
+ }
|
|
|
+ $uids = $noticeGroups = explode('、', $request['uids']);
|
|
|
+ $noticeUsers['uids'] = array_unique($uids);
|
|
|
+ }
|
|
|
+
|
|
|
+ if(isset($request['action_type']) && $request['action_type'] == 'post'){
|
|
|
+ //查看内容的类型
|
|
|
+ $post_type = $this->getPostType($request['action_id']);
|
|
|
+ if(!$post_type){
|
|
|
+ return Response::create([
|
|
|
+ 'message' => '请输入有效内容id',
|
|
|
+ 'status_code' => 500
|
|
|
+ ]);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ $data = [
|
|
|
+ 'notice_type' => 0,
|
|
|
+ 'title' => $request['title'],
|
|
|
+ 'content' => $request['content'],
|
|
|
+ 'notice_user_type' => $request['notice_user_type'],
|
|
|
+ 'notice_users' => json_encode($noticeUsers),
|
|
|
+ 'action_type' => $request['action_type']??'',
|
|
|
+ 'action_id' => $request['action_id']??0,
|
|
|
+ 'post_type' => $post_type,
|
|
|
+ 'cover' => $request['cover']??'',
|
|
|
+ 'notice_status' => 0,
|
|
|
+ 'send_time' => isset($request['send_time']) && $request['send_time']? $request['send_time']:null,
|
|
|
+ 'send_count' => 0,
|
|
|
+ ];
|
|
|
+
|
|
|
+ DB::beginTransaction();
|
|
|
+ try{
|
|
|
+ $this->noticeRule->create($data);
|
|
|
+
|
|
|
+ DB::commit();
|
|
|
+ return Response::create();
|
|
|
+
|
|
|
+ }catch (QueryException $exception){
|
|
|
+ DB::rollBack();
|
|
|
+ Log::debug('创建通知规则失败:'.$exception->getMessage());
|
|
|
+ return Response::create([
|
|
|
+ 'message' => '添加失败,请重试',
|
|
|
+ 'error' => $exception->getMessage(),
|
|
|
+ 'status_code' => 500
|
|
|
+ ]);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 通知规则详情
|
|
|
+ */
|
|
|
+ public function detail($request)
|
|
|
+ {
|
|
|
+ return $this->noticeRule->find($request['id']);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 发送通知规则
|
|
|
+ */
|
|
|
+ public function send($request)
|
|
|
+ {
|
|
|
+ $notice = $this->noticeRule->find($request['id']);
|
|
|
+ if(!$notice || $notice->notice_status != 0){
|
|
|
+ return Response::create([
|
|
|
+ 'message' => '只能发送未发送通知',
|
|
|
+ 'status_code' => 500
|
|
|
+ ]);
|
|
|
+ }
|
|
|
+
|
|
|
+ $notice->send_time = Carbon::now()->toDateTimeString();
|
|
|
+
|
|
|
+ DB::beginTransaction();
|
|
|
+ try{
|
|
|
+ $notice->save();
|
|
|
+
|
|
|
+ DB::commit();
|
|
|
+ return Response::create();
|
|
|
+
|
|
|
+ }catch (QueryException $exception){
|
|
|
+ DB::rollBack();
|
|
|
+ Log::debug('发送通知规则失败:'.$exception->getMessage());
|
|
|
+ return Response::create([
|
|
|
+ 'message' => '发送失败,请重试',
|
|
|
+ 'error' => $exception->getMessage(),
|
|
|
+ 'status_code' => 500
|
|
|
+ ]);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 删除通知
|
|
|
+ */
|
|
|
+ public function delete($request)
|
|
|
+ {
|
|
|
+ $notice = $this->noticeRule->find($request['id']);
|
|
|
+ if(!$notice || $notice->notice_status != 0){
|
|
|
+ return Response::create([
|
|
|
+ 'message' => '只能删除未发送通知',
|
|
|
+ 'status_code' => 500
|
|
|
+ ]);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ DB::beginTransaction();
|
|
|
+ try{
|
|
|
+ $notice->delete();
|
|
|
+
|
|
|
+ DB::commit();
|
|
|
+ return Response::create();
|
|
|
+
|
|
|
+ }catch (QueryException $exception){
|
|
|
+ DB::rollBack();
|
|
|
+ Log::debug('删除通知规则失败:'.$exception->getMessage());
|
|
|
+ return Response::create([
|
|
|
+ 'message' => '删除失败,请重试',
|
|
|
+ 'error' => $exception->getMessage(),
|
|
|
+ 'status_code' => 500
|
|
|
+ ]);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 更新通知规则
|
|
|
+ */
|
|
|
+ public function updateStatus($data)
|
|
|
+ {
|
|
|
+ Log::debug('更新通知规则收到数据:'.json_encode($data));
|
|
|
+ $notice = $this->noticeRule->find($data['id']);
|
|
|
+ if(!$notice || $notice->notice_status != 1){
|
|
|
+ Log::error('更新通知规则状态失败:'.$data['id']);
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ $notice->notice_status = 2;
|
|
|
+ $notice->send_count = $data['num'];
|
|
|
+
|
|
|
+ DB::beginTransaction();
|
|
|
+ try{
|
|
|
+ $notice->save();
|
|
|
+
|
|
|
+ DB::commit();
|
|
|
+ return true;
|
|
|
+
|
|
|
+ }catch (QueryException $exception){
|
|
|
+ DB::rollBack();
|
|
|
+ Log::error('更新通知规则状态:'.$data['id'].$exception->getMessage());
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|