package com.engine.attendance.attendanceanalysis.service.impl;

import com.engine.attendance.attendanceanalysis.cmd.item.*;
import com.engine.attendance.attendanceanalysis.dto.ClockPointDTO;
import com.engine.attendance.attendanceanalysis.service.AbnormalAttendanceService;
import com.engine.attendance.enums.*;
import com.engine.common.util.Utils;
import com.engine.core.impl.Service;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;
import weaver.general.Util;

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

@Slf4j
public class AbnormalAttendanceServiceImpl extends Service implements AbnormalAttendanceService {
    @Override
    public Map<String, Object> fullDayAbsenteeism(Map<String, Object> params) {


        return null;
    }

    @Override
    public List<Map<String,Object>> beLate(Map<String, Object> params) {
        //当前单条明细班次
        Map<String, Object> classInfo =  (Map<String, Object>)params.get("classInfo");
        //当前单个打卡数据
        Map<String,Object> clcokInTimeData = (Map<String, Object>)params.get("clcokInTimeData");
        //考勤项目
        List<Map<String,Object>> attendanceItems = (List<Map<String,Object>>)params.get("attendanceItems");
        //班段类型
        String bdlx = Util.null2String(classInfo.get("bdlx"));
        //卡点
        ClockPointDTO clcokInTimedto = (ClockPointDTO)params.get("point");
        //分析日期
        String analysisDate = Util.null2String(params.get("analysisDate"));
        //班次
        List<Map<String,Object>> scheduleResult = (List<Map<String,Object>>)params.get("scheduleResult");
        //人员
        String userId = Util.null2String(params.get("userId"));
        //请假集合
        List<Map<String,Object>> askForLeaveList = (List<Map<String,Object>>)params.get("askForLeaveList");
        //请假项目
        Map<String,Map<String,Object>> askForLeaveItems = (Map<String,Map<String,Object>>)params.get("askForLeaveItems");
        //出差集合
        List<Map<String,Object>> evectionList = (List<Map<String,Object>>)params.get("evectionList");
        //出差项目
        Map<String,Map<String,Object>> evectionItems = (Map<String,Map<String,Object>>)params.get("evectionItems");

        List<Map<String,Object>> resultList = Lists.newArrayList();

        String clockInTime = clcokInTimeData.get("signdate")+" "+clcokInTimeData.get("signtime");

        String classStartTime = clcokInTimedto.getClassTime();
        if (clcokInTimedto.getElasticTime() != null && !"".equals(clcokInTimedto.getElasticTime())){
            classStartTime = clcokInTimedto.getElasticTime();
        }
        int between = Utils.removeRestTime(classStartTime,clockInTime,scheduleResult,analysisDate);

        Map<String,Object> lateParams = Maps.newHashMap();
        lateParams.put("attendanceItems",attendanceItems);
        lateParams.put("time",between);
        lateParams.put("rqlx",classInfo.get("rqlx"));


        if (ClassSegmentTypeEnum.WORK_TIME.getKey().equals(bdlx)){
            //工作时段
            lateParams.put("workfor",WorkForTimeEnum.WORK_TIME.getKey());
            Map<String,Object> result = commandExecutor.execute(new BeLateItemCmd(lateParams));
            List<Map<String,Object>> workTimeBeLateItems = (List<Map<String,Object>>)result.get("attendanceItems");
            if (workTimeBeLateItems.size() == 0){
                workTimeBeLateItems = (List<Map<String,Object>>)commandExecutor.execute(new AbsenteeismItemCmd(lateParams)).get("attendanceItems");
            }
            if (workTimeBeLateItems.size() == 0){
                workTimeBeLateItems = attendanceItems.stream().filter(e->SystemItemEnum.MISSING_ABSENTEEISM.getKey().equals(e.get("key"))).collect(Collectors.toList());
                workTimeBeLateItems.get(0).put("kczgsc",CheckBoxEnum.CHECKED.getKey());
                workTimeBeLateItems.get(0).put("qbkcsc","1");
                workTimeBeLateItems.get(0).put("cckcbc","1");

            }
            if (workTimeBeLateItems.size() > 0){
                Map<String,Object> saveWorkTimeBeLateParam = Maps.newHashMap();
                double hsl = Double.valueOf(Util.null2String(workTimeBeLateItems.get(0).get("hsl")));
                String hsdw = Util.null2String(workTimeBeLateItems.get(0).get("hsdw"));
                String kczgsc = Util.null2String(workTimeBeLateItems.get(0).get("kczgsc"));
                if (CheckBoxEnum.CHECKED.getKey().equals(kczgsc)){
                    //起步扣除分钟数
                    int qbkcsc = Integer.valueOf(Util.null2String(workTimeBeLateItems.get(0).get("qbkcsc")));
                    //超出后单次累加扣除分钟数
                    int cckcbc = Integer.valueOf(Util.null2String(workTimeBeLateItems.get(0).get("cckcbc")));
                    if (between <= qbkcsc){
                        between = qbkcsc;
                    }else {
                        int deductionDuration = between-qbkcsc;
                        between = Double.valueOf(Utils.getItemduration(cckcbc,AccountingUnitEnum.MINUTES.getKey(),deductionDuration,AccountingUnitEnum.MINUTES)).intValue()+qbkcsc;

                    }
                }
                double itemduration = Utils.getItemduration(hsl,hsdw,between,AccountingUnitEnum.MINUTES);
                saveWorkTimeBeLateParam.put("item",workTimeBeLateItems.get(0).get("key"));
                saveWorkTimeBeLateParam.put("itemduration",itemduration);
                saveWorkTimeBeLateParam.put("betweenMinutes",between);
                saveWorkTimeBeLateParam.put("itemType", AttendanceItemTypeEnum.LATE);
                saveWorkTimeBeLateParam.put("hsdw",hsdw);
                saveWorkTimeBeLateParam.put("hsl",hsl);
                saveWorkTimeBeLateParam.put("kczgsc",kczgsc);
                saveWorkTimeBeLateParam.put("classStartTime",classStartTime);
                saveWorkTimeBeLateParam.put("classEndTime",clockInTime);
                resultList.add(saveWorkTimeBeLateParam);

            }

        }else if (ClassSegmentTypeEnum.EXTENDED_OVERTIME.getKey().equals(bdlx) || ClassSegmentTypeEnum.EARLY_OVERTIME.getKey().equals(bdlx)
        || ClassSegmentTypeEnum.OVERTIME_PLAN.getKey().equals(bdlx) || ClassSegmentTypeEnum.OVERTIME_IN_CLASS.getKey().equals(bdlx)){
            //加班
            List<Map<String,Object>> workTimeItems = Lists.newArrayList();
            if (ClassSegmentTypeEnum.EXTENDED_OVERTIME.getKey().equals(bdlx)){
                lateParams.put("workfor",WorkForTimeEnum.DELAY_TO_WORK_OVERTIME.getKey());
                Map<String,Object> result = commandExecutor.execute(new WorkOvertimeItemCmd(lateParams));
                workTimeItems = (List<Map<String,Object>>)result.get("attendanceItems");
            }else if (ClassSegmentTypeEnum.EARLY_OVERTIME.getKey().equals(bdlx)){
                lateParams.put("workfor",WorkForTimeEnum.EARLY_TO_WORK_OVERTIME.getKey());
                Map<String,Object> result = commandExecutor.execute(new WorkOvertimeItemCmd(lateParams));
                workTimeItems = (List<Map<String,Object>>)result.get("attendanceItems");
            }else if (ClassSegmentTypeEnum.OVERTIME_PLAN.getKey().equals(bdlx)){
                lateParams.put("workfor",WorkForTimeEnum.PLAN_WORK_OVERTIME.getKey());
                workTimeItems = (List<Map<String,Object>>)classInfo.get("jblx");
            }else if (ClassSegmentTypeEnum.OVERTIME_IN_CLASS.getKey().equals(bdlx)){
                lateParams.put("workfor",WorkForTimeEnum.OVERTIME_IN_CLASS.getKey());
                Map<String,Object> result = commandExecutor.execute(new WorkOvertimeItemCmd(lateParams));
                workTimeItems = (List<Map<String,Object>>)result.get("attendanceItems");
            }
            List<Map<String,Object>> lateItems = getBeLateItems(lateParams);
            if (workTimeItems.size() > 0 && lateItems.size() >0){
                double hsl = Double.valueOf(Util.null2String(lateItems.get(0).get("hsl")));
                String hsdw = Util.null2String(lateItems.get(0).get("hsdw"));
                String jbwdhlfzs = Util.null2String(workTimeItems.get(0).get("jbwdhlfzs")).equals("")?"0":Util.null2String(workTimeItems.get(0).get("jbwdhlfzs"));
                if (!"".equals(jbwdhlfzs) && between > Integer.valueOf(jbwdhlfzs)){
                    Map<String,Object> saveWorkTimeBeLateParam = Maps.newHashMap();
                    String kczgsc = Util.null2String(lateItems.get(0).get("kczgsc"));
                    if (CheckBoxEnum.CHECKED.getKey().equals(kczgsc)){
                        //起步扣除分钟数
                        int qbkcsc = Integer.valueOf(Util.null2String(lateItems.get(0).get("qbkcsc")));
                        //超出后单次累加扣除分钟数
                        int cckcbc = Integer.valueOf(Util.null2String(lateItems.get(0).get("cckcbc")));
                        if (between <= qbkcsc){
                            between = qbkcsc;
                        }else {
                            int deductionDuration = between-qbkcsc;
                            between = Double.valueOf(Utils.getItemduration(cckcbc,AccountingUnitEnum.MINUTES.getKey(),deductionDuration,AccountingUnitEnum.MINUTES)).intValue()+qbkcsc;

                        }
                    }
                    double itemduration = Utils.getItemduration(hsl,hsdw,between,AccountingUnitEnum.MINUTES);
                    saveWorkTimeBeLateParam.put("item",lateItems.get(0).get("key"));
                    saveWorkTimeBeLateParam.put("itemduration",itemduration);
                    saveWorkTimeBeLateParam.put("betweenMinutes",between);
                    saveWorkTimeBeLateParam.put("itemType", AttendanceItemTypeEnum.LATE);
                    saveWorkTimeBeLateParam.put("hsdw",hsdw);
                    saveWorkTimeBeLateParam.put("hsl",hsl);
                    saveWorkTimeBeLateParam.put("kczgsc",kczgsc);
                    saveWorkTimeBeLateParam.put("classStartTime",classStartTime);
                    saveWorkTimeBeLateParam.put("classEndTime",clockInTime);
                    resultList.add(saveWorkTimeBeLateParam);

                }
            }

        }else if (ClassSegmentTypeEnum.ASK_FOR_LEAVE.getKey().equals(bdlx) || ClassSegmentTypeEnum.EVECTION.getKey().equals(bdlx)){
            //请假、出差
            Map<String,Object> result = null;
            if (ClassSegmentTypeEnum.ASK_FOR_LEAVE.getKey().equals(bdlx)){
                lateParams.put("workfor",WorkForTimeEnum.WORK_TIME.getKey());
                result = getEvectionAndAskForByEndTime(askForLeaveItems,askForLeaveList,classStartTime.split(" ")[1],"jqlx");
            }else if (ClassSegmentTypeEnum.EVECTION.getKey().equals(bdlx)){
                lateParams.put("workfor",WorkForTimeEnum.WORK_TIME.getKey());
                result = getEvectionAndAskForByEndTime(evectionItems,evectionList,classStartTime.split(" ")[1],"cclx");
            }
            List<Map<String,Object>> beAwayItems = Lists.newArrayList();
            beAwayItems.add(result);
            List<Map<String,Object>> lateItems = getBeLateItems(lateParams);
            if (beAwayItems.size() >0 && lateItems.size() >0){
                double hsl = Double.valueOf(Util.null2String(lateItems.get(0).get("hsl")));
                String hsdw = Util.null2String(lateItems.get(0).get("hsdw"));
                String thfghlfzs = Util.null2String(beAwayItems.get(0).get("thfghlfzs"));
                if (!"".equals(thfghlfzs) &&  between> Integer.valueOf(thfghlfzs)){
                    String kczgsc = Util.null2String(lateItems.get(0).get("kczgsc"));
                    if (CheckBoxEnum.CHECKED.getKey().equals(kczgsc)){
                        //起步扣除分钟数
                        int qbkcsc = Integer.valueOf(Util.null2String(lateItems.get(0).get("qbkcsc")));
                        //超出后单次累加扣除分钟数
                        int cckcbc = Integer.valueOf(Util.null2String(lateItems.get(0).get("cckcbc")));
                        if (between <= qbkcsc){
                            between = qbkcsc;
                        }else {
                            int deductionDuration = between-qbkcsc;
                            between = Double.valueOf(Utils.getItemduration(cckcbc,AccountingUnitEnum.MINUTES.getKey(),deductionDuration,AccountingUnitEnum.MINUTES)).intValue()+qbkcsc;

                        }
                    }
                    double itemduration = Utils.getItemduration(hsl,hsdw,between,AccountingUnitEnum.MINUTES);
                    Map<String,Object> saveWorkTimeBeLateParam = Maps.newHashMap();
                    saveWorkTimeBeLateParam.put("item",lateItems.get(0).get("key"));
                    saveWorkTimeBeLateParam.put("itemduration",itemduration);
                    saveWorkTimeBeLateParam.put("betweenMinutes",between);
                    saveWorkTimeBeLateParam.put("itemType", AttendanceItemTypeEnum.LATE);
                    saveWorkTimeBeLateParam.put("hsdw",hsdw);
                    saveWorkTimeBeLateParam.put("kczgsc",kczgsc);
                    saveWorkTimeBeLateParam.put("hsl",hsl);
                    saveWorkTimeBeLateParam.put("classStartTime",classStartTime);
                    saveWorkTimeBeLateParam.put("classEndTime",clockInTime);
                    resultList.add(saveWorkTimeBeLateParam);

                }
            }
        }

        return resultList;
    }

