package com.api.customization.kq.util;

import com.engine.kq.biz.KQTimesArrayComInfo;
import com.engine.kq.enums.FlowReportTypeEnum;
import com.engine.kq.enums.KqSplitFlowTypeEnum;
import com.engine.kq.log.KQLog;
import com.engine.kq.wfset.bean.SplitBean;
import org.apache.commons.lang3.StringUtils;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;

import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.time.YearMonth;
import java.time.format.DateTimeFormatter;
import java.util.*;

public class CustomLeaveUtil {
    public KQLog kqLog = new KQLog();

    private static DecimalFormat df = new DecimalFormat("0.00");

    private DecimalFormatSymbols symbols = new DecimalFormatSymbols();
    String isline = new BaseBean().getPropValue("qc3560103","isline");//换行


    public String format(double value) {
//     DecimalFormatSymbols symbols = new DecimalFormatSymbols();
        symbols.setDecimalSeparator('.');
        df.setMaximumFractionDigits(5);
        df.setDecimalFormatSymbols(symbols);
        return df.format(value);
    }

    public List<SplitBean> getAllFlowData(Map<String,Object> flowMaps,String type){
        List<SplitBean> allSplitBeans = new ArrayList<>();
        allSplitBeans.addAll(getLeaveData(flowMaps,"",""));
        return allSplitBeans;
    }

    public List<SplitBean> getAllFlowData(Map<String,Object> flowMaps,String belongdate,String type){
        List<SplitBean> allSplitBeans = new ArrayList<>();
        allSplitBeans.addAll(getLeaveData(flowMaps,belongdate));
        return allSplitBeans;
    }

    /**
     * 获取所有的考勤数据
     * 请假，出差，公出，加班的
     * @param flowMaps
     * @param type
     * @return
     */
    public List<SplitBean> getAllFlowData(Map<String,Object> flowMaps,String fromDate,String toDate,String type){
        KqCustomFlowData kqCustomFlowData = new KqCustomFlowData();
        List<SplitBean> allSplitBeans = new ArrayList<>();

        allSplitBeans.addAll(getLeaveData(flowMaps,fromDate,toDate));
        allSplitBeans.addAll(kqCustomFlowData.getEvectionData(flowMaps,fromDate,toDate));
        allSplitBeans.addAll(kqCustomFlowData.getOutData(flowMaps,fromDate,toDate));
        return allSplitBeans;
    }

