package com.api.customization.kq.util;

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 weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class KqCustomFlowData {
    private KQLog kqLog = new KQLog();

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

        RecordSet rs = new RecordSet();
        String tablename = "select a.* from kq_flow_split_temp_evection 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') ";



        List<SplitBean> splitBeans = new ArrayList<>();
        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");

            splitBean.setRequestId(requestId);
            splitBean.setResourceId(resourceid);
            splitBean.setFromDate(fromdate);
            splitBean.setFromTime(fromtime);
            splitBean.setToDate(todate);
            splitBean.setToTime(totime);
            splitBean.setNewLeaveType(newleavetype);
            splitBean.setDuration(duration);
            splitBean.setDurationrule(durationrule);
            splitBeans.add(splitBean);

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


            Map<String,String> infoMap = new HashMap<>();
            infoMap.put(FlowReportTypeEnum.businessLeave.getFlowType(), duration);
            infoMap.put("requestId", requestId);
            infoMap.put("begintime", fromtime);
            infoMap.put("endtime", totime);
            infoMap.put("unitType", unitType);
            infoMap.put("duration", duration);
            infoMap.put("flowtype", FlowReportTypeEnum.EVECTION.getFlowType());
            infoMap.put("signtype", signtype);
            infoMap.put("serial", serial);

            String key = resourceid+"|"+belongdate;
            if(flowMaps != null){
                if(flowMaps.get(key) != null){
                    List<Map<String,String>> time_list_tmp = (List<Map<String,String>>)flowMaps.get(key);
                    time_list_tmp.add(infoMap);
                }else{
                    List<Map<String,String>> time_list = new ArrayList<>();
                    time_list.add(infoMap);
                    flowMaps.put(key, time_list);
                }
            }

        }
        return splitBeans;
    }

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

        RecordSet rs = new RecordSet();
        String tablename = "select a.* from kq_flow_split_temp_out  a left join workflow_requestbase b on a.requestid = b.requestid where  b.requestid > 0 ";
        String leaveSql = "select * from ("+tablename+") t where 1=1 and (status is null or status <> '1') ";
        String sqlWhere = "";
        if(fromDate.length() > 0 && toDate.length() > 0){
            sqlWhere += " and belongdate between '"+fromDate+"' and '"+toDate+"' ";
        }

        if(sqlWhere.length() > 0){
            tablename += sqlWhere;
        }
        List<SplitBean> splitBeans = new ArrayList<>();
        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");

            splitBean.setRequestId(requestId);
            splitBean.setResourceId(resourceid);
            splitBean.setFromDate(fromdate);
            splitBean.setFromTime(fromtime);
            splitBean.setToDate(todate);
            splitBean.setToTime(totime);
            splitBean.setNewLeaveType(newleavetype);
            splitBean.setDuration(duration);
            splitBean.setDurationrule(durationrule);
            splitBeans.add(splitBean);

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


            Map<String,String> infoMap = new HashMap<>();
            infoMap.put(FlowReportTypeEnum.officialBusiness.getFlowType(), duration);
            infoMap.put("requestId", requestId);
            infoMap.put("begintime", fromtime);
            infoMap.put("endtime", totime);
            infoMap.put("unitType", unitType);
            infoMap.put("duration", duration);
            infoMap.put("flowtype", FlowReportTypeEnum.OUT.getFlowType());
            infoMap.put("signtype", signtype);
            infoMap.put("serial", serial);

            String key = resourceid+"|"+belongdate;
            if(flowMaps != null){
                if(flowMaps.get(key) != null){
                    List<Map<String,String>> time_list_tmp = (List<Map<String,String>>)flowMaps.get(key);
                    time_list_tmp.add(infoMap);
                }else{
                    List<Map<String,String>> time_list = new ArrayList<>();
                    time_list.add(infoMap);
                    flowMaps.put(key, time_list);
                }
            }
        }
        return splitBeans;
    }

    /**
     * 这里获取下考勤变更流程
     * @param flowMaps
     * @param fromDate
     * @param toDate
     */
    public void getChangeData(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 changeTableName = new BaseBean().getPropValue("qc3560103","changeTableName");

        //重新写一下吧
        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_toDate as toDate," +
                "b.detail_duration as duration," +
                "a.requestid," +
                "a.changerequestid as changerequestid " +
                "from "+changeTableName+" a left join "+changeTableName+"_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("==getChangeDataNew=="+allSql);
        rs.executeQuery(allSql);
        while(rs.next()){
            String fromDate_field = "fromDate";
            String toDate_field = "toDate";
            String rosource_field = "resourceid";
            String changerequestid_field = "changerequestid";


            //==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);
            String changerequestid = rs.getString(changerequestid_field);
            infoMap = new HashMap<>();
            infoMap.put("fromdatedb", flow_fromDate);
            infoMap.put("todatedb", flow_toDate);
            infoMap.put("requestid",related_requestid);

            String key = resourceid+"|"+changerequestid;//记录的是改变哪个流程对应人员的考勤流程

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

    /**
     * 获取班次数据(获取原始表信息)
     * @param flowMaps
     * @param fromDate
     * @param toDate
     */
    public void getShiftDataNew(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 shiftTableName = new BaseBean().getPropValue("qc3560103","shiftTableName");

        //重新写一下吧
        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 b.detail_resourceId   as resourceId," +
//                "a.departmentId as departmentId," +
                "b.detail_fromDate  as fromDate," +
                "b.detail_toDate as toDate," +
                "a.requestid " +
                "from "+shiftTableName+" a left join "+shiftTableName+"_dt1 b on a.id = b.mainid) u " +
                "left join (select * from hrmresource a where 1 = 1) a on u.resourceid = a.id) f "+
                "inner join workflow_requestbase r on r.requestid = u.requestid ";
        sqlWhere = " where r.currentnodetype in(1,2,3) " +
                "  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("==getShiftDataNew=="+allSql);
        new BaseBean().writeLog("==getShiftDataNew=="+allSql);
        rs.executeQuery(allSql);
        while(rs.next()){
            String fromDate_field = "fromDate";
            String toDate_field = "toDate";
            String rosource_field = "resourceid";

            //==zj 换成自定义字段
            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("fromdatedb", flow_fromDate);
            infoMap.put("todatedb", flow_toDate);
            infoMap.put("flowtype","shift");
            //加下销假流程

            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);
            }
        }
    }

    /**
     * 获取出差流程数据(原始信息)
     * @param flowMaps
     * @param fromDate
     * @param toDate
     */
    public void getEvectionDataNew(Map<String, Object> flowMaps,String fromDate,String toDate){
        RecordSet rs = new RecordSet();
        Map<String, String> infoMap = new HashMap<>();
        String evectionTableName = new BaseBean().getPropValue("qc3560103","evectionTableName");

        String tablename = "select a.* from kq_flow_split_temp_evection  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 distinct resourceid,requestid,fromDatedb,toDatedb from ("+tablename+") t where 1=1 and (status is null or status <> '1') ";


        rs.executeQuery(leaveSql);
        while(rs.next()){
            //==zj 换成自定义字段
            String requestid = rs.getString("requestid");
            String fromDatedb = rs.getString("fromDatedb");
            String toDatedb = rs.getString("toDatedb");
            String resourceid = rs.getString("resourceid");
            infoMap = new HashMap<>();
            infoMap.put("fromdatedb", fromDatedb);
            infoMap.put("todatedb", toDatedb);
            infoMap.put("requestid", requestid);
            infoMap.put("flowtype","evection");
            String key = resourceid;

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

    /**
     * 获取外出流程数据(原始表信息)
     * @param flowMaps
     * @param fromDate
     * @param toDate
     */
    public void getOutDataNew(Map<String, Object> flowMaps,String fromDate,String toDate){
        RecordSet rs = new RecordSet();
        Map<String, String> infoMap = new HashMap<>();
        String outTableName = new BaseBean().getPropValue("qc3560103","outTableName");

        String tablename = "select a.* from kq_flow_split_temp_out  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 distinct resourceid,requestid,fromDatedb,toDatedb from ("+tablename+") t where 1=1 and (status is null or status <> '1') ";


        rs.executeQuery(leaveSql);
        while(rs.next()){
            //==zj 换成自定义字段
            String requestid = rs.getString("requestid");
            String fromDatedb = rs.getString("fromDatedb");
            String toDatedb = rs.getString("toDatedb");
            String resourceid = rs.getString("resourceid");
            infoMap = new HashMap<>();
            infoMap.put("fromdatedb", fromDatedb);
            infoMap.put("todatedb", toDatedb);
            infoMap.put("requestid", requestid);
            infoMap.put("flowtype","evection");
            String key = resourceid;

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