
<%@ page import="weaver.file.Prop" %>
<%@ page import="com.engine.custom.sl.entity.EsbRequestHeader" %>
<%@ page import="java.text.SimpleDateFormat" %>
<%@ page import="com.engine.custom.sl.entity.TravelToEsbBean" %>
<%@ page import="java.nio.charset.StandardCharsets" %>
<%@ page import="com.engine.util.SocketClientUtil" %>
<%@ page import="com.engine.util.XMLUtils" %>
<%@ page import="com.alibaba.fastjson.JSONObject" %>
<%@ page import="com.engine.common.util.ParamUtil" %>
<%@ page import="weaver.hrm.HrmUserVarify" %>
<%@ page import="weaver.hrm.User" %>
<%@ page import="weaver.conn.RecordSet" %>
<%@ page import="weaver.soa.workflow.request.RequestInfo" %>
<%@ page import="weaver.hrm.company.DepartmentComInfo" %>
<%@ page import="weaver.general.Util" %>
<%@ page import="weaver.interfaces.workflow.action.Action" %>
<%@ page import="java.util.*" %>
<%@ 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;
    }
    Map<String, Object> param = ParamUtil.request2Map(request);
    String sql  =  "select main.*, base.REQUESTNAME from formtable_main_11 main  " +
            "left join  uf_tohgLog log on log.REQUEST = main.REQUESTID  " +
            "left join WORKFLOW_REQUESTBASE base on base.REQUESTID = main.REQUESTID  " +
            " where log.ID is  null and main.tohg = 1";
    RecordSet recordSet = new RecordSet();
    RecordSet rs = new RecordSet();
    recordSet.executeQuery(sql);
    StringBuilder execute = new StringBuilder();
    int i = 0;
    while (recordSet.next()){
         String result = this.execute(recordSet, rs);
         execute.append(result).append("\n");
         i++;
    }
    out.print("共添加"+i+"条" +"\n");
    out.print(execute.toString());
%>
<%!
    public static String generateTimestamp() {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
        Date currentDate = new Date();
        return dateFormat.format(currentDate);
    }
    public String execute(RecordSet recordSet, RecordSet rs) {

        try {
            String insertsql = "INSERT INTO uf_tohgLog(REQUEST, CONTENT, ZT, ML, DDSJ, TSSJ, FJML, ZWID, FJID) VALUES ( ?, ?, ?, ?, ?, ?, ?, ?, ? )";

            DepartmentComInfo departmentComInfo = new DepartmentComInfo();
            //公文标题
            String requestname = recordSet.getString("REQUESTNAME");
            String requestId = recordSet.getString("REQUESTID");
            // String requestId = request.getRequestid();
            // //  4：获取表单名称
            // String tablename = request.getRequestManager().getBillTableName();

            //  5：查找表单内容
            // RecordSet rs = new RecordSet();
            // rs.execute("select * from " + tablename + " where requestid =  " + requestId);
            // rs.next();
            //是否推送合规    0 - 否    1 - 是
            // int isTohg = Util.getIntValue(rs.getString("tohg"), 1);
            // if (isTohg == 0){
            //     return Action.SUCCESS;
            // }
            //id：表单主键
            String mainid = recordSet.getString("id");

            //公文编号
            String fwwh = recordSet.getString("bh");
            //发文部门名称
            String ngbm = recordSet.getString("ngbm");
            //成文时间
            String cwrq = recordSet.getString("cwrq");
            String ngr = recordSet.getString("ngr");
            // String  zw = rs.getString("zw");   //word格式
            //生成的pdf
            String  zw = recordSet.getString("dwd");
            String  fj = recordSet.getString("fj");
            User user = new User(Integer.parseInt(ngr));
            //拼接数据
            String subcompanyid = departmentComInfo.getSubcompanyid1(ngbm);
            String departmentNames = departmentComInfo.getAllParentDepartmentBlankNames(ngbm, subcompanyid, "-");
            departmentNames = "天津银行股份有限公司-"+departmentNames;
            String context = "";
            String flag = "|";
            context = requestname + flag + fwwh + flag +departmentNames + flag + cwrq +flag+ user.getLoginid()+flag+user.getLastname()+flag;
            //获取文件命
            Map<String, String> fjName = getIdIMIDName(fj);
            Map<String, String> zwName = getIdIMIDName(zw);
            String fjStr = "";
            String zwStr = "";
            //附件
            ArrayList<String> fileNameList = new ArrayList<>();
            Set<Map.Entry<String, String>> entries = fjName.entrySet();
            for (Map.Entry<String, String> entry : entries) {
                String filename = entry.getValue();
                if (fileNameList.contains(filename)){
                    String tepName= filename.contains(".")? filename.substring(0, filename.indexOf(".")) : "";
                    if(tepName!=null&&!"".equals(tepName)){
                        String extNameTemp = filename.contains(".")? filename.substring(filename.lastIndexOf(".") + 1) : "";
                        filename = tepName + "_"+entry.getKey()+"."+extNameTemp;

                    }
                }else {
                    fileNameList.add(filename);
                }
                fjStr = fjStr + "&&"+ filename;
            }
            if (fjStr.startsWith("&&")){
                fjStr = fjStr.substring(2);
            }
            fileNameList.clear();
            //正文
            Set<Map.Entry<String, String>> zwEntries = zwName.entrySet();
            for (Map.Entry<String, String> entry : zwEntries) {
                String filename = entry.getValue();
                if (fileNameList.contains(filename)){
                    String tepName= filename.contains(".")? filename.substring(0, filename.indexOf(".")) : "";
                    if(tepName!=null&&!"".equals(tepName)){
                        String extNameTemp = filename.contains(".")? filename.substring(filename.lastIndexOf(".") + 1) : "";
                        filename = tepName + "_"+entry.getKey()+"."+extNameTemp;
                    }
                }else {
                    fileNameList.add(filename);
                }
                zwStr = zwStr + "&&" + filename;
            }
            if (zwStr.startsWith("&&")){
                zwStr = zwStr.substring(2);
            }
            context = context+zwStr+flag+fjStr+flag+requestId+flag;
            Date date = new Date();
            String time = new SimpleDateFormat("yyyy-MM-dd hh:mm").format(date);
            rs.executeUpdate(insertsql,requestId,context,0,null,time,null,null,zw,fj);
            return insertsql  + "?"+ requestId + "|"+context + "|"+0 + "|"+null + "|"+time + "|"+null + "|"+null + "|"+zw + "|"+fj;
        } catch (Exception e) {
            // writeLog(e.getMessage()+ "||||||异常流程id==="+request.getRequestid());
            e.printStackTrace();
            //不管是否推送成功,都返回成功,不影响流程流转
            return "error";
        }
        // boolean error = false;
        // if (error) {
        //     request.getRequestManager().setMessageid("90001");
        //     request.getRequestManager().setMessagecontent("System Abnormal Termination Process Submission！");
        // }
        // return Action.SUCCESS;
    }

    public static Map<String,String> getIdIMIDName(String ids   ){
        Map<String,String> idimageIDMap = new HashMap<>();
        String sql = "select docid,df.imagefilename filename from docimagefile df left join imagefile imf on df.imagefileid = imf.imagefileid where DOCID in ("+ids+")";
        RecordSet recordSet = new RecordSet();
        recordSet.execute(sql);
        while (recordSet.next()){
            String docid = Util.null2String(recordSet.getString("docid"));
            String filename = Util.null2String(recordSet.getString("filename"));
            idimageIDMap.put(docid,filename);
        }
        return idimageIDMap;
    };
%>