<?php

namespace app\admin\controller\Product;

use app\BaseController;
use app\common\model\Product\ProductParam as ModelProductParam;
use app\Request;
use think\Validate;
use think\exception\ValidateException;
use think\facade\Filesystem;
use app\common\arw\adjfut\src\Excel;
use app\common\arw\adjfut\src\UploadFile;
use app\common\exception\Tool;
use think\facade\Db;
use think\facade\Env;


class ProductParam extends BaseController
{
    /**
     * 获取产品参数列表接口
     *
     * @param Request request
     * @date 2023-04-02
     * @example
     * @author xjh
     * @since 1.0.0
     */
    public function getProductParamList(Request $request): array
    {
        $con = Tool::getOptionalQuery(['product_param.product_type_guid', '=']);

        $query = ModelProductParam::where($con)
            ->field([
                'product_param.product_param_id',
                'product_param.product_param_guid',
                'product_param.product_param_name',
                'product_param.product_type_guid',
                'product_param.product_param_order',
                'product_type.product_type_name'
            ])
            ->leftJoin('product_type', 'product_type.product_type_guid =  product_param.product_type_guid')
            ->order('product_param_order', 'asc');

        return msg("获取产品参数列表成功!", $query);
    }

    /**
     * 获取指定系列参数模板接口
     *
     * @param Request request
     * @date 2023-04-02
     * @example
     * @author xjh
     * @since 1.0.0
     */
    public function getProductParamTemplate(Request $request): array
    {
        $params = $request->param();
        $this->validate($params, [
            'product_type_guid|产品系列guid' => 'require'
        ]);

        $data = ModelProductParam::field(['product_param_name', 'product_param_order'])
            ->where('product_type_guid', $params['product_type_guid'])
            ->order('product_param_order', 'desc')
            ->append(['product_param_value'])
            ->select();

        return msg(0, '获取指定系列参数模板成功！', ['data' => $data]);
    }

    /**
     * 编辑产品参数接口
     *
     * @param Request request
     * @date 2023-04-02
     * @example
     * @author xjh
     * @since 1.0.0
     */
    public function editProductParam(Request $request): array
    {
        $params = $request->param();
        $this->validate($params, [
            'product_param_name|产品参数名称' => 'require',
            'product_type_guid|产品系列guid' => 'require'
        ]);
        $model = ModelProductParam::where('product_param_guid', $params['product_param_guid'])->find();
        if (!$model) throwErrorMsg("该产品参数不存在", 1);
        $model->allowField([
            'product_param_update_user_guid',
            'product_param_name',
            'product_type_guid',
            'product_param_order'
        ])->save($params);
        return msg('编辑成功!');
    }

    /**
     * 添加产品参数接口
     *
     * @param Request request
     * @date 2023-04-02
     * @example
     * @author xjh
     * @since 1.0.0
     */
    public function addProductParam(Request $request): array
    {
        $params = $request->param();
        $this->validate($params, [
            'product_param_name|产品参数名称' => 'require',
            'product_type_guid|产品系列guid' => 'require'
        ]);
        $model = ModelProductParam::create($params, [
            'product_param_guid',
            'product_param_create_user_guid',
            'product_param_update_user_guid',
            'product_param_name',
            'product_param_order',
            'product_type_guid'
        ]);
        return msg('添加成功!');
    }

    /**
     * 删除产品参数接口
     *
     * @param Request request
     * @date 2023-04-02
     * @example
     * @author xjh
     * @since 1.0.0
     */
    public function deleteProductParam(Request $request): array
    {
        $params = $request->param();
        $this->validate($params, [
            'product_param_guid' => 'require',
        ]);
        $product_param = ModelProductParam::where([
            'product_param_guid' => explode(',', $params['product_param_guid'])
        ])->select();
        $product_param->delete();
        return msg('删除成功!');
    }
}
