<?php

namespace app\admin\controller\Enrol;

use app\BaseController;
use app\common\model\Enrol\Classes as ModelClasses;
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 Classes extends BaseController
{
    /**
     * 获取班型列表
     */
    public function getClassesList(Request $request): array
    {
        $params = $request->param();
        $con = [];

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

        $query = ModelClasses::where($con)
            ->field([
                'classes_id',
                'classes_guid',
                'classes_name',
                'classes_cover',
                'classes_desc',
                'classes_content',
                'classes_status',
                'classes_sort'
            ])
            ->order('classes_sort', 'asc');

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

    /**
     * 编辑班型
     */
    public function editClasses(Request $request): array
    {
        $params = $request->param();
        $this->validate($params, [
            'classes_name|名称' => 'require',
            'classes_sort|排序' => 'require'
        ]);
        $model = ModelClasses::where('classes_guid', $params['classes_guid'])->find();
        if (!$model) throwErrorMsg("该班型不存在", 1);
        $model->allowField([
            'classes_update_user_guid',
            'classes_name',
            'classes_cover',
            'classes_desc',
            'classes_status',
            'classes_sort',
            'classes_content'
        ])->save($params);
        return msg('编辑成功!');
    }

    /**
     * 添加班型
     */
    public function addClasses(Request $request): array
    {
        $params = $request->param();
        $this->validate($params, [
            'classes_name|名称' => 'require',
            'classes_sort|排序' => 'require'
        ]);
        $model = ModelClasses::create($params, [
            'classes_guid',
            'classes_create_user_guid',
            'classes_update_user_guid',
            'classes_name',
            'classes_cover',
            'classes_desc',
            'classes_status',
            'classes_sort',
            'classes_content'
        ]);
        return msg('添加成功!');
    }

    /**
     * 删除班型
     */
    public function deleteClasses(Request $request): array
    {
        $params = $request->param();
        $this->validate($params, [
            'classes_guid' => 'require',
        ]);
        $classes = ModelClasses::where([
            'classes_guid' => explode(',', $params['classes_guid'])
        ])->select();
        $classes->delete();
        return msg('删除成功!');
    }

    /**
     * 导出Excel
     */
    public function exportExcel(Request $request)
    {
        $params = $request->param();
        $params['limit'] = 10000;
        $select = self::getClassesList($request)['data'];
        return ModelClasses::exportExcel($select);
    }

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

    /**
     * 导入excel
     */
    public function importExcel(Request $request)
    {
        $file = new UploadFile('uploads', 'fileExt:xlsx');
        $file->putFile('classes');

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