TopicRepository.php 6.7 KB

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