duqinya 5 年 前
コミット
4ecf0b7961

+ 105 - 0
app/Http/Controllers/V2/FloorController.php

@@ -0,0 +1,105 @@
+<?php
+namespace App\Http\Controllers\V2;
+use App\Http\Controllers\BaseController;
+use App\Repositories\V2\FloorRepository;
+use App\Transformers\FloorTransformer;
+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/6/5
+ * Time: 上午10:51
+ */
+
+class FloorController extends BaseController
+{
+    public function __construct(FloorRepository $floorRepository)
+    {
+        $this->floorRepository = $floorRepository;
+    }
+
+    //楼层列表
+    public function index(Request $request)
+    {
+        $floor = $this->floorRepository->index($request->all());
+
+        $fractal = new Manager();
+        $resource = new Collection($floor, new FloorTransformer());
+        $resource->setPaginator(new IlluminatePaginatorAdapter($floor));
+        $data = $fractal->createData($resource)->toArray();
+        $data['extra'] = [
+            'filters' => [
+            ],
+            'columns' => [
+                'id',
+                'name',
+                'floor_location',
+                'group_ids',
+                'floor_type',
+                'is_open',
+                'updated_at',
+            ]
+        ];
+        return $data;
+    }
+
+
+    //新建楼层
+    public function create(Request $request)
+    {
+        return  $this->floorRepository->create($request->all());
+
+    }
+
+
+    //编辑楼层
+    public function edit(Request $request)
+    {
+        $validator = Validator::make($request->all(), [
+            'id' => 'required|exists:operate_floor_management',
+            'name' => 'required|string',
+            'floor_location' => 'required|integer',
+            'group_ids' => 'required',
+            'floor_type' => 'required|integer',
+            'is_open' => ['required', Rule::in(0,1)],
+        ]);
+
+        if ($validator->fails()) {
+            return $this->response->error($validator->errors()->first(), 500);
+        }
+        return  $this->floorRepository->edit($request->all());
+    }
+
+
+    //修改楼层状态
+    public function editStatus(Request $request)
+    {
+        $validator = Validator::make($request->all(), [
+            'id' => 'required|exists:operate_floor_management',
+            'is_open' => ['required', Rule::in(0,1)],
+        ]);
+
+        if ($validator->fails()) {
+            return $this->response->error($validator->errors()->first(), 500);
+        }
+        return  $this->floorRepository->editStatus($request->all());
+    }
+
+    //关闭banner修改楼层状态
+    public function floorBind(Request $request)
+    {
+        $validator = Validator::make($request->all(), [
+            'banner_id' => 'required|integer',
+        ]);
+
+        if ($validator->fails()) {
+            return $this->response->error($validator->errors()->first(), 500);
+        }
+        return  $this->floorRepository->floorBind($request->all());
+    }
+}

+ 16 - 0
app/Models/Floor.php

@@ -0,0 +1,16 @@
+<?php
+
+namespace App\Models;
+
+
+class Floor extends BaseModel
+{
+    protected  $table = 'cms_floor';
+    /**
+     * 可被批量赋值的字段
+     * @var array
+     */
+    protected $fillable = ['name','floor_location','group_ids','floor_type','is_open'];
+
+
+}

+ 111 - 0
app/Repositories/V2/FloorRepository.php

