CmsSubjectRepository.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. <?php
  2. namespace App\Repositories;
  3. use App\Models\CmsSubject;
  4. use App\Models\CmsSubjectProduct;
  5. use Symfony\Component\HttpKernel\Exception\HttpException;
  6. use Dingo\Api\Http\Response;
  7. use Illuminate\Support\Facades\DB;
  8. use Illuminate\Database\QueryException;
  9. class CmsSubjectRepository {
  10. public function __construct(CmsSubject $cmsSubject,CmsSubjectProduct $cmsSubjectProduct) {
  11. $this->cmsSubject = $cmsSubject;
  12. $this->cmsSubjectProduct = $cmsSubjectProduct;
  13. }
  14. //专题列表
  15. public function index($request)
  16. {
  17. $perPage = isset($request['per_page']) ? $request['per_page'] : env('PER_PAGE');
  18. $where = [];
  19. if(isset($request['id'])){
  20. $where[] = ['id', '=', $request['id']];
  21. }
  22. if(isset($request['city_id'])){
  23. $where[] = ['city_id', '=', $request['city_id']];
  24. }
  25. if(isset($request['is_open'])){
  26. $where[] = ['is_open', '=', $request['is_open']];
  27. }
  28. return $this->cmsSubject->where($where)->orderBy('id', 'asc')->paginate($perPage);
  29. }
  30. /**
  31. * 添加专题
  32. */
  33. public function create($request)
  34. {
  35. if($this->cmsSubject->where('title', trim($request['title']))->exists()){
  36. throw new HttpException(500, '该专题已经存在');
  37. }
  38. $subject = [
  39. 'title' => $request['title'],
  40. 'city_id' => $request['city_id'],
  41. 'city_name' => $request['city_name'],
  42. 'show_type' => $request['show_type'],
  43. 'is_open' => $request['is_open'],
  44. 'used_count' => 0
  45. ];
  46. $date = date('Y-m-d H:i:s');
  47. DB::beginTransaction();
  48. try{
  49. $res = $this->cmsSubject->create($subject);
  50. if ($res && $request['product_id']){
  51. $products = explode(',',$request['product_id']);
  52. $sort = explode(',',$request['sort']);
  53. $product_num = count($products);
  54. $sort_num = count($sort);
  55. if ($sort_num != $product_num){
  56. throw new HttpException(500, '请检查商品与排序数目是否对应');
  57. }
  58. $subject_product = new CmsSubjectProduct();
  59. for ($i = 0;$i < $product_num;$i++){
  60. $resert_data =[
  61. 'product_id' => $products[$i],
  62. 'sort' => $sort[$i],
  63. 'subject_id' => $res['id'],
  64. 'created_at' => $date,
  65. 'updated_at' => $date,
  66. ];
  67. $subject_product->insert($resert_data);
  68. }
  69. }
  70. DB::commit();
  71. return Response::create();
  72. }catch (QueryException $exception){
  73. DB::rollBack();
  74. return Response::create([
  75. 'message' => '添加失败,请重试',
  76. 'error' => $exception->getMessage(),
  77. 'status_code' => 500
  78. ]);
  79. }
  80. }
  81. public function edit($request)
  82. {
  83. $subject = $this->cmsSubject->find($request['id']);
  84. $subject->title = $request['title'];
  85. $subject->city_id = $request['city_id'];
  86. $subject->city_name = $request['city_name'];
  87. $subject->show_type = $request['show_type'];
  88. $subject->is_open = $request['is_open'];
  89. $subject->used_count = 0;
  90. $date = date('Y-m-d H:i:s');
  91. DB::beginTransaction();
  92. try{
  93. $res = $subject->save();
  94. if($res && $request['product_id']) {
  95. $this->cmsSubjectProduct->where('subject_id',$subject->id)->delete();
  96. $products = explode(',', $request['product_id']);
  97. $sort = explode(',', $request['sort']);
  98. $product_num = count($products);
  99. $sort_num = count($sort);
  100. if ($sort_num != $product_num) {
  101. throw new HttpException(500, '请检查商品与排序数目是否对应');
  102. }
  103. $subject_product = new CmsSubjectProduct();
  104. for ($i = 0; $i < $product_num; $i++) {
  105. $resert_data = [
  106. 'product_id' => $products[$i],
  107. 'sort' => $sort[$i],
  108. 'subject_id' => $subject->id,
  109. 'created_at' => $date,
  110. 'updated_at' => $date,
  111. ];
  112. $subject_product->insert($resert_data);
  113. }
  114. }
  115. DB::commit();
  116. return Response::create();
  117. }catch (QueryException $exception){
  118. DB::rollBack();
  119. return Response::create([
  120. 'message' => '编辑失败,请重试',
  121. 'error' => $exception->getMessage(),
  122. 'status_code' => 500
  123. ]);
  124. }
  125. }
  126. public function delete($request)
  127. {
  128. $subject = $this->cmsSubject->find($request['id']);
  129. if ($subject->used_count != 0){
  130. throw new HttpException(500, '该专题正在使用不能被删除');
  131. }
  132. DB::beginTransaction();
  133. try{
  134. $res = $subject->delete();
  135. if ($res){
  136. $this->cmsSubjectProduct->where('subject_id',$subject->id)->delete();
  137. }
  138. DB::commit();
  139. return Response::create();
  140. }catch (QueryException $exception){
  141. DB::rollBack();
  142. return Response::create([
  143. 'message' => '删除失败,请重试',
  144. 'error' => $exception->getMessage(),
  145. 'status_code' => 500
  146. ]);
  147. }
  148. }
  149. public function editStatus($request)
  150. {
  151. $subject = $this->cmsSubject->find($request['id']);
  152. $subject->is_open = $request['is_open'];
  153. $subject->updated_at = date('Y-m-d H:i:s');
  154. if ($subject->used_count != 0 && $request['is_open'] == 0){
  155. throw new HttpException(500, '该专题正在使用不能被关闭');
  156. }
  157. $res = $subject->save();
  158. if (!$res) {
  159. throw new HttpException(500, '修改状态失败');
  160. }
  161. }
  162. }