    /**
     * 获取请假相关的数据
     */
    public List<SplitBean> getLeaveData(Map<String, Object> flowMaps,String fromDate,String toDate){

        RecordSet rs = new RecordSet();
        RecordSet rs1 = new RecordSet();

        String tablename = "select a.* from kq_flow_split_temp_leave  a left join workflow_requestbase b on a.requestid = b.requestid where  b.requestid > 0 ";


        String sqlWhere = "";
        if(fromDate.length() > 0 && toDate.length() > 0){
            sqlWhere += " and belongdate between '"+fromDate+"' and '"+toDate+"' ";
        }

        if(sqlWhere.length() > 0){
            tablename += sqlWhere;
        }
        String leaveSql = "select * from ("+tablename+") t where 1=1 and (status is null or status <> '1') ";

        if(sqlWhere.length() > 0){
            leaveSql += sqlWhere;
        }
        List<SplitBean> splitBeans = new ArrayList<>();
        KQTimesArrayComInfo kqTimesArrayComInfo = new KQTimesArrayComInfo();

        int[] initArrays = kqTimesArrayComInfo.getInitArr();

        rs.execute(leaveSql);
        while(rs.next()){
            SplitBean splitBean = new SplitBean();
            String requestId= rs.getString("requestId");
            String resourceid= rs.getString("resourceid");
            String fromdate= rs.getString("fromdate");
            String belongdate= rs.getString("belongdate");
            String fromtime= rs.getString("fromtime");
            String todate= rs.getString("todate");
            String totime= rs.getString("totime");
            String newleavetype= rs.getString("newleavetype");
            String duration= rs.getString("duration");
            String durationrule= rs.getString("durationrule");
            String leavebackrequestid= Util.null2String(rs.getString("leavebackrequestid"));

            String fromtimedb= rs.getString("fromtimedb");
            String totimedb= rs.getString("totimedb");

            if(Util.getDoubleValue(duration) <= 0){
                //continue;
            }
            //计算规则 1-按天请假 2-按半天请假 3-按小时请假 4-按整天请假
            String unitType = "4".equalsIgnoreCase(durationrule)?"1":"2";
            String card_key = requestId+"_"+resourceid+"_"+KqSplitFlowTypeEnum.LEAVE.getFlowtype();
            String serial_signtype = "";
            String serial = "";
            String signtype = "";


            Map<String,String> infoMap = new HashMap<>();
            infoMap.put("requestId", requestId);
            infoMap.put(newleavetype, duration);
            infoMap.put("begintime", fromtime);
            infoMap.put("endtime", totime);
            infoMap.put("unitType", unitType);
            infoMap.put("durationrule", durationrule);
            if("2".equals(durationrule) && Util.getDoubleValue(duration)<1){
                infoMap.put("ishalf", "true");
            }else{
                infoMap.put("ishalf", "false");
            }
            infoMap.put("flowtype", FlowReportTypeEnum.LEAVE.getFlowType());
            infoMap.put("newleavetype", newleavetype);
            infoMap.put("signtype", signtype);
            infoMap.put("serial", serial);
            infoMap.put("fromtimedb", fromtimedb);
            infoMap.put("totimedb", totimedb);

            String key = resourceid+"|"+belongdate;
            if(flowMaps != null){
                if(flowMaps.get(key) != null){
                    if(leavebackrequestid.length() > 0 && leavebackrequestid.startsWith(",")){
                        initArrays = kqTimesArrayComInfo.getInitArr();
                        int fromTimeIndex = kqTimesArrayComInfo.getArrayindexByTimes(fromtime);
                        int toTimeIndex = kqTimesArrayComInfo.getArrayindexByTimes(totime);
                        //2025-05-21 如果存在跨天，那么可能会报错。加个校验
                        if (toTimeIndex < fromTimeIndex){
                            toTimeIndex += 1440;
                        }
                        Arrays.fill(initArrays, fromTimeIndex, toTimeIndex+1, 1);
                        leavebackrequestid = leavebackrequestid.substring(1);
                        String backSql = "select * from "+KqSplitFlowTypeEnum.LEAVEBACK.getTablename()+" where "+Util.getSubINClause(leavebackrequestid, "requestid", "in")+" and fromdate= '"+fromdate+"'";
                        rs1.executeQuery(backSql);

                        while (rs1.next()){
                            String back_fromtime = rs1.getString("fromtime");
                            String back_totime = rs1.getString("totime");
                            String back_duration= rs.getString("duration");
                            if(Util.getDoubleValue(back_duration) <= 0){
//                continue;
                            }
                            if(back_fromtime.equalsIgnoreCase(fromtime)){
                                //2025-05-21 如果存在跨天，那么可能会报错。加个校验
                                if ((kqTimesArrayComInfo.getArrayindexByTimes(back_totime)) < (kqTimesArrayComInfo.getArrayindexByTimes(back_fromtime))){
                                    Arrays.fill(initArrays, kqTimesArrayComInfo.getArrayindexByTimes(back_fromtime), kqTimesArrayComInfo.getArrayindexByTimes(back_totime)+1440, -1);
                                }else {
                                    Arrays.fill(initArrays, kqTimesArrayComInfo.getArrayindexByTimes(back_fromtime), kqTimesArrayComInfo.getArrayindexByTimes(back_totime), -1);
                                }
                            }else{
                                if ((kqTimesArrayComInfo.getArrayindexByTimes(back_totime)) < (kqTimesArrayComInfo.getArrayindexByTimes(back_fromtime))){
                                    Arrays.fill(initArrays, kqTimesArrayComInfo.getArrayindexByTimes(back_fromtime)+1, kqTimesArrayComInfo.getArrayindexByTimes(back_totime)+1440, -1);
                                }else {
                                    Arrays.fill(initArrays, kqTimesArrayComInfo.getArrayindexByTimes(back_fromtime)+1, kqTimesArrayComInfo.getArrayindexByTimes(back_totime), -1);

                                }
                            }
                        }
                        List<List<String>> backLists = new ArrayList<>();
                        List<String> backList = new ArrayList<>();
                        for(int i = fromTimeIndex ; i <= toTimeIndex ; i ++){
                            if(initArrays[i] == 1){
                                backList.add(kqTimesArrayComInfo.getTimesByArrayindex(i));
                            }else{
                                if(!backList.isEmpty()){
                                    backLists.add(backList);
                                    backList = new ArrayList<>();
                                }else{
                                    continue;
                                }
                            }
                        }
                        if(!backList.isEmpty()){
                            backLists.add(backList);
                        }
                        if(backLists != null && !backLists.isEmpty()){
                            List<Map<String,String>> time_list_tmp = (List<Map<String,String>>)flowMaps.get(key);
                            for(int j = 0 ; j < backLists.size() ;j++){
                                List<String> backListTmp = backLists.get(j);
                                String back_tmp_fromtime = backListTmp.get(0);
                                String back_tmp_totime = backListTmp.get(backListTmp.size()-1);
                                infoMap = new HashMap<>();
                                infoMap.put("requestId", requestId);
                                infoMap.put(newleavetype, duration);
                                infoMap.put("begintime", back_tmp_fromtime);
                                infoMap.put("endtime", back_tmp_totime);
                                infoMap.put("unitType", unitType);
                                infoMap.put("durationrule", durationrule);
                                if("2".equals(durationrule)){
                                    infoMap.put("ishalf", "true");
                                }
                                infoMap.put("flowtype", FlowReportTypeEnum.LEAVE.getFlowType());
                                infoMap.put("newleavetype", newleavetype);
                                time_list_tmp.add(infoMap);
                            }
                        }
                    }else{
                        List<Map<String,String>> time_list_tmp = (List<Map<String,String>>)flowMaps.get(key);
                        time_list_tmp.add(infoMap);
                    }
                }else{
                    if(leavebackrequestid.length() > 0 && leavebackrequestid.startsWith(",")){
                        initArrays = kqTimesArrayComInfo.getInitArr();
                        int fromTimeIndex = kqTimesArrayComInfo.getArrayindexByTimes(fromtime);
                        int toTimeIndex = kqTimesArrayComInfo.getArrayindexByTimes(totime);
                        if (toTimeIndex < fromTimeIndex){
                            toTimeIndex += 1440;
                        }
                        Arrays.fill(initArrays, fromTimeIndex, toTimeIndex+1, 1);
                        leavebackrequestid = leavebackrequestid.substring(1);
                        String backSql = "select * from "+KqSplitFlowTypeEnum.LEAVEBACK.getTablename()+" where "+Util.getSubINClause(leavebackrequestid, "requestid", "in")+" and fromdate= '"+fromdate+"'";
                        rs1.executeQuery(backSql);

                        while (rs1.next()){
                            String back_fromtime = rs1.getString("fromtime");
                            String back_totime = rs1.getString("totime");
                            if(back_fromtime.equalsIgnoreCase(fromtime)){
                                if ((kqTimesArrayComInfo.getArrayindexByTimes(back_totime))<(kqTimesArrayComInfo.getArrayindexByTimes(back_fromtime))){
                                    Arrays.fill(initArrays, kqTimesArrayComInfo.getArrayindexByTimes(back_fromtime), kqTimesArrayComInfo.getArrayindexByTimes(back_totime)+1440, -1);

                                }else {
                                    Arrays.fill(initArrays, kqTimesArrayComInfo.getArrayindexByTimes(back_fromtime), kqTimesArrayComInfo.getArrayindexByTimes(back_totime), -1);

                                }
                            }else{
                                if(back_fromtime.compareTo(back_totime) < 0){
                                    Arrays.fill(initArrays, kqTimesArrayComInfo.getArrayindexByTimes(back_fromtime)+1, kqTimesArrayComInfo.getArrayindexByTimes(back_totime), -1);
                                }
                            }
                        }
                        List<List<String>> backLists = new ArrayList<>();
                        List<String> backList = new ArrayList<>();
                        for(int i = fromTimeIndex ; i <= toTimeIndex ; i ++){
                            if(initArrays[i] == 1){
                                backList.add(kqTimesArrayComInfo.getTimesByArrayindex(i));
                            }else{
                                if(!backList.isEmpty()){
                                    backLists.add(backList);
                                    backList = new ArrayList<>();
                                }else{
                                    continue;
                                }
                            }
                        }
                        if(!backList.isEmpty()){
                            backLists.add(backList);
                        }
                        if(backLists != null && !backLists.isEmpty()){
                            List<Map<String,String>> time_list = new ArrayList<>();
                            for(int j = 0 ; j < backLists.size() ;j++){
                                List<String> backListTmp = backLists.get(j);
                                String back_tmp_fromtime = backListTmp.get(0);
                                String back_tmp_totime = backListTmp.get(backListTmp.size()-1);
                                infoMap = new HashMap<>();
                                infoMap.put("requestId", requestId);
                                infoMap.put(newleavetype, duration);
                                infoMap.put("begintime", back_tmp_fromtime);
                                infoMap.put("endtime", back_tmp_totime);
                                infoMap.put("unitType", unitType);
                                infoMap.put("durationrule", durationrule);
                                if("2".equals(durationrule)){
                                    infoMap.put("ishalf", "true");
                                }
                                infoMap.put("flowtype", FlowReportTypeEnum.LEAVE.getFlowType());
                                infoMap.put("newleavetype", newleavetype);
                                time_list.add(infoMap);
                            }
                            flowMaps.put(key, time_list);
                        }
                    }else{
                        List<Map<String,String>> time_list = new ArrayList<>();
                        time_list.add(infoMap);
                        flowMaps.put(key, time_list);
                    }
                }
            }

        }
        return splitBeans;
    }

