package com.ruoyi.system.service.impl;

import java.util.*;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.CourseChange;
import com.ruoyi.system.domain.CourseTableTime;
import com.ruoyi.system.mapper.CourseChangeMapper;
import com.ruoyi.system.mapper.CourseTableTimeMapper;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.CourseTableMapper;
import com.ruoyi.system.domain.CourseTable;
import com.ruoyi.system.service.ICourseTableService;

/**
 * 课程Service业务层处理
 *
 * @author ruoyi
 * @date 2023-05-24
 */
@Service
public class CourseTableServiceImpl implements ICourseTableService {
    @Autowired
    private CourseTableMapper courseTableMapper;
    @Autowired
    private CourseTableTimeMapper courseTableTimeMapper;

    @Autowired
    private RedisCache redisCache;

    @Autowired
    private CourseChangeMapper courseChangeMapper;

    /**
     * 查询课程
     *
     * @param id 课程主键
     * @return 课程
     */
    @Override
    public CourseTable selectCourseTableById(Long id) {
        return courseTableMapper.selectCourseTableById(id);
    }

    /**
     * 查询课程列表
     *
     * @param courseTable 课程
     * @return 课程
     */
    @SneakyThrows
    @Override
    public List<CourseTable> selectCourseTableList(CourseTable courseTable) {
        if(StringUtils.isEmpty(courseTable.getClassId()) || StringUtils.isEmpty(courseTable.getSchoolId())){
            return new ArrayList<>();
        }
        List<CourseTable> courseTableList = courseTableMapper.selectCourseTableList(courseTable);
        //获取本周一和本周五的时间
        String fist = DateUtils.getTimeIntervalOne(new Date());
        String five = DateUtils.getTimeIntervalFive(fist);
        List<CourseChange> courseChangeList = courseChangeMapper.selectCourseChangeListByEdit(courseTable.getClassId(), fist, five);
        if (courseChangeList == null || courseChangeList.size() <=0){
            return courseTableList;
        }

        for (CourseTable table : courseTableList) {
            changeCourseTable(table,courseChangeList,fist,five);
        }

        return courseTableList;
    }