    @Override
    public List<Map<String,Object>> leaveEarly(Map<String, Object> params) {
        //当前单条明细班次
        Map<String, Object> classInfo =  (Map<String, Object>)params.get("classInfo");
        //当前单个打卡数据
        Map<String,Object> clcokInTimeData = (Map<String, Object>)params.get("clcokInTimeData");
        //考勤项目
        List<Map<String,Object>> attendanceItems = (List<Map<String,Object>>)params.get("attendanceItems");
        List<Map<String,Object>> scheduleResult = (List<Map<String,Object>>)params.get("scheduleResult");
        //班段类型
        String bdlx = Util.null2String(classInfo.get("bdlx"));
        //分析日期
        String analysisDate = Util.null2String(params.get("analysisDate"));
        //人员
        String userId = Util.null2String(params.get("userId"));
        //卡点
        ClockPointDTO clockPointDTO = (ClockPointDTO)params.get("point");
        //请假集合
        List<Map<String,Object>> askForLeaveList = (List<Map<String,Object>>)params.get("askForLeaveList");
        //请假项目
        Map<String,Map<String,Object>> askForLeaveItems = (Map<String,Map<String,Object>>)params.get("askForLeaveItems");
        //出差集合
        List<Map<String,Object>> evectionList = (List<Map<String,Object>>)params.get("evectionList");
        //出差项目
        Map<String,Map<String,Object>> evectionItems = (Map<String,Map<String,Object>>)params.get("evectionItems");

        List<Map<String,Object>> resultList = Lists.newArrayList();

        String clockInTime = clcokInTimeData.get("signdate")+" "+clcokInTimeData.get("signtime");
        String classEndTime = clockPointDTO.getClassTime();
        if (clockPointDTO.getElasticTime() != null && !"".equals(clockPointDTO.getElasticTime())){
            classEndTime = clockPointDTO.getElasticTime();
        }

        int between = Utils.removeRestTime(clockInTime,classEndTime,scheduleResult,analysisDate);

        Map<String,Object> earlyParams = Maps.newHashMap();
        earlyParams.put("attendanceItems",attendanceItems);
        earlyParams.put("time",between);
        earlyParams.put("rqlx",classInfo.get("rqlx"));

        boolean ifLeaveEarly = false;

        if (ClassSegmentTypeEnum.WORK_TIME.getKey().equals(bdlx)){
            //工作时段
            earlyParams.put("workfor",WorkForTimeEnum.WORK_TIME.getKey());
            Map<String,Object> result = commandExecutor.execute(new LeaveEarlyItemCmd(earlyParams));
            List<Map<String,Object>> workTimeBeLateItems = (List<Map<String,Object>>)result.get("attendanceItems");
            if (workTimeBeLateItems.size() == 0){
                workTimeBeLateItems = (List<Map<String,Object>>)commandExecutor.execute(new AbsenteeismItemCmd(earlyParams)).get("attendanceItems");
            }
            if (workTimeBeLateItems.size() == 0){
                workTimeBeLateItems = attendanceItems.stream().filter(e->SystemItemEnum.MISSING_ABSENTEEISM.getKey().equals(e.get("key"))).collect(Collectors.toList());
                workTimeBeLateItems.get(0).put("kczgsc",CheckBoxEnum.CHECKED.getKey());
                workTimeBeLateItems.get(0).put("qbkcsc","1");
                workTimeBeLateItems.get(0).put("cckcbc","1");
            }
            if (workTimeBeLateItems.size() > 0){
                Map<String,Object> saveWorkTimeBeLateParam = Maps.newHashMap();
                double hsl = Double.valueOf(Util.null2String(workTimeBeLateItems.get(0).get("hsl")));
                String hsdw = Util.null2String(workTimeBeLateItems.get(0).get("hsdw"));
                String kczgsc = Util.null2String(workTimeBeLateItems.get(0).get("kczgsc"));
                if (CheckBoxEnum.CHECKED.getKey().equals(kczgsc)){
                    //起步扣除分钟数
                    int qbkcsc = Integer.valueOf(Util.null2String(workTimeBeLateItems.get(0).get("qbkcsc")));
                    //超出后单次累加扣除分钟数
                    int cckcbc = Integer.valueOf(Util.null2String(workTimeBeLateItems.get(0).get("cckcbc")));
                    if (between <= qbkcsc){
                        between = qbkcsc;
                    }else {
                        int deductionDuration = between-qbkcsc;
                        between = Double.valueOf(Utils.getItemduration(cckcbc,AccountingUnitEnum.MINUTES.getKey(),deductionDuration,AccountingUnitEnum.MINUTES)).intValue()+qbkcsc;

                    }
                }
                double itemduration = Utils.getItemduration(hsl,hsdw,between,AccountingUnitEnum.MINUTES);
//                saveWorkTimeBeLateParam.put("userId",userId);
//                saveWorkTimeBeLateParam.put("date",analysisDate);

                saveWorkTimeBeLateParam.put("item",workTimeBeLateItems.get(0).get("key"));
                saveWorkTimeBeLateParam.put("itemduration",itemduration);
                saveWorkTimeBeLateParam.put("betweenMinutes",between);
                saveWorkTimeBeLateParam.put("itemType", AttendanceItemTypeEnum.LEAVE_EARLY);
                saveWorkTimeBeLateParam.put("hsdw",hsdw);
                saveWorkTimeBeLateParam.put("kczgsc",kczgsc);
                saveWorkTimeBeLateParam.put("hsl",hsl);
                saveWorkTimeBeLateParam.put("classStartTime",clockInTime);
                saveWorkTimeBeLateParam.put("classEndTime",classEndTime);
                resultList.add(saveWorkTimeBeLateParam);

            }

        }else if (ClassSegmentTypeEnum.EXTENDED_OVERTIME.getKey().equals(bdlx) || ClassSegmentTypeEnum.EARLY_OVERTIME.getKey().equals(bdlx)
                || ClassSegmentTypeEnum.OVERTIME_PLAN.getKey().equals(bdlx)){
            //加班
            List<Map<String,Object>> workTimeItems = Lists.newArrayList();
            if (ClassSegmentTypeEnum.EXTENDED_OVERTIME.getKey().equals(bdlx)){
                earlyParams.put("workfor",WorkForTimeEnum.DELAY_TO_WORK_OVERTIME.getKey());
                Map<String,Object> result = commandExecutor.execute(new WorkOvertimeItemCmd(earlyParams));
                workTimeItems = (List<Map<String,Object>>)result.get("attendanceItems");
            }else if (ClassSegmentTypeEnum.EARLY_OVERTIME.getKey().equals(bdlx)){
                earlyParams.put("workfor",WorkForTimeEnum.EARLY_TO_WORK_OVERTIME.getKey());
                Map<String,Object> result = commandExecutor.execute(new WorkOvertimeItemCmd(earlyParams));
                workTimeItems = (List<Map<String,Object>>)result.get("attendanceItems");
            }else if (ClassSegmentTypeEnum.OVERTIME_PLAN.getKey().equals(bdlx)){
                earlyParams.put("workfor",WorkForTimeEnum.PLAN_WORK_OVERTIME.getKey());
                workTimeItems = (List<Map<String,Object>>)classInfo.get("jblx");
            }
            List<Map<String,Object>> beEarlyItems = getBeEarlyItems(earlyParams);
            if (workTimeItems.size() > 0 && beEarlyItems.size() >0){
                String jbzzhlfzs = Util.null2String(workTimeItems.get(0).get("jbzzhlfzs")).equals("")?"0":Util.null2String(workTimeItems.get(0).get("jbzzhlfzs"));
                String hsdw = Util.null2String(beEarlyItems.get(0).get("hsdw"));
                double hsl = Double.valueOf(Util.null2String(beEarlyItems.get(0).get("hsl")));
                if (!"".equals(jbzzhlfzs) && between > Integer.valueOf(jbzzhlfzs)){
                    log.info("between :{},jbzzhlfzs:{}",between,jbzzhlfzs);
                    Map<String,Object> saveWorkTimeBeLateParam = Maps.newHashMap();
                    String kczgsc = Util.null2String(beEarlyItems.get(0).get("kczgsc"));
                    if (CheckBoxEnum.CHECKED.getKey().equals(kczgsc)){
                        //起步扣除分钟数
                        int qbkcsc = Integer.valueOf(Util.null2String(beEarlyItems.get(0).get("qbkcsc")));
                        //超出后单次累加扣除分钟数
                        int cckcbc = Integer.valueOf(Util.null2String(beEarlyItems.get(0).get("cckcbc")));
                        if (between <= qbkcsc){
                            between = qbkcsc;
                        }else {
                            int deductionDuration = between-qbkcsc;
                            between = Double.valueOf(Utils.getItemduration(cckcbc,AccountingUnitEnum.MINUTES.getKey(),deductionDuration,AccountingUnitEnum.MINUTES)).intValue()+qbkcsc;

                        }
                    }
                    double itemduration = Utils.getItemduration(hsl,hsdw,between,AccountingUnitEnum.MINUTES);
                    saveWorkTimeBeLateParam.put("item",beEarlyItems.get(0).get("key"));
                    saveWorkTimeBeLateParam.put("itemduration",itemduration);
                    saveWorkTimeBeLateParam.put("betweenMinutes",between);
                    saveWorkTimeBeLateParam.put("itemType", AttendanceItemTypeEnum.LEAVE_EARLY);
                    saveWorkTimeBeLateParam.put("hsdw",hsdw);
                    saveWorkTimeBeLateParam.put("kczgsc",kczgsc);
                    saveWorkTimeBeLateParam.put("hsl",hsl);
                    saveWorkTimeBeLateParam.put("classStartTime",clockInTime);
                    saveWorkTimeBeLateParam.put("classEndTime",classEndTime);
                    resultList.add(saveWorkTimeBeLateParam);

                }
            }

        }else if (ClassSegmentTypeEnum.ASK_FOR_LEAVE.getKey().equals(bdlx) || ClassSegmentTypeEnum.EVECTION.getKey().equals(bdlx)){
            //请假、出差
            Map<String,Object> result = null;
            if (ClassSegmentTypeEnum.ASK_FOR_LEAVE.getKey().equals(bdlx)){
                earlyParams.put("workfor",WorkForTimeEnum.WORK_TIME.getKey());
                result = getEvectionAndAskForByStartTime(askForLeaveItems,askForLeaveList,classEndTime.split(" ")[1],"jqlx");
            }else if (ClassSegmentTypeEnum.EVECTION.getKey().equals(bdlx)){
                earlyParams.put("workfor",WorkForTimeEnum.WORK_TIME.getKey());
                result = getEvectionAndAskForByStartTime(evectionItems,evectionList,classEndTime.split(" ")[1],"cclx");
            }
            List<Map<String,Object>> beAwayItems = Lists.newArrayList();
            beAwayItems.add(result);
            List<Map<String,Object>> beEarlyItems = getBeEarlyItems(earlyParams);
            if(beEarlyItems.size() > 0 && beAwayItems.size()>0){
                double hsl = Double.valueOf(Util.null2String(beEarlyItems.get(0).get("hsl")));
                String hsdw = Util.null2String(beEarlyItems.get(0).get("hsdw"));
                String tqlghlfzs = Util.null2String(beAwayItems.get(0).get("tqlghlfzs"));
                if (!"".equals(tqlghlfzs) &&  between> Integer.valueOf(tqlghlfzs) && beEarlyItems.size()>0){
                    String kczgsc = Util.null2String(beEarlyItems.get(0).get("kczgsc"));
                    if (CheckBoxEnum.CHECKED.getKey().equals(kczgsc)){
                        //起步扣除分钟数
                        int qbkcsc = Integer.valueOf(Util.null2String(beEarlyItems.get(0).get("qbkcsc")));
                        //超出后单次累加扣除分钟数
                        int cckcbc = Integer.valueOf(Util.null2String(beEarlyItems.get(0).get("cckcbc")));
                        if (between <= qbkcsc){
                            between = qbkcsc;
                        }else {
                            int deductionDuration = between-qbkcsc;
                            between = Double.valueOf(Utils.getItemduration(cckcbc,AccountingUnitEnum.MINUTES.getKey(),deductionDuration,AccountingUnitEnum.MINUTES)).intValue()+qbkcsc;

                        }
                    }
                    double itemduration = Utils.getItemduration(hsl,hsdw,between,AccountingUnitEnum.MINUTES);
                    Map<String,Object> saveWorkTimeBeLateParam = Maps.newHashMap();

                    saveWorkTimeBeLateParam.put("item",beEarlyItems.get(0).get("key"));
                    saveWorkTimeBeLateParam.put("itemduration",itemduration);
                    saveWorkTimeBeLateParam.put("betweenMinutes",between);
                    saveWorkTimeBeLateParam.put("itemType", AttendanceItemTypeEnum.LEAVE_EARLY);
                    saveWorkTimeBeLateParam.put("hsdw",hsdw);
                    saveWorkTimeBeLateParam.put("kczgsc",kczgsc);
                    saveWorkTimeBeLateParam.put("hsl",hsl);
                    saveWorkTimeBeLateParam.put("classStartTime",clockInTime);
                    saveWorkTimeBeLateParam.put("classEndTime",classEndTime);
                    resultList.add(saveWorkTimeBeLateParam);

                }
            }
        }

        return resultList;
    }

