CmsSubjectRepository.php 7.2 KB

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