package com.foreverwin.mesnac.dispatch.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.foreverwin.mesnac.dispatch.dto.WorkShiftDTO;
import com.foreverwin.mesnac.dispatch.mapper.WorkShiftMapper;
import com.foreverwin.mesnac.dispatch.model.WorkShift;
import com.foreverwin.mesnac.dispatch.service.WorkShiftService;
import com.foreverwin.mesnac.meapi.util.StringUtils;
import com.foreverwin.modular.core.exception.BusinessException;
import com.foreverwin.modular.core.util.FrontPage;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author syngna
 * @since 2020-08-18
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class WorkShiftServiceImpl extends ServiceImpl<WorkShiftMapper, WorkShift> implements WorkShiftService {


    @Autowired
    private WorkShiftMapper workShiftMapper;

    @Override
    public IPage<WorkShift> selectPage(FrontPage<WorkShift> frontPage, WorkShift workShift) {
        QueryWrapper<WorkShift> queryWrapper = new QueryWrapper<>();
        queryWrapper.setEntity(workShift);
        return super.page(frontPage.getPagePlus(), queryWrapper);
    }

    @Override
    public List<WorkShift> selectList(WorkShift workShift) {
        QueryWrapper<WorkShift> queryWrapper = new QueryWrapper<>();
        queryWrapper.setEntity(workShift);
        return super.list(queryWrapper);
    }

    /**
     * 验证班次列表中时间是否有重叠
     *
     * @param workShiftList
     */
    @Override
    public void validateCrossSetting(List<WorkShift> workShiftList) {
        for (WorkShift workShift : workShiftList) {
            validateCrossSetting(workShiftList, workShift.getHandle(), workShift.getStartTime(), workShift.getEndTime());
        }
    }

    /**
     * 验证当前detailHandle对应的开始结束时间是否与其他时间有重叠
     *
     * @param workShiftList
     * @param detailHandle
     * @param startTime
     * @param endTime
     */
    @Override
    public void validateCrossSetting(List<WorkShift> workShiftList, String detailHandle, String startTime, String endTime) {
        Integer currentStartMinutes = Integer.parseInt(startTime.split(":")[0].trim()) * 60 + Integer.parseInt(startTime.split(":")[1].trim());
        Integer currentEndMinutes = Integer.parseInt(endTime.split(":")[0].trim()) * 60 + Integer.parseInt(endTime.split(":")[1].trim());
        Integer nextStartMinutes = null;
        Integer nextEndMinutes = null;
        if (currentStartMinutes > currentEndMinutes) {
            //currentEndMinutes = currentEndMinutes + 24 * 60;
            nextStartMinutes = 0;
            nextEndMinutes = currentEndMinutes;

            currentEndMinutes = 24 * 60;
        }
        List<WorkShiftDTO> newList = new ArrayList<>();
        if (workShiftList != null && workShiftList.size() > 0) {
            for (WorkShift workShift : workShiftList) {
                if (workShift.getHandle().equals(detailHandle)) {
                    continue;
                }
                String[] startTimeArray = workShift.getStartTime().split(":");
                String[] endTimeArray = workShift.getEndTime().split(":");
                int startMinutes = Integer.parseInt(startTimeArray[0].trim()) * 60 + Integer.parseInt(startTimeArray[1].trim());
                int endMinutes = Integer.parseInt(endTimeArray[0].trim()) * 60 + Integer.parseInt(endTimeArray[1].trim());
                if (startMinutes > endMinutes) {
                    //endMinutes = endMinutes + 24 * 60;
                    WorkShiftDTO newWorkShift = new WorkShiftDTO();
                    BeanUtils.copyProperties(workShift, newWorkShift);
                    newWorkShift.setStartMinute(0);
                    newWorkShift.setEndMinute(endMinutes);
                    newList.add(newWorkShift);
                    endMinutes = 24 * 60;
                }
                WorkShiftDTO newWorkShift = new WorkShiftDTO();
                BeanUtils.copyProperties(workShift, newWorkShift);
                newWorkShift.setStartMinute(startMinutes);
                newWorkShift.setEndMinute(endMinutes);
                newList.add(newWorkShift);
            }

            for (WorkShiftDTO workShift : newList) {
                if (workShift.getHandle().equals(detailHandle)) {
                    continue;
                }
                int startMinutes = workShift.getStartMinute();
                int endMinutes = workShift.getEndMinute();

                validateCrossTime(currentStartMinutes, currentEndMinutes, startMinutes, endMinutes);
                if (nextStartMinutes != null && nextEndMinutes != null) {
                    validateCrossTime(nextStartMinutes, nextEndMinutes, startMinutes, endMinutes);
                }
            }
        }
    }

    private void validateCrossTime(Integer currentStartMinutes, Integer currentEndMinutes, int startMinutes, int endMinutes) {
        if ((startMinutes > currentStartMinutes && startMinutes < currentEndMinutes) ||
                (endMinutes > currentStartMinutes && endMinutes < currentEndMinutes) ||
                (startMinutes < currentStartMinutes && endMinutes >= currentEndMinutes) ||
                (startMinutes <= currentStartMinutes && endMinutes > currentEndMinutes)) {
            String currentStart = StringUtils.leftPad(currentStartMinutes / 60 + "", "0", 2) + ":" + StringUtils.leftPad(currentStartMinutes % 60 + "", "0", 2);
            String currentEnd = StringUtils.leftPad(currentEndMinutes / 60 + "", "0", 2) + ":" + StringUtils.leftPad(currentEndMinutes % 60 + "", "0", 2);
            String start = StringUtils.leftPad(startMinutes / 60 + "", "0", 2) + ":" + StringUtils.leftPad(startMinutes % 60 + "", "0", 2);
            String end = StringUtils.leftPad(endMinutes / 60 + "", "0", 2) + ":" + StringUtils.leftPad(endMinutes % 60 + "", "0", 2);
            throw BusinessException.build("存在交叉的时间设定：[" + currentStart + "-" + currentEnd + "]<<<>>>[" + start + "-" + end + "]");
        }
    }
}