package com.ruoyi.system.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.*;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.mapper.FormalParentsStudentMapper;
import com.ruoyi.system.mapper.FormalTeacherClassMapper;
import com.ruoyi.system.mapper.SysDeptMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.CourseChangeMapper;
import com.ruoyi.system.domain.CourseChange;
import com.ruoyi.system.service.ICourseChangeService;

/**
 * 调课Service业务层处理
 *
 * @author ruoyi
 * @date 2023-06-02
 */
@Service
public class CourseChangeServiceImpl implements ICourseChangeService {
    @Autowired
    private CourseChangeMapper courseChangeMapper;
    @Autowired
    private FormalTeacherClassMapper formalTeacherClassMapper;
    @Autowired
    private FormalParentsStudentMapper formalParentsStudentMapper;
    @Autowired
    private SysDeptMapper deptMapper;

    /**
     * 查询调课
     *
     * @param id 调课主键
     * @return 调课
     */
    @Override
    public CourseChange selectCourseChangeById(Long id) {
        return courseChangeMapper.selectCourseChangeById(id);
    }

    /**
     * 查询调课列表
     *
     * @param courseChange 调课
     * @return 调课
     */
    @Override
    public List<CourseChange> selectCourseChangeList(CourseChange courseChange) {

        SysUser user = SecurityUtils.getLoginUser().getUser();
        List<SysRole> roles = user.getRoles();
        boolean bl = false;
        for (SysRole role : roles) {
            if ("teacher".equals(role.getRoleKey())) {
                bl = true;
                break;
            }
        }
        //角色是老师
        if (bl) {
            //查询老师班级
            FormalTeacherClass formalTeacherClass = new FormalTeacherClass();
            formalTeacherClass.setTeacherId(user.getUserId());
            List<FormalTeacherClass> formalTeacherClassList = formalTeacherClassMapper.selectFormalTeacherClassList(formalTeacherClass);
            List<Long> classIds = formalTeacherClassList.stream().map(FormalTeacherClass::getClassId).collect(Collectors.toList());
            courseChange.setSubjectClassIds(classIds);
            courseChange.setApplyId(user.getUserId());
            courseChange.setBePersonnelId(user.getUserId());
        }
        return courseChangeMapper.selectCourseChangeList(courseChange);
    }

    @Override
    public List<CourseChange> courseList(CourseChange courseChange) {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        List<SysRole> roles = user.getRoles();
        List<Long> classIdList = new ArrayList<>();
        for (SysRole role : roles) {
            if ("teacher".equals(role.getRoleKey())) {
                //查询老师班级
                FormalTeacherClass formalTeacherClass = new FormalTeacherClass();
                formalTeacherClass.setTeacherId(user.getUserId());
                List<FormalTeacherClass> formalTeacherClassList = formalTeacherClassMapper.selectFormalTeacherClassList(formalTeacherClass);
                List<Long> classIds = formalTeacherClassList.stream().map(FormalTeacherClass::getClassId).collect(Collectors.toList());
                classIdList.addAll(classIds);
            }
            if("parents".equals(role.getRoleKey())){
                FormalParentsStudent formalParentsStudent = new FormalParentsStudent();
                formalParentsStudent.setParentsId(user.getUserId());
                List<FormalParentsStudent> formalParentsStudentList = formalParentsStudentMapper.selectFormalParentsStudentList(formalParentsStudent);
                List<Long> classIds = formalParentsStudentList.stream().map(FormalParentsStudent::getClassId).collect(Collectors.toList());
                classIdList.addAll(classIds);
            }
            if("school".equals(role.getRoleKey())){
                List<SysDept> depts = deptMapper.selectDeptListChildren(user.getDeptId());
                List<Long> classIds = depts.stream().map(SysDept::getDeptId).collect(Collectors.toList());
                classIdList.addAll(classIds);
            }
        }
        courseChange.setSubjectClassIds(classIdList);
        return courseChangeMapper.selectCourseChangeList(courseChange);
    }