    @SneakyThrows
    private void changeCourseTable(CourseTable table, List<CourseChange> courseChangeList, String fist, String five) {
        String format = "yyyy-MM-dd";
        Set<Long> classNum = new HashSet<>();
        for (CourseChange courseChange : courseChangeList) {
            //调课的时间是否在本周
            if(DateUtils.isClass(fist,five,DateUtils.dateTime(courseChange.getSubjectTime()),format)){
                if(StringUtils.equals(courseChange.getSubjectWeek(),table.getWeek())){
                    classNum.add(Long.valueOf(courseChange.getIsNum()));
                    //第几节课
                    if(StringUtils.equals("1",courseChange.getIsNum())){
                        table.setOneClass(courseChange.getBeSubject());
                        table.setOneTeacher(courseChange.getBePersonnel());
                    }else if(StringUtils.equals("2",courseChange.getIsNum())){
                        table.setTwoClass(courseChange.getBeSubject());
                        table.setTwoTeacher(courseChange.getBePersonnel());
                    }else if(StringUtils.equals("3",courseChange.getIsNum())){
                        table.setThreeClass(courseChange.getBeSubject());
                        table.setThreeTeacher(courseChange.getBePersonnel());
                    }else if(StringUtils.equals("4",courseChange.getIsNum())){
                        table.setFourClass(courseChange.getBeSubject());
                        table.setFourTeacher(courseChange.getBePersonnel());
                    }else if(StringUtils.equals("5",courseChange.getIsNum())){
                        table.setFiveClass(courseChange.getBeSubject());
                        table.setFiveTeacher(courseChange.getBePersonnel());
                    }else if(StringUtils.equals("6",courseChange.getIsNum())){
                        table.setSixClass(courseChange.getBeSubject());
                        table.setSixTeacher(courseChange.getBePersonnel());
                    }else if(StringUtils.equals("7",courseChange.getIsNum())){
                        table.setSevenClass(courseChange.getBeSubject());
                        table.setSevenTeacher(courseChange.getBePersonnel());
                    }else if(StringUtils.equals("8",courseChange.getIsNum())){
                        table.setEightClass(courseChange.getBeSubject());
                        table.setEightTeacher(courseChange.getBePersonnel());
                    }
                }
            }
            //被调课是否在本周
            if(DateUtils.isClass(fist,five,DateUtils.dateTime(courseChange.getBeSubjectTime()),format)) {
                if(StringUtils.equals(courseChange.getWeek(),table.getWeek())){
                    classNum.add(Long.valueOf(courseChange.getBeIsNum()));
                    //第几节课
                    if(StringUtils.equals("1",courseChange.getBeIsNum())){
                        table.setOneClass(courseChange.getSubject());
                        table.setOneTeacher(courseChange.getApplyName());
                    }else if(StringUtils.equals("2",courseChange.getBeIsNum())){
                        table.setTwoClass(courseChange.getSubject());
                        table.setTwoTeacher(courseChange.getApplyName());
                    }else if(StringUtils.equals("3",courseChange.getBeIsNum())){
                        table.setThreeClass(courseChange.getSubject());
                        table.setThreeTeacher(courseChange.getApplyName());
                    }else if(StringUtils.equals("4",courseChange.getBeIsNum())){
                        table.setFourClass(courseChange.getSubject());
                        table.setFourTeacher(courseChange.getApplyName());
                    }else if(StringUtils.equals("5",courseChange.getBeIsNum())){
                        table.setFiveClass(courseChange.getSubject());
                        table.setFiveTeacher(courseChange.getApplyName());
                    }else if(StringUtils.equals("6",courseChange.getBeIsNum())){
                        table.setSixClass(courseChange.getSubject());
                        table.setSixTeacher(courseChange.getApplyName());
                    }else if(StringUtils.equals("7",courseChange.getBeIsNum())){
                        table.setSevenClass(courseChange.getSubject());
                        table.setSevenTeacher(courseChange.getApplyName());
                    }else if(StringUtils.equals("8",courseChange.getBeIsNum())){
                        table.setEightClass(courseChange.getSubject());
                        table.setEightTeacher(courseChange.getApplyName());
                    }
                }
            }
        }
        table.setClassNum(classNum);
    }


    /**
     * 新增课程
     *
     * @param courseTable 课程
     * @return 结果
     */
    @Override
    public int insertCourseTable(CourseTable courseTable) {
        courseTable.setCreateTime(DateUtils.getNowDate());
        return courseTableMapper.insertCourseTable(courseTable);
    }

    /**
     * 修改课程
     *
     * @param courseTable 课程
     * @return 结果
     */
    @Override
    public int updateCourseTable(CourseTable courseTable) {
        courseTable.setUpdateTime(DateUtils.getNowDate());
        return courseTableMapper.updateCourseTable(courseTable);
    }

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

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