    /**
     * 获得迟到项目
     * @return
     */
    public List<Map<String,Object>> getBeLateItems(Map<String,Object> lateParams){
        Map<String,Object> result = commandExecutor.execute(new BeLateItemCmd(lateParams));
        List<Map<String,Object>> workTimeBeLateItems = (List<Map<String,Object>>)result.get("attendanceItems");
        if (workTimeBeLateItems.size() == 0){
            workTimeBeLateItems = (List<Map<String,Object>>)commandExecutor.execute(new AbsenteeismItemCmd(lateParams)).get("attendanceItems");
        }
        if (workTimeBeLateItems.size() == 0){
            List<Map<String,Object>> attendanceItems = (List<Map<String,Object>>)lateParams.get("attendanceItems");
            workTimeBeLateItems = attendanceItems.stream().filter(e->SystemItemEnum.MISSING_ABSENTEEISM.getKey().equals(e.get("key"))).collect(Collectors.toList());
            workTimeBeLateItems.get(0).put("kczgsc",CheckBoxEnum.CHECKED.getKey());
            workTimeBeLateItems.get(0).put("qbkcsc","1");
            workTimeBeLateItems.get(0).put("cckcbc","1");

        }
        return workTimeBeLateItems;
    }
    /**
     * 获得早退项目
     * @return
     */
    public List<Map<String,Object>> getBeEarlyItems(Map<String,Object> earlyParams){
        Map<String,Object> result = commandExecutor.execute(new LeaveEarlyItemCmd(earlyParams));
        List<Map<String,Object>> workTimeBeLateItems = (List<Map<String,Object>>)result.get("attendanceItems");
        if (workTimeBeLateItems.size() == 0){
            workTimeBeLateItems = (List<Map<String,Object>>)commandExecutor.execute(new AbsenteeismItemCmd(earlyParams)).get("attendanceItems");
        }
        if (workTimeBeLateItems.size() == 0){
            List<Map<String,Object>> attendanceItems = (List<Map<String,Object>>)earlyParams.get("attendanceItems");
            workTimeBeLateItems = attendanceItems.stream().filter(e->SystemItemEnum.MISSING_ABSENTEEISM.getKey().equals(e.get("key"))).collect(Collectors.toList());
            workTimeBeLateItems.get(0).put("kczgsc",CheckBoxEnum.CHECKED.getKey());
            workTimeBeLateItems.get(0).put("qbkcsc","1");
            workTimeBeLateItems.get(0).put("cckcbc","1");

        }
        return workTimeBeLateItems;
    }


    public Map<String,Object> getEvectionAndAskForByStartTime(Map<String,Map<String,Object>> items,List<Map<String,Object>> beAwayist,String time,String key){
        Map<String,Object> map = Maps.newHashMap();
        for (Map<String,Object> beAway:beAwayist){
            if (beAway.get("kssj").equals(time)){
                map = items.get(beAway.get(key));
            }
        }
        return map;
    }

    public Map<String,Object> getEvectionAndAskForByEndTime(Map<String,Map<String,Object>> items,List<Map<String,Object>> beAwayist,String time,String key){
        Map<String,Object> map = Maps.newHashMap();
        for (Map<String,Object> beAway:beAwayist){
            if (beAway.get("jssj").equals(time)){
                map = items.get(beAway.get(key));
            }
        }
        return map;
    }
}
