package com.repair.common.utils;

import com.repair.model.pojo.RepairShiftSettings;
import com.repair.model.request.timeVo;
import lombok.SneakyThrows;

import java.text.ParseException;
import java.util.*;

/**
 * @Author: binguo
 * @Date: 2023/8/3 星期四 11:10
 * @Description: com.repair.common.utils
 * @Version: 1.0
 */
public class DelayedUtils {

    public static List<timeVo> timeVos(List<RepairShiftSettings> shiftDatas) throws ParseException {
        List<timeVo> tvs = new ArrayList<>();
        //判断考核时间是否在工作时间段内
        //弄出一个时间段集合 开始时间 结束时间 是否工作时间段
        for (RepairShiftSettings sd : shiftDatas) {
            if (sd.getIsDuty() == 1) {
                continue;
            }

            String startTime = sd.getStartTime();
            List<String> nw = Arrays.asList(sd.getNotWorkTime().split(","));
            List<timeVo> tvDatas = new ArrayList<>();
            //非工作时间必须都在结束时间之内 防止非工作时间重叠
            for (String i : nw) {
                String[] time = i.split("-");
                if (TimeExchange.CompareDate(time[0], time[1], "HH:mm")) {
                    timeVo tv = new timeVo();
                    tv.setStart(time[0]);
                    tv.setEnd(time[1]);
                    tvDatas.add(tv);
                }
            }

            Collections.sort(tvDatas, new Comparator<timeVo>() {
                @SneakyThrows
                @Override
                public int compare(timeVo s1, timeVo s2) {
                    return TimeExchange.StringToDate(s1.getStart(), "HH:mm").compareTo(TimeExchange.StringToDate(s2.getStart(), "HH:mm"));
                }
            });

            for (int i = 0; i <= tvDatas.size(); i++) {

                timeVo tvData = new timeVo();
                tvData.setStart(startTime);
                if (i == tvDatas.size()) {
                    tvData.setEnd(sd.getEndTime());
                } else {
                    tvData.setEnd(tvDatas.get(i).getStart());
                }

                tvData.setIsWork(1);

                tvs.add(tvData);

                if (i < tvDatas.size()) {
                    startTime = tvDatas.get(i).getEnd();
                }
            }
        }

        if (tvs.size() > 0) {

            Collections.sort(tvs, new Comparator<timeVo>() {
                @SneakyThrows
                @Override
                public int compare(timeVo s1, timeVo s2) {
                    return TimeExchange.StringToDate(s1.getStart(), "HH:mm").compareTo(TimeExchange.StringToDate(s2.getStart(), "HH:mm"));
                }
            });
        }

        return tvs;
    }

    public static Integer addMinute(List<timeVo> tvs, int assessTime, Date nowDate, String dateNow, int workMinte, int i, int num) throws ParseException {
        if (workMinte < assessTime) {
            int count = i + num;
            if (count < tvs.size()) {
                if (count < tvs.size()) {
                    Date nextSatrtTime = TimeExchange.StringToDate(dateNow + " " + tvs.get(count).getEnd() + ":00", "yyyy-MM-dd HH:mm:ss");
                    int newWorkMinte = TimeExchange.getOffsetMinutes(nowDate, nextSatrtTime);
                    assessTime = assessTime + newWorkMinte;
                    num++;
                    assessTime = addMinute(tvs, assessTime, nowDate, dateNow, workMinte, i, num);
                }
            }
        }

        return assessTime;
    }

    public static Integer addMinuteStart(List<timeVo> tvs, Date lastDate, int assessTime, Date nowDate, String dateNow, int workMinte, int i, int num) throws ParseException {
        if (workMinte < assessTime) {
            int count = i + num;

            if (count < tvs.size()) {
                Date nextStartWorkTime = TimeExchange.StringToDate(dateNow + " " + tvs.get(count).getStart() + ":00", "yyyy-MM-dd HH:mm:ss");
                Date nextEndWorkTime = TimeExchange.StringToDate(dateNow + " " + tvs.get(count).getEnd() + ":00", "yyyy-MM-dd HH:mm:ss");
                int minute = TimeExchange.getOffsetMinutes(lastDate, nextStartWorkTime);
                workMinte = TimeExchange.getOffsetMinutes(nowDate, nextEndWorkTime);
                assessTime = assessTime + minute;
                num++;
                assessTime = addMinuteStart(tvs, nextEndWorkTime, assessTime, nowDate, dateNow, workMinte, i, num);
            }
        }
        return assessTime;
    }

