<?php

namespace app\admin\controller\Test;

use app\BaseController;
use app\common\model\Test\Test as ModelTest;
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 Test extends BaseController
{
    /**
     * 获取测试列表
     */
    public function getTestList(Request $request): array
    {
        $params = $request->param();
        $con = [];

        $con = Tool::getOptionalQuery(['test_name','LIKE'],);

        $query = ModelTest::where($con)
            ->field([
'test_id',
'test_guid',
'test_name',
'test_show_status',
'test_score',
'test_sort'
])
            ->order('test_sort', 'desc');

        return msg("获取测试列表成功!",$query);
    }

    /**
     * 编辑测试
     */
    public function editTest(Request $request): array
    {
        $params = $request->param();
        $this->validate($params, [
'test_name|名称'=>'require',
'test_show_status|首页是否展示'=>'require',
'test_score|评分'=>'require',
'test_sort|排序'=>'require'
]);
        $model = ModelTest::where('test_guid',$params['test_guid'])->find();
        if (!$model) throwErrorMsg("该测试不存在", 1);
        $model->allowField([
'test_update_user_guid',
'test_name',
'test_show_status',
'test_score',
'test_sort'
])->save($params);
        return msg('编辑成功!');
    }

    /**
     * 添加测试
     */
    public function addTest(Request $request): array
    {
        $params = $request->param();
        $this->validate($params, [
'test_name|名称'=>'require',
'test_show_status|首页是否展示'=>'require',
'test_score|评分'=>'require',
'test_sort|排序'=>'require'
]);
        $model = ModelTest::create($params,[
'test_guid',
'test_create_user_guid',
'test_update_user_guid',
'test_name',
'test_show_status',
'test_score',
'test_sort'
]);
        return msg('添加成功!');
    }

    /**
     * 删除测试
     */
    public function deleteTest(Request $request): array
    {
        $params = $request->param();
        $this->validate($params, [
            'test_guid' => 'require',
        ]);
        $test = ModelTest::where([
            'test_guid' => explode(',', $params['test_guid'])
        ])->select();
        $test->delete();
        return msg('删除成功!');
    }

    /**
* 导出Excel
*/
                    public function exportExcel(Request $request)
                    {
                        $params = $request->param();
                        $select = ModelTest::field([
'test_name',
'test_show_status',
'test_score',
'test_sort'
])
                            ->order('test_sort', 'desc' )
                            ->select();
                        return ModelTest::exportExcel($select);
                    }

    /**
* 下载导入模板
*/
                public function downloadTemplate(Request $request)
                {
                    $params = $request->param();
                    $data = array_values(ModelTest::EXCELFIELD);
                    $excel = (new Excel())->exporTsheet($data);
                    $excel->save('测试导入模板.xlsx');
                }

    /**
* 导入excel
*/
                public function importExcel(Request $request)
                {
                    $file = new UploadFile('uploads', 'fileExt:xlsx');
                    $file->putFile('test');
               
                    $msg = ModelTest::importExcel($file);
                    return [
                        'code' => 0,
                        'msg' => $msg
                    ];
                }

}
