CmsSubjectRepository.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. return $this->cmsSubject->where($where)->orderBy('id', 'asc')->paginate($perPage);
  26. }
  27. /**
  28. * 添加专题
  29. */
  30. public function create($request)
  31. {
  32. if($this->cmsSubject->where('title', trim($request['title']))->exists()){
  33. throw new HttpException(500, '该专题已经存在');
  34. }
  35. $subject = [
  36. 'title' => $request['title'],
  37. 'city_id' => $request['city_id'],
  38. 'city_name' => $request['city_name'],
  39. 'show_type' => $request['show_type'],
  40. 'is_open' => $request['is_open'],
  41. ];
  42. $date = date('Y-m-d H:i:s');
  43. DB::beginTransaction();
  44. try{
  45. $res = $this->cmsSubject->create($subject);
  46. if ($res && $request['product_id']){
  47. $products = explode(',',$request['product_id']);
  48. $sort = explode(',',$request['sort']);
  49. $product_num = count($products);
  50. $sort_num = count($sort);
  51. if ($sort_num != $product_num){
  52. throw new HttpException(500, '请检查商品与排序数目是否对应');
  53. }
  54. $subject_product = new CmsSubjectProduct();
  55. for ($i = 0;$i < $product_num;$i++){
  56. $resert_data =[
  57. 'product_id' => $products[$i],
  58. 'sort' => $sort[$i],
  59. 'subject_id' => $res['id'],
  60. 'created_at' => $date,
  61. 'updated_at' => $date,
  62. ];
  63. $subject_product->insert($resert_data);
  64. }
  65. }
  66. DB::commit();
  67. return Response::create();
  68. }catch (QueryException $exception){
  69. DB::rollBack();
  70. return Response::create([
  71. 'message' => '添加失败,请重试',
  72. 'error' => $exception->getMessage(),
  73. 'status_code' => 500
  74. ]);
  75. }
  76. }
  77. public function edit($request)
  78. {
  79. $subject = $this->cmsSubject->find($request['id']);
  80. $subject->title = $request['title'];
  81. $subject->city_id = $request['city_id'];
  82. $subject->city_name = $request['city_name'];
  83. $subject->show_type = $request['show_type'];
  84. $subject->is_open = $request['is_open'];
  85. $date = date('Y-m-d H:i:s');
  86. DB::beginTransaction();
  87. try{
  88. $res = $subject->save();
  89. if($res && $request['product_id']) {
  90. $this->cmsSubjectProduct->where('subject_id',$subject->id)->delete();
  91. $products = explode(',', $request['product_id']);
  92. $sort = explode(',', $request['sort']);
  93. $product_num = count($products);
  94. $sort_num = count($sort);
  95. if ($sort_num != $product_num) {
  96. throw new HttpException(500, '请检查商品与排序数目是否对应');
  97. }
  98. $subject_product = new CmsSubjectProduct();
  99. for ($i = 0; $i < $product_num; $i++) {
  100. $resert_data = [
  101. 'product_id' => $products[$i],
  102. 'sort' => $sort[$i],
  103. 'subject_id' => $subject->id,
  104. 'created_at' => $date,
  105. 'updated_at' => $date,
  106. ];
  107. $subject_product->insert($resert_data);
  108. }
  109. }
  110. DB::commit();
  111. return Response::create();
  112. }catch (QueryException $exception){
  113. DB::rollBack();
  114. return Response::create([
  115. 'message' => '编辑失败,请重试',
  116. 'error' => $exception->getMessage(),
  117. 'status_code' => 500
  118. ]);
  119. }
  120. }
  121. public function delete($request)
  122. {
  123. $subject = $this->cmsSubject->find($request['id']);
  124. DB::beginTransaction();
  125. try{
  126. $res = $subject->delete();
  127. if ($res){
  128. $this->cmsSubjectProduct->where('subject_id',$subject->id)->delete();
  129. }
  130. DB::commit();
  131. return Response::create();
  132. }catch (QueryException $exception){
  133. DB::rollBack();
  134. return Response::create([
  135. 'message' => '删除失败,请重试',
  136. 'error' => $exception->getMessage(),
  137. 'status_code' => 500
  138. ]);
  139. }
  140. }
  141. public function editStatus($request)
  142. {
  143. $subject = $this->cmsSubject->find($request['id']);
  144. $subject->is_open = $request['is_open'];
  145. $subject->updated_at = date('Y-m-d H:i:s');
  146. $res = $subject->save();
  147. if (!$res) {
  148. throw new HttpException(500, '修改状态失败');
  149. }
  150. }
  151. }