TopicRepository.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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)->orderBy('id','asc')->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. $oldName = $topic->name;
  99. $_topic = $this->topic->where(['name'=>$request['name']])->where('id','<>',$topic->id)->first();
  100. if($_topic){
  101. return Response::create([
  102. 'message' => '该话题已存在',
  103. 'status_code' => 500
  104. ]);
  105. }
  106. $topic->name = $request['name'];
  107. $topic->img = $request['img'];
  108. $topic->desc = $request['desc'];
  109. $category_ids = [];
  110. if(!empty($request['category_ids'])){
  111. $category_ids = explode(',', $request['category_ids']);
  112. }
  113. DB::beginTransaction();
  114. try{
  115. //保存
  116. $topicInfo = $topic->save();
  117. //删除原来关联关系
  118. $this->categoryTopic->where(['topic_id'=>$topic->id])->delete();
  119. if($topicInfo){
  120. if($category_ids){
  121. $category_topic_data = [];
  122. foreach($category_ids as $value){
  123. $category_topic_data[] = [
  124. 'category_id' => $value,
  125. 'topic_id' => $topic->id,
  126. ];
  127. }
  128. $this->categoryTopic->insert($category_topic_data);
  129. }
  130. }
  131. DB::commit();
  132. if($oldName != $topic->name){
  133. Redis::zrem('topic.name', $oldName);
  134. Redis::zadd('topic.name', $topic->id, $topic->name);
  135. }
  136. return Response::create();
  137. }catch (QueryException $exception){
  138. DB::rollBack();
  139. Log::debug('编辑话题:'.$exception->getMessage());
  140. return Response::create([
  141. 'message' => '编辑话题,请重试',
  142. 'error' => $exception->getMessage(),
  143. 'status_code' => 500
  144. ]);
  145. }
  146. }
  147. //修改
  148. public function update($request){
  149. $topic = $this->topic->where('id', $request['id'])->first();
  150. if(!$topic){
  151. return Response::create([
  152. 'message' => '该话题不存在',
  153. 'status_code' => 500
  154. ]);
  155. }
  156. if(isset($request['is_open']) && $request['is_open'] !== null){
  157. $topic->is_open = $request['is_open'];
  158. }
  159. if(!empty($request['type'])){
  160. if($topic->is_open == 0){
  161. return Response::create([
  162. 'message' => '该话题未开启',
  163. 'status_code' => 500
  164. ]);
  165. }
  166. if(isset($request['type']) && $request['type'] == 1){
  167. $topic->is_suggest = $request['status'];
  168. }
  169. if(isset($request['type']) && $request['type'] == 2){
  170. $topic->is_hot = $request['status'];
  171. }
  172. }
  173. $res = $topic->save();
  174. if($res){
  175. return Response::create();
  176. }
  177. }
  178. //详情
  179. public function view($request){
  180. return $this->topic->where('id', $request['id'])->first();
  181. }
  182. //获取多个话题
  183. public function getTopics($ids = []){
  184. return $this->topic->whereIn('id', $ids)->select('id','name')->get();
  185. }
  186. /**
  187. * 重置话题redis
  188. */
  189. public function resetRedis()
  190. {
  191. $TopicName = $this->topic->pluck('id', 'name')->toArray();
  192. $res = Redis::zadd('topic.name', $TopicName);
  193. if($res){
  194. return Response::create([
  195. 'message' => '重置话题成功',
  196. 'status_code' => 500
  197. ]);
  198. }else{
  199. return Response::create([
  200. 'message' => '重置话题失败',
  201. 'status_code' => 500
  202. ]);
  203. }
  204. }
  205. }