CmsSubjectRepository.php 6.4 KB

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