TopicGroupRepository.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Administrator
  5. * Date: 2019-06-10
  6. * Time: 17:07
  7. */
  8. namespace App\Repositories;
  9. use App\Models\TopicGroup;
  10. use App\Models\TopicGroupInfo;
  11. use Dingo\Api\Http\Response;
  12. use Illuminate\Support\Facades\DB;
  13. use Illuminate\Database\QueryException;
  14. use Illuminate\Support\Facades\Log;
  15. class TopicGroupRepository {
  16. public function __construct(TopicGroup $topicGroup,TopicGroupInfo $topicGroupInfo) {
  17. $this->topicGroup = $topicGroup;
  18. $this->topicGroupInfo = $topicGroupInfo;
  19. }
  20. public function index($request){
  21. $perPage = isset($request['per_page']) ? $request['per_page'] : 20;
  22. $where = [];
  23. if(isset($request['name'])){
  24. $where[] = ['name', 'like', "%{$request['name']}%"];
  25. }
  26. return $this->topicGroup->where($where)->paginate($perPage);
  27. }
  28. //添加
  29. public function create($request){
  30. $category = $this->topicGroup->where(['name'=>$request['name']])->first();
  31. if($category){
  32. return Response::create([
  33. 'message' => '该分组已存在',
  34. 'status_code' => 500
  35. ]);
  36. }
  37. $data = [
  38. 'name' => $request['name'],
  39. 'status' => 1,
  40. 'desc' => '',
  41. ];
  42. if(!empty($request['topic_ids'])){
  43. $topic_ids = explode(',', $request['topic_ids']);
  44. }
  45. DB::beginTransaction();
  46. try{
  47. $topicGroup = $this->topicGroup->create($data);
  48. if($topicGroup){
  49. if($topic_ids){
  50. $topic_group_inof_data = [];
  51. foreach($topic_ids as $value){
  52. $topic_group_inof_data[] = [
  53. 'topic_group_id' => $topicGroup->id,
  54. 'topic_id' => $value,
  55. ];
  56. }
  57. $this->topicGroupInfo->insert($topic_group_inof_data);
  58. }
  59. }
  60. DB::commit();
  61. return Response::create();
  62. }catch (QueryException $exception){
  63. DB::rollBack();
  64. Log::debug('新增话题分组:'.$exception->getMessage());
  65. return Response::create([
  66. 'message' => '新增话题失败,请重试',
  67. 'error' => $exception->getMessage(),
  68. 'status_code' => 500
  69. ]);
  70. }
  71. }
  72. //添加
  73. public function edit($request){
  74. $topicGroup = $this->topicGroup->where(['id'=>$request['id']])->first();
  75. if(!$topicGroup){
  76. return Response::create([
  77. 'message' => '该话题组不存在',
  78. 'status_code' => 500
  79. ]);
  80. }
  81. $topicGroup->name = $request['name'];
  82. if(!empty($request['topic_ids'])){
  83. $topic_ids = explode(',', $request['topic_ids']);
  84. }
  85. DB::beginTransaction();
  86. try{
  87. $topicGroupInfo = $topicGroup->save();
  88. if($topicGroupInfo){
  89. //删除
  90. $this->topicGroupInfo->where(['topic_group_id'=>$topicGroup->id])->delete();
  91. if($topic_ids){
  92. $category_topic_data = [];
  93. foreach($topic_ids as $value){
  94. $category_topic_data[] = [
  95. 'topic_group_id' => $topicGroup->id,
  96. 'topic_id' => $value,
  97. ];
  98. }
  99. $this->topicGroupInfo->insert($category_topic_data);
  100. }
  101. }
  102. DB::commit();
  103. return Response::create();
  104. }catch (QueryException $exception){
  105. DB::rollBack();
  106. Log::debug('新增推荐话题组:'.$exception->getMessage());
  107. return Response::create([
  108. 'message' => '新增推荐话题组失败,请重试',
  109. 'error' => $exception->getMessage(),
  110. 'status_code' => 500
  111. ]);
  112. }
  113. }
  114. //详情
  115. public function view($request){
  116. return $this->topicGroup->where(['id'=>$request['id']])->first();
  117. }
  118. //删除
  119. public function delete($request){
  120. $topicGroup = $this->topicGroup->where(['id'=>$request['id']])->first();
  121. DB::beginTransaction();
  122. try{
  123. if($topicGroup){
  124. $res = $this->topicGroup->where(['id'=>$request['id']])->delete();
  125. if($res){
  126. $this->topicGroupInfo->where(['topic_group_id'=>$topicGroup->id])->delete();
  127. }
  128. }
  129. DB::commit();
  130. return Response::create();
  131. }catch (QueryException $exception){
  132. DB::rollBack();
  133. Log::debug('删除推荐话题组:'.$exception->getMessage());
  134. return Response::create([
  135. 'message' => '删除推荐话题组失败,请重试',
  136. 'error' => $exception->getMessage(),
  137. 'status_code' => 500
  138. ]);
  139. }
  140. }
  141. }