<?php

namespace app\admin\controller\TeachersStrength;

use app\BaseController;
use app\common\model\TeachersStrength\Teacher as ModelTeacher;
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 Teacher extends BaseController
{
    /**
     * 获取教师列表
     */
    public function getTeacherList(Request $request): array
    {
        $params = $request->param();
        $con = [];

        $con = Tool::getOptionalQuery(['teacher_name', 'LIKE'], ['teacher_position', 'LIKE'], ['a.subject_guid', '='], ['a.teacher_status', '='],);

        $query = ModelTeacher::where($con)
            ->field([
                'a.teacher_id',
                'a.teacher_guid',
                'a.teacher_name',
                'a.teacher_position',
                'a.teacher_img',
                'a.subject_guid',
                'b.subject_name',
                'a.teacher_intro',
                'a.teacher_content',
                'a.teacher_status',
                'a.teacher_order'
            ])
            ->alias('a')
            ->leftJoin('subject b', 'a.subject_guid = b.subject_guid')
            ->order('teacher_order', 'asc');

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

    /**
     * 编辑教师
     */
    public function editTeacher(Request $request): array
    {
        Db::startTrans();
        try {
            $params = $request->param();
            $this->validate($params, [
                'teacher_name|教师名称' => 'require',
                'teacher_position|教师职位' => 'require',
                'teacher_img|教师图片' => 'require',
                'subject_guid|授课科目' => 'require',
                'teacher_intro|教师简介' => 'require',
                'teacher_order|教师排序' => 'require'
            ]);
            $model = ModelTeacher::where('teacher_guid', $params['teacher_guid'])->find();
            if (!$model) throwErrorMsg("该教师不存在", 1);
            $model->allowField([
                'teacher_update_user_guid',
                'teacher_name',
                'teacher_position',
                'teacher_img',
                'teacher_content',
                'subject_guid',
                'teacher_intro',
                'teacher_status',
                'teacher_order'
            ])->save($params);
            Db::commit();
            return msg('编辑成功!');
        } catch (\Throwable $th) {
            Db::rollback();
            throw  $th;
        }
    }

    /**
     * 添加教师
     */
    public function addTeacher(Request $request): array
    {
        Db::startTrans();
        try {
            $params = $request->param();
            $this->validate($params, [
                'teacher_name|教师名称' => 'require',
                'teacher_position|教师职位' => 'require',
                'teacher_img|教师图片' => 'require',
                'subject_guid|授课科目' => 'require',
                'teacher_intro|教师简介' => 'require',
                'teacher_order|教师排序' => 'require'
            ]);
            $model = ModelTeacher::create($params, [
                'teacher_guid',
                'teacher_create_user_guid',
                'teacher_update_user_guid',
                'teacher_name',
                'teacher_position',
                'teacher_content',
                'teacher_img',
                'subject_guid',
                'teacher_intro',
                'teacher_status',
                'teacher_order'
            ]);
            Db::commit();
            return msg('添加成功!');
        } catch (\Throwable $th) {
            Db::rollback();
            throw  $th;
        }
    }

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

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

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

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

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