    /**
     * 获取请假相关的数据
     */
    public List<SplitBean> getLeaveData(Map<String, Object> flowMaps,String kqdate){

        RecordSet rs = new RecordSet();
        RecordSet rs1 = new RecordSet();

        String tablename = "select a.* from kq_flow_split_temp_leave  a left join workflow_requestbase b on a.requestid = b.requestid where  b.requestid > 0 ";


        String sqlWhere = "";
        if(kqdate.length() > 0 ){
            sqlWhere += " and belongdate = '"+kqdate+"'";
        }

        if(sqlWhere.length() > 0){
            tablename += sqlWhere;
        }
        String leaveSql = "select * from ("+tablename+") t where 1=1 and (status is null or status <> '1') ";

        if(sqlWhere.length() > 0){
            leaveSql += sqlWhere;
        }
        List<SplitBean> splitBeans = new ArrayList<>();
        KQTimesArrayComInfo kqTimesArrayComInfo = new KQTimesArrayComInfo();

        int[] initArrays = kqTimesArrayComInfo.getInitArr();

        rs.execute(leaveSql);
        while(rs.next()){
            SplitBean splitBean = new SplitBean();
            String requestId= rs.getString("requestId");
            String resourceid= rs.getString("resourceid");
            String fromdate= rs.getString("fromdate");
            String belongdate= rs.getString("belongdate");
            String fromtime= rs.getString("fromtime");
            String todate= rs.getString("todate");
            String totime= rs.getString("totime");
            String newleavetype= rs.getString("newleavetype");
            String duration= rs.getString("duration");
            String durationrule= rs.getString("durationrule");
            String leavebackrequestid= Util.null2String(rs.getString("leavebackrequestid"));

            String fromtimedb= rs.getString("fromtimedb");
            String totimedb= rs.getString("totimedb");

            if(Util.getDoubleValue(duration) <= 0){
                //continue;
            }
            //计算规则 1-按天请假 2-按半天请假 3-按小时请假 4-按整天请假
            String unitType = "4".equalsIgnoreCase(durationrule)?"1":"2";
            String card_key = requestId+"_"+resourceid+"_"+KqSplitFlowTypeEnum.LEAVE.getFlowtype();
            String serial_signtype = "";
            String serial = "";
            String signtype = "";


            Map<String,String> infoMap = new HashMap<>();
            infoMap.put("requestId", requestId);
            infoMap.put(newleavetype, duration);
            infoMap.put("begintime", fromtime);
            infoMap.put("endtime", totime);
            infoMap.put("unitType", unitType);
            infoMap.put("durationrule", durationrule);
            if("2".equals(durationrule) && Util.getDoubleValue(duration)<1){
                infoMap.put("ishalf", "true");
            }else{
                infoMap.put("ishalf", "false");
            }
            infoMap.put("flowtype", FlowReportTypeEnum.LEAVE.getFlowType());
            infoMap.put("newleavetype", newleavetype);
            infoMap.put("signtype", signtype);
            infoMap.put("serial", serial);
            infoMap.put("fromtimedb", fromtimedb);
            infoMap.put("totimedb", totimedb);

            String key = resourceid+"|"+belongdate;
            if(flowMaps != null){
                if(flowMaps.get(key) != null){
                    if(leavebackrequestid.length() > 0 && leavebackrequestid.startsWith(",")){
                        initArrays = kqTimesArrayComInfo.getInitArr();
                        int fromTimeIndex = kqTimesArrayComInfo.getArrayindexByTimes(fromtime);
                        int toTimeIndex = kqTimesArrayComInfo.getArrayindexByTimes(totime);
                        Arrays.fill(initArrays, fromTimeIndex, toTimeIndex+1, 1);
                        leavebackrequestid = leavebackrequestid.substring(1);
                        String backSql = "select * from "+KqSplitFlowTypeEnum.LEAVEBACK.getTablename()+" where "+Util.getSubINClause(leavebackrequestid, "requestid", "in")+" and fromdate= '"+fromdate+"'";
                        rs1.executeQuery(backSql);

                        while (rs1.next()){
                            String back_fromtime = rs1.getString("fromtime");
                            String back_totime = rs1.getString("totime");
                            String back_duration= rs.getString("duration");
                            if(Util.getDoubleValue(back_duration) <= 0){
//                continue;
                            }
                            if(back_fromtime.equalsIgnoreCase(fromtime)){
                                Arrays.fill(initArrays, kqTimesArrayComInfo.getArrayindexByTimes(back_fromtime), kqTimesArrayComInfo.getArrayindexByTimes(back_totime), -1);
                            }else{
                                Arrays.fill(initArrays, kqTimesArrayComInfo.getArrayindexByTimes(back_fromtime)+1, kqTimesArrayComInfo.getArrayindexByTimes(back_totime), -1);
                            }
                        }
                        List<List<String>> backLists = new ArrayList<>();
                        List<String> backList = new ArrayList<>();
                        for(int i = fromTimeIndex ; i <= toTimeIndex ; i ++){
                            if(initArrays[i] == 1){
                                backList.add(kqTimesArrayComInfo.getTimesByArrayindex(i));
                            }else{
                                if(!backList.isEmpty()){
                                    backLists.add(backList);
                                    backList = new ArrayList<>();
                                }else{
                                    continue;
                                }
                            }
                        }
                        if(!backList.isEmpty()){
                            backLists.add(backList);
                        }
                        if(backLists != null && !backLists.isEmpty()){
                            List<Map<String,String>> time_list_tmp = (List<Map<String,String>>)flowMaps.get(key);
                            for(int j = 0 ; j < backLists.size() ;j++){
                                List<String> backListTmp = backLists.get(j);
                                String back_tmp_fromtime = backListTmp.get(0);
                                String back_tmp_totime = backListTmp.get(backListTmp.size()-1);
                                infoMap = new HashMap<>();
                                infoMap.put("requestId", requestId);
                                infoMap.put(newleavetype, duration);
                                infoMap.put("begintime", back_tmp_fromtime);
                                infoMap.put("endtime", back_tmp_totime);
                                infoMap.put("unitType", unitType);
                                infoMap.put("durationrule", durationrule);
                                if("2".equals(durationrule)){
                                    infoMap.put("ishalf", "true");
                                }
                                infoMap.put("flowtype", FlowReportTypeEnum.LEAVE.getFlowType());
                                infoMap.put("newleavetype", newleavetype);
                                time_list_tmp.add(infoMap);
                            }
                        }
                    }else{
                        List<Map<String,String>> time_list_tmp = (List<Map<String,String>>)flowMaps.get(key);
                        time_list_tmp.add(infoMap);
                    }
                }else{
                    if(leavebackrequestid.length() > 0 && leavebackrequestid.startsWith(",")){
                        initArrays = kqTimesArrayComInfo.getInitArr();
                        int fromTimeIndex = kqTimesArrayComInfo.getArrayindexByTimes(fromtime);
                        int toTimeIndex = kqTimesArrayComInfo.getArrayindexByTimes(totime);
                        Arrays.fill(initArrays, fromTimeIndex, toTimeIndex+1, 1);
                        leavebackrequestid = leavebackrequestid.substring(1);
                        String backSql = "select * from "+KqSplitFlowTypeEnum.LEAVEBACK.getTablename()+" where "+Util.getSubINClause(leavebackrequestid, "requestid", "in")+" and fromdate= '"+fromdate+"'";
                        rs1.executeQuery(backSql);

                        while (rs1.next()){
                            String back_fromtime = rs1.getString("fromtime");
                            String back_totime = rs1.getString("totime");
                            if(back_fromtime.equalsIgnoreCase(fromtime)){
                                Arrays.fill(initArrays, kqTimesArrayComInfo.getArrayindexByTimes(back_fromtime), kqTimesArrayComInfo.getArrayindexByTimes(back_totime), -1);
                            }else{
                                if(back_fromtime.compareTo(back_totime) < 0){
                                    Arrays.fill(initArrays, kqTimesArrayComInfo.getArrayindexByTimes(back_fromtime)+1, kqTimesArrayComInfo.getArrayindexByTimes(back_totime), -1);
                                }
                            }
                        }
                        List<List<String>> backLists = new ArrayList<>();
                        List<String> backList = new ArrayList<>();
                        for(int i = fromTimeIndex ; i <= toTimeIndex ; i ++){
                            if(initArrays[i] == 1){
                                backList.add(kqTimesArrayComInfo.getTimesByArrayindex(i));
                            }else{
                                if(!backList.isEmpty()){
                                    backLists.add(backList);
                                    backList = new ArrayList<>();
                                }else{
                                    continue;
                                }
                            }
                        }
                        if(!backList.isEmpty()){
                            backLists.add(backList);
                        }
                        if(backLists != null && !backLists.isEmpty()){
                            List<Map<String,String>> time_list = new ArrayList<>();
                            for(int j = 0 ; j < backLists.size() ;j++){
                                List<String> backListTmp = backLists.get(j);
                                String back_tmp_fromtime = backListTmp.get(0);
                                String back_tmp_totime = backListTmp.get(backListTmp.size()-1);
                                infoMap = new HashMap<>();
                                infoMap.put("requestId", requestId);
                                infoMap.put(newleavetype, duration);
                                infoMap.put("begintime", back_tmp_fromtime);
                                infoMap.put("endtime", back_tmp_totime);
                                infoMap.put("unitType", unitType);
                                infoMap.put("durationrule", durationrule);
                                if("2".equals(durationrule)){
                                    infoMap.put("ishalf", "true");
                                }
                                infoMap.put("flowtype", FlowReportTypeEnum.LEAVE.getFlowType());
                                infoMap.put("newleavetype", newleavetype);
                                time_list.add(infoMap);
                            }
                            flowMaps.put(key, time_list);
                        }
                    }else{
                        List<Map<String,String>> time_list = new ArrayList<>();
                        time_list.add(infoMap);
                        flowMaps.put(key, time_list);
                    }
                }
            }
        }
        return splitBeans;
    }
    /**
     * 获取请假相关的数据
     */
    public void getLeaveDataNew(Map<String, Object> flowMaps,String fromDate,String toDate){
        RecordSet rs = new RecordSet();
        Map<String, String> infoMap = new HashMap<>();

        String backfields = "  * ";
        String fromSql  = "";
        String sqlWhere = "";
        String leaveTableName = new BaseBean().getPropValue("qc3560103","leaveTableName");
        String leaveWfid = new BaseBean().getPropValue("qc3560103","leaveWfid");

        //重新写一下吧
                fromSql = " from (select distinct " +
                "u.*," +
                "u.requestid as req_requestid," +
                "a.managerid," +
                "a.managerstr," +
                "a.loginid," +
                "a.subcompanyid1 as asubcompanyid1," +
                "a.departmentid  as adepartmentid," +
                "a.lastname " +
                "from (select a.qjrxm   as resourceId," +
//                "a.departmentId as departmentId," +
                "a.newLeaveType as newLeaveType," +
                "a.fromDate as fromDate," +
                "a.fromTime as fromTime," +
                "a.toDate as toDate," +
                "a.toTime as toTime," +
                "a.duration as duration," +
                "a.requestid " +
                "from "+leaveTableName+" a) u " +
                "left join (select * from hrmresource a where 1 = 1) a on u.resourceid = a.id) f";
        sqlWhere = " where 1 = 1" +
                "  and req_requestid > 0" +
                "  and (fromDate between '"+fromDate+"' and '"+toDate+"' or toDate between '"+fromDate+"' and '"+toDate+"' or" +
                "       '"+fromDate+"' between fromDate and toDate or '"+toDate+"' between fromDate and toDate)";
        String allSql = "select "+ backfields + fromSql+sqlWhere;
        kqLog.info("==getLeaveDataNew=="+allSql);
        rs.executeQuery(allSql);
        while(rs.next()){
            String fromDate_field = "fromDate";
            String fromTime_field = "fromTime";
            String toDate_field = "toDate";
            String toTime_field = "toTime";
            String rosource_field = "resourceid";


            //==zj 换成自定义字段
            String related_requestid = rs.getString("REQUESTSYNCID");
            String flow_fromDate = rs.getString(fromDate_field);
            String flow_toDate = rs.getString(toDate_field);
            String resourceid = rs.getString(rosource_field);
            infoMap = new HashMap<>();
            infoMap.put("flow_fromDate", flow_fromDate);
            infoMap.put("flow_toDate", flow_toDate);
            infoMap.put("requestid", related_requestid);
            infoMap.put("flowtype","leave");
            if (flowMaps.get(resourceid) == null){
                List<Map<String,String>> time_list = new ArrayList<>();
                time_list.add(infoMap);
                flowMaps.put(resourceid,time_list);
            }else {
                List<Map<String,String>>  time_list = ( List<Map<String,String>>) flowMaps.get(resourceid);
                time_list.add(infoMap);
                flowMaps.put(resourceid,time_list);
            }
        }
    }

