


<%@ page import="weaver.hrm.User" %>
<%@ page import="weaver.hrm.HrmUserVarify" %>
<%@ page import="com.engine.common.util.ParamUtil" %>
<%@ page import="weaver.conn.RecordSet" %>
<%@ page import="weaver.general.BaseBean" %>
<%@ page import="com.alibaba.fastjson.JSONObject" %>
<%@ page import="java.time.format.DateTimeFormatter" %>
<%@ page import="java.time.LocalDateTime" %>
<%@ page import="java.time.format.DateTimeParseException" %>
<%@ page import="weaver.conn.RecordSetTrans" %>
<%@ page import="java.util.*" %>
<%@ page language="java" contentType="text/html; charset=UTF-8" %>


<%
    User user = HrmUserVarify.getUser(request, response);
    if (user == null){
        out.print("暂无权限");
    }
    Map<String, Object> param = ParamUtil.request2Map(request);
    String reqid = (String)param.get("requestid");
    String sqr = (String)param.get("sqr");
    String startDate = (String)param.get("startDate");
    String endDate = (String)param.get("endDate");
    //差旅行程变更审批单
    String sql = "select * from ";
    sql += getFormName(reqid);
    sql += " main left join workflow_requestBase req on main.REQUESTID = req.REQUESTID ";
    sql += "where sqr = ? AND ";
    sql += "wcsj <= ?  AND hgsj >= ? ";
    //在审批状态
    sql += "and req.currentnodetype != 0 ";
    sql += "and req.REQUESTID !=  ? ";
    RecordSet recordSet = new RecordSet();
    recordSet.executeQuery(sql,sqr,endDate,startDate,reqid);
    writeLog(sql,sqr,endDate,startDate,reqid);
    ArrayList<Map<String, String>> records = new ArrayList<>();
    ArrayList<Map<String, String>> specialobjs = new ArrayList<>();
    ArrayList<String> requestid = new ArrayList<>();
    ArrayList<String> requestname = new ArrayList<>();
    ArrayList<String> clxcdh = new ArrayList<>();
    int i = 0;
    while (recordSet.next()){
        HashMap<String, String> record = new HashMap<>();
        record.put("requestid",recordSet.getString("requestid"));
        record.put("requestname",recordSet.getString("requestname"));
        record.put("clxcdh",recordSet.getString("clxcdh"));
        requestid.add(recordSet.getString("requestid"));
        // requestname.add(recordSet.getString("requestname"));
        // clxcdh.add(recordSet.getString("clxcdh"));
        records.add(record);
        HashMap<String, String> specialobj = new HashMap<>();
        specialobj.put("id",recordSet.getString("requestid"));
        specialobj.put("name",recordSet.getString("qjbt"));
        specialobjs.add(specialobj);
        i++;
    }
    //查看变更单是否有对冲突时间流程的修改
    String querybgdsql = "select * from formtable_main_293 where id = " +
            "(select max(id) from formtable_main_293 where dcyslc = ? )";
    ArrayList<String> requestidNew =  new ArrayList<>();
    for (String rid : requestid) {
        recordSet.executeQuery(querybgdsql,rid);
        if(recordSet.next()){
            //获取修改变更单最新的一条
            String start = recordSet.getString("wcsj");
            String end = recordSet.getString("hgsj");
            if (isOverlapping(start,end,startDate,endDate)){
                requestidNew.add(rid);
            }else{
                // 如果变更后不冲突去掉这条数据
                removeElement(records,"requestid",rid);
                removeElement(specialobjs,"id",rid);
                i--;
            }
        }else{
            // 没有变更单的情况
            requestidNew.add(rid);
        }
    }
    requestid = requestidNew;

    //再查询下变更表里有没有变更后冲突的数据
    String queryBGBSql = "select * from formtable_main_293 main left join workflow_requestBase base  " +
            "on main.requestid = base.requestid " +
            "where dcyslc not in ( " +String.join(",",requestid)+" ) " +
            "AND wcsj <= ?  AND hgsj >= ? " +
            "AND sqr = ? ";
    recordSet.executeQuery(queryBGBSql,endDate,startDate,sqr);
    while (recordSet.next()){
        HashMap<String, String> record = new HashMap<>();
        record.put("requestid",recordSet.getString("requestid"));
        record.put("requestname",recordSet.getString("requestname"));
        // record.put("clxcdh",recordSet.getString("clxcdh"));
        requestid.add(recordSet.getString("requestid"));
        // requestname.add(recordSet.getString("requestname"));
        // clxcdh.add(recordSet.getString("clxcdh"));
        records.add(record);
        HashMap<String, String> specialobj = new HashMap<>();
        specialobj.put("id",recordSet.getString("requestid"));
        specialobj.put("name",recordSet.getString("qjbt"));
        specialobjs.add(specialobj);
        i++;
    }

    HashMap<String, Object> result = new HashMap<>();
    result.put("code",0);
    result.put("count",i);
    result.put("requestid",String.join(",",requestid));
    result.put("requestname",String.join(",",requestname));
    result.put("clxcdh",String.join(",",clxcdh));
    result.put("specialobjs",specialobjs);
    result.put("result",records);
    out.print(JSONObject.toJSONString(result));
%>
<%!
    private void writeLog(Object... log){
        new BaseBean().writeLog("queryDuplicatedataSL===>"+ Arrays.toString(log));;
    }

    /**
     * 根据request获取表单名称
     * @param requestid
     * @return
     */
    private String getFormName(String requestid){
        String QueryWfidsql = "select workflowid from  workflow_requestbase  where requestid = ?";
        String QueryFormsql = "select base.ID , bill.TABLENAME  from  workflow_base base " +
                        "left join workflow_bill bill on base.FORMID = bill.ID where base.id = ?";
        RecordSet recordSet = new RecordSet();
        recordSet.executeQuery(QueryWfidsql,requestid);
        recordSet.next();
        String workflowid = recordSet.getString("workflowid");
        recordSet.executeQuery(QueryFormsql,workflowid);
        recordSet.next();

        return recordSet.getString("TABLENAME");
    }


    public static boolean isOverlapping(String start1, String end1, String start2, String end2) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        try {
            LocalDateTime dateTimeStart1 = LocalDateTime.parse(start1, formatter);
            LocalDateTime dateTimeEnd1 = LocalDateTime.parse(end1, formatter);
            LocalDateTime dateTimeStart2 = LocalDateTime.parse(start2, formatter);
            LocalDateTime dateTimeEnd2 = LocalDateTime.parse(end2, formatter);

            return !dateTimeStart1.isAfter(dateTimeEnd2) && !dateTimeEnd1.isBefore(dateTimeStart2);
        } catch (DateTimeParseException e) {
            e.printStackTrace();
            return false;
        }

    }
    public static void removeElement(List <Map<String, String>> list ,String key,String value){
        // 条件：删除所有 age 值为 "30" 的 Map
        Iterator<Map<String, String>> iterator = list.iterator();
        while (iterator.hasNext()) {
            Map<String, String> map = iterator.next();
            if (map.getOrDefault(key, "").equals(value)) {
                iterator.remove();
            }
        }
    }



%>