<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ page import="com.engine.kq.biz.KQGroupMemberComInfo" %>
<%@ page import="com.engine.kq.entity.KQGroupEntity" %>
<%@ page import="cn.hutool.core.date.DateUtil" %>
<%@ page import="weaver.general.Util" %>
<%@ page import="weaver.workflow.workflow.WorkflowComInfo" %>
<%@ page import="com.engine.kq.biz.KQWorkTime" %>
<%@ page import="com.engine.kq.entity.WorkTimeEntity" %>
<%@ page import="com.alibaba.fastjson.JSONObject" %>
<%@ page import="com.engine.kq.entity.TimeScopeEntity" %>
<%@ page import="org.apache.commons.lang.StringUtils" %>
<%@ page import="weaver.workflow.webservices.*" %>
<%@ page import="java.time.format.DateTimeFormatter" %>
<%@ page import="java.time.LocalDate" %>
<%@ page import="weaver.general.BaseBean" %>
<jsp:useBean id="rs" class="weaver.conn.RecordSet" scope="page" />
<jsp:useBean id="bb" class="weaver.general.BaseBean" />
<%
    int sum = 0;

    String requestid = "-1";

    String resourceId = Util.null2String(request.getParameter("resourceId"));
    String startDate = Util.null2String(request.getParameter("startDate"));
    String endDate = Util.null2String(request.getParameter("endDate"));
    String startTime = Util.null2String(request.getParameter("startTime"));
    String endTime = Util.null2String(request.getParameter("endTime"));

    List<String> cardDates = new ArrayList<>();
    List<String> allDates = getAllDates(startDate, endDate);

    bb.writeLog("--allDates: " + allDates );

    for (String date : allDates) {
        bb.writeLog("--date: " + date );
        KQGroupMemberComInfo kqGroupMemberComInfo = new KQGroupMemberComInfo();
        //根据考勤组判断是否开启外勤打卡
        String outsidesign = "";  //人员所在考勤组是否开启外勤签到转考勤
        KQGroupEntity kqGroupEntity = kqGroupMemberComInfo.getUserKQGroupInfo(resourceId, date);
        if ( kqGroupEntity != null) {
            outsidesign = kqGroupEntity.getOutsidesign();
        }
        bb.writeLog("--outsidesign: " + outsidesign );
        if ( !"1".equals(outsidesign)) {
            cardDates.add(date);
        }
    }

    bb.writeLog("--cardDates: " + cardDates );

    String today = DateUtil.format(new Date(), "yyyy-MM-dd");
    //获取补卡流程workflowid
    String cardWorkflowId = Util.null2String(bb.getPropValue("syn_othersys_mfkj","cardWorkflowId"));
    bb.writeLog("--cardWorkflowId: " + cardWorkflowId );

    //获取申请人信息
    String lastName = "";
    String workCode = "";
    String departmentId = "";
    String jobTitle = "";
    String acqLastNameSql = "select lastname, workCode, departmentId, jobTitle from hrmresource where id = ? ";
    rs.executeQuery(acqLastNameSql, resourceId);
    while (rs.next()) {
        lastName = Util.null2String(rs.getString("lastname"));
        workCode = Util.null2String(rs.getString("workCode"));
        departmentId = Util.null2String(rs.getString("departmentId"));
        jobTitle = Util.null2String(rs.getString("jobTitle"));
    }

    WorkflowComInfo workflowComInfo = new WorkflowComInfo();
    KQWorkTime kqWorkTime = new KQWorkTime();
    //流程基本信息
    Map<String, String> workflowInfo = new HashMap<>();
    String workflowname = workflowComInfo.getWorkflowname(cardWorkflowId);
    String requestName = workflowname + "-" + lastName;
    workflowInfo.put("requestName", requestName);
    workflowInfo.put("requestLevel", "1");
    workflowInfo.put("creatorId", resourceId);
    workflowInfo.put("workflowId", cardWorkflowId);
    workflowInfo.put("isNextFlow", "1");

    bb.writeLog("--workflowInfo: " + workflowInfo );

    //流程主表信息
    Map<String, Object> mainTableInfo = new HashMap<>();
    mainTableInfo.put("sqr", resourceId);
    mainTableInfo.put("ygbh", workCode);
    mainTableInfo.put("szbm", departmentId);
    mainTableInfo.put("szzw", jobTitle);
    mainTableInfo.put("sqrq", today);

    bb.writeLog("--mainTableInfo: " + mainTableInfo );

    try {
        List<Map<String, Object>> detailTableInfos = new ArrayList<>();
        if ( cardDates != null && cardDates.size() > 0) {

            //流程明细表信息
            if ( !startDate.equals(endDate)) {
                for (String date : cardDates) {
                    //如果出差是多天的情况，那么只有开始日期的那天的开始卡是开始时间，其他的日期开始卡是取班次的开始时间
                    //获取当天班次，如果没有班次，按照开始时间处理
                    WorkTimeEntity workTime = kqWorkTime.getWorkTime(resourceId, date);
                    bb.writeLog("--workTime:" + JSONObject.toJSONString(workTime));
                    List<TimeScopeEntity> lsWorkTime = new ArrayList<>();
                    if (workTime != null) {
                        lsWorkTime = workTime.getWorkTime();//工作时间
                    }
                    if ( date.equals(startDate)) {
                        if (lsWorkTime != null && lsWorkTime.size() ==1 ) {
                            TimeScopeEntity workTimeEntity = lsWorkTime.get(0);
                            String shiftendTime = workTimeEntity.getEndTime();
                            if (StringUtils.isNotBlank(shiftendTime)) {
                                Map<String, Object> detailTableInfo = new HashMap<>();
                                detailTableInfo.put("bkrq", date);
                                detailTableInfo.put("bklx", "0");//上班
                                detailTableInfo.put("bksj1", startTime);
                                detailTableInfo.put("bkyy", "5");
                                detailTableInfos.add(detailTableInfo);
                                detailTableInfo = new HashMap<>();
                                detailTableInfo.put("bkrq", date);
                                detailTableInfo.put("bklx", "1");//下班
                                detailTableInfo.put("bksj1", shiftendTime);
                                detailTableInfo.put("bkyy", "5");
                                detailTableInfos.add(detailTableInfo);
                            }
                        }
                    } else {
                        if ( !date.equals(endDate)) {
                            if (lsWorkTime != null && lsWorkTime.size() ==1 ) {
                                TimeScopeEntity workTimeEntity = lsWorkTime.get(0);
                                String shiftendTime = workTimeEntity.getEndTime();
                                String shiftbeginTime = workTimeEntity.getBeginTime();
                                if (StringUtils.isNotBlank(shiftendTime) && StringUtils.isNotBlank(shiftbeginTime)) {
                                    Map<String, Object> detailTableInfo = new HashMap<>();
                                    detailTableInfo.put("bkrq", date);
                                    detailTableInfo.put("bklx", "0");//上班
                                    detailTableInfo.put("bksj1", shiftbeginTime);
                                    detailTableInfo.put("bkyy", "5");
                                    detailTableInfos.add(detailTableInfo);
                                    detailTableInfo = new HashMap<>();
                                    detailTableInfo.put("bkrq", date);
                                    detailTableInfo.put("bklx", "1");//下班
                                    detailTableInfo.put("bksj1", shiftendTime);
                                    detailTableInfo.put("bkyy", "5");
                                    detailTableInfos.add(detailTableInfo);
                                }
                            }
                        } else {
                            if (lsWorkTime != null && lsWorkTime.size() ==1 ) {
                                TimeScopeEntity workTimeEntity = lsWorkTime.get(0);
                                String beginTime = workTimeEntity.getBeginTime();
                                if (StringUtils.isNotBlank(beginTime)) {
                                    Map<String, Object> detailTableInfo = new HashMap<>();
                                    detailTableInfo.put("bkrq", date);
                                    detailTableInfo.put("bklx", "0");//上班
                                    detailTableInfo.put("bksj1", beginTime);
                                    detailTableInfo.put("bkyy", "5");
                                    detailTableInfos.add(detailTableInfo);
                                    detailTableInfo = new HashMap<>();
                                    detailTableInfo.put("bkrq", date);
                                    detailTableInfo.put("bklx", "1");//下班
                                    detailTableInfo.put("bksj1", endTime);
                                    detailTableInfo.put("bkyy", "5");
                                    detailTableInfos.add(detailTableInfo);
                                } else {
                                    Map<String, Object> detailTableInfo = new HashMap<>();
                                    detailTableInfo.put("bkrq", date);
                                    detailTableInfo.put("bklx", "0");//上班
                                    detailTableInfo.put("bksj1", startTime);
                                    detailTableInfo.put("bkyy", "5");
                                    detailTableInfos.add(detailTableInfo);
                                    detailTableInfo = new HashMap<>();
                                    detailTableInfo.put("bkrq", date);
                                    detailTableInfo.put("bklx", "1");//下班
                                    detailTableInfo.put("bksj1", endTime);
                                    detailTableInfo.put("bkyy", "5");
                                    detailTableInfos.add(detailTableInfo);
                                }
                            } else {
                                Map<String, Object> detailTableInfo = new HashMap<>();
                                detailTableInfo.put("bkrq", date);
                                detailTableInfo.put("bklx", "0");//上班
                                detailTableInfo.put("bksj1", startTime);
                                detailTableInfo.put("bkyy", "5");
                                detailTableInfos.add(detailTableInfo);
                                detailTableInfo = new HashMap<>();
                                detailTableInfo.put("bkrq", date);
                                detailTableInfo.put("bklx", "1");//下班
                                detailTableInfo.put("bksj1", endTime);
                                detailTableInfo.put("bkyy", "5");
                                detailTableInfos.add(detailTableInfo);
                            }
                        }
                    }
                }
            } else {
                Map<String, Object> detailTableInfo = new HashMap<>();
                detailTableInfo.put("bkrq", startDate);
                detailTableInfo.put("bklx", "0");//上班
                detailTableInfo.put("bksj1", startTime);
                detailTableInfo.put("bkyy", "5");
                detailTableInfos.add(detailTableInfo);
                detailTableInfo = new HashMap<>();
                detailTableInfo.put("bkrq", startDate);
                detailTableInfo.put("bklx", "1");//下班
                detailTableInfo.put("bksj1", endTime);
                detailTableInfo.put("bkyy", "5");
                detailTableInfos.add(detailTableInfo);
            }
            bb.writeLog("--detailTableInfos:" + detailTableInfos);

            requestid = WorkflowCreate(workflowInfo, mainTableInfo, detailTableInfos);
        } else {
            bb.writeLog("申请人所在考勤组在申请日期中没有关闭外勤打卡的考勤日期，不创建补卡流程");
        }

    }catch (Exception e) {
        bb.writeLog(" -- Exception:" + e);
    }


    sum++;

