<?php
// +----------------------------------------------------------------------
// | [ WE CAN DO IT MORE SIMPLE  ]
// +----------------------------------------------------------------------
// | Copyright (c) 2018-2020 rights reserved.
// +----------------------------------------------------------------------
// | Author: TABLE ME
// +----------------------------------------------------------------------
// | Date: 2020-08-31 20:43
// +----------------------------------------------------------------------

namespace app\system\controller\v1;


use app\BaseController;
use app\model\system\Platform as PlatformModel;
use app\Request;
use library\services\UtilService;

class Platform extends  BaseController
{
    /**
     * 平台列表
     * @param Request $request
     */
    public function list(Request $request) {
        $pageSize = 50;
        [$page,$status,$name] =  UtilService::getMore(
            [
                ['page',1],
                ['status','1'],
                ['name','']
            ],$request,true
        );
        list($pageCount,$data) = (new PlatformModel)
            ->getList($page, ['name'=>[$name,'!empty']],$pageSize,'seq desc');
        $result = UtilService::getParam([
            'id',
            'name',
            'code',
            'seq',
            'img',
            'status'
        ],$data);
        return app('json')->success([
            'list'      => $result,
            'pageCount' => $pageCount,
            'pageSize'  => $pageSize,
            'page'      => $page
        ]);
    }



    /**
     * 保存数据
     * @param Request $request
     */
    public function  save(Request $request) {
        $post = UtilService::getMore(
            [
                ['id','0'],
                ['name','','empty','请输入平台来源'],
                ['code','','empty','请输入平台code'],
                ['status','0'],
                ['seq','0'],
                ['img','']
            ],
            $request
        );
        $bool = (new PlatformModel)->saveModel($post);
        if($bool)
            return app('json')->success("操作成功", []);
        else
            return app('json')->fail(PlatformModel::getErrorInfo());
    }

    /**
     * 获取详情数据
     * @param Request $request
     * @return mixed
     */
    public function info(Request $request) {
        [$id] = UtilService::getMore([
            ['id','','empty','参数错误']
        ],$request,true);
        $info = PlatformModel::get(compact('id'))->toArray();
        return app('json')->success($info);
    }

    /**
     * 删除数据
     * @param Request $request
     */
    public function del(Request $request) {
        [$id] = UtilService::getMore(
            [
                ['id','0'],
            ],
            $request,true
        );
        PlatformModel::del($id);
        return app('json')->success("删除成功", []);
    }
}