TopicRepository.php 5.9 KB

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