<%@ page import="java.text.SimpleDateFormat" %>
<%@ page import="weaver.hrm.HrmUserVarify" %>
<%@ page import="weaver.hrm.User" %>
<%@ page import="weaver.conn.RecordSet" %>
<%@ page import="java.util.*" %>
<%@ page import="weaver.general.Util" %>
<%@ page import="weaver.workflow.workflow.WorkflowComInfo" %>
<%@ page import="weaver.workflow.workflow.WorkflowBillComInfo" %>
<%@ page import="weaver.conn.BatchRecordSet" %>
<%@ page language="java" contentType="text/html; charset=UTF-8" %>


<%
    // out.print(Prop.getPropValue("qwe","host"));
    User user = HrmUserVarify.getUser(request, response);
    if (user == null){
        out.print("暂无权限");
    }
    if (!(1 == (user.getUID()))){
        out.print("暂无权限");
        return;
    }
    out.print("开始执行");
    out.print("<br/>");
    long startTime = System.currentTimeMillis();
    RecordSet recordSet = new RecordSet();
    String sql = "select MAINWFID , endnodeid , type from uf_ArchivesSet";
    recordSet.executeQuery(sql);
    ArrayList<HashMap<String, String>> wfids = new ArrayList<>();
    while (recordSet.next()){
        HashMap<String, String> map = new HashMap<>();
        String wfid = Util.null2String(recordSet.getString("MAINWFID"));
        String endnodeid = Util.null2String(recordSet.getString("endnodeid"));
        String type = Util.null2String(recordSet.getString("type"));
        map.put("wfid",wfid);
        map.put("endnodeid",endnodeid);
        map.put("type",type);

        wfids.add(map);
    }
    WorkflowComInfo wf = new WorkflowComInfo();
    WorkflowBillComInfo wcInfo = new WorkflowBillComInfo();
    ArrayList<String> insertParam = new ArrayList<>();
    String sep = Util.getSeparator()+"";
    BatchRecordSet brs = new BatchRecordSet();
    String timestamp = generateTimestamp();
    String insertSql = "insert into uf_pushArchivesLog (request,ddsj,type) values (?,?,?)";
    for (HashMap<String, String> map : wfids) {
        String wfid = map.get("wfid");
        String endnodeid = map.get("endnodeid");
        endnodeid = endnodeid.length() > 0 ? endnodeid : "-1";
        String type = map.get("type");
        String formId = wf.getFormId(wfid);
        String tablename = wcInfo.getTablename(formId);
        String wfsql = "select base.requestid reqid from "+ tablename +" business left join " +
                " WORKFLOW_REQUESTBASE base on business.requestid = base.requestid " +
                " left join uf_pushArchivesLog log on business.requestid = log.request " +
                " where ( base.currentnodeid in ( "+ endnodeid +" ) or currentnodetype = 3 )  " +
                "  and log.id  is null";
        recordSet.executeQuery( wfsql);
        out.println("查询sql:"+wfsql);
        out.print("<br/>");
        while (recordSet.next()){
            String reqid = recordSet.getString("reqid");
            String insertStr = reqid + sep + timestamp + sep + type;
            out.print(insertStr);
            out.print("<br/>");
            insertParam.add(insertStr);
        }
    }
    brs.executeSqlBatchNew(insertSql,insertParam);
    long endTime = System.currentTimeMillis();
    long executionTime = endTime - startTime;
    out.print("执行完成 ->"+ "程序执行时间: " + executionTime + " 毫秒");

%>
<%!
    public static String generateTimestamp() {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date currentDate = new Date();
        return dateFormat.format(currentDate);
    }

%>