CircleMessageRepository.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Administrator
  5. * Date: 2019/6/5
  6. * Time: 16:03
  7. */
  8. namespace App\Repositories\Circle;
  9. use App\Models\InterestCircle;
  10. use App\Models\InterestCircleMessage;
  11. use App\Models\InterestCircleMessageComment;
  12. use Illuminate\Database\QueryException;
  13. use Dingo\Api\Http\Response;
  14. use Illuminate\Support\Carbon;
  15. use Illuminate\Support\Facades\DB;
  16. use Illuminate\Support\Facades\Log;
  17. use Illuminate\Support\Facades\Redis;
  18. class CircleMessageRepository
  19. {
  20. public function __construct(InterestCircle $interestCircle,
  21. InterestCircleMessage $interestCircleMessage,
  22. InterestCircleMessageComment $interestCircleMessageComment)
  23. {
  24. $this->interestCircle = $interestCircle;
  25. $this->interestCircleMessage = $interestCircleMessage;
  26. $this->interestCircleMessageComment = $interestCircleMessageComment;
  27. }
  28. /**
  29. * 圈子留言列表
  30. */
  31. public function lists($request)
  32. {
  33. $perPage = isset($request['per_page']) ? $request['per_page'] : 20;
  34. $where = [];
  35. if (isset($request['circle_id'])) {
  36. $where[] = ['circle_id', $request['circle_id']];
  37. }
  38. if (isset($request['uid'])) {
  39. $where[] = ['uid', $request['uid']];
  40. }
  41. return $this->interestCircleMessage
  42. ->where($where)
  43. ->where(function ($query) use ($request) {
  44. if (isset($request['content'])) {
  45. $query->Where('content', 'like', "%{$request['content']}%");
  46. }
  47. })
  48. ->where(function ($query) use ($request) {
  49. if (isset($request['created_at'])) {
  50. $time = explode('_', $request['created_at']);
  51. $query->whereBetween('post.created_at', $time);
  52. }
  53. })
  54. ->orderBy('is_recommend', 'desc')
  55. ->orderBy('created_at', 'desc')
  56. ->paginate($perPage);
  57. }
  58. /**
  59. * 留言详情
  60. */
  61. public function detail($request, $isTrashed = false)
  62. {
  63. $model = $this->interestCircleMessage;
  64. if ($isTrashed) {
  65. $model->withTrashed();
  66. }
  67. return $model->find($request['id']);
  68. }
  69. /**
  70. * 推荐留言
  71. */
  72. public function messageRecommend($request)
  73. {
  74. $circle = $this->interestCircleMessage
  75. ->where('id', $request['id'])
  76. ->where('circle_id', $request['circle_id'])->first();
  77. if (!$circle) {
  78. return Response::create([
  79. 'message' => '获取留言信息失败',
  80. 'status_code' => 500
  81. ]);
  82. }
  83. if ($circle->is_recommend == 1) {
  84. $circle->is_recommend = 0;
  85. } else {
  86. $circle->is_recommend = 1;
  87. }
  88. DB::beginTransaction();
  89. try {
  90. $circle->save();
  91. DB::commit();
  92. return Response::create();
  93. } catch (QueryException $exception) {
  94. DB::rollBack();
  95. Log::debug('推荐留言:' . $request['id'] . '-' . $request['circle_id'] . $exception->getMessage());
  96. return Response::create([
  97. 'message' => '操作失败,请重试',
  98. 'error' => $exception->getMessage(),
  99. 'status_code' => 500
  100. ]);
  101. }
  102. }
  103. /**
  104. * 删除留言
  105. */
  106. public function delete($request)
  107. {
  108. $circle = $this->interestCircleMessage
  109. ->where('id', $request['id'])
  110. ->where('circle_id', $request['circle_id'])
  111. ->first();
  112. if (!$circle) {
  113. return Response::create([
  114. 'message' => '获取留言信息失败',
  115. 'status_code' => 500
  116. ]);
  117. }
  118. DB::beginTransaction();
  119. try {
  120. $circle->delete();
  121. $this->interestCircle->where('id',$request['circle_id'])->decrement('message_count');
  122. DB::commit();
  123. return Response::create();
  124. } catch (QueryException $exception) {
  125. DB::rollBack();
  126. Log::debug('删除留言:' . $request['id'] . $exception->getMessage());
  127. return Response::create([
  128. 'message' => '操作失败,请重试',
  129. 'error' => $exception->getMessage(),
  130. 'status_code' => 500
  131. ]);
  132. }
  133. }
  134. /**
  135. * 圈子留言评论列表
  136. */
  137. public function commentList($request)
  138. {
  139. $perPage = isset($request['per_page']) ? $request['per_page'] : 20;
  140. $where = [];
  141. if (isset($request['circle_id'])) {
  142. $where[] = ['circle_id', $request['circle_id']];
  143. }
  144. if (isset($request['msg_id'])) {
  145. $where[] = ['msg_id', $request['msg_id']];
  146. }
  147. return $this->interestCircleMessageComment
  148. ->where($where)
  149. ->orderBy('created_at', 'desc')
  150. ->paginate($perPage);
  151. }
  152. /**
  153. * 删除评论
  154. */
  155. public function commentDelete($request)
  156. {
  157. $comment = $this->interestCircleMessageComment->find($request['id']);
  158. if (!$comment) {
  159. return Response::create([
  160. 'message' => '获取评论信息失败',
  161. 'status_code' => 500
  162. ]);
  163. }
  164. if ($comment->is_delete == 1) {
  165. return Response::create([
  166. 'message' => '该评论已经删除',
  167. 'status_code' => 500
  168. ]);
  169. }
  170. DB::beginTransaction();
  171. try {
  172. $comment->is_delete = 1;
  173. $comment->save();
  174. DB::commit();
  175. // Redis::SADD('delete_post_comment_ids', $comment->id);
  176. //
  177. // if (!$comment->parent_id) {
  178. // Redis::DEL('post_new_comment_' . $comment->post_id);
  179. // } else {
  180. // Redis::DEL('post_new_reply_' . $comment->id);
  181. // }
  182. return Response::create();
  183. } catch (QueryException $exception) {
  184. DB::rollBack();
  185. Log::debug('删除评论:' . $request['id'] . $exception->getMessage());
  186. return Response::create([
  187. 'message' => '操作失败,请重试',
  188. 'error' => $exception->getMessage(),
  189. 'status_code' => 500
  190. ]);
  191. }
  192. }
  193. }