CircleRepository.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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\InterestCirclePicture;
  11. use Illuminate\Database\QueryException;
  12. use Dingo\Api\Http\Response;
  13. use Illuminate\Support\Carbon;
  14. use Illuminate\Support\Facades\DB;
  15. use Illuminate\Support\Facades\Log;
  16. use Illuminate\Support\Facades\Redis;
  17. class CircleRepository
  18. {
  19. public function __construct(InterestCircle $interestCircle, InterestCirclePicture $interestCirclePicture)
  20. {
  21. $this->interestCircle = $interestCircle;
  22. $this->interestCirclePicture = $interestCirclePicture;
  23. }
  24. /**
  25. * 创建圈子
  26. */
  27. public function create($request)
  28. {
  29. $data = [
  30. 'name' => $request['name'],
  31. 'notice' => $request['notice'],
  32. 'image' => $request['image'] ?? '',
  33. 'join_limit' => $request['join_limit'],
  34. 'limit_condition' => $request['limit_condition'],
  35. 'join_question' => json_encode($request['join_question']) ?? '',
  36. 'limit_article_ids' => $request['limit_article_ids'] ?? '',
  37. 'contains_function' => json_encode($request['contains_function']) ?? '',
  38. ];
  39. DB::beginTransaction();
  40. try {
  41. $circle = $this->interestCircle->create($data);
  42. DB::commit();
  43. return Response::create();
  44. } catch (QueryException $exception) {
  45. DB::rollBack();
  46. Log::debug('创建圈子exception:' . $exception->getMessage());
  47. return Response::create([
  48. 'message' => '创建失败,请重试',
  49. 'error' => $exception->getMessage(),
  50. 'status_code' => 500
  51. ]);
  52. }
  53. }
  54. /**
  55. * 修改圈子
  56. */
  57. public function update($request)
  58. {
  59. $data = [
  60. 'name' => $request['name'],
  61. 'notice' => $request['notice'],
  62. 'image' => $request['image'] ?? '',
  63. 'join_limit' => $request['join_limit'],
  64. 'limit_condition' => $request['limit_condition'],
  65. 'join_question' => json_encode($request['join_question']),
  66. 'limit_article_ids' => $request['limit_article_ids'] ?? '',
  67. 'contains_function' => json_encode($request['contains_function']),
  68. ];
  69. DB::beginTransaction();
  70. try {
  71. $circle = $this->interestCircle->where('id', $request['id'])->update($data);
  72. DB::commit();
  73. return Response::create();
  74. } catch (QueryException $exception) {
  75. DB::rollBack();
  76. Log::debug('修改圈子exception:' . $exception->getMessage());
  77. return Response::create([
  78. 'message' => '修改失败,请重试',
  79. 'error' => $exception->getMessage(),
  80. 'status_code' => 500
  81. ]);
  82. }
  83. }
  84. /**
  85. * 圈子列表
  86. */
  87. public function circleLists($request)
  88. {
  89. $perPage = isset($request['per_page']) ? $request['per_page'] : 20;
  90. $where = [];
  91. if (isset($request['is_open'])) {
  92. $where[] = ['is_open', $request['is_open']];
  93. }
  94. return $this->interestCircle
  95. ->where($where)
  96. ->where(function ($query) use ($request) {
  97. if (isset($request['keyword'])) {
  98. $query->Where('name', 'like', "%{$request['keyword']}%");
  99. }
  100. })
  101. ->orderBy('is_recommend', 'desc')
  102. ->orderBy('created_at', 'desc')
  103. ->paginate($perPage);
  104. }
  105. /**
  106. * 圈子详情
  107. */
  108. public function detail($request, $isTrashed = false)
  109. {
  110. $model = $this->interestCircle;
  111. if ($isTrashed) {
  112. $model->withTrashed();
  113. }
  114. return $model->find($request['id']);
  115. }
  116. /**
  117. * 推荐圈子
  118. */
  119. public function circleRecommend($request)
  120. {
  121. $circle = $this->interestCircle->where('id', $request['id'])->first();
  122. if (!$circle) {
  123. return Response::create([
  124. 'message' => '获取圈子信息失败',
  125. 'status_code' => 500
  126. ]);
  127. }
  128. if ($circle->is_recommend == 1) {
  129. $circle->is_recommend = 0;
  130. } else {
  131. $circle->is_recommend = 1;
  132. }
  133. DB::beginTransaction();
  134. try {
  135. $circle->save();
  136. DB::commit();
  137. return Response::create();
  138. } catch (QueryException $exception) {
  139. DB::rollBack();
  140. Log::debug('推荐圈子:' . $request['id'] . $exception->getMessage());
  141. return Response::create([
  142. 'message' => '操作失败,请重试',
  143. 'error' => $exception->getMessage(),
  144. 'status_code' => 500
  145. ]);
  146. }
  147. }
  148. /**
  149. * 开启/关闭圈子
  150. */
  151. public function circleStatus($request)
  152. {
  153. $circle = $this->interestCircle->where('id', $request['id'])->first();
  154. if (!$circle) {
  155. return Response::create([
  156. 'message' => '获取圈子信息失败',
  157. 'status_code' => 500
  158. ]);
  159. }
  160. if ($circle->is_open == 1) {
  161. $circle->is_open = 0;
  162. } else {
  163. $circle->is_open = 1;
  164. }
  165. DB::beginTransaction();
  166. try {
  167. $circle->save();
  168. DB::commit();
  169. return Response::create();
  170. } catch (QueryException $exception) {
  171. DB::rollBack();
  172. Log::debug('操作圈子:' . $request['id'] . $exception->getMessage());
  173. return Response::create([
  174. 'message' => '操作失败,请重试',
  175. 'error' => $exception->getMessage(),
  176. 'status_code' => 500
  177. ]);
  178. }
  179. }
  180. /**
  181. * 相册列表
  182. */
  183. public function pictureLists($request)
  184. {
  185. $perPage = isset($request['per_page']) ? $request['per_page'] : 20;
  186. $where = [];
  187. if (isset($request['circle_id'])) {
  188. $where[] = ['circle_id', $request['circle_id']];
  189. }
  190. if (isset($request['uid'])) {
  191. $where[] = ['uid', $request['uid']];
  192. }
  193. return $this->interestCirclePicture
  194. ->where($where)
  195. ->orderBy('created_at', 'desc')
  196. ->paginate($perPage);
  197. }
  198. /**
  199. * 删除相册图片
  200. */
  201. public function deletePicture($request)
  202. {
  203. $circle = $this->interestCirclePicture->where('id', $request['id'])->first();
  204. if (!$circle) {
  205. return Response::create([
  206. 'message' => '获取图片信息失败',
  207. 'status_code' => 500
  208. ]);
  209. }
  210. DB::beginTransaction();
  211. try {
  212. $circle->delete();
  213. //删除图片后,判断该条是否为主信息,如果为主信息,则更新同批次下一条为主信息
  214. if($circle->is_main==1){
  215. $tempPic = $this->interestCirclePicture->where('patch_num', $circle->patch_num)->first();
  216. $tempPic->is_main=1;
  217. $tempPic->save();
  218. }
  219. $this->interestCircle->where('id',$circle->circle_id)->decrement('picture_count');
  220. DB::commit();
  221. $key = 'circle_picture_' . $circle->patch_num;
  222. Redis::del($key);
  223. return Response::create();
  224. } catch (QueryException $exception) {
  225. DB::rollBack();
  226. Log::debug('删除相册图片:' . $request['id'] . $exception->getMessage());
  227. return Response::create([
  228. 'message' => '操作失败,请重试',
  229. 'error' => $exception->getMessage(),
  230. 'status_code' => 500
  231. ]);
  232. }
  233. }
  234. }