CircleMessageRepository.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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. DB::commit();
  122. return Response::create();
  123. } catch (QueryException $exception) {
  124. DB::rollBack();
  125. Log::debug('删除留言:' . $request['id'] . $exception->getMessage());
  126. return Response::create([
  127. 'message' => '操作失败,请重试',
  128. 'error' => $exception->getMessage(),
  129. 'status_code' => 500
  130. ]);
  131. }
  132. }
  133. /**
  134. * 圈子留言评论列表
  135. */
  136. public function commentList($request)
  137. {
  138. $perPage = isset($request['per_page']) ? $request['per_page'] : 20;
  139. $where = [];
  140. if (isset($request['circle_id'])) {
  141. $where[] = ['circle_id', $request['circle_id']];
  142. }
  143. if (isset($request['msg_id'])) {
  144. $where[] = ['msg_id', $request['msg_id']];
  145. }
  146. return $this->interestCircleMessageComment
  147. ->where($where)
  148. ->orderBy('created_at', 'desc')
  149. ->paginate($perPage);
  150. }
  151. /**
  152. * 删除评论
  153. */
  154. public function commentDelete($request)
  155. {
  156. $comment = $this->interestCircleMessageComment->find($request['id']);
  157. if (!$comment) {
  158. return Response::create([
  159. 'message' => '获取评论信息失败',
  160. 'status_code' => 500
  161. ]);
  162. }
  163. if ($comment->is_delete == 1) {
  164. return Response::create([
  165. 'message' => '该评论已经删除',
  166. 'status_code' => 500
  167. ]);
  168. }
  169. DB::beginTransaction();
  170. try {
  171. $comment->is_delete = 1;
  172. $comment->save();
  173. DB::commit();
  174. // Redis::SADD('delete_post_comment_ids', $comment->id);
  175. //
  176. // if (!$comment->parent_id) {
  177. // Redis::DEL('post_new_comment_' . $comment->post_id);
  178. // } else {
  179. // Redis::DEL('post_new_reply_' . $comment->id);
  180. // }
  181. return Response::create();
  182. } catch (QueryException $exception) {
  183. DB::rollBack();
  184. Log::debug('删除评论:' . $request['id'] . $exception->getMessage());
  185. return Response::create([
  186. 'message' => '操作失败,请重试',
  187. 'error' => $exception->getMessage(),
  188. 'status_code' => 500
  189. ]);
  190. }
  191. }
  192. }