import com.engine.attendance.enums.ClassSegmentTypeEnum;
import com.engine.common.util.DateUtil;
import com.google.common.collect.Lists;

import java.time.ZoneOffset;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class TestMergeAskForLeaveAndEvctionSchedule {
    public static void main(String[] args) {
        List<Map<String,Object>> askForLeaveAndEvctionSchedule = Lists.newArrayList();
        askForLeaveAndEvctionSchedule.add(new HashMap(){{
            put("edsc","9.50");
            put("bdlx", ClassSegmentTypeEnum.ASK_FOR_LEAVE.getKey());
            put("jsdk","0");
            put("tqdkfzs","60");
            put("sfkt","1");
            put("dtjssj","11:00");
            put("zddxfz","30");
            put("bcxx","25");
            put("dxhs","5");
            put("bcsdxx","09:00-13:00|13:00-18:30");
            put("sfdx","1");
            put("dtkssj","10:00");
            put("thdkfzs","60");
            put("ksdk","1");
            put("rqlx","11");
        }});
        askForLeaveAndEvctionSchedule.add(new HashMap(){{
            put("edsc","9.50");
            put("bdlx",ClassSegmentTypeEnum.ASK_FOR_LEAVE.getKey());
            put("jsdk","1");
            put("tqdkfzs","60");
            put("sfkt","1");
            put("dtjssj","13:00");
            put("zddxfz","30");
            put("bcxx","25");
            put("dxhs","5");
            put("bcsdxx","09:00-13:00|13:00-18:30");
            put("sfdx","1");
            put("dtkssj","11:00");
            put("thdkfzs","60");
            put("ksdk","1");
            put("rqlx","11");
        }});

        askForLeaveAndEvctionSchedule.add(new HashMap(){{
            put("edsc","9.50");
            put("bdlx",ClassSegmentTypeEnum.ASK_FOR_LEAVE.getKey());
            put("jsdk","1");
            put("tqdkfzs","60");
            put("sfkt","1");
            put("dtjssj","15:00");
            put("zddxfz","30");
            put("bcxx","25");
            put("dxhs","5");
            put("bcsdxx","09:00-13:00|13:00-18:30");
            put("sfdx","1");
            put("dtkssj","14:00");
            put("thdkfzs","60");
            put("ksdk","1");
            put("rqlx","11");
        }});
        askForLeaveAndEvctionSchedule.add(new HashMap(){{
            put("edsc","9.50");
            put("bdlx",ClassSegmentTypeEnum.ASK_FOR_LEAVE.getKey());
            put("jsdk","1");
            put("tqdkfzs","60");
            put("sfkt","1");
            put("dtjssj","18:00");
            put("zddxfz","30");
            put("bcxx","25");
            put("dxhs","5");
            put("bcsdxx","09:00-13:00|13:00-18:30");
            put("sfdx","1");
            put("dtkssj","15:00");
            put("thdkfzs","60");
            put("ksdk","1");
            put("rqlx","11");
        }});
        System.out.println(mergeAskForLeaveAndEvctionSchedule(askForLeaveAndEvctionSchedule,"2023-12-01"));
    }

    public static List<Map<String,Object>> mergeAskForLeaveAndEvctionSchedule(List<Map<String,Object>> askForLeaveAndEvctionSchedule, String analysisDate){

        askForLeaveAndEvctionSchedule = askForLeaveAndEvctionSchedule.stream().sorted(Comparator.comparing(e-> DateUtil.getTime(analysisDate+" "+e.get("dtkssj")).toInstant(ZoneOffset.of("+8")).toEpochMilli())).collect(Collectors.toList());

        for (int i=0;i<askForLeaveAndEvctionSchedule.size()-1;i++){
            String beforeDtkssj = askForLeaveAndEvctionSchedule.get(i).get("dtkssj").toString();
            String beforeDtjssj = askForLeaveAndEvctionSchedule.get(i).get("dtjssj").toString();

            for (int j=i+1;j<askForLeaveAndEvctionSchedule.size();j++){
                String dtkssj = askForLeaveAndEvctionSchedule.get(j).get("dtkssj").toString();
                String dtjssj = askForLeaveAndEvctionSchedule.get(j).get("dtjssj").toString();
                if (beforeDtjssj.equals(dtkssj)){
                    askForLeaveAndEvctionSchedule.get(i).put("dtjssj",dtjssj);
                    askForLeaveAndEvctionSchedule.get(i).put("jsdk",askForLeaveAndEvctionSchedule.get(j).get("jsdk"));
                    askForLeaveAndEvctionSchedule.get(i).put("thfghlfzs",askForLeaveAndEvctionSchedule.get(j).get("thfghlfzs"));
                    askForLeaveAndEvctionSchedule.remove(askForLeaveAndEvctionSchedule.get(j));
                    j--;
                    i--;
                }
            }
        }

        return askForLeaveAndEvctionSchedule;
    }
}
