package com.ruoyi.system.service.impl;

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

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.FormalTeacherClass;
import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.mapper.FormalTeacherClassMapper;
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;

    /**
     * 查询调课
     * 
     * @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);
        }

        return courseChangeMapper.selectCourseChangeList(courseChange);
    }

    /**
     * 新增调课
     * 
     * @param courseChange 调课
     * @return 结果
     */
    @Override
    public int insertCourseChange(CourseChange courseChange)
    {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        courseChange.setApplyId(user.getUserId());
        courseChange.setApplyName(user.getNickName());
        courseChange.setCreateTime(DateUtils.getNowDate());
        return courseChangeMapper.insertCourseChange(courseChange);
    }

    /**
     * 修改调课
     * 
     * @param courseChange 调课
     * @return 结果
     */
    @Override
    public int updateCourseChange(CourseChange courseChange)
    {
        courseChange.setUpdateTime(DateUtils.getNowDate());
        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());
        return AjaxResult.success();
    }
}
