CmsSubjectRepository.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. Log::debug('request-param-createSubject:'.json_encode($subject));
  48. $date = date('Y-m-d H:i:s');
  49. DB::beginTransaction();
  50. try{
  51. $res = $this->cmsSubject->create($subject);
  52. if ($res && $request['product_id']){
  53. $products = explode(',',$request['product_id']);
  54. $sort = explode(',',$request['sort']);
  55. $product_num = count($products);
  56. $sort_num = count($sort);
  57. if ($sort_num != $product_num){
  58. throw new HttpException(500, '请检查商品与排序数目是否对应');
  59. }
  60. $subject_product = new CmsSubjectProduct();
  61. for ($i = 0;$i < $product_num;$i++){
  62. $resert_data =[
  63. 'product_id' => $products[$i],
  64. 'sort' => $sort[$i],
  65. 'subject_id' => $res['id'],
  66. 'created_at' => $date,
  67. 'updated_at' => $date,
  68. ];
  69. $subject_product->insert($resert_data);
  70. }
  71. }
  72. DB::commit();
  73. return Response::create();
  74. }catch (QueryException $exception){
  75. DB::rollBack();
  76. return Response::create([
  77. 'message' => '添加失败,请重试',
  78. 'error' => $exception->getMessage(),
  79. 'status_code' => 500
  80. ]);
  81. }
  82. }
  83. public function edit($request)
  84. {
  85. $subject = $this->cmsSubject->find($request['id']);
  86. $subject->title = $request['title'];
  87. $subject->city_id = $request['city_id'];
  88. $subject->city_name = $request['city_name'];
  89. $subject->show_type = $request['show_type'];
  90. $subject->is_open = $request['is_open'];
  91. $subject->used_count = 0;
  92. $date = date('Y-m-d H:i:s');
  93. DB::beginTransaction();
  94. try{
  95. $res = $subject->save();
  96. if($res && $request['product_id']) {
  97. $this->cmsSubjectProduct->where('subject_id',$subject->id)->delete();
  98. $products = explode(',', $request['product_id']);
  99. $sort = explode(',', $request['sort']);
  100. $product_num = count($products);
  101. $sort_num = count($sort);
  102. if ($sort_num != $product_num) {
  103. throw new HttpException(500, '请检查商品与排序数目是否对应');
  104. }
  105. $subject_product = new CmsSubjectProduct();
  106. for ($i = 0; $i < $product_num; $i++) {
  107. $resert_data = [
  108. 'product_id' => $products[$i],
  109. 'sort' => $sort[$i],
  110. 'subject_id' => $subject->id,
  111. 'created_at' => $date,
  112. 'updated_at' => $date,
  113. ];
  114. $subject_product->insert($resert_data);
  115. }
  116. }
  117. DB::commit();
  118. return Response::create();
  119. }catch (QueryException $exception){
  120. DB::rollBack();
  121. return Response::create([
  122. 'message' => '编辑失败,请重试',
  123. 'error' => $exception->getMessage(),
  124. 'status_code' => 500
  125. ]);
  126. }
  127. }
  128. public function delete($request)
  129. {
  130. $subject = $this->cmsSubject->find($request['id']);
  131. if ($subject->used_count != 0){
  132. throw new HttpException(500, '该专题正在使用不能被删除');
  133. }
  134. DB::beginTransaction();
  135. try{
  136. $res = $subject->delete();
  137. if ($res){
  138. $this->cmsSubjectProduct->where('subject_id',$subject->id)->delete();
  139. }
  140. DB::commit();
  141. return Response::create();
  142. }catch (QueryException $exception){
  143. DB::rollBack();
  144. return Response::create([
  145. 'message' => '删除失败,请重试',
  146. 'error' => $exception->getMessage(),
  147. 'status_code' => 500
  148. ]);
  149. }
  150. }
  151. public function editStatus($request)
  152. {
  153. $subject = $this->cmsSubject->find($request['id']);
  154. $subject->is_open = $request['is_open'];
  155. $subject->updated_at = date('Y-m-d H:i:s');
  156. if ($subject->used_count != 0 && $request['is_open'] == 0){
  157. throw new HttpException(500, '该专题正在使用不能被关闭');
  158. }
  159. $res = $subject->save();
  160. if (!$res) {
  161. throw new HttpException(500, '修改状态失败');
  162. }
  163. }
  164. }