<?php

namespace app\admin\controller\TeachersStrength;

use app\BaseController;
use app\common\model\TeachersStrength\Subject as ModelSubject;
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 Subject extends BaseController
{
    /**
     * 获取授课科目列表
     */
    public function getSubjectList(Request $request): array
    {
        $params = $request->param();
        $con = [];

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

        $query = ModelSubject::where($con)
            ->field([
                'subject_id',
                'subject_guid',
                'subject_name',
                'subject_sort'
            ])
            ->order('subject_sort', 'asc');

        return msg("获取授课科目列表成功!", $query);
    }

    /**
     * 编辑授课科目
     */
    public function editSubject(Request $request): array
    {
        Db::startTrans();
        try {
        $params = $request->param();
        $this->validate($params, [
            'subject_name|科目名字' => 'require',
            'subject_sort|排序' => 'require'
        ]);
        $model = ModelSubject::where('subject_guid', $params['subject_guid'])->find();
        if (!$model) throwErrorMsg("该授课科目不存在", 1);
        $model->allowField([
            'subject_update_user_guid',
            'subject_name',
            'subject_sort'
        ])->save($params);
        Db::commit();
        return msg('编辑成功!');
    } catch (\Throwable $th) {
        Db::rollback();
        throw  $th;
    }
}

    /**
     * 添加授课科目
     */
    public function addSubject(Request $request): array
    {
        Db::startTrans();
        try {
        $params = $request->param();
        $this->validate($params, [
            'subject_name|科目名字' => 'require',
            'subject_sort|排序' => 'require'
        ]);
        $model = ModelSubject::create($params, [
            'subject_guid',
            'subject_create_user_guid',
            'subject_update_user_guid',
            'subject_name',
            'subject_sort'
        ]);
        Db::commit();
        return msg('编辑成功!');
    } catch (\Throwable $th) {
        Db::rollback();
        throw  $th;
    }
}
    /**
     * 删除授课科目
     */
    public function deleteSubject(Request $request): array
    {
     Db::startTrans();
        try {
        $params = $request->param();
        $this->validate($params, [
            'subject_guid' => 'require',
        ]);
        $subject = ModelSubject::where([
            'subject_guid' => explode(',', $params['subject_guid'])
        ])->select();
        $subject->delete();
        Db::commit();
        return msg('编辑成功!');
    } catch (\Throwable $th) {
        Db::rollback();
        throw  $th;
    }
}
    /**
     * 导出Excel
     */
    public function exportExcel(Request $request)
    {
        $params = $request->param();
        $select = ModelSubject::field([
            'subject_name',
            'subject_sort'
        ])
            ->order('subject_update_time', 'desc')
            ->select();
        return ModelSubject::exportExcel($select);
    }

    /**
     * 下载导入模板
     */
    public function downloadTemplate(Request $request)
    {
        $params = $request->param();
        $data = [
            array_values(ModelSubject::EXCELFIELD),
            ['管理组', '1']
        ];
        $excel = (new Excel())->exporTsheet($data);
        $excel->save('授课科目导入模板.xlsx');
    }

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

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