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

import com.engine.attendance.attendanceanalysis.cmd.item.WorkOvertimeItemCmd;
import com.engine.attendance.attendanceanalysis.service.WorkOverTimeService;
import com.engine.attendance.enums.*;
import com.engine.common.util.DateUtil;
import com.engine.common.util.DbTools;
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.TimeUtil;
import weaver.general.Util;

import java.math.BigDecimal;
import java.util.Calendar;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Slf4j
public class WorkOverTimeServiceImpl extends Service implements WorkOverTimeService {
    @Override
    public List<Map<String, Object>> recordWorkOverTime(Map<String, Object> params) {
        //考勤项目
        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 analysisDate = Util.null2String(params.get("analysisDate"));
        //请假记录
        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,Map<String,Object>>> clcokInTimeList = (List<Map<String,Map<String,Object>>>)params.get("clcokInTimeList");
        //请假后消除的异常
        List<Map<String,Object>> offsetAskForLeaveAnomaly = (List<Map<String,Object>>)params.get("offsetAskForLeaveAnomaly");
        //出差外出后消除的异常
        List<Map<String,Object>> offsetEvectionAnomaly = (List<Map<String,Object>>)params.get("offsetEvectionAnomaly");
        //经过请假外出处理过的异常项目
        List<Map<String,Object>> abnormalClockInList = (List<Map<String,Object>>)params.get("abnormalClockInList");

        //总抵消异常
        List<Map<String,Object>> anomalyList = Lists.newArrayList();
        anomalyList.addAll(offsetEvectionAnomaly);
        anomalyList.addAll(abnormalClockInList);


        Map<String,Map<String,Object>> clcokInTimeMap = Maps.newHashMap();

        List<Map<String, Object>> workOverTimeResults = null;



        Calendar calendar = Calendar.getInstance();
        calendar.set(Integer.valueOf(analysisDate.split("-")[0]), Integer.valueOf(analysisDate.split("-")[1]) - 1, 1);

        long diffdays  = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);

        for (Map<String,Map<String,Object>> map :clcokInTimeList){
            String point = "";
            //当天打卡数据
            Map<String,Object> clcokInTimeData = null;
            for (Map.Entry<String,Map<String,Object>> entry :map.entrySet()){
                point = entry.getKey();
                clcokInTimeData = entry.getValue();
            }
            //需要计算的班次打卡时间点
            String pointTime = point.split("\\|")[0];
            clcokInTimeMap.put(pointTime.split(" ")[1],clcokInTimeData);
        }
        //人员
        String userId = Util.null2String(params.get("userId"));
        List<Map<String, Object>> resultLists = Lists.newArrayList();

