<?php

namespace app\admin\controller\Product;

use app\BaseController;
use app\common\model\Product\Product as ModelProduct;
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 Product extends BaseController
{
    /**
     * 获取产品列表接口
     *
     * @param Request request
     * @date 2023-03-25
     * @example
     * @author xjh
     * @since 1.0.0
     */
    public function getProductList(Request $request): array
    {
        Db::startTrans();
        try {
            $params = $request->param();
            $con = [];

            $con = Tool::getOptionalQuery(
                ['product.product_name', 'LIKE'],
                ['product.product_type_guid'],
            );

            $query = ModelProduct::where($con)
                ->leftJoin('product_type', 'product_type.product_type_guid = product.product_type_guid')
                ->field([
                    'product.product_id',
                    'product.product_guid',
                    'product.product_type_guid',
                    'product.product_name',
                    'product.product_img',
                    'product.product_params',
                    'product.product_details',
                    'product.product_price',
                    'product_type.product_type_name'
                ])
                ->order('product_id', 'desc');

            return msg("获取产品列表成功!", $query);
            Db::commit();
        } catch (\Throwable $th) {
            Db::rollback();
            throw  $th;
        }
    }

    /**
     * 编辑产品接口
     *
     * @param Request request
     * @date 2023-03-25
     * @example
     * @author xjh
     * @since 1.0.0
     */
    public function editProduct(Request $request): array
    {
        $params = $request->param();
        $this->validate($params, [
            'product_type_guid|产品系列guid' => 'require',
            'product_name|名称' => 'require',
            'product_img|图片' => 'require',
            'product_details|详情' => 'require',
            'product_price|价格' => 'require'
        ]);
        $model = ModelProduct::where('product_guid', $params['product_guid'])->find();
        if (!$model) throwErrorMsg("该产品不存在", 1);
        // Tool::sortEditProc(
        //     new ModelProduct,
        //     ["product_guid" => $params['product_guid']],
        //     ["product_type_order" => $params['product_type_order']],
        // );
        $model->allowField([
            'product_update_user_guid',
            'product_type_guid',
            'product_name',
            'product_img',
            'product_params',
            'product_details',
            'product_price'
        ])->save($params);
        return msg('编辑成功!');
    }

    /**
     * 添加产品接口
     *
     * @param Request request
     * @date 2023-03-25
     * @example
     * @author xjh
     * @since 1.0.0
     */
    public function addProduct(Request $request): array
    {
        $params = $request->param();
        $this->validate($params, [
            'product_type_guid|产品系列guid' => 'require',
            'product_name|名称' => 'require',
            'product_img|图片' => 'require',
            'product_details|详情' => 'require',
            'product_price|价格' => 'require'
        ]);

        ModelProduct::create($params, [
            'product_guid',
            'product_create_user_guid',
            'product_update_user_guid',
            'product_type_guid',
            'product_name',
            'product_img',
            'product_params',
            'product_details',
            'product_price'
        ]);
        return msg('添加成功!');
    }

    /**
     * 删除产品接口
     *
     * @param Request request
     * @date 2023-03-25
     * @example
     * @author xjh
     * @since 1.0.0
     */
    public function deleteProduct(Request $request): array
    {
        $params = $request->param();
        $this->validate($params, [
            'product_guid|产品系列' => 'require',
        ]);
        $product = ModelProduct::where([
            'product_guid' => explode(',', $params['product_guid'])
        ])->select();
        $product->delete();
        return msg('删除成功!');
    }

    /**
     * 导出Excel接口
     *
     * @param Request request
     * @date 2023-03-25
     * @example
     * @author xjh
     * @since 1.0.0
     */
    public function exportExcel(Request $request)
    {
        $params = $request->param();
        $select = ModelProduct::field([
            'product_type_guid',
            'product_name',
            'product_img',
            'product_params',
            'product_details',
            'product_price'
        ])
            ->order('product_update_time', 'desc')
            ->select();
        return ModelProduct::exportExcel($select);
    }

    /**
     * 下载导入模板接口
     *
     * @param Request request
     * @date 2023-03-25
     * @example
     * @author xjh
     * @since 1.0.0
     */
    public function downloadTemplate(Request $request)
    {
        $params = $request->param();
        $data = array_values(ModelProduct::EXCELFIELD);
        $excel = (new Excel())->exporTsheet($data);
        $excel->save('产品导入模板.xlsx');
    }

    /**
     * 导入excel接口
     *
     * @param Request request
     * @date 2023-03-25
     * @example
     * @author xjh
     * @since 1.0.0
     */
    public function importExcel(Request $request)
    {
        $file = new UploadFile('uploads', 'fileExt:xlsx');
        $file->putFile('product');

        $msg = ModelProduct::importExcel($file);
        return [
            'code' => 0,
            'msg' => $msg
        ];
    }
}
