<?php

namespace app\admin\controller;

use app\admin\logic\CompanyGoodLogic;
use app\BaseController;
use app\model\CommonModel;
use app\model\GoodModel;
use think\exception\ValidateException;
use think\facade\Config;
use think\facade\Validate;

//【公司商品】
class CompanyGood extends BaseController
{
    //列表
    public function list()
    {
        $param = $this->request->only(['group_id' => '', 'page' => 1, 'size' => 10, 'good_name' => '', 'good_code' => '', 'status' => '', 'type' => ''], 'post');

        $val = Validate::rule([
            'group_id|分组id' => 'number|gt:0',
            'page|页码' => 'require|number|gt:0',
            'size|每页数量' => 'require|number|elt:100',
            'good_name|商品名称' => 'number|elt:100',
            'good_cood|商品编码' => 'number|elt:100',
            'status|状态' => 'number|in:' . CommonModel::$status_normal . ',' . CommonModel::$status_disable,
            'type|商品类型' => 'number|in:' . GoodModel::$type_shopping . ',' . GoodModel::$type_exchange
        ]);

        if (!$val->check($param)) throw new ValidateException($val->getError());

        return CompanyGoodLogic::list($param);
    }

    //添加
    public function add()
    {
        $param = $this->request->only(['group_id', 'good_id'], 'post');

        $val = Validate::rule(Config::get('validate_rules.CompanyGoodAdd'));

        if (!$val->check($param)) throw new ValidateException($val->getError());

        return CompanyGoodLogic::add($param);
    }

    //详情
    public function read()
    {
        $id = $this->request->post('id/d', 0);
        return CompanyGoodLogic::read($id);
    }

    //编辑
    public function edit()
    {
        $param = $this->request->only(['id', 'group_id', 'good_id', 'is_top' => CommonModel::$top_no, 'weight' => 0], 'post');
        $val = Validate::rule(array_merge(Config::get('validate_rules.CompanyGoodAdd'), [
            'id|公司商品id' => 'require|number|gt:0',
        ]));

        if (!$val->check($param)) throw new ValidateException($val->getError());

        return CompanyGoodLogic::edit($param);
    }

    //删除
    public function delete()
    {
        $ids = $this->request->post('id/a', []);
        return CompanyGoodLogic::delete($ids);
    }

    //置顶
    public function top()
    {
        $param = $this->request->only(['id', 'is_top'], 'post');

        $val = Validate::rule([
            'id' => 'require|number|gt:0',
            'is_top|是否置顶' => 'require|number|in:' . CommonModel::$top_no . ',' . CommonModel::$top_yes,
        ]);

        if (!$val->check($param)) throw new ValidateException($val->getError());

        return CompanyGoodLogic::top($param);
    }

    //启禁用
    public function status()
    {
        $param = $this->request->only(['id', 'status'], 'post');

        $val = Validate::rule(Config::get('validate_rules.status'));

        if (!$val->check($param)) throw new ValidateException($val->getError());

        return CompanyGoodLogic::status($param);
    }

}