    /**
     * 新增调课
     *
     * @param courseChange 调课
     * @return 结果
     */
    @Override
    public AjaxResult insertCourseChange(CourseChange courseChange) {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        FormalTeacherClass formalTeacherClass = new FormalTeacherClass();
        formalTeacherClass.setTeacherId(user.getUserId());
        List<FormalTeacherClass> formalTeacherClassList = formalTeacherClassMapper.selectFormalTeacherClassList(formalTeacherClass);
        Boolean bl = false;
        for (FormalTeacherClass teacherClass : formalTeacherClassList) {
            if(courseChange.getSubjectClassId().equals(teacherClass.getClassId()) && courseChange.getSubject().equals(teacherClass.getDiscipline())){
                bl = true;
                break;
            }
        }
        if(!bl){
            return AjaxResult.error("请选择属于自己的课程。");
        }
        courseChange.setSchoolId(formalTeacherClassList.get(0).getSchoolId());
        courseChange.setSchoolName(formalTeacherClassList.get(0).getSchoolName());
        courseChange.setApplyId(user.getUserId());
        courseChange.setApplyName(formalTeacherClassList.get(0).getTeacherName());
        courseChange.setCreateTime(DateUtils.getNowDate());
        //查询被调课的老师信息
        FormalTeacherClass formalTeacherClassBe = new FormalTeacherClass();
        formalTeacherClassBe.setClassId(courseChange.getSubjectClassId());
        formalTeacherClassBe.setDiscipline(courseChange.getBeSubject());
        List<FormalTeacherClass> formalTeacherClassBeList = formalTeacherClassMapper.selectFormalTeacherClassList(formalTeacherClassBe);
        if(formalTeacherClassBeList==null || formalTeacherClassBeList.size()<=0){
            return AjaxResult.error();
        }
        courseChange.setBePersonnel(formalTeacherClassBeList.get(0).getTeacherName());
        courseChange.setBePersonnelId(formalTeacherClassBeList.get(0).getTeacherId());
        courseChangeMapper.insertCourseChange(courseChange);
        return AjaxResult.success();
    }

    /**
     * 修改调课
     *
     * @param courseChange 调课
     * @return 结果
     */
    @Override
    public int updateCourseChange(CourseChange courseChange) {
        courseChange.setUpdateTime(DateUtils.getNowDate());
        //查询被调课的老师信息
        FormalTeacherClass formalTeacherClassBe = new FormalTeacherClass();
        formalTeacherClassBe.setClassId(courseChange.getSubjectClassId());
        formalTeacherClassBe.setDiscipline(courseChange.getBeSubject());
        List<FormalTeacherClass> formalTeacherClassBeList = formalTeacherClassMapper.selectFormalTeacherClassList(formalTeacherClassBe);
        courseChange.setBePersonnel(formalTeacherClassBeList.get(0).getTeacherName());
        courseChange.setBePersonnelId(formalTeacherClassBeList.get(0).getTeacherId());
        return courseChangeMapper.updateCourseChange(courseChange);
    }

    /**
     * 批量删除调课
     *
     * @param ids 需要删除的调课主键
     * @return 结果
     */
    @Override
    public int deleteCourseChangeByIds(Long[] ids) {
        return courseChangeMapper.deleteCourseChangeByIds(ids);
    }

    /**
     * 删除调课信息
     *
     * @param id 调课主键
     * @return 结果
     */
    @Override
    public int deleteCourseChangeById(Long id) {
        return courseChangeMapper.deleteCourseChangeById(id);
    }

    @Override
    public AjaxResult updateCourseChangeReply(CourseChange courseChange) {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        //判断当前老师是否有权限回复该信息
        FormalTeacherClass formalTeacherClass = new FormalTeacherClass();
        formalTeacherClass.setTeacherId(user.getUserId());
        formalTeacherClass.setClassId(courseChange.getSubjectClassId());
        formalTeacherClass.setDiscipline(courseChange.getBeSubject());
        List<FormalTeacherClass> formalTeacherClassList = formalTeacherClassMapper.selectFormalTeacherClassList(formalTeacherClass);
        if (formalTeacherClassList == null || formalTeacherClassList.size() <= 0) {
            return AjaxResult.error("请回复自己授课的班级与课程");
        }
        courseChange.setAuditId(user.getUserId());
        courseChange.setAuditUser(user.getNickName());
        courseChange.setCreateTime(DateUtils.getNowDate());
        courseChangeMapper.updateCourseChange(courseChange);
        return AjaxResult.success();
    }

}
