<?php

namespace app\admin\controller\Gen\GenApi\GenController;

use app\admin\controller\Gen\GenApi\GenApi;
use app\common\exception\Tool;

/**
 * 后端后台接口生成类
 */
class Admin extends GenApi
{
    /**
     * 生成后台控制器
     */
    public function createAdminController(): void
    {
        //创建后台控制器层模块目录
        self::createModuleMkdir($this->admin_con_module_path);
        //模板文件字符串替换
        $temp_str = Tool::strReplacePlus(
            self::getTempStr($this->admin_con_temp_path),
            [
                //实体类名
                '{$className}' => $this->class_name,
                //模块名
                '{$moduleName}' => $this->module_name,
                //业务名
                '{$businessName}' => $this->business_name,
                //方法名
                '{$functionName}' => $this->function_name,
                //查询列表显示字段
                '{$queryFields}' => self::toFormTempStr($this->query_list_fields),
                //排序字段
                '{$orderField}' => $this->sort_field,
                //排序类型
                '{$orderMode}' => $this->sort_type,
                //新增允许字段
                '{$addAllowFields}' => $this->add_fields_temp,
                //编辑允许字段
                '{$editAllowFields}' => $this->edit_fields_temp,
                //列表查询条件
                '{$whereContent}' => $this->query_list_where_temp,
                //导出接口
                '{$exportExcelContent}' => $this->buildExportExcelApiTemp(),
                //导入接口
                '{$importExcelContent}' => $this->buildImportExcelApiTemp(),
                //下载模板接口
                '{$downloadTempContent}' => $this->buildDownloadTemplateApiTemp(),
                //编辑必填验证
                '{$editRequireFields}' => $this->edit_required_verify_temp,
                //新增必填验证
                '{$addRequireFields}' => $this->add_required_verify_temp,
            ]
        );
        //文件写入
        self::writeFile($this->admin_con_path, $temp_str);
    }

    /**
     * 构建Excel导出接口模板字符串
     */
    private function buildExportExcelApiTemp(): string
    {
        if (!$this->is_export) return '';

        //模板字符串构建并返回
        return "/**\n* 导出Excel\n*/
        public function exportExcel(Request \$request):void
        {
             Model{$this->class_name}::exportExcel(self::get{$this->class_name}List(\$request, true));
        }";
    }

    /**
     * 构建Excel导入接口模板字符串
     */
    private function buildImportExcelApiTemp(): string
    {
        if (!$this->is_import) return '';

        //模板字符串构建并返回
        return "/**\n* 导入excel\n*/
        public function importExcel(Request \$request):array
        {
            \$file = new UploadFile('uploads', 'fileExt:xlsx');
            \$file->putFile('{$this->business_name}');
            \$msg = Model{$this->class_name}::importExcel(\$file);
            return [
                'code' => 0,
                'msg' => \$msg
            ];
        }";
    }

    /**
     * 构建Excel下载模板接口模板字符串
     */
    private function buildDownloadTemplateApiTemp(): string
    {
        if (!$this->is_import) return '';

        $initial_value_temp = '';
        for($i =0 ;$i<count($this->business_fields);$i++){
            $index =$i+1;
            $initial_value_temp .= "'默认值{$index}',";
        }

        //模板字符串构建并返回
        return "/**\n* 下载导入模板\n*/
        public function downloadTemplate(Request \$request):void
        {
            \$params = \$request->param();
            \$data = [
                array_values(Model{$this->class_name}::EXCELFIELD),
                [{$initial_value_temp}]
            ];
            \$excel = (new Excel())->exporTsheet(\$data);
            \$excel->save('{$this->function_name}导入模板.xlsx');
        }";
    }
}
