package com.koobietech.eas.service.impl;

import com.koobietech.eas.common.result.PageDataResult;
import com.koobietech.eas.dao.dto.ConflictDto;
import com.koobietech.eas.mbg.mapper.EasEduClassroomMapper;
import com.koobietech.eas.mbg.mapper.EasEduScheduleMapper;
import com.koobietech.eas.mbg.model.EasEduClassroomExample;
import com.koobietech.eas.mbg.model.EasEduSchedule;
import com.koobietech.eas.mbg.model.EasEduScheduleExample;
import com.koobietech.eas.service.EasEduScheduleService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Objects;

@Service

public class EasEduScheduleServiceImpl implements EasEduScheduleService {
    @Resource
    EasEduScheduleMapper easEduScheduleMapper;

    @Resource
    EasEduClassroomMapper easEduClassroomMapper;

    @Override
    public Boolean update(EasEduSchedule schedule) {
        schedule.setModifyTime(new Date());
        return easEduScheduleMapper.updateByPrimaryKey(schedule) == 1;
    }

    @Override
    public Boolean add(EasEduSchedule schedule, int repeatTime) {
        Calendar startCalender = Calendar.getInstance();
        Calendar endCalender = Calendar.getInstance();

        startCalender.setTime(schedule.getStartTime());
        endCalender.setTime(schedule.getEndTime());

        schedule.setCreateTime(new Date());
        schedule.setModifyTime(new Date());

        for (int re = 0; re < repeatTime; re++) {
            if (re != 0) {
                startCalender.add(Calendar.DATE, 1);
                endCalender.add(Calendar.DATE, 1);
            }
            schedule.setStartTime(startCalender.getTime());
            schedule.setEndTime(endCalender.getTime());
            if ((schedule.getWeek() + 1) != 7) {
                schedule.setWeek((schedule.getWeek() + 1) % 7);
            } else {
                schedule.setWeek(7);
            }
            if (easEduScheduleMapper.insert(schedule) == 0) {
                return false;
            }
        }
        return true;
    }

    @Override
    public Boolean delete(Long id) {
        return easEduScheduleMapper.deleteByPrimaryKey(id) == 1;
    }

    @Override
    public PageDataResult query(EasEduSchedule schedule) {

        EasEduScheduleExample easEduScheduleExample = new EasEduScheduleExample();
        EasEduScheduleExample.Criteria criteria = easEduScheduleExample.createCriteria();
        if (Objects.nonNull(schedule)) {

            if (Objects.nonNull(schedule.getId()) && schedule.getId() != 0) {
                criteria.andIdEqualTo(schedule.getId());
            }

            if (Objects.nonNull(schedule.getWeek()) && schedule.getWeek() != 0) {
                criteria.andWeekEqualTo(schedule.getWeek());
            }

            if (Objects.nonNull(schedule.getStartTime())) {
                criteria.andStartTimeGreaterThanOrEqualTo(schedule.getStartTime());
            }

            if (Objects.nonNull(schedule.getEndTime())) {
                criteria.andEndTimeLessThanOrEqualTo(schedule.getEndTime());
            }

            if (Objects.nonNull(schedule.getRoomId()) && schedule.getRoomId() != 0) {
                criteria.andRoomIdEqualTo(schedule.getRoomId());
            }

            if (Objects.nonNull(schedule.getClassId()) && schedule.getClassId() != 0) {
                criteria.andClassIdEqualTo(schedule.getClassId());
            }

            if (Objects.nonNull(schedule.getAssistantId()) && schedule.getAssistantId() != 0) {
                criteria.andAssistantIdEqualTo(schedule.getAssistantId());
            }

            if (Objects.nonNull(schedule.getTeacherId()) && schedule.getTeacherId() != 0) {
                criteria.andTeacherIdEqualTo(schedule.getTeacherId());
            }

            if (Objects.nonNull(schedule.getCategoryId()) && schedule.getCategoryId() != 0) {
                criteria.andCategoryIdEqualTo(schedule.getCategoryId());
            }

            if (Objects.nonNull(schedule.getSubjectsId()) && schedule.getSubjectsId() != 0) {
                criteria.andSubjectsIdEqualTo(schedule.getSubjectsId());
            }

            if (Objects.nonNull(schedule.getCreateTime())) {
                criteria.andCreateTimeGreaterThanOrEqualTo(schedule.getCreateTime());
            }

            if (Objects.nonNull(schedule.getModifyTime())) {
                criteria.andModifyTimeGreaterThanOrEqualTo(schedule.getModifyTime());
            }

            if (Objects.nonNull(schedule.getCreateUid()) && schedule.getCreateUid() != 0) {
                criteria.andCreateUidEqualTo(schedule.getCreateUid());
            }

            if (StringUtils.hasText(schedule.getDisabled())) {
                criteria.andDisabledEqualTo(schedule.getDisabled());
            }

        }
        List<EasEduSchedule> schedules =
                easEduScheduleMapper.selectByExample(easEduScheduleExample);
        long l = easEduScheduleMapper.countByExample(easEduScheduleExample);
        PageDataResult ret = new PageDataResult();
        ret.setData(schedules);
        ret.setTotal(l);
        return ret;
    }

    @Override
    public Boolean queryConflict(ConflictDto conflictDto) {

        Boolean ret = false;

        EasEduScheduleExample easEduScheduleExample = new EasEduScheduleExample();
        EasEduScheduleExample.Criteria criteria = easEduScheduleExample.createCriteria();

        EasEduClassroomExample easEduClassroomExample = new EasEduClassroomExample();
        EasEduClassroomExample.Criteria classroomCriteria = easEduClassroomExample.createCriteria();
        classroomCriteria.andDisabledEqualTo("Y");

        if (Objects.nonNull(conflictDto)) {

            if (Objects.nonNull(conflictDto.getStartTime())
                    && Objects.nonNull(conflictDto.getEndTime())) {
                criteria.andStartTimeBetween(conflictDto.getStartTime(), conflictDto.getEndTime());
            }

            if (Objects.nonNull(conflictDto.getTeacherId()) && conflictDto.getTeacherId() != 0) {
                criteria.andTeacherIdEqualTo(conflictDto.getTeacherId());
            }

            if (Objects.nonNull(conflictDto.getRoomId()) && conflictDto.getRoomId() != 0) {
                criteria.andRoomIdEqualTo(conflictDto.getRoomId());
            }
            criteria.andDisabledEqualTo("N");

        }

        if (easEduScheduleMapper.countByExample(easEduScheduleExample) == 0 &&
                easEduClassroomMapper.countByExample(easEduClassroomExample) == 0) {
            ret = true;
        }
        return ret;

    }
}
