package com.yida.data.school.classSchedule.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yida.data.common.core.entity.classschedule.EduClassSchedule;
import com.yida.data.common.core.entity.classschedule.EduClassScheduleTimeClass;
import com.yida.data.school.classSchedule.mapper.EduClassScheduleMapper;
import com.yida.data.school.classSchedule.mapper.EduClassScheduleTimeClassMapper;
import com.yida.data.school.classSchedule.service.EduClassScheduleTimeClassService;
import com.yida.data.school.vo.classSchedule.ClassScheduleInfoVO;
import com.yida.data.school.vo.classSchedule.CourseTimeInfoVO;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

import cn.hutool.core.collection.CollUtil;
import lombok.RequiredArgsConstructor;

/**
 * 课程表关联班级表 Service实现
 *
 * @author wjm
 * @date 2021-08-13 16:53:38
 */
@Service
@RequiredArgsConstructor
@Transactional
public class EduClassScheduleTimeClassServiceImpl extends ServiceImpl
        <EduClassScheduleTimeClassMapper, EduClassScheduleTimeClass> implements EduClassScheduleTimeClassService {
    private final EduClassScheduleMapper eduClassScheduleMapper;

    @Override
    public List<ClassScheduleInfoVO> listClassScheduleInfo(Long classScheduleId, Long classId) {
        List<EduClassScheduleTimeClass> classScheduleInfo = baseMapper.getClassScheduleInfo(classScheduleId, classId);
        List<ClassScheduleInfoVO> classInfoList = new ArrayList<>();
        if (CollUtil.isNotEmpty(classScheduleInfo)) {
            // 按班级进行分组
            Map<Long, List<EduClassScheduleTimeClass>> classMap =
                    classScheduleInfo.stream().collect(Collectors.groupingBy(EduClassScheduleTimeClass::getClassId));
            for (Map.Entry<Long, List<EduClassScheduleTimeClass>> clazzInfo : classMap.entrySet()) {
                // 遍历每一个班级
                List<EduClassScheduleTimeClass> classTime = clazzInfo.getValue();
                ClassScheduleInfoVO classInfo = new ClassScheduleInfoVO();
                classInfo.setClassId(classTime.get(0).getClassId());
                classInfo.setGradeClass(classTime.get(0).getClassName());
                List<CourseTimeInfoVO> mon = new ArrayList<>();
                List<CourseTimeInfoVO> thue = new ArrayList<>();
                List<CourseTimeInfoVO> wed = new ArrayList<>();
                List<CourseTimeInfoVO> thu = new ArrayList<>();
                List<CourseTimeInfoVO> fri = new ArrayList<>();
                List<CourseTimeInfoVO> sat = new ArrayList<>();
                List<CourseTimeInfoVO> sun = new ArrayList<>();
                for (EduClassScheduleTimeClass timeClass : classTime) {
                    // 遍历每一堂课  填充到每天
                    CourseTimeInfoVO timeInfo = new CourseTimeInfoVO();
                    timeInfo.setCourseid(timeClass.getSubjectId());
                    timeInfo.setCourseName(timeClass.getCourseName());
                    timeInfo.setTeacherId(timeClass.getTeacherId());
                    timeInfo.setTeacherName(timeClass.getTeacherName());
                    timeInfo.setBeginTime(timeClass.getBeginTime());
                    timeInfo.setEndTime(timeClass.getEndTime());
                    timeInfo.setPlace(timeClass.getPlace());
                    timeInfo.setId(timeClass.getId());
                    switch (timeClass.getWeekId()) {
                        case 1:
                            mon.add(timeInfo);
                            break;
                        case 2:
                            thue.add(timeInfo);
                            break;
                        case 3:
                            wed.add(timeInfo);
                            break;
                        case 4:
                            thu.add(timeInfo);
                            break;
                        case 5:
                            fri.add(timeInfo);
                            break;
                        case 6:
                            sat.add(timeInfo);
                            break;
                        case 7:
                            sun.add(timeInfo);
                            break;
                    }
                }
                classInfo.setMon(mon);
                classInfo.setThue(thue);
                classInfo.setWed(wed);
                classInfo.setThu(thu);
                classInfo.setFri(fri);
                classInfo.setSat(sat);
                classInfo.setSun(sun);
                classInfoList.add(classInfo);
            }
        }
        return classInfoList;
    }

    @Override
    public ClassScheduleInfoVO getClassScheduleInfo(Long gradeId, Long classId, Long semsterId) {
        List<EduClassSchedule> scheduleList = eduClassScheduleMapper.selectList(Wrappers.lambdaQuery(new EduClassSchedule())
                .eq(EduClassSchedule::getGradeId, gradeId)
                .eq(EduClassSchedule::getStatus, 1)
                .eq(EduClassSchedule::getSemesterId, semsterId));
        ClassScheduleInfoVO classInfo = new ClassScheduleInfoVO();
        if (CollUtil.isNotEmpty(scheduleList)) {
            EduClassSchedule schedule = scheduleList.get(0);
            // 获取课程表和时间信息
            List<EduClassScheduleTimeClass> classTime = baseMapper.getClassScheduleInfo(schedule.getId(), classId);
            classInfo.setClassId(classTime.get(0).getClassId());
            classInfo.setGradeClass(classTime.get(0).getClassName());
            // 填充每一天的时间
            List<CourseTimeInfoVO> mon = new ArrayList<>();
            List<CourseTimeInfoVO> thue = new ArrayList<>();
            List<CourseTimeInfoVO> wed = new ArrayList<>();
            List<CourseTimeInfoVO> thu = new ArrayList<>();
            List<CourseTimeInfoVO> fri = new ArrayList<>();
            List<CourseTimeInfoVO> sat = new ArrayList<>();
            List<CourseTimeInfoVO> sun = new ArrayList<>();
            for (EduClassScheduleTimeClass timeClass : classTime) {
                // 遍历每一堂课  填充到每天
                CourseTimeInfoVO timeInfo = new CourseTimeInfoVO();
                timeInfo.setCourseid(timeClass.getSubjectId());
                timeInfo.setCourseName(timeClass.getCourseName());
                timeInfo.setTeacherId(timeClass.getTeacherId());
                timeInfo.setTeacherName(timeClass.getTeacherName());
                timeInfo.setBeginTime(timeClass.getBeginTime());
                timeInfo.setEndTime(timeClass.getEndTime());
                timeInfo.setPlace(timeClass.getPlace());
                timeInfo.setId(timeClass.getId());
                switch (timeClass.getWeekId()) {
                    case 1:
                        mon.add(timeInfo);
                        break;
                    case 2:
                        thue.add(timeInfo);
                        break;
                    case 3:
                        wed.add(timeInfo);
                        break;
                    case 4:
                        thu.add(timeInfo);
                        break;
                    case 5:
                        fri.add(timeInfo);
                        break;
                    case 6:
                        sat.add(timeInfo);
                        break;
                    case 7:
                        sun.add(timeInfo);
                        break;
                }
            }
            classInfo.setMon(mon);
            classInfo.setThue(thue);
            classInfo.setWed(wed);
            classInfo.setThu(thu);
            classInfo.setFri(fri);
            classInfo.setSat(sat);
            classInfo.setSun(sun);
        }
        return classInfo;
    }

    @Override
    public void updateClassTime(EduClassScheduleTimeClass timeClass) {
        if (timeClass.getSubjectId() == null && timeClass.getTeacherId() == null) {
            baseMapper.cleanTimeClass(timeClass.getId());
        } else {
            updateById(timeClass);
        }
    }
}