    /**
     * 获取销假相关的数据
     */
    public void getLeaveBackDataNew(Map<String, Object> flowMaps,String fromDate,String toDate){
        RecordSet rs = new RecordSet();
        Map<String, String> infoMap = new HashMap<>();

        String backfields = "  * ";
        String fromSql  = "";
        String sqlWhere = "";
        String leaveTableName = new BaseBean().getPropValue("qc3560103","leaveBackTableName");
        String leaveWfid = new BaseBean().getPropValue("qc3560103","leaveBackWfid");

        //重新写一下吧
        fromSql = " from (select distinct " +
                "u.*," +
                "u.requestid as req_requestid," +
                "a.managerid," +
                "a.managerstr," +
                "a.loginid," +
                "a.subcompanyid1 as asubcompanyid1," +
                "a.departmentid  as adepartmentid," +
                "a.lastname " +
                "from (select a.resourceId   as resourceId," +
//                "a.departmentId as departmentId," +
                "b.detail_fromDate  as fromDate," +
                "b.detail_fromTime as fromTime," +
                "b.detail_toDate as toDate," +
                "b.detail_toTime as toTime," +
                "b.detail_duration as duration," +
                "a.requestid," +
                "a.leaverequestid as leavebackrequestid " +
                "from "+leaveTableName+" a left join "+leaveTableName+"_dt1 b on a.id = b.mainid) u " +
                "left join (select * from hrmresource a where 1 = 1) a on u.resourceid = a.id) f";
        sqlWhere = " where 1 = 1" +
                "  and req_requestid > 0" +
                "  and (fromDate between '"+fromDate+"' and '"+toDate+"' or toDate between '"+fromDate+"' and '"+toDate+"' or" +
                "       '"+fromDate+"' between fromDate and toDate or '"+toDate+"' between fromDate and toDate)" ;
        String allSql = "select "+ backfields + fromSql+sqlWhere;
        kqLog.info("==getLeaveBackDataNew=="+allSql);
        rs.executeQuery(allSql);
        while(rs.next()){
            String fromDate_field = "fromDate";
            String fromTime_field = "fromTime";
            String toDate_field = "toDate";
            String toTime_field = "toTime";
            String rosource_field = "resourceid";
            String leavebackrequestid_field = "leavebackrequestid";


            //==zj 换成自定义字段
            String related_requestid = rs.getString("REQUESTSYNCID");
            String flow_fromDate = rs.getString(fromDate_field);
            String flow_fromTime = rs.getString(fromTime_field);
            String flow_toDate = rs.getString(toDate_field);
            String flow_toTime = rs.getString(toTime_field);
            String resourceid = rs.getString(rosource_field);
            String leavebackrequestid = rs.getString(leavebackrequestid_field);
            infoMap = new HashMap<>();
            infoMap.put("flow_fromDate", flow_fromDate);
            infoMap.put("flow_toDate", flow_toDate);
            //加下销假流程
            infoMap.put("leavebackrequestid", leavebackrequestid);

            if (flowMaps.get(leavebackrequestid) == null){
                List<Map<String,String>> time_list = new ArrayList<>();
                time_list.add(infoMap);
                flowMaps.put(leavebackrequestid,time_list);
            }else {
                List<Map<String,String>>  time_list = ( List<Map<String,String>>) flowMaps.get(leavebackrequestid);
                time_list.add(infoMap);
                flowMaps.put(leavebackrequestid,time_list);
            }
        }
    }

