123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215 |
- <?php
- /**
- * Created by PhpStorm.
- * User: Administrator
- * Date: 2019/6/5
- * Time: 16:03
- */
- namespace App\Repositories\Circle;
- use App\Models\InterestCircle;
- use App\Models\InterestCircleMessage;
- use App\Models\InterestCircleMessageComment;
- 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;
- class CircleMessageRepository
- {
- public function __construct(InterestCircle $interestCircle,
- InterestCircleMessage $interestCircleMessage,
- InterestCircleMessageComment $interestCircleMessageComment)
- {
- $this->interestCircle = $interestCircle;
- $this->interestCircleMessage = $interestCircleMessage;
- $this->interestCircleMessageComment = $interestCircleMessageComment;
- }
- /**
- * 圈子留言列表
- */
- public function lists($request)
- {
- $perPage = isset($request['per_page']) ? $request['per_page'] : 20;
- $where = [];
- if (isset($request['circle_id'])) {
- $where[] = ['circle_id', $request['circle_id']];
- }
- if (isset($request['uid'])) {
- $where[] = ['uid', $request['uid']];
- }
- return $this->interestCircleMessage
- ->where($where)
- ->where(function ($query) use ($request) {
- if (isset($request['content'])) {
- $query->Where('content', 'like', "%{$request['content']}%");
- }
- })
- ->where(function ($query) use ($request) {
- if (isset($request['created_at'])) {
- $time = explode('_', $request['created_at']);
- $query->whereBetween('post.created_at', $time);
- }
- })
- ->orderBy('is_recommend', 'desc')
- ->orderBy('created_at', 'desc')
- ->paginate($perPage);
- }
- /**
- * 留言详情
- */
- public function detail($request, $isTrashed = false)
- {
- $model = $this->interestCircleMessage;
- if ($isTrashed) {
- $model->withTrashed();
- }
- return $model->find($request['id']);
- }
- /**
- * 推荐留言
- */
- public function messageRecommend($request)
- {
- $circle = $this->interestCircleMessage
- ->where('id', $request['id'])
- ->where('circle_id', $request['circle_id'])->first();
- if (!$circle) {
- return Response::create([
- 'message' => '获取留言信息失败',
- 'status_code' => 500
- ]);
- }
- if ($circle->is_recommend == 1) {
- $circle->is_recommend = 0;
- } else {
- $circle->is_recommend = 1;
- }
- DB::beginTransaction();
- try {
- $circle->save();
- DB::commit();
- return Response::create();
- } catch (QueryException $exception) {
- DB::rollBack();
- Log::debug('推荐留言:' . $request['id'] . '-' . $request['circle_id'] . $exception->getMessage());
- return Response::create([
- 'message' => '操作失败,请重试',
- 'error' => $exception->getMessage(),
- 'status_code' => 500
- ]);
- }
- }
- /**
- * 删除留言
- */
- public function delete($request)
- {
- $circle = $this->interestCircleMessage
- ->where('id', $request['id'])
- ->where('circle_id', $request['circle_id'])
- ->first();
- if (!$circle) {
- return Response::create([
- 'message' => '获取留言信息失败',
- 'status_code' => 500
- ]);
- }
- DB::beginTransaction();
- try {
- $circle->delete();
- $this->interestCircle->where('id',$request['circle_id'])->decrement('message_count');
- DB::commit();
- return Response::create();
- } catch (QueryException $exception) {
- DB::rollBack();
- Log::debug('删除留言:' . $request['id'] . $exception->getMessage());
- return Response::create([
- 'message' => '操作失败,请重试',
- 'error' => $exception->getMessage(),
- 'status_code' => 500
- ]);
- }
- }
- /**
- * 圈子留言评论列表
- */
- public function commentList($request)
- {
- $perPage = isset($request['per_page']) ? $request['per_page'] : 20;
- $where = [];
- if (isset($request['circle_id'])) {
- $where[] = ['circle_id', $request['circle_id']];
- }
- if (isset($request['msg_id'])) {
- $where[] = ['msg_id', $request['msg_id']];
- }
- return $this->interestCircleMessageComment
- ->where($where)
- ->orderBy('created_at', 'desc')
- ->paginate($perPage);
- }
- /**
- * 删除评论
- */
- public function commentDelete($request)
- {
- $comment = $this->interestCircleMessageComment->find($request['id']);
- if (!$comment) {
- return Response::create([
- 'message' => '获取评论信息失败',
- 'status_code' => 500
- ]);
- }
- if ($comment->is_delete == 1) {
- return Response::create([
- 'message' => '该评论已经删除',
- 'status_code' => 500
- ]);
- }
- DB::beginTransaction();
- try {
- $comment->is_delete = 1;
- $comment->save();
- DB::commit();
- // Redis::SADD('delete_post_comment_ids', $comment->id);
- //
- // if (!$comment->parent_id) {
- // Redis::DEL('post_new_comment_' . $comment->post_id);
- // } else {
- // Redis::DEL('post_new_reply_' . $comment->id);
- // }
- return Response::create();
- } catch (QueryException $exception) {
- DB::rollBack();
- Log::debug('删除评论:' . $request['id'] . $exception->getMessage());
- return Response::create([
- 'message' => '操作失败,请重试',
- 'error' => $exception->getMessage(),
- 'status_code' => 500
- ]);
- }
- }
- }
|