package com.ruoyi.education.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.education.mapper.EduSemesterMapper;
import com.ruoyi.education.domain.EduSemester;
import com.ruoyi.education.service.IEduSemesterService;

/**
 * 学期信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-09-26
 */
@Service
public class EduSemesterServiceImpl implements IEduSemesterService 
{
    @Autowired
    private EduSemesterMapper eduSemesterMapper;

    /**
     * 查询学期信息
     * 
     * @param semesterId 学期信息主键
     * @return 学期信息
     */
    @Override
    public EduSemester selectEduSemesterBySemesterId(Long semesterId)
    {
        return eduSemesterMapper.selectEduSemesterBySemesterId(semesterId);
    }

    /**
     * 查询学期信息列表
     * 
     * @param eduSemester 学期信息
     * @return 学期信息
     */
    @Override
    public List<EduSemester> selectEduSemesterList(EduSemester eduSemester)
    {
        return eduSemesterMapper.selectEduSemesterList(eduSemester);
    }

    /**
     * 新增学期信息
     * 
     * @param eduSemester 学期信息
     * @return 结果
     */
    @Override
    public int insertEduSemester(EduSemester eduSemester)
    {
        eduSemester.setCreateTime(DateUtils.getNowDate());
        return eduSemesterMapper.insertEduSemester(eduSemester);
    }

    /**
     * 修改学期信息
     * 
     * @param eduSemester 学期信息
     * @return 结果
     */
    @Override
    public int updateEduSemester(EduSemester eduSemester)
    {
        eduSemester.setUpdateTime(DateUtils.getNowDate());
        return eduSemesterMapper.updateEduSemester(eduSemester);
    }

    /**
     * 批量删除学期信息
     * 
     * @param semesterIds 需要删除的学期信息主键
     * @return 结果
     */
    @Override
    public int deleteEduSemesterBySemesterIds(Long[] semesterIds)
    {
        return eduSemesterMapper.deleteEduSemesterBySemesterIds(semesterIds);
    }

    /**
     * 删除学期信息信息
     * 
     * @param semesterId 学期信息主键
     * @return 结果
     */
    @Override
    public int deleteEduSemesterBySemesterId(Long semesterId)
    {
        return eduSemesterMapper.deleteEduSemesterBySemesterId(semesterId);
    }

    /**
     * 根据学期编码查询学期信息
     * 
     * @param semesterCode 学期编码
     * @return 学期信息
     */
    @Override
    public EduSemester selectEduSemesterBySemesterCode(String semesterCode)
    {
        return eduSemesterMapper.selectEduSemesterBySemesterCode(semesterCode);
    }

    /**
     * 查询当前进行中的学期
     * 
     * @return 当前学期信息
     */
    @Override
    public EduSemester selectCurrentSemester()
    {
        return eduSemesterMapper.selectCurrentSemester();
    }

    /**
     * 查询可选课的学期列表
     * 
     * @return 学期信息集合
     */
    @Override
    public List<EduSemester> selectEnrollableSemesters()
    {
        return eduSemesterMapper.selectEnrollableSemesters();
    }

    /**
     * 发布学期（将状态改为进行中）
     * 
     * @param semesterId 学期ID
     * @return 结果
     */
    @Override
    public int publishSemester(Long semesterId)
    {
        EduSemester semester = new EduSemester();
        semester.setSemesterId(semesterId);
        semester.setStatus("1");
        semester.setUpdateBy(SecurityUtils.getUsername());
        semester.setUpdateTime(DateUtils.getNowDate());
        return eduSemesterMapper.updateEduSemester(semester);
    }

    /**
     * 归档学期（将状态改为已归档）
     * 
     * @param semesterId 学期ID
     * @return 结果
     */
    @Override
    public int archiveSemester(Long semesterId)
    {
        EduSemester semester = new EduSemester();
        semester.setSemesterId(semesterId);
        semester.setStatus("2");
        semester.setUpdateBy(SecurityUtils.getUsername());
        semester.setUpdateTime(DateUtils.getNowDate());
        return eduSemesterMapper.updateEduSemester(semester);
    }

    /**
     * 校验学期编码是否唯一
     * 
     * @param eduSemester 学期信息
     * @return 结果
     */
    @Override
    public String checkSemesterCodeUnique(EduSemester eduSemester)
    {
        Long semesterId = StringUtils.isNull(eduSemester.getSemesterId()) ? -1L : eduSemester.getSemesterId();
        EduSemester info = eduSemesterMapper.selectEduSemesterBySemesterCode(eduSemester.getSemesterCode());
        if (StringUtils.isNotNull(info) && info.getSemesterId().longValue() != semesterId.longValue())
        {
            return "NOT_UNIQUE";
        }
        return "UNIQUE";
    }

    /**
     * 校验学期时间是否冲突
     * 
     * @param eduSemester 学期信息
     * @return 结果
     */
    @Override
    public String checkSemesterTimeConflict(EduSemester eduSemester)
    {
        Long semesterId = StringUtils.isNull(eduSemester.getSemesterId()) ? -1L : eduSemester.getSemesterId();
        EduSemester queryParam = new EduSemester();
        
        List<EduSemester> semesterList = eduSemesterMapper.selectEduSemesterList(queryParam);
        
        for (EduSemester semester : semesterList)
        {
            if (semester.getSemesterId().longValue() != semesterId.longValue())
            {
                if (isTimeOverlap(eduSemester.getStartDate(), eduSemester.getEndDate(), 
                                 semester.getStartDate(), semester.getEndDate()))
                {
                    return "CONFLICT";
                }
                
                if (isTimeOverlap(eduSemester.getEnrollStartTime(), eduSemester.getEnrollEndTime(), 
                                 semester.getEnrollStartTime(), semester.getEnrollEndTime()))
                {
                    return "ENROLL_CONFLICT";
                }
            }
        }
        return "NO_CONFLICT";
    }

    /**
     * 判断两个时间段是否重叠
     * 
     * @param start1 开始时间1
     * @param end1 结束时间1
     * @param start2 开始时间2
     * @param end2 结束时间2
     * @return 是否重叠
     */
    private boolean isTimeOverlap(java.util.Date start1, java.util.Date end1, 
                                  java.util.Date start2, java.util.Date end2)
    {
        if (start1 == null || end1 == null || start2 == null || end2 == null)
        {
            return false;
        }
        
        return start1.before(end2) && end1.after(start2);
    }
}