%>

<%!
    /**
     * 创建流程
     */
    private String WorkflowCreate(Map<String, String> workflowInfo, Map<String, Object> mainTableInfo, List<Map<String, Object>> detailTableInfos) {

        BaseBean bb = new BaseBean();

        bb.writeLog("--WorkflowCreate start");

        String requestid = "";
        //工作流程请求信息
        WorkflowRequestInfo workflowRequestInfo = new WorkflowRequestInfo();
        //请求标题
        workflowRequestInfo.setRequestName(workflowInfo.get("requestName"));
        //请求重要级别
        workflowRequestInfo.setRequestLevel(workflowInfo.get("requestLevel"));
        //显示
        workflowRequestInfo.setCanView(true);
        //创建者id
        workflowRequestInfo.setCreatorId(workflowInfo.get("creatorId"));
        //是否流转
        workflowRequestInfo.setIsnextflow(workflowInfo.get("isNextFlow"));
        //工作流信息
        WorkflowBaseInfo workflowBaseInfo = new WorkflowBaseInfo();
        workflowBaseInfo.setWorkflowId(workflowInfo.get("workflowId"));
        workflowRequestInfo.setWorkflowBaseInfo(workflowBaseInfo);
        //主表赋值
        WorkflowRequestTableField[] workflowRequestTableField = new WorkflowRequestTableField[mainTableInfo.size()];
        int k = 0;
        for (String key : mainTableInfo.keySet()) {
            String name = key;
            String value = Util.null2String(mainTableInfo.get(key));
            workflowRequestTableField[k] = createWorkflowRequestTableField(name, value);
            k++;
        }
//        workflowRequestTableField[0] = createWorkflowRequestTableField("sqr", "4");
//        workflowRequestTableField[1] = createWorkflowRequestTableField("ygbh", "10086");
        WorkflowMainTableInfo workflowMainTableInfo = new WorkflowMainTableInfo();
        WorkflowRequestTableRecord[] workflowRequestTableRecord = new WorkflowRequestTableRecord[1];
        workflowRequestTableRecord[0] = new WorkflowRequestTableRecord();
        workflowRequestTableRecord[0].setWorkflowRequestTableFields(workflowRequestTableField);
        workflowMainTableInfo.setRequestRecords(workflowRequestTableRecord);
        workflowRequestInfo.setWorkflowMainTableInfo(workflowMainTableInfo);

        //明细表赋值
        WorkflowDetailTableInfo[] workflowDetailTableInfo = new WorkflowDetailTableInfo[1];
        workflowRequestTableRecord = new WorkflowRequestTableRecord[detailTableInfos.size()];
        for (int i = 0; i < detailTableInfos.size(); i++) {
            Map<String, Object> detailTableInfo = detailTableInfos.get(i);
            workflowRequestTableField = new WorkflowRequestTableField[detailTableInfo.size()];
            int j = 0;
            for (String key : detailTableInfo.keySet()) {
                String name = key;
                String value = Util.null2String(detailTableInfo.get(key));
                workflowRequestTableField[j] = createWorkflowRequestTableField(name, value);
                j++;
            }
            workflowRequestTableRecord[i] = new WorkflowRequestTableRecord();
            workflowRequestTableRecord[i].setWorkflowRequestTableFields(workflowRequestTableField);
        }
        workflowDetailTableInfo[0] = new WorkflowDetailTableInfo();
        workflowDetailTableInfo[0].setWorkflowRequestTableRecords(workflowRequestTableRecord);
        workflowRequestInfo.setWorkflowDetailTableInfos(workflowDetailTableInfo);

        WorkflowService workflow = new WorkflowServiceImpl();
        bb.writeLog("--requestid start:");
        bb.writeLog("--workflowRequestInfo:" + JSONObject.toJSONString(workflowRequestInfo));
        try {
//            RequestService
            requestid = workflow.doCreateWorkflowRequest(workflowRequestInfo, Util.getIntValue(Util.null2String(workflowInfo.get("creatorId"))));
            bb.writeLog("--requestid:" + requestid);
            if (Util.getIntValue(requestid) > 0) {
                return requestid;
            }
            bb.writeLog("createOrgAdjustWorkflow failed");
        }catch (Exception e ) {
            bb.writeLog("createOrgAdjustWorkflow exception: " + e);
        }
        return null;
    }

    private WorkflowRequestTableField createWorkflowRequestTableField(String fieldName, String fieldValue) {
        WorkflowRequestTableField tableField = new WorkflowRequestTableField();
        tableField.setFieldName(fieldName);
        tableField.setFieldValue(fieldValue);
        tableField.setView(true);
        tableField.setEdit(true);
        return tableField;
    }

    /**
     * 根据开始日期和结束日期，计算出之间的所有日期
     * @params startDate 指定日期
     * @params endDate 结束日期
     */
    public List<String> getAllDates(String startDate, String endDate) {
        List<String> result = new ArrayList<>();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");

        LocalDate start = LocalDate.parse(startDate, formatter);
        LocalDate end = LocalDate.parse(endDate, formatter);

        while (!start.isAfter(end)) {
            result.add(start.format(formatter));
            start = start.plusDays(1);
        }

        return result;
    }
%>

<HEAD>
</HEAD>
<BODY>
<h1>Congratulation!</h1>
<h2> 55555555555555         <%=requestid%> </h2>
</BODY>
