cmsSubject = $cmsSubject; $this->cmsSubjectProduct = $cmsSubjectProduct; } //专题列表 public function index($request) { $perPage = isset($request['per_page']) ? $request['per_page'] : env('PER_PAGE'); $where = []; if(isset($request['id'])){ $where[] = ['id', '=', $request['id']]; } if(isset($request['city_id'])){ $where[] = ['city_id', '=', $request['city_id']]; } if(isset($request['is_open'])){ $where[] = ['is_open', '=', $request['is_open']]; } if(isset($request['used_mall'])){ $where[] = ['used_mall', '=', $request['used_mall']]; } return $this->cmsSubject->where($where)->orderBy('id', 'desc')->paginate($perPage); } public function detail($request) { return $this->cmsSubject->find($request['id']); } /** * 添加专题 */ public function create($request) { if($this->cmsSubject->where('title', trim($request['title']))->exists()){ throw new HttpException(500, '该专题已经存在'); } $subject = [ 'title' => $request['title'], 'city_id' => isset($request['city_id']) ? $request['city_id'] : 0, 'city_name' => isset($request['city_name']) ? $request['city_name'] : '', 'show_type' => $request['show_type'], 'is_open' => $request['is_open'], 'used_count' => 0, 'used_mall' => isset($request['used_mall']) ? $request['used_mall'] : 1, 'subject_img' => isset($request['subject_img']) ? $request['subject_img'] : '' ]; $date = date('Y-m-d H:i:s'); DB::beginTransaction(); try{ $res = $this->cmsSubject->create($subject); if ($res && $request['product_id']){ $products = explode(',',$request['product_id']); $sort = explode(',',$request['sort']); $product_num = count($products); $sort_num = count($sort); if ($sort_num != $product_num){ throw new HttpException(500, '请检查商品与排序数目是否对应'); } $resert_data = []; for ($i = 0;$i < $product_num;$i++){ $resert_data[] =[ 'product_id' => $products[$i], 'sort' => $sort[$i], 'subject_id' => $res['id'], 'created_at' => $date, 'updated_at' => $date, ]; } $result = $this->cmsSubjectProduct->insert($resert_data); if (!$result){ throw new HttpException(500, '专题商品添加失败'); } } DB::commit(); return Response::create(); }catch (QueryException $exception){ DB::rollBack(); return Response::create([ 'message' => '添加失败,请重试', 'error' => $exception->getMessage(), 'status_code' => 500 ]); } } public function edit($request) { $subject = $this->cmsSubject->find($request['id']); if ($subject->used_count != 0){ $request['is_open'] = 1; } $subject->title = $request['title']; $subject->city_id = isset($request['city_id']) ? $request['city_id'] : 0; $subject->city_name = isset($request['city_name']) ? $request['city_name'] : ''; $subject->show_type = $request['show_type']; $subject->is_open = $request['is_open']; //$subject->used_count = 0; $subject->used_mall = isset($request['used_mall']) ? $request['used_mall'] : 1; $subject->subject_img = isset($request['subject_img']) ? $request['subject_img'] : ''; $date = date('Y-m-d H:i:s'); DB::beginTransaction(); try{ $res = $subject->save(); if($res && $request['product_id']) { $this->cmsSubjectProduct->where('subject_id',$subject->id)->delete(); $products = explode(',', $request['product_id']); $sort = explode(',', $request['sort']); $product_num = count($products); $sort_num = count($sort); if ($sort_num != $product_num) { throw new HttpException(500, '请检查商品与排序数目是否对应'); } $update_data = []; for ($i = 0;$i < $product_num;$i++){ $update_data[] =[ 'product_id' => $products[$i], 'sort' => $sort[$i], 'subject_id' => $subject->id, 'created_at' => $date, 'updated_at' => $date, ]; } $result = $this->cmsSubjectProduct->insert($update_data); if (!$result){ throw new HttpException(500, '专题商品添加失败'); } } DB::commit(); return Response::create(); }catch (QueryException $exception){ DB::rollBack(); return Response::create([ 'message' => '编辑失败,请重试', 'error' => $exception->getMessage(), 'status_code' => 500 ]); } } public function delete($request) { $subject = $this->cmsSubject->find($request['id']); if ($subject->used_count != 0){ throw new HttpException(500, '该专题正在使用不能被删除'); } DB::beginTransaction(); try{ $res = $subject->delete(); if ($res){ $this->cmsSubjectProduct->where('subject_id',$subject->id)->delete(); } DB::commit(); return Response::create(); }catch (QueryException $exception){ DB::rollBack(); return Response::create([ 'message' => '删除失败,请重试', 'error' => $exception->getMessage(), 'status_code' => 500 ]); } } public function editStatus($request) { $subject = $this->cmsSubject->find($request['id']); $subject->is_open = $request['is_open']; $subject->updated_at = date('Y-m-d H:i:s'); if ($subject->used_count != 0 && $request['is_open'] == 0){ throw new HttpException(500, '该专题正在使用不能被关闭'); } $res = $subject->save(); if (!$res) { throw new HttpException(500, '修改状态失败'); } } }