    /**
     * 判断当天是否有请假
     * @param workFlowInfo
     * @param resourceid
     * @return
     */
    public boolean isLeaveNew(Map<String, Object> workFlowInfo,String resourceid,String kqdate,Map<String, Object> workFlowInfoBackNew){
        List<Object> workFlow =Optional.ofNullable( (List<Object>) workFlowInfo.get(resourceid)).orElse(Collections.emptyList());
        boolean ishave = false;
        for (int i = 0; i < workFlow.size(); i++) {
            Map<String, Object> workFlowDetail = (Map<String, Object>) workFlow.get(i);
            String fromdatedb = Util.null2String(workFlowDetail.get("fromdatedb"));
            String todatedb = Util.null2String(workFlowDetail.get("todatedb"));
            String requestid = Util.null2String(workFlowDetail.get("requestid"));
            String flowtype = Util.null2String(workFlowDetail.get("flowtype"));
            if (kqdate.compareTo(fromdatedb) >= 0 && kqdate.compareTo(todatedb) <=0){
                ishave = true;
                if (ishave){
                    //再看看是否有销假流程
                    if (workFlowInfoBackNew != null){
                        List<Object> workFlowBack = new ArrayList<>();
                        String key = "";
                        if ("leave".equals(flowtype)){
                            key = resourceid;
                        }else if ("out".equals(flowtype) || "evection".equals(flowtype)){
                            key = resourceid+"|"+requestid;
                        }
                         workFlowBack = Optional.ofNullable((List<Object>) workFlowInfoBackNew.get(key))
                                .orElse(Collections.emptyList());
                        for (int j = 0; j < workFlowBack.size(); j++) {
                            Map<String, Object> workFlowBackDetail = (Map<String, Object>) workFlowBack.get(j);
                            String fromdateBackdb = Util.null2String(workFlowBackDetail.get("fromdatedb"));
                            String todateBackdb = Util.null2String(workFlowBackDetail.get("todatedb"));
                            if ( kqdate.compareTo(fromdateBackdb) >= 0 && kqdate.compareTo(todateBackdb) <=0){
                                ishave = false;
                            }

                        }
                    }
                }
            }
        }
        return ishave;
    }