@@ -0,0 +1,111 @@
+<?php
+namespace App\Repositories\V2;
+use App\Models\Floor;
+use Symfony\Component\HttpKernel\Exception\HttpException;
+use Dingo\Api\Http\Response;
+use Illuminate\Support\Facades\DB;
+use Illuminate\Database\QueryException;
+
+/**
+ * Created by PhpStorm.
+ * User: durong
+ * Date: 2019/6/3
+ * Time: 下午2:36
+ */
+
+class FloorRepository
+{
+    public function __construct(Floor $floor)
+    {
+        $this->floor = $floor;
+    }
+
+    public function index()
+    {
+        return $this->floor->paginate(20);
+    }
+
+    public function create($request)
+    {
+        $data = [
+            'name' => $request['name'],
+            'floor_location' => $request['floor_location'],
+            'group_ids' => $request['group_ids'],//多个banner用英文,隔开
+            'floor_type' => $request['floor_type'],
+            'is_open' => $request['is_open']
+        ];
+        if (!$this->floor->create($data)) {
+            throw new HttpException(500, '添加失败');
+        }
+    }
+
+    public function edit($request)
+    {
+        $floor = $this->floor->where('id', $request['id'])->first();
+
+        $floor->name = $request['name'];
+        $floor->floor_location = $request['floor_location'];
+        $floor->group_ids = $request['group_ids'];
+        $floor->floor_type = $request['floor_type'];
+        $floor->is_open = $request['is_open'] ?? 0;
+
+        DB::beginTransaction();
+        try{
+            $res = $floor->save();
+
+            if($res){
+                DB::commit();
+                return Response::create();
+            }
+            DB::rollBack();
+            return Response::create([
+                'message'  => '编辑失败,请重试',
+                'status_code'   => 500
+            ]);
+
+        }catch (QueryException $exception){
+            DB::rollBack();
+            return Response::create([
+                'message'  => '编辑失败,请重试',
+                'error' => $exception->getMessage(),
+                'status_code'   => 500
+            ]);
+        }
+    }
+
+    public function editStatus($request)
+    {
+        $floor = $this->floor->find($request['id']);
+
+        $floor->is_open = $request['is_open'];
+        $floor->updated_at = date('Y-m-d H:i:s');
+
+        $res = $floor->save();
+        if (!$res) {
+            throw new HttpException(500, '修改状态失败');
+        }
+
+    }
+
+    public function floorBind($request)
+    {
+        $banner_id = $request['banner_id'];
+        $floor_array = $this->floor->select('id','group_ids')->where('floor_type',0)->get();
+
+        $floor_ids = [];
+        foreach ($floor_array->toArray() as $value){
+            if ($banner_id == $value['group_ids']){
+                $floor_ids[] = $value['id'];
+            }
+        }
+        if (count($floor_ids)>0){
+            $edit_floor = $this->floor->whereIn('id',$floor_ids)->update(['is_open'=>0]);
+            if (!$edit_floor){
+                throw new HttpException(500, '该楼层关闭失败');
+            }
+        }
+
+    }
+
+}
+

+ 28 - 0
app/Transformers/FloorTransformer.php

@@ -0,0 +1,28 @@
+<?php
+namespace App\Transformers;
+/**
+ * Created by PhpStorm.
+ * User: durong
+ * Date: 2019/6/11
+ * Time: 下午3:22
+ */
+
+use App\Models\Floor;
+use League\Fractal\TransformerAbstract;
+
+class FloorTransformer extends TransformerAbstract
+{
+
+    public function transform(Floor $floor)
+    {
+        return [
+            'id'  => $floor['id'],
+            'name'  => $floor['name'],
+            'floor_location'    => $floor['floor_location'],
+            'group_ids'    => $floor['group_ids'],
+            'floor_type'    => $floor['floor_type'],
+            'is_open'    => $floor['is_open'],
+            'updated_at'    => date($floor['updated_at']),
+        ];
+    }
+}

+ 37 - 0
database/migrations/2019_06_04_021845_create_cms_floor_table.php

@@ -0,0 +1,37 @@
+<?php
+
+use Illuminate\Support\Facades\Schema;
+use Illuminate\Database\Schema\Blueprint;
+use Illuminate\Database\Migrations\Migration;
+
+class CreateCmsFloorTable extends Migration
+{
+    /**
+     * Run the migrations.
+     *
+     * @return void
+     */
+    public function up()
+    {
+        Schema::create('cms_floor', function (Blueprint $table) {
+            $table->increments('id');
+            $table->string('name', 20)->comment('社区-楼层名称');
+            $table->integer('floor_location')->comment('社区-楼层位置');
+            $table->string('group_ids')->nullable()->comment('社区-楼层类型对应的组id');
+            $table->tinyInteger('floor_type')->nullable()->comment('社区-楼层类型:0:banner;1.推荐关注;2.热门视频;3.推荐话题');
+            $table->tinyInteger('is_open')->default(0)->comment('社区-是否开启:0.否,1.是');
+            $table->timestamps();
+            $table->softDeletes();
+        });
+    }
+
+    /**
+     * Reverse the migrations.
+     *
+     * @return void
+     */
+    public function down()
+    {
+        Schema::dropIfExists('cms_floor');
+    }
+}

+ 13 - 1
routes/api.php

@@ -101,8 +101,20 @@ $api->version('v1', [
         'prefix' => 'v2'
     ], function ($api) {
         $api->group(['middleware' => 'jwt.chxq_auth'], function ($api) {
+
         });
         //banner专题计数
-        $api->post('count_subject', 'CmsContentTemplateSetController@count_subject');
+        $api->post('countSubject', 'CmsContentTemplateSetController@countSubject');
+
+        //楼层列表
+        $api->get('/floor/index', 'FloorController@index');
+        //新增楼层
+        $api->post('/floor/create', 'FloorController@create');
+        //编辑楼层
+        $api->put('/floor/edit', 'FloorController@edit');
+        //修改楼层状态
+        $api->put('/floor/editStatus', 'FloorController@editStatus');
+        //关闭banner修改楼层状态
+        $api->put('floorBind', 'FloorController@floorBind');
     });
 });