TopicRepository.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Administrator
  5. * Date: 2019-06-10
  6. * Time: 18:12
  7. */
  8. namespace App\Repositories;
  9. use App\Models\Category;
  10. use App\Models\CategoryTopic;
  11. use App\Models\MemberFollowTopic;
  12. use App\Models\Topic;
  13. use Dingo\Api\Http\Response;
  14. use Illuminate\Support\Facades\DB;
  15. use Illuminate\Database\QueryException;
  16. use Illuminate\Support\Facades\Log;
  17. use Illuminate\Support\Facades\Redis;
  18. class TopicRepository
  19. {
  20. public function __construct(Topic $topic, CategoryTopic $categoryTopic, MemberFollowTopic $memberFollowTopic)
  21. {
  22. $this->topic = $topic;
  23. $this->categoryTopic = $categoryTopic;
  24. $this->memberFollowTopic = $memberFollowTopic;
  25. }
  26. //列表
  27. public function index($request)
  28. {
  29. $perPage = isset($request['per_page']) ? $request['per_page'] : 20;
  30. $where = [];
  31. if (isset($request['name'])) {
  32. $where[] = ['name', 'like', "%{$request['name']}%"];
  33. }
  34. if (isset($request['topic_status'])) {
  35. if ($request['topic_status'] == 'is_suggest') {
  36. $where[] = ['is_suggest', 1];
  37. }
  38. if ($request['topic_status'] == 'is_hot') {
  39. $where[] = ['is_hot', 1];
  40. }
  41. if ($request['topic_status'] == 'is_open') {
  42. $where[] = ['is_open', 1];
  43. }
  44. if ($request['topic_status'] == 'is_close') {
  45. $where[] = ['is_open', 0];
  46. }
  47. }
  48. return $this->topic
  49. ->where($where)
  50. ->orderBy('is_hot', 'desc')
  51. ->orderBy('is_suggest', 'desc')
  52. ->orderBy('is_open', 'desc')
  53. ->orderBy('id', 'desc')
  54. ->paginate($perPage);
  55. }
  56. //新增
  57. public function create($request)
  58. {
  59. $topic = $this->topic->where(['name' => $request['name']])->first();
  60. if ($topic) {
  61. return Response::create([
  62. 'message' => '该话题已存在',
  63. 'status_code' => 500
  64. ]);
  65. }
  66. $data = [
  67. 'name' => $request['name'],
  68. 'img' => $request['img'],
  69. 'desc' => $request['desc'],
  70. 'circle_id' => $request['desc'] ?? 0,
  71. ];
  72. if (!empty($request['base_count'])) {
  73. $data['base_count'] = $request['base_count'];
  74. }
  75. if (isset($request['category_ids'])) {
  76. $category_ids = explode(',', $request['category_ids']);
  77. }
  78. DB::beginTransaction();
  79. try {
  80. $topicInfo = $this->topic->create($data);
  81. if ($topicInfo) {
  82. if ($category_ids) {
  83. $category_topic_data = [];
  84. foreach ($category_ids as $value) {
  85. $category_topic_data[] = [
  86. 'category_id' => $value,
  87. 'topic_id' => $topicInfo->id,
  88. ];
  89. }
  90. $this->categoryTopic->insert($category_topic_data);
  91. }
  92. }
  93. DB::commit();
  94. Redis::zadd('topic.name', $topicInfo->id, $topicInfo->name);
  95. return Response::create();
  96. } catch (QueryException $exception) {
  97. DB::rollBack();
  98. Log::debug('新增话题:' . $exception->getMessage());
  99. return Response::create([
  100. 'message' => '新增话题,请重试',
  101. 'error' => $exception->getMessage(),
  102. 'status_code' => 500
  103. ]);
  104. }
  105. }
  106. public function edit($request)
  107. {
  108. $topic = $this->topic->where(['id' => $request['id']])->first();
  109. if (!$topic) {
  110. return Response::create([
  111. 'message' => '该话题不存在',
  112. 'status_code' => 500
  113. ]);
  114. }
  115. $oldName = $topic->name;
  116. $_topic = $this->topic->where(['name' => $request['name']])->where('id', '<>', $topic->id)->first();
  117. if ($_topic) {
  118. return Response::create([
  119. 'message' => '该话题已存在',
  120. 'status_code' => 500
  121. ]);
  122. }
  123. $topic->name = $request['name'];
  124. $topic->img = $request['img'];
  125. $topic->desc = $request['desc'];
  126. $topic->circle_id = $request['circle_id'] ?? 0;
  127. if (!empty($request['base_count'])) {
  128. $topic->base_count = $request['base_count'];
  129. }
  130. $category_ids = [];
  131. if (!empty($request['category_ids'])) {
  132. $category_ids = explode(',', $request['category_ids']);
  133. }
  134. DB::beginTransaction();
  135. try {
  136. //保存
  137. $topicInfo = $topic->save();
  138. //删除原来关联关系
  139. $this->categoryTopic->where(['topic_id' => $topic->id])->delete();
  140. if ($topicInfo) {
  141. if ($category_ids) {
  142. $category_topic_data = [];
  143. foreach ($category_ids as $value) {
  144. $category_topic_data[] = [
  145. 'category_id' => $value,
  146. 'topic_id' => $topic->id,
  147. ];
  148. }
  149. $this->categoryTopic->insert($category_topic_data);
  150. }
  151. }
  152. DB::commit();
  153. if ($oldName != $topic->name) {
  154. Redis::zrem('topic.name', $oldName);
  155. Redis::zadd('topic.name', $topic->id, $topic->name);
  156. }
  157. return Response::create();
  158. } catch (QueryException $exception) {
  159. DB::rollBack();
  160. Log::debug('编辑话题:' . $exception->getMessage());
  161. return Response::create([
  162. 'message' => '编辑话题,请重试',
  163. 'error' => $exception->getMessage(),
  164. 'status_code' => 500
  165. ]);
  166. }
  167. }
  168. //修改
  169. public function update($request)
  170. {
  171. $topic = $this->topic->where('id', $request['id'])->first();
  172. if (!$topic) {
  173. return Response::create([
  174. 'message' => '该话题不存在',
  175. 'status_code' => 500
  176. ]);
  177. }
  178. if (isset($request['is_open']) && $request['is_open'] !== null) {
  179. $topic->is_open = $request['is_open'];
  180. }
  181. if (!empty($request['type'])) {
  182. if ($topic->is_open == 0) {
  183. return Response::create([
  184. 'message' => '该话题未开启',
  185. 'status_code' => 500
  186. ]);
  187. }
  188. if (isset($request['type']) && $request['type'] == 1) {
  189. $topic->is_suggest = $request['status'];
  190. }
  191. if (isset($request['type']) && $request['type'] == 2) {
  192. $topic->is_hot = $request['status'];
  193. }
  194. }
  195. $res = $topic->save();
  196. if ($res) {
  197. return Response::create();
  198. }
  199. }
  200. //详情
  201. public function view($request)
  202. {
  203. return $this->topic->where('id', $request['id'])->first();
  204. }
  205. //获取多个话题
  206. public function getTopics($ids = [])
  207. {
  208. return $this->topic->whereIn('id', $ids)->select('id', 'name')->get();
  209. }
  210. public function getMemberTopics($uid)
  211. {
  212. return $this->memberFollowTopic
  213. ->leftJoin('topic', 'topic.id', '=', 'member_follow_topic.topic_id')
  214. ->where('member_follow_topic.uid', $uid)
  215. ->select('topic.id', 'topic.name')
  216. ->get();
  217. }
  218. /**
  219. * 重置话题redis
  220. */
  221. public function resetRedis()
  222. {
  223. $TopicName = $this->topic->pluck('id', 'name')->toArray();
  224. $res = Redis::zadd('topic.name', $TopicName);
  225. if ($res) {
  226. return Response::create([
  227. 'message' => '重置话题成功',
  228. 'status_code' => 500
  229. ]);
  230. } else {
  231. return Response::create([
  232. 'message' => '重置话题失败',
  233. 'status_code' => 500
  234. ]);
  235. }
  236. }
  237. }