|
@@ -0,0 +1,173 @@
|
|
|
|
+<?php
|
|
|
|
+namespace App\Http\Controllers;
|
|
|
|
+
|
|
|
|
+use App\Models\CmsSubjectProduct;
|
|
|
|
+use App\Repositories\CmsSubjectRepository;
|
|
|
|
+use App\Transformers\CmsSubjectTransformer;
|
|
|
|
+use App\Transformers\CmsSubjectViewTransformer;
|
|
|
|
+use Illuminate\Http\Request;
|
|
|
|
+use Illuminate\Support\Facades\Validator;
|
|
|
|
+use League\Fractal\Resource\Collection;
|
|
|
|
+use League\Fractal\Manager;
|
|
|
|
+use Illuminate\Validation\Rule;
|
|
|
|
+use League\Fractal\Pagination\IlluminatePaginatorAdapter;
|
|
|
|
+/**
|
|
|
|
+ * Created by PhpStorm.
|
|
|
|
+ * User: qinyaer
|
|
|
|
+ * Date: 2019/4/28
|
|
|
|
+ * Time: 下午15:12
|
|
|
|
+ */
|
|
|
|
+
|
|
|
|
+class CmsSubjectController extends BaseController
|
|
|
|
+{
|
|
|
|
+ public function __construct(CmsSubjectRepository $cmsSubjectRepository)
|
|
|
|
+ {
|
|
|
|
+ $this->cmsSubjectRepository = $cmsSubjectRepository;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ //专题列表
|
|
|
|
+ public function index(Request $request)
|
|
|
|
+ {
|
|
|
|
+ $cmsSubject = $this->cmsSubjectRepository->index($request->all());
|
|
|
|
+ if (count($cmsSubject)>0) {
|
|
|
|
+ foreach ($cmsSubject as $k => $v) {
|
|
|
|
+ $v->product_count = 0;
|
|
|
|
+ $product = CmsSubjectProduct::where('subject_id',$v['id'])->get();
|
|
|
|
+ $count = $product->toArray();
|
|
|
|
+ foreach ($count as $key => $value){
|
|
|
|
+ if ($value['subject_id'] == $v['id']){
|
|
|
|
+ $v->product_count ++ ;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ $fractal = new Manager();
|
|
|
|
+ $resource = new Collection($cmsSubject, new CmsSubjectTransformer());
|
|
|
|
+ $resource->setPaginator(new IlluminatePaginatorAdapter($cmsSubject));
|
|
|
|
+ $data = $fractal->createData($resource)->toArray();
|
|
|
|
+ $data['extra'] = [
|
|
|
|
+ 'filters' => [
|
|
|
|
+ 'id'
|
|
|
|
+ ],
|
|
|
|
+ 'columns' => [
|
|
|
|
+ 'id',
|
|
|
|
+ 'city_id',
|
|
|
|
+ 'city_name',
|
|
|
|
+ 'title',
|
|
|
|
+ 'show_type',
|
|
|
|
+ 'is_open',
|
|
|
|
+ 'product_count',
|
|
|
|
+ ]
|
|
|
|
+ ];
|
|
|
|
+ return $data;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ //新建专题
|
|
|
|
+ public function create(Request $request)
|
|
|
|
+ {
|
|
|
|
+ $validator = Validator::make($request->all(), [
|
|
|
|
+ 'product_id' => 'required',
|
|
|
|
+ 'sort' => 'required',
|
|
|
|
+ 'city_id' => 'required|integer',
|
|
|
|
+ 'city_name' => 'required|string',
|
|
|
|
+ 'title' => 'required|string',
|
|
|
|
+ 'show_type' => ['required', Rule::in(0,1,2)],
|
|
|
|
+ 'is_open' => ['required', Rule::in(0,1)],
|
|
|
|
+ ]);
|
|
|
|
+ if ($validator->fails()) {
|
|
|
|
+ return $this->response->error($validator->errors()->first(), 500);
|
|
|
|
+ }
|
|
|
|
+ return $this->cmsSubjectRepository->create($request->all());
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ //编辑专题
|
|
|
|
+ public function edit(Request $request)
|
|
|
|
+ {
|
|
|
|
+ $validator = Validator::make($request->all(), [
|
|
|
|
+ 'id' => 'required|exists:cms_subject',
|
|
|
|
+ 'product_id' => 'required',
|
|
|
|
+ 'sort' => 'required',
|
|
|
|
+ 'city_id' => 'required|integer',
|
|
|
|
+ 'city_name' => 'required|string',
|
|
|
|
+ 'title' => 'required|string',
|
|
|
|
+ 'show_type' => ['required', Rule::in(0,1,2)],
|
|
|
|
+ 'is_open' => ['required', Rule::in(0,1)],
|
|
|
|
+ ]);
|
|
|
|
+
|
|
|
|
+ if ($validator->fails()) {
|
|
|
|
+ return $this->response->error($validator->errors()->first(), 500);
|
|
|
|
+ }
|
|
|
|
+ return $this->cmsSubjectRepository->edit($request->all());
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ //查看某专题
|
|
|
|
+ public function view(Request $request)
|
|
|
|
+ {
|
|
|
|
+ $all = $request->all();
|
|
|
|
+ $validator = Validator::make($all, [
|
|
|
|
+ 'id' => 'required|integer',
|
|
|
|
+ ]);
|
|
|
|
+
|
|
|
|
+ if($validator->fails()) {
|
|
|
|
+
|
|
|
|
+ return $this->response->error($validator->errors()->first(), 500);
|
|
|
|
+ }
|
|
|
|
+ $getSubject = $this->cmsSubjectRepository->index($request->all());
|
|
|
|
+
|
|
|
|
+ if (count($getSubject)>0) {
|
|
|
|
+ foreach ($getSubject as $k => $v) {
|
|
|
|
+ $product = CmsSubjectProduct::select('subject_id','product_id','sort')->where('subject_id',$v['id'])->get();
|
|
|
|
+ $num = $product->toArray();
|
|
|
|
+ $product_arr = array_column($num,'product_id');
|
|
|
|
+ $getSubject[$k]->product_id = implode(',',$product_arr);
|
|
|
|
+ $sort_arry = array_column($num,'sort');
|
|
|
|
+ $getSubject[$k]->sort = implode(',',$sort_arry);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if(!$getSubject){
|
|
|
|
+ return $this->response->error($validator->errors()->first(), 500);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ $fractal = new Manager();
|
|
|
|
+ $resource = new Collection($getSubject, new CmsSubjectViewTransformer());
|
|
|
|
+ $resource->setPaginator(new IlluminatePaginatorAdapter($getSubject));
|
|
|
|
+ $data = $fractal->createData($resource)->toArray();
|
|
|
|
+ $data['extra'] = [
|
|
|
|
+ 'filters' => [
|
|
|
|
+ 'id'
|
|
|
|
+ ],
|
|
|
|
+ 'columns' => [
|
|
|
|
+ 'id',
|
|
|
|
+ 'city_id',
|
|
|
|
+ 'city_name',
|
|
|
|
+ 'title',
|
|
|
|
+ 'show_type',
|
|
|
|
+ 'is_open',
|
|
|
|
+ 'product_id',
|
|
|
|
+ 'sort',
|
|
|
|
+ ]
|
|
|
|
+ ];
|
|
|
|
+ return $data;
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ //删除专题
|
|
|
|
+ public function delete(Request $request)
|
|
|
|
+ {
|
|
|
|
+ $validator = Validator::make($request->all(), [
|
|
|
|
+ 'id' => 'required|exists:cms_subject'
|
|
|
|
+ ]);
|
|
|
|
+ if ($validator->fails()) {
|
|
|
|
+ return $this->response->error($validator->errors()->first(), 500);
|
|
|
|
+ }
|
|
|
|
+ return $this->cmsSubjectRepository->delete($request->all());
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+}
|