TopicGroupRepository.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. ];
  41. if(!empty($request['topic_ids'])){
  42. $topic_ids = explode(',', $request['topic_ids']);
  43. }
  44. DB::beginTransaction();
  45. try{
  46. $topicGroup = $this->topicGroup->create($data);
  47. if($topicGroup){
  48. if($topic_ids){
  49. $topic_group_inof_data = [];
  50. foreach($topic_ids as $value){
  51. $topic_group_inof_data[] = [
  52. 'topic_group_id' => $topicGroup->id,
  53. 'topic_id' => $value,
  54. ];
  55. }
  56. $this->topicGroupInfo->insert($topic_group_inof_data);
  57. }
  58. }
  59. DB::commit();
  60. return Response::create();
  61. }catch (QueryException $exception){
  62. DB::rollBack();
  63. Log::debug('新增话题分组:'.$exception->getMessage());
  64. return Response::create([
  65. 'message' => '新增话题失败,请重试',
  66. 'error' => $exception->getMessage(),
  67. 'status_code' => 500
  68. ]);
  69. }
  70. }
  71. //添加
  72. public function edit($request){
  73. $topicGroup = $this->topicGroup->where(['id'=>$request['id']])->first();
  74. if(!$topicGroup){
  75. return Response::create([
  76. 'message' => '该话题组不存在',
  77. 'status_code' => 500
  78. ]);
  79. }
  80. $topicGroup->name = $request['name'];
  81. if(!empty($request['topic_ids'])){
  82. $topic_ids = explode(',', $request['topic_ids']);
  83. }
  84. DB::beginTransaction();
  85. try{
  86. $topicGroupInfo = $topicGroup->save();
  87. if($topicGroupInfo){
  88. //删除
  89. $this->topicGroupInfo->where(['topic_group_id'=>$topicGroup->id])->delete();
  90. if($topic_ids){
  91. $category_topic_data = [];
  92. foreach($topic_ids as $value){
  93. $category_topic_data[] = [
  94. 'topic_group_id' => $topicGroup->id,
  95. 'topic_id' => $value,
  96. ];
  97. }
  98. $this->topicGroupInfo->insert($category_topic_data);
  99. }
  100. }
  101. DB::commit();
  102. return Response::create();
  103. }catch (QueryException $exception){
  104. DB::rollBack();
  105. Log::debug('新增推荐话题组:'.$exception->getMessage());
  106. return Response::create([
  107. 'message' => '新增推荐话题组失败,请重试',
  108. 'error' => $exception->getMessage(),
  109. 'status_code' => 500
  110. ]);
  111. }
  112. }
  113. //详情
  114. public function view($request){
  115. return $this->topicGroup->where(['id'=>$request['id']])->first();
  116. }
  117. //删除
  118. public function delete($request){
  119. $topicGroup = $this->topicGroup->where(['id'=>$request['id']])->first();
  120. DB::beginTransaction();
  121. try{
  122. if($topicGroup){
  123. $res = $this->topicGroup->where(['id'=>$request['id']])->delete();
  124. if($res){
  125. $this->topicGroupInfo->where(['topic_group_id'=>$topicGroup->id])->delete();
  126. }
  127. }
  128. }catch (QueryException $exception){
  129. DB::rollBack();
  130. Log::debug('删除推荐话题组:'.$exception->getMessage());
  131. return Response::create([
  132. 'message' => '删除推荐话题组失败,请重试',
  133. 'error' => $exception->getMessage(),
  134. 'status_code' => 500
  135. ]);
  136. }
  137. }
  138. }