    /**
     * 出差校验
     * @param workFlowInfo
     * @param resourceid
     * @return
     */
    public boolean isEcectionNew(Map<String, Object> workFlowInfo,String resourceid,String kqdate,Map<String, Object> workFlowInfoChangeNew){

        List<Object> workFlow =Optional.ofNullable( (List<Object>) workFlowInfo.get(resourceid)).orElse(Collections.emptyList());
        boolean ishave = false;
        for (int i = 0; i < workFlow.size(); i++) {
            Map<String, Object> workFlowDetail = (Map<String, Object>) workFlow.get(i);
            String fromdatedb = Util.null2String(workFlowDetail.get("fromdatedb"));
            String todatedb = Util.null2String(workFlowDetail.get("todatedb"));
            String requestid = Util.null2String(workFlowDetail.get("requestid"));
            String flowtype = Util.null2String(workFlowDetail.get("flowtype"));
            if (kqdate.compareTo(fromdatedb) >= 0 && kqdate.compareTo(todatedb) <=0){
                ishave = true;
                    if (workFlowInfoChangeNew != null){
                        //这里兼容下考勤变更流程
                        List<Object> workFlowBack = new ArrayList<>();
                        String key = "";
                        if ("leave".equals(flowtype)){
                            key = resourceid;
                        }else if ("out".equals(flowtype) || "evection".equals(flowtype)){
                            key = resourceid+"|"+requestid;
                        }
                        workFlowBack = Optional.ofNullable((List<Object>) workFlowInfoChangeNew.get(key))
                                .orElse(Collections.emptyList());
                        for (int j = 0; j < workFlowBack.size(); j++) {
                            ishave = false;
                            //如果是出差或者外出流程的话，说明流程被更改，状态需要重置
                            if ("out".equals(flowtype) || "evection".equals(flowtype)){
                                ishave = false;
                            }
                            Map<String, Object> workFlowBackDetail = (Map<String, Object>) workFlowBack.get(j);
                            String fromdateBackdb = Util.null2String(workFlowBackDetail.get("fromdatedb"));
                            String todateBackdb = Util.null2String(workFlowBackDetail.get("todatedb"));
                            if (("out".equals(flowtype) || "evection".equals(flowtype)) && kqdate.compareTo(fromdateBackdb) >= 0 && kqdate.compareTo(todateBackdb) <=0){
                                ishave = true;
                            }
                        }
                    }

            }
        }
        return ishave;
    }