    public static void main(String[] args) throws ParseException {
        List<RepairShiftSettings> shiftDatas = new ArrayList<>();
        RepairShiftSettings shiftData = new RepairShiftSettings();
        shiftData.setStartTime("7:00");
        shiftData.setEndTime("19:00");
        shiftData.setIsDuty(0);
        shiftData.setNotWorkTime("17:00-18:30,14:00-15:50,10:00-11:11");
        shiftDatas.add(shiftData);
        List<timeVo> result = timeVos(shiftDatas);
        //考核 100  160
        //int ssss = addMinuteStart(result, TimeExchange.StringToDate("2023-08-03 14:00:00", "yyyy-MM-dd HH:mm:ss"), 270, TimeExchange.StringToDate("2023-08-03 10:11:00", "yyyy-MM-dd HH:mm:ss"), "2023-08-03", 229, 0, 2);

        List<timeVo> nsn = new ArrayList<>();
        timeVo s1 = new timeVo();
        s1.setNowDate(TimeExchange.StringToDate("2023-08-03 10:00:00", "yyyy-MM-dd HH:mm:ss"));
        s1.setAcceptanceAssessTime(50);
        s1.setMaintenanceAssessTime(60);
        nsn.add(s1);

        timeVo s2 = new timeVo();
        s2.setNowDate(TimeExchange.StringToDate("2023-08-03 06:00:00", "yyyy-MM-dd HH:mm:ss"));
        s2.setAcceptanceAssessTime(200);
        s2.setMaintenanceAssessTime(600);
        nsn.add(s2);
        timeVo s3 = new timeVo();
        s3.setNowDate(TimeExchange.StringToDate("2023-08-03 07:30:00", "yyyy-MM-dd HH:mm:ss"));
        s3.setAcceptanceAssessTime(50);
        s3.setMaintenanceAssessTime(300);
        nsn.add(s3);
        timeVo s4 = new timeVo();
        s4.setNowDate(TimeExchange.StringToDate("2023-08-03 10:11:00", "yyyy-MM-dd HH:mm:ss"));
        s4.setAcceptanceAssessTime(320);
        s4.setMaintenanceAssessTime(100);
        nsn.add(s4);
        timeVo s5 = new timeVo();
        s5.setNowDate(TimeExchange.StringToDate("2023-08-03 12:00:00", "yyyy-MM-dd HH:mm:ss"));
        s5.setAcceptanceAssessTime(200);
        s5.setMaintenanceAssessTime(160);
        nsn.add(s5);
        timeVo s6 = new timeVo();
        s6.setNowDate(TimeExchange.StringToDate("2023-08-03 14:20:00", "yyyy-MM-dd HH:mm:ss"));
        s6.setAcceptanceAssessTime(150);
        s6.setMaintenanceAssessTime(280);
        nsn.add(s6);
        timeVo s7 = new timeVo();
        s7.setNowDate(TimeExchange.StringToDate("2023-08-03 16:00:00", "yyyy-MM-dd HH:mm:ss"));
        s7.setAcceptanceAssessTime(30);
        s7.setMaintenanceAssessTime(150);
        nsn.add(s7);
        timeVo s8 = new timeVo();
        s8.setNowDate(TimeExchange.StringToDate("2023-08-03 17:30:00", "yyyy-MM-dd HH:mm:ss"));
        s8.setAcceptanceAssessTime(50);
        s8.setMaintenanceAssessTime(200);
        nsn.add(s8);
        timeVo s9 = new timeVo();
        s9.setNowDate(TimeExchange.StringToDate("2023-08-03 19:00:00", "yyyy-MM-dd HH:mm:ss"));
        s9.setAcceptanceAssessTime(90);
        s9.setMaintenanceAssessTime(70);
        nsn.add(s9);
        timeVo s10 = new timeVo();
        s10.setNowDate(TimeExchange.StringToDate("2023-08-03 07:00:00", "yyyy-MM-dd HH:mm:ss"));
        s10.setAcceptanceAssessTime(100);
        s10.setMaintenanceAssessTime(90);
        nsn.add(s10);
        timeVo s20 = new timeVo();
        s20.setNowDate(TimeExchange.StringToDate("2023-08-03 07:00:00", "yyyy-MM-dd HH:mm:ss"));
        s20.setAcceptanceAssessTime(200);
        s20.setMaintenanceAssessTime(400);
        nsn.add(s20);
        timeVo s11 = new timeVo();
        s11.setNowDate(TimeExchange.StringToDate("2023-08-03 11:11:00", "yyyy-MM-dd HH:mm:ss"));
        s11.setAcceptanceAssessTime(150);
        s11.setMaintenanceAssessTime(170);
        nsn.add(s11);
        timeVo s12 = new timeVo();
        s12.setNowDate(TimeExchange.StringToDate("2023-08-03 18:30:00", "yyyy-MM-dd HH:mm:ss"));
        s12.setAcceptanceAssessTime(50);
        s12.setMaintenanceAssessTime(60);
        nsn.add(s12);
        timeVo s13 = new timeVo();
        s13.setNowDate(TimeExchange.StringToDate("2023-08-03 20:00:00", "yyyy-MM-dd HH:mm:ss"));
        s13.setAcceptanceAssessTime(70);
        s13.setMaintenanceAssessTime(90);
        nsn.add(s13);


        for (timeVo t:nsn) {
            teststs(t.getNowDate(),t.getAcceptanceAssessTime(),t.getMaintenanceAssessTime(),shiftDatas);
        }
        String ssss = "";
    }

