package com.mortals.xhx.module.metting.service.impl;

import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateRange;
import cn.hutool.core.date.DateUtil;
import com.mortals.xhx.common.code.MeetStatusEnum;
import com.mortals.xhx.module.metting.model.MettingRecordQuery;
import org.checkerframework.checker.units.qual.A;
import org.springframework.stereotype.Service;
import com.mortals.framework.service.impl.AbstractCRUDServiceImpl;
import com.mortals.framework.exception.AppException;
import com.mortals.framework.model.Context;
import com.mortals.xhx.module.metting.dao.MettingRecordDao;
import com.mortals.xhx.module.metting.model.MettingRecordEntity;
import com.mortals.xhx.module.metting.service.MettingRecordService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.ObjectUtils;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * MettingRecordService
 * 会议记录 service实现
 *
 * @author zxfei
 * @date 2023-05-22
 */
@Service("mettingRecordService")
@Slf4j
public class MettingRecordServiceImpl extends AbstractCRUDServiceImpl<MettingRecordDao, MettingRecordEntity, Long> implements MettingRecordService {


    @Override
    protected void validData(MettingRecordEntity entity, Context context) throws AppException {
        super.validData(entity, context);
        Date meetTimeStart = entity.getMeetTimeStart();
        Date meetTimeEnd = entity.getMeetTimeEnd();
        if (!ObjectUtils.isEmpty(meetTimeStart) && !ObjectUtils.isEmpty(meetTimeEnd)) {


            MettingRecordQuery mettingRecordQuery = new MettingRecordQuery();
            mettingRecordQuery.setMeetStatus(MeetStatusEnum.等待中.getValue());
            mettingRecordQuery.setRoomId(entity.getRoomId());
            //查询当前日期是否存在会议室占用
            mettingRecordQuery.setMeetDate(entity.getMeetDate());
            //判断选定的时间段 是否已经存在 时间段 求交集
            List<MettingRecordEntity> list = this.find(mettingRecordQuery);
            //校验当前时间段中是否已经存在未开始的会议
            if (!entity.newEntity()) {
                //更新时间段判断 排除已经存在的
                list = list.stream().filter(f -> f.getId() != entity.getId()).collect(Collectors.toList());
            }
            if (!ObjectUtils.isEmpty(list)) {
                //校验时间段是否有重合
                boolean bool = false;
                for (MettingRecordEntity mettingRecord : list) {
                    Date meetTimeStart1 = mettingRecord.getMeetTimeStart();
                    Date meetTimeEnd1 = mettingRecord.getMeetTimeEnd();
                    // DateRange range1 = DateUtil.range(meetTimeStart, meetTimeEnd, DateField.MINUTE);
                    Boolean aBoolean = checkTimesHasOverlap(meetTimeStart, meetTimeEnd, meetTimeStart1, meetTimeEnd1);
                    if (aBoolean) {
                        bool = true;
                        break;
                    }
                }
                if (bool) {
                    throw new AppException("当前会议室该时段已经存在等待的会议，请选择其它时间段！");
                }
            }

        }
    }


    /**
     * 判断两个时间范围是否有交集
     *
     * @param dynaStartTime  比较时间段开始时间
     * @param dynaEndTime    比较时间段结束时间
     * @param fixedStartTime 参考时间段开始时间
     * @param fixedEndTime   参考时间段结束时间
     * @return
     */
    public Boolean checkTimesHasOverlap(Date dynaStartTime, Date dynaEndTime, Date fixedStartTime, Date fixedEndTime) {
        return !(dynaEndTime.getTime() < fixedStartTime.getTime() || dynaStartTime.getTime() > fixedEndTime.getTime());
    }
}