    @SneakyThrows
    @Override
    public AjaxResult now(Long schoolId, Long classId) {
        //SysUser user = SecurityUtils.getLoginUser().getUser();
        Map<String, Object> map = new HashMap<>();
        map.put("teacher", "");
        map.put("subject", "");
        //家长端查询下课时间
        String key = schoolId + ":" + classId;
        //key = 学校id:班级id
        //Redis根据key键，查询对应的值
        String xiakeTime = "";
        String value = redisCache.getCacheObject(key);
        if (StringUtils.isNotEmpty(value)) {
            String[] split = value.split(":");
            //下课时间
            xiakeTime = split[1];
        }
        map.put("time", xiakeTime);
        //获取当前时间
        String nowTime = DateUtils.getTime();
        //判断当天是周几
        int dayForWeek = DateUtils.dayForWeek(nowTime);
        if (dayForWeek == 6 || dayForWeek == 7) {
            return AjaxResult.success(map);
        }
        //获取当前账号所属学校时间表
        CourseTableTime courseTableTime = courseTableTimeMapper.selectCourseTableTimeBySchoolId(schoolId);
        if (courseTableTime == null) {
            return AjaxResult.success(map);
        }
        //判断当前时间是第几节课
        int index = isClass(courseTableTime);
        if (index == 0) {
            return AjaxResult.success(map);
        }
        //获取当前班级课程
        CourseTable courseTable = courseTableMapper.selectCourseTableByClassIdAndWeek(classId, dayForWeek);
        if (courseTable == null) {
            return AjaxResult.success(map);
        }
        if (index == 1) {
            map.put("teacher", courseTable.getOneTeacher());
            map.put("subject", courseTable.getOneClass());
        } else if (index == 2) {
            map.put("teacher", courseTable.getTwoTeacher());
            map.put("subject", courseTable.getTwoClass());
        } else if (index == 3) {
            map.put("teacher", courseTable.getThreeTeacher());
            map.put("subject", courseTable.getThreeClass());
        } else if (index == 4) {
            map.put("teacher", courseTable.getFourTeacher());
            map.put("subject", courseTable.getFourClass());
        } else if (index == 5) {
            map.put("teacher", courseTable.getFiveTeacher());
            map.put("subject", courseTable.getFiveClass());
        } else if (index == 6) {
            map.put("teacher", courseTable.getSixTeacher());
            map.put("subject", courseTable.getSixClass());
        } else if (index == 7) {
            map.put("teacher", courseTable.getSevenTeacher());
            map.put("subject", courseTable.getSevenClass());
        } else if (index == 8) {
            map.put("teacher", courseTable.getEightTeacher());
            map.put("subject", courseTable.getEightClass());
        }

        return AjaxResult.success(map);
    }

    @SneakyThrows
    private int isClass(CourseTableTime courseTableTime) {
        //获取当前时间
        String nowTime = DateUtils.getNowTime();
        int index = 0;
        if (StringUtils.isNotEmpty(courseTableTime.getOneStartTime()) && DateUtils.isClass(courseTableTime.getOneStartTime(), courseTableTime.getOneEndTime(), nowTime, "HH:mm")) {
            index = 1;
        } else if (StringUtils.isNotEmpty(courseTableTime.getTwoStartTime()) && DateUtils.isClass(courseTableTime.getTwoStartTime(), courseTableTime.getTwoEndTime(), nowTime, "HH:mm")) {
            index = 2;
        } else if (StringUtils.isNotEmpty(courseTableTime.getThreeStartTime()) && DateUtils.isClass(courseTableTime.getThreeStartTime(), courseTableTime.getThreeEndTime(), nowTime, "HH:mm")) {
            index = 3;
        } else if (StringUtils.isNotEmpty(courseTableTime.getFourStartTime()) && DateUtils.isClass(courseTableTime.getFourStartTime(), courseTableTime.getFourEndTime(), nowTime, "HH:mm")) {
            index = 4;
        } else if (StringUtils.isNotEmpty(courseTableTime.getFiveStartTime()) && DateUtils.isClass(courseTableTime.getFiveStartTime(), courseTableTime.getFiveEndTime(), nowTime, "HH:mm")) {
            index = 5;
        } else if (StringUtils.isNotEmpty(courseTableTime.getSixStartTime()) && DateUtils.isClass(courseTableTime.getSixStartTime(), courseTableTime.getSixEndTime(), nowTime, "HH:mm")) {
            index = 6;
        } else if (StringUtils.isNotEmpty(courseTableTime.getSevenStartTime()) && DateUtils.isClass(courseTableTime.getSevenStartTime(), courseTableTime.getSevenEndTime(), nowTime, "HH:mm")) {
            index = 7;
        } else if (StringUtils.isNotEmpty(courseTableTime.getEightStartTime()) && DateUtils.isClass(courseTableTime.getEightStartTime(), courseTableTime.getEightEndTime(), nowTime, "HH:mm")) {
            index = 8;
        }
        return index;
    }

}
