<?php

namespace app\api\controller\TeachersStrength;

use app\BaseController;
use app\common\model\TeachersStrength\Teacher as ModelTeachers;
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 think\facade\Db;
use app\common\exception\Tool;
use think\facade\Env;


class Teachers extends BaseController
{
    /**
     * 获取教师列表
     */
    public function getTeachersList(Request $request): array
    {
        $params = $request->param();
        $con = [];

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

        $params['subject_id'] = null;
        foreach ($subject as $key => $value) {
            if ($key == $params['idx']) {
                $params['subject_id'] = $value['subject_id'];
            }
        }

        $con = Tool::getOptionalQuery(
            ['params' => $params],
            ['teacher_name', 'LIKE'],
            ['b.subject_id', '='],
        );

        $query = ModelTeachers::where($con)
            ->field([
                'a.teacher_id',
                'a.teacher_name',
                'a.teacher_position',
                'a.teacher_img',
                'a.subject_guid',
                'b.subject_name',
            ])
            ->alias('a')
            ->leftjoin('subject b', 'a.subject_guid = b.subject_guid')
            ->append(['teacher_eng_name'])
            ->hidden(['subject_guid'])
            ->order('teacher_order', 'asc')
            ->select();

        return msg(0, "获取教师列表成功!", [
            'count' => count($query),
            'data' => $query,
        ]);
    }

    /**
     * 获取教师详情
     */
    public function getTeachersInfo(Request $request): array
    {
        $params = $request->param();

        $this->validate($params, [
            'teacher_id|教师id' => 'require'
        ]);

        $find = ModelTeachers::field([
            'a.teacher_id',
            'a.teacher_name',
            'a.teacher_position',
            'a.teacher_img',
            'a.subject_guid',
            'b.subject_name',
            'a.teacher_content',
            'a.teacher_intro',
            'a.teacher_order',
        ])
            ->alias('a')
            ->leftjoin('subject b', 'a.subject_guid = b.subject_guid')
            ->append(['teacher_eng_name'])
            ->hidden(['subject_guid'])
            ->where('teacher_id', $params['teacher_id'])
            ->find();

        $last_next =  Tool::getLastNextData(
            ModelTeachers::class,
            $find->teacher_order,
            [
                'field' => ['teacher_id', 'teacher_name'],
                'extraWhere' => [['subject_guid', '=', $find->subject_guid]],
            ]
        );

        return msg(0, '获取教师详情成功!', [
            'data' => $find,
            'last' => $last_next[0],
            'next' => $last_next[1],
        ]);
    }
}