    /**
     * 判断当天是否有考勤流程
     * @param workFlowInfo
     * @param resourceid
     * @return
     */
    public boolean isKqFlowNew(Map<String, Object> workFlowInfo,String resourceid,String kqdate){

        List<Object> workFlow =Optional.ofNullable( (List<Object>) workFlowInfo.get(resourceid)).orElse(Collections.emptyList());
        boolean ishave = false;
        for (int i = 0; i < workFlow.size(); i++) {
            Map<String, Object> workFlowDetail = (Map<String, Object>) workFlow.get(i);
            String fromdatedb = Util.null2String(workFlowDetail.get("fromdatedb"));
            String todatedb = Util.null2String(workFlowDetail.get("todatedb"));
            String flowtype = Util.null2String(workFlowDetail.get("flowtype"));
            if (kqdate.compareTo(fromdatedb) >= 0 && kqdate.compareTo(todatedb) <=0){
                ishave = true;
                break;
            }
        }

        return ishave;
    }

    /**
     * 获取月份第一天和最后一天
     * @param kqMonth
     * @param isFirst
     * @return
     */
    public String getMonthDay(String kqMonth,boolean isFirst){
        String monthDay = "";

        try {
            DateTimeFormatter dateTimeFormatter =  DateTimeFormatter.ofPattern("yyyy-MM-dd");
            if (isFirst){
                YearMonth yearMonth = YearMonth.parse(kqMonth);
                monthDay =  yearMonth.atDay(1).format(dateTimeFormatter);
            }else {
                YearMonth yearMonth = YearMonth.parse(kqMonth);
                monthDay =  yearMonth.atEndOfMonth().format(dateTimeFormatter); // 返回当月的最后一天
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
return monthDay;
    }

    /**
     * 判断当天是否有请假
     * @param workFlowInfo
     * @param resourceid
     * @param date
     * @return
     */
    public boolean isLeave(Map<String, Object> workFlowInfo,String resourceid,String date,KQTimesArrayComInfo kqTimesArrayComInfo){
        List<Object> workFlow = (List<Object>) workFlowInfo.get(resourceid+"|"+date);
        boolean ishave = false;
        for (int k = 0; k < workFlow.size(); k++) {
            Map<String, Object> workFlowDetail = (Map<String, Object>) workFlow.get(k);
            String begintime = Util.null2String(workFlowDetail.get("begintime"));
            String endtime = Util.null2String(workFlowDetail.get("endtime"));
            String flowType = Util.null2String(workFlowDetail.get("flowtype"));

            int beginIdx = kqTimesArrayComInfo.getArrayindexByTimes(begintime);
            int endIdx = kqTimesArrayComInfo.getArrayindexByTimes(endtime);
            if (beginIdx >= endIdx) {
                continue;
            }else {
                ishave = true;
                break;
            }
        }
        return ishave;
    }

    public String isLeave(Map<String, Object> workFlowInfo,String resourceid,String date,KQTimesArrayComInfo kqTimesArrayComInfo,String lastname,String warnMessage){
        List<Object> workFlow = (List<Object>) workFlowInfo.get(resourceid+"|"+date);
        boolean isLeave = false;
        boolean isEvection = false;
        boolean isOut= false;
        StringBuilder warnMessageBuilder = new StringBuilder(warnMessage);
        for (int k = 0; k < workFlow.size(); k++) {
            Map<String, Object> workFlowDetail = (Map<String, Object>) workFlow.get(k);
            String begintime = Util.null2String(workFlowDetail.get("begintime"));
            String endtime = Util.null2String(workFlowDetail.get("endtime"));
            String flowType = Util.null2String(workFlowDetail.get("flowtype"));

            int beginIdx = kqTimesArrayComInfo.getArrayindexByTimes(begintime);
            int endIdx = kqTimesArrayComInfo.getArrayindexByTimes(endtime);
            if (beginIdx >= endIdx) {
                continue;
            }else {
//               if (flowType.equals(FlowReportTypeEnum.EVECTION.getFlowType()) && !isEvection){
//                   isEvection = true;
//                   warnMessageBuilder.append(lastname).append(",").append(date).append(",已有出差。").append("1".equals(isline) ? "\n" : "");
//               }else
               if (flowType.equals(FlowReportTypeEnum.LEAVE.getFlowType())&& !isLeave){
                   isLeave = true;
                   warnMessageBuilder.append(lastname).append(",").append(date).append(",已有请假。").append("1".equals(isline) ? "\n" : "");

               }
//               else if (flowType.equals(FlowReportTypeEnum.OUT.getFlowType())&& !isOut){
//                   isOut = true;
//                   warnMessageBuilder.append(lastname).append(",").append(date).append(",已有外出。").append("1".equals(isline) ? "\n" : "");
//
//               }
            }
        }
        kqLog.info("==warnMessage=="+warnMessage);
        warnMessage = warnMessageBuilder.toString();
        return warnMessage;
    }



    /**
     * 判断当天是否有请假
     * @param workFlowInfo
     * @param key
     * @return
     */
    public boolean isLeave(Map<String, Object> workFlowInfo,String key,KQTimesArrayComInfo kqTimesArrayComInfo){
        List<Object> workFlow = (List<Object>) workFlowInfo.get(key);
        boolean ishave = false;
        for (int k = 0; k < workFlow.size(); k++) {
            Map<String, Object> workFlowDetail = (Map<String, Object>) workFlow.get(k);
            String begintime = Util.null2String(workFlowDetail.get("begintime"));
            String endtime = Util.null2String(workFlowDetail.get("endtime"));
            int beginIdx = kqTimesArrayComInfo.getArrayindexByTimes(begintime);
            int endIdx = kqTimesArrayComInfo.getArrayindexByTimes(endtime);
            if (beginIdx >= endIdx) {
                continue;
            }else {
                ishave = true;
                break;
            }
        }
        return ishave;
    }

}