        for (Map<String, Object> scheduleMap :scheduleResult){
            String bdlx = Util.null2String(scheduleMap.get("bdlx"));
            String rqlx = Util.null2String(scheduleMap.get("rqlx"));
            Map<String,Object> getWorkOverTimeParam = Maps.newHashMap();
            Map<String,Object> workOverTimeItems = Maps.newHashMap();
            //加班开始时间
            String dtkssj = Util.null2String(scheduleMap.get("dtkssj"));
            //加班结束时间
            String dtjssj = Util.null2String(scheduleMap.get("dtjssj"));

            String kssj = analysisDate +" "+dtkssj;
            String jssj = analysisDate +" "+dtjssj;
            if (DateUtil.getTime(kssj).compareTo(DateUtil.getTime(jssj)) >0){
                jssj = DateUtil.AfterDay(analysisDate,1)+" "+Util.null2String(scheduleMap.get("dtjssj"));
            }
            //加班实际开始时间
            String realityStartTime = "";
            //加班实际结束时间
            String realityEndime = "";


            getWorkOverTimeParam.put("attendanceItems",attendanceItems);
            getWorkOverTimeParam.put("rqlx",scheduleMap.get("rqlx"));

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

            if (ClassSegmentTypeEnum.EXTENDED_OVERTIME.getKey().equals(bdlx)){
                getWorkOverTimeParam.put("workfor", WorkForTimeEnum.DELAY_TO_WORK_OVERTIME.getKey());
                Map<String,Object> result = commandExecutor.execute(new WorkOvertimeItemCmd(getWorkOverTimeParam));
                //加班项目
                workTimeBeLateItems = (List<Map<String,Object>>)result.get("attendanceItems");
            }else if (ClassSegmentTypeEnum.EARLY_OVERTIME.getKey().equals(bdlx)){
                getWorkOverTimeParam.put("workfor",WorkForTimeEnum.EARLY_TO_WORK_OVERTIME.getKey());
                Map<String,Object> result = commandExecutor.execute(new WorkOvertimeItemCmd(getWorkOverTimeParam));
                workTimeBeLateItems = (List<Map<String,Object>>)result.get("attendanceItems");
            }else if (ClassSegmentTypeEnum.OVERTIME_PLAN.getKey().equals(bdlx)){
                getWorkOverTimeParam.put("workfor",WorkForTimeEnum.PLAN_WORK_OVERTIME.getKey());
                workTimeBeLateItems = (List<Map<String,Object>>)scheduleMap.get("jblx");
            }else if (ClassSegmentTypeEnum.OVERTIME_IN_CLASS.getKey().equals(bdlx)){
                getWorkOverTimeParam.put("workfor",WorkForTimeEnum.OVERTIME_IN_CLASS.getKey());
                Map<String,Object> result = commandExecutor.execute(new WorkOvertimeItemCmd(getWorkOverTimeParam));
                workTimeBeLateItems = (List<Map<String,Object>>)result.get("attendanceItems");
            }else  {
                continue;
            }

            if (workTimeBeLateItems.size() == 0){
                continue;
            }
            double jbsc = Double.valueOf(Util.null2String(scheduleMap.get("edxss")));


            //开始加班必须打卡
            String ksjbbxydk = Util.null2String(workTimeBeLateItems.get(0).get("ksjbbxydk"));
            //结束加班必须打卡
            String jsjbbxydk = Util.null2String(workTimeBeLateItems.get(0).get("jsjbbxydk"));
            //提前打卡开始的时长计入加班
            String tqdkjrjb = Util.null2String(workTimeBeLateItems.get(0).get("tqdkjrjb"));
            //推后打卡结束的时长计入加班
            String thdkjrjb = Util.null2String(workTimeBeLateItems.get(0).get("thdkjrjb"));
            //结算加班时长不得超过申请的时长
            String jbscbdccsqsc = Util.null2String(workTimeBeLateItems.get(0).get("jbscbdccsqsc"));
            //是否扣除时间区间内的就餐休息时长
            String zdkcjcxxsc = Util.null2String(workTimeBeLateItems.get(0).get("zdkcjcxxsc"));
            //超出限制时长的处理方式
            String ccclfs = Util.null2String(workTimeBeLateItems.get(0).get("ccclfs"));
            //工作日加班最大小时数
            String rzdjbxss = Util.null2String(workTimeBeLateItems.get(0).get("rzdjbxss"));
            //每周最大加班小时数
            String yzdjbxss = Util.null2String(workTimeBeLateItems.get(0).get("yzdjbxss"));
            //每月最大加班小时数
            String zzdjbxss = Util.null2String(workTimeBeLateItems.get(0).get("zzdjbxss"));
            //加班时长自动转入假期余额
            String jbzdzjqye= Util.null2String(workTimeBeLateItems.get(0).get("jbzdzjqye"));
            //最小加班分钟数
            String jbqsfzs = Util.null2String(workTimeBeLateItems.get(0).get("jbqsfzs"));
            //超出最小时长后的单次累加分钟数
            String ccqszhdhsfzs = Util.null2String(workTimeBeLateItems.get(0).get("ccqszhdhsfzs"));
            //核算量
            double workOverTimeHsl = Double.valueOf(Util.null2String(workTimeBeLateItems.get(0).get("hsl")));
            //核算单位
            String workOverTimeHsdw = Util.null2String(workTimeBeLateItems.get(0).get("hsdw"));
            //晚于开始时间忽略分钟数
            String jbwdhlfzs = Util.null2String(workTimeBeLateItems.get(0).get("jbwdhlfzs"));
            //早于结束时间忽略分钟数
            String jbzzhlfzs = Util.null2String(workTimeBeLateItems.get(0).get("jbzzhlfzs"));

            /**
             * 获得实际加班时间
             */
            if (CheckBoxEnum.CHECKED.getKey().equals(ksjbbxydk)){
                Map<String,Object> clcokInTimeData = clcokInTimeMap.get(dtkssj);
                if (clcokInTimeData == null){
                    log.info("加班开始时间漏卡: {},clcokInTimeData:{}",dtkssj,clcokInTimeMap);
                    workOverTimeItems.put("item",workTimeBeLateItems.get(0).get("key"));
                    workOverTimeItems.put("itemduration","0");
                    resultLists.add(workOverTimeItems);
                    continue;
                }
                String hourTime = clcokInTimeData.get("signtime").toString();
                if (hourTime.length() > 5){
                    hourTime = hourTime.substring(0,5);
                }
                String signTime = clcokInTimeData.get("signdate")+" "+hourTime;
                realityStartTime = signTime;
            }else {
                realityStartTime = kssj;
            }
            if (CheckBoxEnum.CHECKED.getKey().equals(jsjbbxydk)){
                Map<String,Object> clcokInTimeData = clcokInTimeMap.get(dtjssj);
                if (clcokInTimeData == null){
                    log.info("加班结束时间漏卡: {},clcokInTimeData:{}",dtjssj,clcokInTimeData);
                    workOverTimeItems.put("item",workTimeBeLateItems.get(0).get("key"));
                    workOverTimeItems.put("itemduration","0");
                    resultLists.add(workOverTimeItems);
                    continue;
                }
                String hourTime = clcokInTimeData.get("signtime").toString();
                if (hourTime.length() > 5){
                    hourTime = hourTime.substring(0,5);
                }
                String signTime = clcokInTimeData.get("signdate")+" "+hourTime;
                    realityEndime = signTime;
            }else {
                realityEndime= jssj;
            }
            jbsc = jbsc*60;
            if (CheckBoxEnum.CHECKED.getKey().equals(tqdkjrjb)){
                //提前打卡开始的时长计入加班
                if (DateUtil.getTime(realityStartTime).compareTo(DateUtil.getTime(kssj)) <0){
                    jbsc +=DateUtil.getBetWeenMinutes(realityStartTime,kssj);
                }
            }
            if (CheckBoxEnum.CHECKED.getKey().equals(thdkjrjb)){
               // 推后打卡结束的时长计入加班
                if (DateUtil.getTime(realityEndime).compareTo(DateUtil.getTime(jssj)) >0){
                    jbsc +=DateUtil.getBetWeenMinutes(jssj,realityEndime);
                }
            }

            /**
             * 当存在迟到项目时扣除异常时长，不存在时扣除实际迟到时间
             */
            List<Map<String,Object>> beLateAbnormal = abnormalClockInList.stream().filter(e->e.get("pointTime").toString().split(" ")[1].equals(dtkssj)).collect(Collectors.toList());
            double beLateTime = 0;
            if (beLateAbnormal.size() >0){
                String hsdw = Util.null2String(beLateAbnormal.get(0).get("hsdw"));
                String itemduration = Util.null2String(beLateAbnormal.get(0).get("itemduration"));
                AttendanceItemTypeEnum itemType =  (AttendanceItemTypeEnum)beLateAbnormal.get(0).get("itemType");
                if (itemType == AttendanceItemTypeEnum.MISSE_CARD){
                    //早上漏卡
                    beLateTime = jbsc;
                }else {
                    if (AccountingUnitEnum.DAY.getKey().equals(hsdw)){
                        beLateTime = Integer.valueOf(scheduleMap.get("edsc").toString()) * Double.valueOf(itemduration);
                    }else if (AccountingUnitEnum.HOUR.getKey().equals(hsdw)){
                        beLateTime = Double.valueOf(itemduration)*60;
                    }else if (AccountingUnitEnum.MINUTES.getKey().equals(hsdw)){
                        beLateTime = Double.valueOf(itemduration);
                    }else if (AccountingUnitEnum.ONCE.getKey().equals(hsdw)){
                        beLateTime = jbsc;
                    }
                }
            }else {
                if (DateUtil.getTime(realityStartTime).compareTo(DateUtil.getTime(kssj)) >0){
                    beLateTime = DateUtil.getBetWeenMinutes(kssj,realityStartTime);
                    List<Map<String,Object>> collect1 = anomalyList.stream().filter(e->e.get("pointTime").toString().split(" ")[1].equals(dtkssj)).collect(Collectors.toList());

                    if (!"".equals(jbwdhlfzs) && beLateTime < Integer.valueOf(jbwdhlfzs)){
                        beLateTime=0;
                    }
                    //当存在迟到异常被抵消的情况
                    if (collect1.size() > 0){
                        beLateTime=0;
                    }

                }
            }
            /**
             * 当存在早退项目时扣除异常时长,不存在时扣除实际早退时间
             */
            List<Map<String,Object>> leaveEarlyAbnormal = abnormalClockInList.stream().filter(e->e.get("pointTime").toString().split(" ")[1].equals(dtjssj)).collect(Collectors.toList());
            double leaveElaryTime=0;
            if (leaveEarlyAbnormal.size() > 0){
                String hsdw = Util.null2String(leaveEarlyAbnormal.get(0).get("hsdw"));
                String itemduration = Util.null2String(leaveEarlyAbnormal.get(0).get("itemduration"));
                AttendanceItemTypeEnum itemType =  (AttendanceItemTypeEnum)leaveEarlyAbnormal.get(0).get("itemType");
                if (itemType == AttendanceItemTypeEnum.MISSE_CARD){
                    //下午漏卡
                    leaveElaryTime = jbsc;

                }else {
                    if (AccountingUnitEnum.DAY.getKey().equals(hsdw)){
                        leaveElaryTime = Integer.valueOf(scheduleMap.get("edsc").toString()) * Double.valueOf(itemduration);
                    }else if (AccountingUnitEnum.HOUR.getKey().equals(hsdw)){
                        leaveElaryTime = Double.valueOf(itemduration)*60;
                    }else if (AccountingUnitEnum.MINUTES.getKey().equals(hsdw)){
                        leaveElaryTime = Double.valueOf(itemduration);
                    }else if (AccountingUnitEnum.ONCE.getKey().equals(hsdw)){
                        leaveElaryTime = jbsc;
                    }
                }
            }else {
                if (DateUtil.getTime(realityEndime).compareTo(DateUtil.getTime(jssj)) <0){
                    leaveElaryTime = DateUtil.getBetWeenMinutes(realityEndime,jssj);
                    List<Map<String,Object>> collect2 = anomalyList.stream().filter(e->e.get("pointTime").toString().split(" ")[1].equals(dtjssj)).collect(Collectors.toList());
                    if (!"".equals(jbzzhlfzs) && leaveElaryTime < Integer.valueOf(jbzzhlfzs)){
                        leaveElaryTime=0;
                    }
                    //当存在早退异常被抵消的情况
                    if (collect2.size()>0){
                        leaveElaryTime=0;
                    }
                }

            }

            /**
             * 当存在请假时，考虑扣除请假时长
             */
            int askForLeaveTime = removeAskForLeave(realityStartTime,realityEndime,Util.null2String(getWorkOverTimeParam.get("workfor"))
                    ,bdlx,askForLeaveList,askForLeaveItems,offsetAskForLeaveAnomaly);

            /**
             * 当存在外出时，考虑扣除外出时长
             */
            int evectionTime = removeEvection(realityStartTime,realityEndime,Util.null2String(getWorkOverTimeParam.get("workfor"))
                    ,bdlx,evectionList,evectionItems,offsetEvectionAnomaly);

            log.info("加班时长: {}",jbsc);
            log.info("beLateTime :[{}],leaveElaryTime :[{}],askForLeaveTime:[{}],evectionTime:[{}]",beLateTime,leaveElaryTime,askForLeaveTime,evectionTime);
            BigDecimal jbscbig = new BigDecimal(jbsc);
            jbsc = jbscbig.subtract(new BigDecimal(beLateTime)).subtract(new BigDecimal(leaveElaryTime)).subtract(new BigDecimal(askForLeaveTime)).subtract(new BigDecimal(evectionTime)).intValue();
            log.info("jbsc :[{}]",jbsc);
            if (!"".equals(jbqsfzs)){
                if (jbsc < Integer.valueOf(jbqsfzs)){
                    //小于最小加班分钟数不算加班
                    workOverTimeItems.put("item",workTimeBeLateItems.get(0).get("key"));
                    workOverTimeItems.put("itemduration","0");
                    resultLists.add(workOverTimeItems);
                    continue;
                }else if (jbsc >= Integer.valueOf(jbqsfzs) && !"".equals(ccqszhdhsfzs)){

                    jbsc = Double.valueOf(Utils.getItemdurationDown(Integer.valueOf(ccqszhdhsfzs),AccountingUnitEnum.MINUTES.getKey(),Double.valueOf(jbsc).intValue()-Integer.valueOf(jbqsfzs),AccountingUnitEnum.MINUTES)).intValue()+Integer.valueOf(jbqsfzs);
                }
            }
            if (jbsc < 0){
                //加班时长为0
                workOverTimeItems.put("item",workTimeBeLateItems.get(0).get("key"));
                workOverTimeItems.put("itemduration","0");
                resultLists.add(workOverTimeItems);
                continue;
            }


            //自动扣除
            if ("2".equals(ccclfs) && workOverTimeResults == null){
                String startDate = analysisDate.split("-")[0]+"-"+ analysisDate.split("-")[1]+"-01";
                String endDate = analysisDate.split("-")[0]+"-"+ analysisDate.split("-")[1]+"-"+diffdays;
                workOverTimeResults =getWorkOverTimeResults(startDate,endDate,userId);

            }
            if ("2".equals(ccclfs) && !"".equals(rzdjbxss) && DateTypeEnum.WORK_DAY.getKey().equals(rqlx)){
                //工作日加班最大数
                double dayTime = getWorkDayTime(workOverTimeResults,analysisDate) *60;
                double maxDayTime = Double.valueOf(rzdjbxss) *60;
                double time = jbsc+dayTime;
                if (time > maxDayTime){
                    jbsc = maxDayTime-dayTime;
                }
            }
            if ("2".equals(ccclfs) && !"".equals(zzdjbxss)){
                //每周最大加班小时数
                double weekTime = getWeekTime(workOverTimeResults,analysisDate);
                double maxWeekTime = Double.valueOf(zzdjbxss) *60;
                double time = jbsc+weekTime;
                if (time > maxWeekTime){
                    jbsc = maxWeekTime-weekTime;
                }
            }
            if ("2".equals(ccclfs) && !"".equals(yzdjbxss)){
                //每月最大加班数
                double monthTime = getMonthTime(workOverTimeResults);
                double maxMonthTime = Double.valueOf(yzdjbxss) *60;
                double time = jbsc+monthTime;
                if (time > maxMonthTime){
                    jbsc = maxMonthTime-monthTime;
                }
            }
            if (CheckBoxEnum.CHECKED.getKey().equals(jbscbdccsqsc)){
                if (jbsc > Double.valueOf(Util.null2String(scheduleMap.get("edxss")))*60){
                    jbsc = Double.valueOf(Util.null2String(scheduleMap.get("edxss")))*60;
                }
            }
            log.info("最终加班时长: {}",jbsc);
            workOverTimeItems.put("item",workTimeBeLateItems.get(0).get("key"));
            double itemduration = Utils.getItemdurationDown(workOverTimeHsl,workOverTimeHsdw,Long.valueOf(Math.round(jbsc)).intValue(),AccountingUnitEnum.MINUTES);
            workOverTimeItems.put("itemduration",itemduration);

            /**
             * 入加班结果表
             */
            Map<String,Object> insertParam = Maps.newHashMap();
            insertParam.put("jbry",userId);
            insertParam.put("sjksrq",realityStartTime.split(" ")[0]);
            insertParam.put("sjkssj",realityStartTime.split(" ")[1]);
            insertParam.put("sjjsrq",realityEndime.split(" ")[0]);
            insertParam.put("sjjssj",realityEndime.split(" ")[1]);
            insertParam.put("sjjbsc",itemduration);
            insertParam.put("jbjgly","4");
            insertParam.put("jblx",workTimeBeLateItems.get(0).get("id"));
            insertParam.put("lyid",userId+"-"+DateUtil.getCurrentDate());
            insertParam.put("zt","1");
            Utils.InsertFormTable("uf_jcl_kq_jbjg",insertParam,(Map<String,String>)params.get("formmodeIdMap"));

            /**
             * 入假期余额
             */
            //加班时长自动转入假期余额
            if (CheckBoxEnum.CHECKED.getKey().equals(jbzdzjqye)){
                Map<String,Object> insertHoliDayParam = Maps.newHashMap();
                insertHoliDayParam.put("ygid",userId);
                insertHoliDayParam.put("jqid",workTimeBeLateItems.get(0).get("zrdjb"));
                insertHoliDayParam.put("lyid",userId+"-"+DateUtil.getCurrentDate());
                //转入的假期额度生效日期
                String yesxrq = Util.null2String(workTimeBeLateItems.get(0).get("yesxrq"));
                if ("0".equals(yesxrq)){
                    //一月后
                    insertHoliDayParam.put("sxrq",DateUtil.lastMonth(analysisDate,DateUtil.yyyyMMdd));
                }else if ("1".equals(yesxrq)){
                    //次月
                    insertHoliDayParam.put("sxrq",DateUtil.lastMonth(analysisDate)+"-01");
                }else if ("2".equals(yesxrq)){
                    //次日
                    insertHoliDayParam.put("sxrq",DateUtil.AfterDay(analysisDate,1));
                }
                //额度可用的周期
                String yekyzq = Util.null2String(workTimeBeLateItems.get(0).get("yekyzq"));
                if ("0".equals(yekyzq)){
                    //一个月
                    insertHoliDayParam.put("jzrq",DateUtil.nextMonth(insertHoliDayParam.get("sxrq").toString(),1,DateUtil.yyyyMMdd));
                }else if ("1".equals(yekyzq)){
                    //两个月
                    insertHoliDayParam.put("jzrq",DateUtil.nextMonth(insertHoliDayParam.get("sxrq").toString(),2,DateUtil.yyyyMMdd));
                }else if ("2".equals(yekyzq)){
                    //三个月
                    insertHoliDayParam.put("jzrq",DateUtil.nextMonth(insertHoliDayParam.get("sxrq").toString(),3,DateUtil.yyyyMMdd));
                }else if ("3".equals(yekyzq)){
                    //六个月
                    insertHoliDayParam.put("jzrq",DateUtil.nextMonth(insertHoliDayParam.get("sxrq").toString(),6,DateUtil.yyyyMMdd));
                }else if ("4".equals(yekyzq)){
                    //十二个月
                    insertHoliDayParam.put("jzrq",DateUtil.nextMonth(insertHoliDayParam.get("sxrq").toString(),12,DateUtil.yyyyMMdd));
                }else if ("5".equals(yekyzq)){
                    //季度
                    insertHoliDayParam.put("jzrq",DateUtil.nextMonth(insertHoliDayParam.get("sxrq").toString(),3,DateUtil.yyyyMMdd));
                }else if ("6".equals(yekyzq)){
                    //半年
                    insertHoliDayParam.put("jzrq",DateUtil.nextMonth(insertHoliDayParam.get("sxrq").toString(),6,DateUtil.yyyyMMdd));
                }else if ("7".equals(yekyzq)){
                    //一年
                    insertHoliDayParam.put("jzrq",DateUtil.nextMonth(insertHoliDayParam.get("sxrq").toString(),12,DateUtil.yyyyMMdd));
                }
                insertHoliDayParam.put("ktsc",itemduration);

                String zdyqsc = Util.null2String(workTimeBeLateItems.get(0).get("zdyqsc"));
                if (DelayTypeEnum.ONE_MONTH.getKey().equals(zdyqsc)){
                    //一个月
                    insertHoliDayParam.put("yqsxrq",DateUtil.nextMonth(insertHoliDayParam.get("jzrq").toString(),1,DateUtil.yyyyMMdd));
                }else if (DelayTypeEnum.TWO_MONTH.getKey().equals(zdyqsc)){
                    //两个月
                    insertHoliDayParam.put("yqsxrq",DateUtil.nextMonth(insertHoliDayParam.get("jzrq").toString(),2,DateUtil.yyyyMMdd));
                }else if (DelayTypeEnum.THREE_MONTH.getKey().equals(zdyqsc)){
                    //三个月
                    insertHoliDayParam.put("yqsxrq",DateUtil.nextMonth(insertHoliDayParam.get("jzrq").toString(),3,DateUtil.yyyyMMdd));
                }else if (DelayTypeEnum.HALF_YEAR.getKey().equals(zdyqsc)){
                    //半年
                    insertHoliDayParam.put("yqsxrq",DateUtil.nextMonth(insertHoliDayParam.get("jzrq").toString(),6,DateUtil.yyyyMMdd));
                }else if (DelayTypeEnum.ONE_YEAR.getKey().equals(zdyqsc)){
                    //一年
                    insertHoliDayParam.put("yqsxrq",DateUtil.nextMonth(insertHoliDayParam.get("jzrq").toString(),12,DateUtil.yyyyMMdd));
                }
                Utils.InsertFormTable("uf_jcl_kq_jqye",insertHoliDayParam,(Map<String,String>)params.get("formmodeIdMap"));

            }
            resultLists.add(workOverTimeItems);
        }

