CmsSubjectRepository.php 5.1 KB

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