<?php

namespace app\admin\controller\{$moduleName};

use app\BaseController;
use app\common\model\{$moduleName}\{$className} as Model{$className};
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 {$className} extends BaseController
{
    /**
     * 获取{$functionName}列表
     */
    public function get{$className}List(Request $request, $isExport = false): array
    {
        $params = $request->param();
        $con = [];

        {$whereContent}

        $query = Model{$className}::where($con)
            ->field({$queryFields})
            ->order('{$orderField}', '{$orderMode}');

        return $isExport ?  $query->select()->toArray() : msg("获取{$functionName}列表成功!",$query);
    }

    /**
     * 编辑{$functionName}
     */
    public function edit{$className}(Request $request): array
    {
        Db::startTrans();
        try {
        $params = $request->param();
        $this->validate($params, {$editRequireFields});
        $model = Model{$className}::where('{$businessName}_guid',$params['{$businessName}_guid'])->find();
        if (!$model) throwErrorMsg("该{$functionName}不存在", 1);
        $model->allowField({$editAllowFields})->save($params);
        Db::commit();
        return msg('编辑成功!');
        } catch (\Throwable $th) {
            Db::rollback();
            throw  $th;
        }
    }

    /**
     * 添加{$functionName}
     */
    public function add{$className}(Request $request): array
    {
        Db::startTrans();
        try {
        $params = $request->param();
        $this->validate($params, {$addRequireFields});
        $model = Model{$className}::create($params,{$addAllowFields});
        Db::commit();
        return msg('添加成功!');
        } catch (\Throwable $th) {
            Db::rollback();
            throw  $th;
        }
    }

    /**
     * 删除{$functionName}
     */
    public function delete{$className}(Request $request): array
    {
        Db::startTrans();
        try {
        $params = $request->param();
        $this->validate($params, [
            '{$businessName}_guid' => 'require',
        ]);
        ${$businessName} = Model{$className}::where([
            '{$businessName}_guid' => explode(',', $params['{$businessName}_guid'])
        ])->select();
        ${$businessName}->delete();
        Db::commit();
        return msg('删除成功!');
        } catch (\Throwable $th) {
            Db::rollback();
            throw  $th;
        }
    }

    {$exportExcelContent}

    {$downloadTempContent}

    {$importExcelContent}

}
