TopicRepository.php 5.8 KB

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