TopicRepository.php 7.1 KB

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