CmsSubjectRepository.php 6.1 KB

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