package com.ruoyi.system.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

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

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

    /**
     * 查询课程列表
     * 
     * @param courseTable 课程
     * @return 课程
     */
    @Override
    public List<CourseTable> selectCourseTableList(CourseTable courseTable)
    {
        return courseTableMapper.selectCourseTableList(courseTable);
    }

    /**
     * 新增课程
     * 
     * @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 nowTime = DateUtils.getTime();
        //判断当天是周几
        int dayForWeek = DateUtils.dayForWeek(nowTime);
        if(dayForWeek==6 || dayForWeek==7){
            return AjaxResult.success(map);
        }
        //获取当前账号所属学校时间表
        CourseTableTime courseTableTime = courseTableTimeMapper.selectCourseTableTimeBySchoolId(schoolId);
        //判断当前时间是第几节课
        int index = isClass(courseTableTime);
        if(index == 0){
            return AjaxResult.success(map);
        }
        //获取当前班级课程
        CourseTable courseTable = courseTableMapper.selectCourseTableByClassIdAndWeek(classId,dayForWeek);
        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.getTime();
        int index = 0;
        if(DateUtils.isClass(courseTableTime.getOneStartTime(),courseTableTime.getOneEndTime(),nowTime,"HH:mm:ss")){
            index = 1;
        }else if(DateUtils.isClass(courseTableTime.getTwoStartTime(),courseTableTime.getTwoEndTime(),nowTime,"HH:mm:ss")){
            index = 2;
        }else if(DateUtils.isClass(courseTableTime.getThreeStartTime(),courseTableTime.getThreeEndTime(),nowTime,"HH:mm:ss")){
            index = 3;
        }else if(DateUtils.isClass(courseTableTime.getFourStartTime(),courseTableTime.getFourEndTime(),nowTime,"HH:mm:ss")){
            index = 4;
        }else if(DateUtils.isClass(courseTableTime.getFiveStartTime(),courseTableTime.getFiveEndTime(),nowTime,"HH:mm:ss")){
            index = 5;
        }else if(DateUtils.isClass(courseTableTime.getSixStartTime(),courseTableTime.getSixEndTime(),nowTime,"HH:mm:ss")){
            index = 6;
        }else if(DateUtils.isClass(courseTableTime.getSevenStartTime(),courseTableTime.getSevenEndTime(),nowTime,"HH:mm:ss")){
            index = 7;
        }else if(DateUtils.isClass(courseTableTime.getEightStartTime(),courseTableTime.getEightEndTime(),nowTime,"HH:mm:ss")){
            index = 8;
        }
        return index;
    }

}