        return resultLists;
    }
    public List<Map<String, Object>> getWorkOverTimeResults(String startDate,String endDate,String userId){
        String sql = "select sjjbsc,sjksrq,sjjsrq from uf_jcl_kq_jbjg where jbry=? and sjksrq>? and sjjsrq<?";
        List<Map<String, Object>> dataList = DbTools.getSqlToList(sql,userId,startDate,endDate);
        return dataList;
    }

    /**
     * 获得工作日加班数
     * @param dataList
     * @return
     */
    public int getWorkDayTime(List<Map<String, Object>> dataList,String date){
        List<Map<String, Object>> list = dataList.stream().filter(e->date.equals(e.get("sjksrq"))).collect(Collectors.toList());
        int totalHour = list.stream().mapToInt(e->Integer.valueOf(e.get("sjjbsc").toString())).sum();
        return totalHour;
    }
    /**
     * 获得一周加班数
     * @param dataList
     * @return
     */
    public int getWeekTime(List<Map<String, Object>> dataList,String date){
        int day = TimeUtil.getDayOfWeek(date);
        if (day ==0){
            day = 7;
        }
        String startDate = DateUtil.beforeDay(date,day-1);
        String endDate = DateUtil.AfterDay(date,7-day);
        List<Map<String, Object>> list = dataList.stream().filter(e->{
            String sjksrq = Util.null2String(e.get("sjksrq"));
            if (DateUtil.getTime(sjksrq).compareTo(DateUtil.getTime(startDate)) >=0 &&
                    DateUtil.getTime(sjksrq).compareTo(DateUtil.getTime(endDate)) >=0){
                return true;
            }else {
                return false;
            }
        }).collect(Collectors.toList());
        int totalHour = list.stream().mapToInt(e->Integer.valueOf(e.get("sjjbsc").toString())).sum();
        return totalHour;
    }
    /**
     * 获得一个月加班数
     * @param dataList
     * @return
     */
    public int getMonthTime(List<Map<String, Object>> dataList){
        int totalHour = dataList.stream().mapToInt(e->Integer.valueOf(e.get("sjjbsc").toString())).sum();
        return totalHour;
    }

    /**
     * 请假影响加班时长
     * @param kssj 加班开始时间
     * @param jssj 加班结束时间
     * @param workFor 作用时段
     * @param askForLeaveList 请假集合
     * @param askForLeaveItems 请假项目
     * @return 请假在加班中所占时间
     */
    public int removeAskForLeave(String kssj,String jssj,String workFor,String bdlx,List<Map<String, Object>> askForLeaveList,
                                 Map<String,Map<String,Object>> askForLeaveItems,List<Map<String, Object>> offsetAskForLeaveAnomaly ){
        int employTime = 0;
        for (int i=0;i<askForLeaveList.size();i++){
            //请假时长
            String qjsc = Util.null2String(askForLeaveList.get(i).get("qjsc"));
            //请假项目
            Map<String,Object> askForLeaveItem = askForLeaveItems.get(askForLeaveList.get(i).get("jqlx"));
            //作用时段
            String zysd = Util.null2String(askForLeaveItem.get("zysd"));
            if (!zysd.contains(workFor) && !zysd.contains(WorkForTimeEnum.ALL_TIME.getKey())){
                continue;
            }
            if ("".equals(Util.null2String(askForLeaveList.get(i).get("kssj"))) || "".equals(Util.null2String(askForLeaveList.get(i).get("jssj")))){
                if (!"".equals(qjsc)){
                    //时长请假
                    if (offsetAskForLeaveAnomaly.size() > 0){
                        List<Map<String, Object>> list = offsetAskForLeaveAnomaly.stream().filter(e->bdlx.equals(e.get("bdlx"))).collect(Collectors.toList());
                        employTime += Math.round(list.size()/Double.valueOf(offsetAskForLeaveAnomaly.size()) *Double.valueOf(qjsc)*60);
                    }
                }
            }else {
                //按照开始时间，结束时间请假
                employTime +=Utils.getStartAndEndTime(kssj,jssj,askForLeaveList.get(i));
            }
        }
        return employTime;
    }

    /**
     * 外出影响加班时长
     * @param kssj 加班开始时间
     * @param jssj 加班结束时间
     * @param evectionList 外出、请假集合
     * @return
     */
    public int removeEvection(String kssj,String jssj,String workFor,String bdlx,List<Map<String, Object>> evectionList,Map<String,
            Map<String,Object>> evectionItems,List<Map<String, Object>> offsetEvectionAnomaly){
        int askForLeaveTime=0;
        for (int i=0;i<evectionList.size();i++){
            //出差时长
            String qjsc = Util.null2String(evectionList.get(i).get("ccsc"));
            //出差项目
            Map<String,Object> evectionItem = evectionItems.get(evectionList.get(i).get("cclx"));
            //作用时段
            String zysd = Util.null2String(evectionItem.get("zysd"));
            if (!zysd.contains(workFor) && !WorkForTimeEnum.ALL_TIME.getKey().equals(zysd)){
                continue;
            }

            if ("".equals(Util.null2String(evectionList.get(i).get("kssj"))) || "".equals(Util.null2String(evectionList.get(i).get("jssj")))){
                if (!"".equals(qjsc)){
                    if (offsetEvectionAnomaly.size() > 0){
                        List<Map<String, Object>> list = offsetEvectionAnomaly.stream().filter(e->bdlx.equals(e.get("bdlx"))).collect(Collectors.toList());
                        askForLeaveTime += Math.round(list.size()/Double.valueOf(offsetEvectionAnomaly.size()) *Double.valueOf(qjsc)*60);
                    }
                }
            }else {
                //按照开始时间，结束时间请假
                askForLeaveTime +=Utils.getStartAndEndTime(kssj,jssj,evectionList.get(i));
            }
        }
        return askForLeaveTime;
    }



}
