package com.xhkjedu.suser.service.gradeclass;

import com.xhkjedu.exception.MissingParametersException;
import com.xhkjedu.suser.mapper.gradeclass.ClassTeacherMapper;
import com.xhkjedu.suser.mapper.gradeclass.SchoolMapper;
import com.xhkjedu.suser.model.gradeclass.TClassTeacher;
import com.xhkjedu.suser.model.gradeclass.TSchool;
import com.xhkjedu.suser.model.system.TUser;
import com.xhkjedu.suser.service.system.UserService;
import com.xhkjedu.suser.vo.gradeclass.ClassTeacherParams;
import com.xhkjedu.suser.vo.gradeclass.ClassTeacherVo;
import com.xhkjedu.suser.vo.gradeclass.ClassVo;
import com.xhkjedu.utils.N_Utils;
import com.xhkjedu.vo.ResultVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author ywx
 * @classname TClassTeacherService
 * @description
 * @date 2020/6/2 16:45
 **/
@Service
public class ClassTeacherService {
    @Resource
    private ClassTeacherMapper classTeacherMapper;
    @Resource
    private UserService userService;
    @Resource
    private SchoolMapper schoolMapper;

    //添加
    public Integer save(TClassTeacher model) {
        String userphone = model.getUserphone();
        TUser user = userService.getObjByLoginName(userphone);
        if (user == null) {//教师信息在用户表不存在，添加教师信息
            user = new TUser();
            user.setUsertype(1);
            user.setUsername(model.getTeachername());
            user.setLoginname(userphone);
            user.setLoginpwd("123456");
            user.setUserphone(userphone);
            user.setCreateid(model.getCreateid());
            user.setSchoolid(model.getSchoolid());
            ResultVo vo = userService.save(user);
            if (vo.getCode() != 0) throw new MissingParametersException(vo.getMsg());
            user.setUserid((Integer) vo.getObj());
        }
        model.setTeacherid(user.getUserid());
        model.setCreatetime(N_Utils.getSecondTimestamp());
        classTeacherMapper.insertUseGeneratedKeys(model);//添加班级教师
        return model.getId();
    }

    //修改
    public void update(TClassTeacher model) {
        //修改教师信息
        TUser user = new TUser();
        user.setUserid(model.getTeacherid());
        user.setUsername(model.getTeachername());
        String userphone = model.getUserphone();
        user.setUserphone(userphone);
        userService.update(user);
    }


    //获取列表
    public List <ClassTeacherVo> findAll(TClassTeacher teacher, Integer year) {
        teacher.setYear(year);
        String fieldname = teacher.getFieldname();
        if (N_Utils.isEmpty(fieldname) || fieldname.equals("createtime")){
            fieldname = "u.createtime";
            if (N_Utils.isEmpty(teacher.getFieldorder())) {
                teacher.setFieldorder("desc");
            }
        } else if (fieldname.equals("teachername")){
            fieldname = "convert(u.username using gbk)";
        } else if (fieldname.equals("loginname")){
            fieldname = "u.loginname";
        }
        teacher.setFieldname(fieldname);
        return classTeacherMapper.findAll(teacher);
    }

    public Integer getYear(Integer schoolid) {
        return schoolMapper.getYearById(schoolid);
    }

    //当前年份教师任教班级
    public List<ClassVo> listTeacherClass(Integer teacher,Integer classtype){
        TSchool school = schoolMapper.getSchoolInfoByUserid(teacher);
        Integer year = school.getYear();
        return classTeacherMapper.listTeacherClass(teacher,year,classtype,school.getSchoolid());
    }
    //当前年份教师任教班级
    public List<ClassVo> listTeacherClass2(Integer teacher){
        TSchool school = schoolMapper.getSchoolInfoByUserid(teacher);
        Integer year = school.getYear();
        return classTeacherMapper.listTeacherClass2(teacher,year,school.getSchoolid());
    }

    /**
     * 获取作业学生列表
     * @Param [paperid]
     * @Author ywx
     * @Date 2020/7/10 15:51
     * @return java.util.List<java.util.Map>
     **/
    public List<Map> listPaperStudent(Integer paperid) {
        return classTeacherMapper.listPaperStudent(paperid);
    }

    //获取作业教师列表
    public List<Map> listPaperTeacher(Integer paperid, String subjectid) {
        return classTeacherMapper.listPaperTeacher(paperid, subjectid);
    }

    /**
     *功能描述 年级下所有教师
     * @author WN
     * @date 2021/1/23
     * @param  * @param schoolid
     * @param gradeid
     * @return java.util.List<com.xhkjedu.model.gradeclass.TClassTeacher>
     */
    public List<ClassTeacherVo> listTeachersByGradeid(Integer schoolid,Integer gradeid){
        Integer year = schoolMapper.getYearById(schoolid);
        return classTeacherMapper.listTeachersByGradeid(schoolid,gradeid,year);
    }

    /**
     *功能描述  获取年级科目下教师集合
     * @author WN
     * @date 2021/1/29
     * @param  * @param ctp
     * @return java.util.List<com.xhkjedu.suser.vo.gradeclass.ClassTeacherVo>
     */
    public List<ClassTeacherVo> listTeahcersBySubjectGrade(ClassTeacherParams ctp) {
        Integer year = schoolMapper.getYearById(ctp.getSchoolid());
        ctp.setYear(year);
        return classTeacherMapper.listTeahcersBySubjectGrade(ctp);
    }

    /**
     * 获取年级科目下老师(标识是否是本班教师)
     *
     * @param [ctp]
     * @return java.util.List<java.util.Map>
     * @author ywx
     * @date 2022/5/5 11:29
     */
    public List<Map> listTeahcersBySubjectGrade2(ClassTeacherParams ctp) {
        Integer year = schoolMapper.getYearById(ctp.getSchoolid());
        ctp.setYear(year);
        return classTeacherMapper.listTeahcersBySubjectGrade2(ctp);
    }
}