    private static void teststs(Date nowDate, int acceptanceAssessTime, int maintenanceAssessTime, List<RepairShiftSettings> shiftDatas) throws ParseException {
        String dateNow = "2023-08-03";
        int isDuty = 0;
        //下班之后或者值班接的单不考核 将报修工单中的is_duty改为1
        //下班之后或者值班接的单不考核 将报修工单中的is_duty改为1
        Date workTime = TimeExchange.StringToDate(dateNow + " " + shiftDatas.get(0).getEndTime() + ":00", "yyyy-MM-dd HH:mm:ss");
        if (workTime.before(nowDate)) {
            isDuty = 1;
        }

        if (isDuty == 0) {
            List<timeVo> tvs = DelayedUtils.timeVos(shiftDatas);

            //工作的时间是否够修完 临下班接的单不做延长
            for (int i = 0; i < tvs.size(); i++) {
                Date startWorkTime = TimeExchange.StringToDate(dateNow + " " + tvs.get(i).getStart() + ":00", "yyyy-MM-dd HH:mm:ss");
                Date endWorkTime = TimeExchange.StringToDate(dateNow + " " + tvs.get(i).getEnd() + ":00", "yyyy-MM-dd HH:mm:ss");

                //小于工作时间段的开始时间
                if (nowDate.before(startWorkTime) || nowDate.equals(startWorkTime) && i == 0) {
                    //早上开始上班之前的单子
                    int minute = TimeExchange.getOffsetMinutes(nowDate, startWorkTime);
                    int workMinte = TimeExchange.getOffsetMinutes(nowDate, endWorkTime);
                    acceptanceAssessTime = acceptanceAssessTime + minute;
                    acceptanceAssessTime = DelayedUtils.addMinuteStart(tvs, endWorkTime, acceptanceAssessTime, nowDate, dateNow, workMinte, i, 1);
                    maintenanceAssessTime = maintenanceAssessTime + minute;
                    maintenanceAssessTime = DelayedUtils.addMinuteStart(tvs, endWorkTime, maintenanceAssessTime, nowDate, dateNow, workMinte, i, 1);
                    System.out.println("时间："+TimeExchange.DateToString(nowDate)+"acc："+acceptanceAssessTime+"main："+maintenanceAssessTime);
                    break;
                } else if ((startWorkTime.before(nowDate) || startWorkTime.equals(nowDate)) && (nowDate.before(endWorkTime) || nowDate.equals(endWorkTime))) {
                    //工作时间段内的单子
                    int workMinte = TimeExchange.getOffsetMinutes(nowDate, endWorkTime);
                    acceptanceAssessTime = DelayedUtils.addMinuteStart(tvs, endWorkTime, acceptanceAssessTime, nowDate, dateNow, workMinte, i, 1);
                    maintenanceAssessTime = DelayedUtils.addMinuteStart(tvs, endWorkTime, maintenanceAssessTime, nowDate, dateNow, workMinte, i, 1);
                    System.out.println("时间："+TimeExchange.DateToString(nowDate)+"acc："+acceptanceAssessTime+"main："+maintenanceAssessTime);
                    break;

                } else{
                    //非工作时间段的单子
                    Date nextStartWorkTime = TimeExchange.StringToDate(dateNow + " " + tvs.get(i + 1).getStart() + ":00", "yyyy-MM-dd HH:mm:ss");
                    if ((endWorkTime.before(nowDate) || endWorkTime.equals(nowDate)) && (nowDate.before(nextStartWorkTime) || nowDate.equals(nextStartWorkTime))) {
                        Date nextEndWorkTime = TimeExchange.StringToDate(dateNow + " " + tvs.get(i + 1).getEnd() + ":00", "yyyy-MM-dd HH:mm:ss");
                        int minute = TimeExchange.getOffsetMinutes(nowDate, nextStartWorkTime);
                        int workMinte = TimeExchange.getOffsetMinutes(nowDate, nextEndWorkTime);
                        acceptanceAssessTime = acceptanceAssessTime + minute;
                        acceptanceAssessTime = DelayedUtils.addMinuteStart(tvs, nextEndWorkTime, acceptanceAssessTime, nowDate, dateNow, workMinte, i, 2);
                        maintenanceAssessTime = maintenanceAssessTime + minute;
                        maintenanceAssessTime = DelayedUtils.addMinuteStart(tvs, nextEndWorkTime, maintenanceAssessTime, nowDate, dateNow, workMinte, i, 2);
                        System.out.println("时间："+TimeExchange.DateToString(nowDate)+"acc："+acceptanceAssessTime+"main："+maintenanceAssessTime);
                        break;
                    }
                }
            }
            String sss = "0";
        }
    }
}
