package com.ym.mec.biz.service.impl;

import com.ym.mec.biz.dal.dao.CourseScheduleDao;
import com.ym.mec.biz.dal.dto.CourseScheduleDto;
import com.ym.mec.biz.dal.entity.CourseSchedule;
import com.ym.mec.biz.dal.enums.GroupType;
import com.ym.mec.biz.service.TeacherCourseScheduleService;
import com.ym.mec.common.exception.BizException;
import com.ym.mec.common.tenant.TenantContextHolder;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @Author Joburgess
 * @Date 2020/3/10
 */
@Service
public class TeacherCourseScheduleServiceImpl implements TeacherCourseScheduleService {

    @Autowired
    private CourseScheduleDao courseScheduleDao;

    @Override
    public List<Date> findTeacherHaveClassesDates(Integer teacherId, Date month, String groupId, GroupType type) {
        return courseScheduleDao.findHaveClassesDatesWithMonth(teacherId,month,groupId,type, TenantContextHolder.getTenantId());
    }

    @Override
    public List<CourseScheduleDto> findGroupClassesWithCourse(Long courseScheduleId) {
        if(Objects.isNull(courseScheduleId)){
            throw new BizException("请指定课程");
        }
        CourseSchedule currentCourseSchedule = courseScheduleDao.get(courseScheduleId);
        List<CourseScheduleDto> teacherCourseSchedulesWithDate = courseScheduleDao.findCourseSchedulesWithDate(null,null,currentCourseSchedule.getMusicGroupId(),currentCourseSchedule.getGroupType(),TenantContextHolder.getTenantId());
        List<CourseScheduleDto> notStartClasses=new ArrayList<>();
        List<CourseScheduleDto> overClasses=new ArrayList<>();
        CourseScheduleDto currentClasses=null;
        Date now=new Date();
        for (CourseScheduleDto courseScheduleDto : teacherCourseSchedulesWithDate) {
            if(currentCourseSchedule.getId().equals(courseScheduleDto.getId())){
                currentClasses=courseScheduleDto;
                continue;
            }
            if(courseScheduleDto.getEndClassTime().before(now)){
                overClasses.add(courseScheduleDto);
            }else{
                notStartClasses.add(courseScheduleDto);
            }
        }

        List<CourseScheduleDto> courseSchedules=new ArrayList<>();
        courseSchedules.add(currentClasses);
        courseSchedules.addAll(notStartClasses);
        courseSchedules.addAll(overClasses);

        return courseSchedules;
    }
}
