package com.weaver.seconddev.interfaces.workflow.action;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import com.weaver.common.distribution.genid.IdGenerator;
import com.weaver.common.hrm.util.HrmContextUtil;
import com.weaver.ebuilder.form.client.ebform.data.EbFormDataDubboService;
import com.weaver.framework.rpc.annotation.RpcReference;
import com.weaver.seconddev.interfaces.workflow.util.CommonDevUtil;
import com.weaver.teams.domain.user.SimpleEmployee;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.weaver.common.i18n.tool.util.I18nContextUtil;
import com.weaver.verupgrade.conn.CONN_TYPE;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.weaver.verupgrade.conn.RecordSet;
import com.weaver.verupgrade.docs.docs.DocViewer;
import com.weaver.verupgrade.general.Util;
import com.weaver.verupgrade.soa.workflow.request.MainTableInfo;
import com.weaver.verupgrade.soa.workflow.request.Property;
import com.weaver.verupgrade.soa.workflow.request.RequestInfo;
import com.weaver.verupgrade.workflow.request.RequestManager;
import com.weaver.verupgrade.workflow.webservices.WorkflowBaseInfo;
import com.weaver.verupgrade.workflow.webservices.WorkflowMainTableInfo;
import com.weaver.verupgrade.workflow.webservices.WorkflowRequestInfo;
import com.weaver.verupgrade.workflow.webservices.WorkflowRequestTableField;
import com.weaver.verupgrade.workflow.webservices.WorkflowRequestTableRecord;
import com.weaver.verupgrade.workflow.webservices.WorkflowServiceImpl;
import com.weaver.verupgrade.general.BaseBean;
import org.springframework.beans.factory.annotation.Autowired;

public class DoTheAction {

    private static final Logger logger_7213d31d = LoggerFactory.getLogger(DoTheAction.class);

    private Log log = LogFactory.getLog(DoTheAction.class.getName());

    @Autowired
    private CommonDevUtil commonDevUtil;

    @RpcReference(group = "ebuilderform")
    private EbFormDataDubboService ebFormDataDubboService;

    public boolean doAction(RequestInfo paramRequestInfo, int actionType) throws Exception {
        RecordSet rs = I18nContextUtil.getBean(RecordSet.class);
        RecordSet rs1 = I18nContextUtil.getBean(RecordSet.class);
        String workflowid = paramRequestInfo.getWorkflowid();
        String requestid = paramRequestInfo.getRequestid();
        Map mainMap = new HashMap();
        Property[] properties = paramRequestInfo.getMainTableInfo().getProperty();
        for (int i = 0; i < properties.length; i++) {
            String name = properties[i].getName();
            String value = Util.null2String(properties[i].getValue());
            mainMap.put(name, value);
        }
        boolean isr = true;
        // E9workflowid无法找到相关流程
        logger_7213d31d.error("DoTheAction start ---  "+ workflowid );
        if ((workflowid.equals("100003460000001108")) || (workflowid.equals("100003460000001111")) || (workflowid.equals("100003460000001113")) || (workflowid.equals("100003460000001115")) || (workflowid.equals("100003460000001114")) || (workflowid.equals("100003460000001112"))) {
            String mxid = Util.null2String((String) mainMap.get("mxid"));
            String fklx = "";
            String tjzt = "";
            String gdzt = "";
            String thzt = "";
            if(workflowid.equals("100003460000001108")){
                logger_7213d31d.error("actionType1---  "+ actionType );
                if (actionType == 2) {
                    String xgzrw = Util.null2String((String) mainMap.get("xgzrw"));
                    String xzrr = Util.null2String((String) mainMap.get("xzzr"));
                    if (!"".equals(mxid)) {
                        if (!"".equals(xgzrw)) {
                            String poolname = CONN_TYPE.workflow.getType();
                            // TODO E10执行sql方法第二参数必须指定源，默认使用流程源，单体客户无需修改，微服务/组合客户需根据查询表所属服务切换源，或使用外部数据源
                            boolean b = rs.executeSql("update e10_common.dbo.JhrwMx set gzrw='" + xgzrw + "' where mxid=" + mxid, poolname);
                            logger_7213d31d.error("b--"+b);
                        }
                        if (!"".equals(xzrr)) {
                            String poolname_i5 = CONN_TYPE.workflow.getType();
                            // TODO E10执行sql方法第二参数必须指定源，默认使用流程源，单体客户无需修改，微服务/组合客户需根据查询表所属服务切换源，或使用外部数据源
                            boolean b = rs.executeSql("update e10_common.dbo.JhrwMx set zrr='" + xzrr + "' where mxid=" + mxid, poolname_i5);
                            logger_7213d31d.error("b--"+b);
                        }
                    }
                    gdzt = "执行中";
                } else if (actionType == 1) {
                    tjzt = "变更审批中";
                } else if (actionType == 3) {
                    thzt = "执行中";
                }
                fklx = "变更申请";
            }else if(workflowid.equals("100003460000001111")){
                fklx = "处理过程反馈";
                gdzt = "执行中";
            }else if(workflowid.equals("100003460000001113")){
                logger_7213d31d.error("actionType2---  "+ actionType );
                if (actionType == 2) {
                    if (!"".equals(mxid)) {
                        String poolname_w5 = CONN_TYPE.workflow.getType();
                        // TODO E10执行sql方法第二参数必须指定源，默认使用流程源，单体客户无需修改，微服务/组合客户需根据查询表所属服务切换源，或使用外部数据源
                        boolean b = rs.executeSql("update e10_common.dbo.JhrwMx set rwzt='已取消' where mxid=" + mxid, poolname_w5);
                        logger_7213d31d.error("b--"+b);
                        jhrwDate("qxjsrq", mxid);
                        String poolname_zj = CONN_TYPE.workflow.getType();
                        // TODO E10执行sql方法第二参数必须指定源，默认使用流程源，单体客户无需修改，微服务/组合客户需根据查询表所属服务切换源，或使用外部数据源
                        boolean b1 = rs.executeSql("select qxsqrq,qxjsrq from e10_common.dbo.Jhrwmx where mxid='" + mxid + "'", poolname_zj);
                        logger_7213d31d.error("b1--"+b1);
                        if (rs.next()) {
                            String ksrq = Util.null2String(rs.getString("qxsqrq"));
                            String jsrq = Util.null2String(rs.getString("qxjsrq"));
                            qxRw(mxid, ksrq, jsrq);
                        }
                    }
                    gdzt = "已取消";
                } else if (actionType == 1) {
                    tjzt = "取消审批中";
                    jhrwDate("qxsqrq", mxid);
                } else if (actionType == 3) {
                    String poolname_dp = CONN_TYPE.workflow.getType();
                    // TODO E10执行sql方法第二参数必须指定源，默认使用流程源，单体客户无需修改，微服务/组合客户需根据查询表所属服务切换源，或使用外部数据源
                    boolean b = rs.executeSql("update e10_common.dbo.JhrwMx set qxjsrq='',qxsqrq='' where mxid=" + mxid, poolname_dp);
                    logger_7213d31d.error("b--"+b);
                    thzt = "执行中";
                }
                fklx = "取消申请";
            }else if(workflowid.equals("100003460000001115")){
                logger_7213d31d.error("actionType3---  "+ actionType );
                if (actionType == 2) {
                    if (!"".equals(mxid)) {
                        String poolname_u7 = CONN_TYPE.workflow.getType();
                        // TODO E10执行sql方法第二参数必须指定源，默认使用流程源，单体客户无需修改，微服务/组合客户需根据查询表所属服务切换源，或使用外部数据源
                        boolean b = rs.executeSql("update e10_common.dbo.JhrwMx set rwzt='执行中',wcjsrq='',wcsqrq='' where mxid=" + mxid, poolname_u7);
                        logger_7213d31d.error("b--"+b);
                    }
                    gdzt = "执行中";
                } else if (actionType == 3) {
                    thzt = "执行中";
                }
                fklx = "审批回退";
            }else if(workflowid.equals("100003460000001114")){
                logger_7213d31d.error("actionType4---  "+ actionType );
                if (actionType == 2) {
                    if (!"".equals(mxid)) {
                        String poolname_c1 = CONN_TYPE.workflow.getType();
                        // TODO E10执行sql方法第二参数必须指定源，默认使用流程源，单体客户无需修改，微服务/组合客户需根据查询表所属服务切换源，或使用外部数据源
                        boolean b = rs.executeSql("update e10_common.dbo.JhrwMx set rwzt='已完成' where mxid=" + mxid, poolname_c1);
                        logger_7213d31d.error("b--"+b);
                        jhrwDate("wcjsrq", mxid);
                        String ypid = "";
                        String poolname_pr = CONN_TYPE.workflow.getType();
                        // TODO E10执行sql方法第二参数必须指定源，默认使用流程源，单体客户无需修改，微服务/组合客户需根据查询表所属服务切换源，或使用外部数据源
                        boolean b1 = rs.executeSql("select pid from e10_common.dbo.JhrwMx where mxid=" + mxid, poolname_pr);
                        logger_7213d31d.error("b1--"+b1);
                        if (rs.next()) {
                            ypid = Util.null2String(rs.getString("pid"));
                            logger_7213d31d.error("ypid：" + ypid);
                        }
                        String poolname_xd = CONN_TYPE.workflow.getType();
                        // TODO E10执行sql方法第二参数必须指定源，默认使用流程源，单体客户无需修改，微服务/组合客户需根据查询表所属服务切换源，或使用外部数据源
                        boolean b2 = rs.executeSql("select count(*) as cnum from e10_common.dbo.JhrwMx where pid=" + ypid, poolname_xd);
                        logger_7213d31d.error("b2--"+b2);
                        String sqlaa = "select count(*) as cnum from e10_common.dbo.JhrwMx where  pid=" + ypid;
                        logger_7213d31d.error("sqlaa：" + sqlaa);
                        if (rs.next()) {
                            logger_7213d31d.error("cnum：" + rs.getString("cnum"));
                            if (Util.getIntValue(rs.getString("cnum")) == 1) {
                                String poolname_ro = CONN_TYPE.workflow.getType();
                                // TODO E10执行sql方法第二参数必须指定源，默认使用流程源，单体客户无需修改，微服务/组合客户需根据查询表所属服务切换源，或使用外部数据源
                                boolean b3 = rs.executeSql("select rwlx,rwjb from e10_common.dbo.JhrwMx where pid=" + ypid, poolname_ro);
                                logger_7213d31d.error("b3--"+b3);
                                if (rs.next()) {
                                    String rwlx = rs.getString("rwlx");
                                    String rwjb = rs.getString("rwjb");
                                    if ((rwlx != "0") || (rwjb != "4")) {
                                        logger_7213d31d.error("ypid1：" + ypid);
                                        update_upworkflow(ypid);
                                    }
                                }
                            }
                        }
                    }
                    gdzt = "已完成";
                } else if (actionType == 1) {
                    tjzt = "结案审批中";
                    jhrwDate("wcsqrq", mxid);
                } else if (actionType == 3) {
                    thzt = "执行中";
                    String poolname_jh = CONN_TYPE.workflow.getType();
                    // TODO E10执行sql方法第二参数必须指定源，默认使用流程源，单体客户无需修改，微服务/组合客户需根据查询表所属服务切换源，或使用外部数据源
                    boolean b = rs.executeSql("update e10_common.dbo.JhrwMx set wcjsrq='',wcsqrq='' where mxid='" + mxid + "' ", poolname_jh);
                    logger_7213d31d.error("b--"+b);
                }
                fklx = "结案申请";
            }else if(workflowid.equals("100003460000001112")){
                logger_7213d31d.error("actionType5---  "+ actionType );
                if (actionType == 2) {
                    String xyqrq = Util.null2String((String) mainMap.get("xyqrq"));
                    if (!"".equals(mxid)) {
                        String poolname_91 = CONN_TYPE.workflow.getType();
                        // TODO E10执行sql方法第二参数必须指定源，默认使用流程源，单体客户无需修改，微服务/组合客户需根据查询表所属服务切换源，或使用外部数据源
                        boolean b = rs.executeSql("update e10_common.dbo.JhrwMx set yqrq='" + xyqrq + "' where mxid=" + mxid, poolname_91);
                        logger_7213d31d.error("b--"+b);
                    }
                    jhrwDate("yqjsrq", mxid);
                    gdzt = "执行中";
                } else if (actionType == 1) {
                    jhrwDate("yqsqrq", mxid);
                    tjzt = "延期审批中";
                } else if (actionType == 3) {
                    String poolname_hu = CONN_TYPE.workflow.getType();
                    // TODO E10执行sql方法第二参数必须指定源，默认使用流程源，单体客户无需修改，微服务/组合客户需根据查询表所属服务切换源，或使用外部数据源
                    boolean b = rs.executeSql("update e10_common.dbo.JhrwMx set yqsqrq='',yqjsrq='' where mxid='" + mxid + "' ", poolname_hu);
                    logger_7213d31d.error("b--"+b);
                    thzt = "执行中";
                }
                fklx = "延期申请";
            }

            if (actionType == 2) {
                String poolname_nz = CONN_TYPE.workflow.getType();
                // TODO E10执行sql方法第二参数必须指定源，默认使用流程源，单体客户无需修改，微服务/组合客户需根据查询表所属服务切换源，或使用外部数据源
                boolean b = rs.executeSql("update e10_common.dbo.JhrwMx set rwzt='" + gdzt + "' where mxid=" + mxid, poolname_nz);
                logger_7213d31d.error("b--"+b);
            } else if (actionType == 1) {
                String poolname_qs = CONN_TYPE.workflow.getType();
                // TODO E10执行sql方法第二参数必须指定源，默认使用流程源，单体客户无需修改，微服务/组合客户需根据查询表所属服务切换源，或使用外部数据源
                boolean b = rs.executeSql("update e10_common.dbo.JhrwMx set rwzt='" + tjzt + "' where mxid=" + mxid, poolname_qs);
                logger_7213d31d.error("b--"+b);
            } else if (actionType == 3) {
                String poolname_s9 = CONN_TYPE.workflow.getType();
                // TODO E10执行sql方法第二参数必须指定源，默认使用流程源，单体客户无需修改，微服务/组合客户需根据查询表所属服务切换源，或使用外部数据源
                boolean b = rs.executeSql("update e10_common.dbo.JhrwMx set rwzt='" + thzt + "' where mxid=" + mxid, poolname_s9);
                logger_7213d31d.error("b--"+b);
            }
            yjxr(mainMap, workflowid, fklx, paramRequestInfo);
        }
        return isr;
    }

    public void update_upworkflow(String mxid) {
        WorkflowRequestTableField[] wrti = new WorkflowRequestTableField[16];
        Map map = new HashMap();
        RecordSet rs = I18nContextUtil.getBean(RecordSet.class);
        String sql = "select * from e10_common.dbo.jhrwmx where mxid=" + mxid;
        String poolname_ji = CONN_TYPE.workflow.getType();
        // TODO E10执行sql方法第二参数必须指定源，默认使用流程源，单体客户无需修改，微服务/组合客户需根据查询表所属服务切换源，或使用外部数据源
        boolean b = rs.executeSql(sql, poolname_ji);
        logger_7213d31d.error("b--"+b);
        if (rs.next()) {
            String[] names = rs.getColumnName();
            for (String name : names) {
                map.put(name, Util.null2String(rs.getString(name)));
            }
        }
        wrti[0] = new WorkflowRequestTableField();
        wrti[0].setFieldName("mxid");
        wrti[0].setFieldValue((String) map.get("MXID"));
        wrti[0].setView(true);
        wrti[0].setEdit(true);
        wrti[1] = new WorkflowRequestTableField();
        wrti[1].setFieldName("qrr");
        wrti[1].setFieldValue((String) map.get("QRR"));
        wrti[1].setView(true);
        wrti[1].setEdit(true);
        wrti[2] = new WorkflowRequestTableField();
        wrti[2].setFieldName("ddr");
        wrti[2].setFieldValue((String) map.get("DDR"));
        wrti[2].setView(true);
        wrti[2].setEdit(true);
        wrti[3] = new WorkflowRequestTableField();
        wrti[3].setFieldName("rwbh");
        wrti[3].setFieldValue((String) map.get("RWBH"));
        wrti[3].setView(true);
        wrti[3].setEdit(true);
        wrti[4] = new WorkflowRequestTableField();
        wrti[4].setFieldName("rwlx");
        wrti[4].setFieldValue((String) map.get("RWLX"));
        wrti[4].setView(true);
        wrti[4].setEdit(true);
        wrti[5] = new WorkflowRequestTableField();
        wrti[5].setFieldName("rwjb");
        wrti[5].setFieldValue((String) map.get("RWJB"));
        wrti[5].setView(true);
        wrti[5].setEdit(true);
        wrti[6] = new WorkflowRequestTableField();
        wrti[6].setFieldName("tdsj");
        wrti[6].setFieldValue((String) map.get("TDSJ"));
        wrti[6].setView(true);
        wrti[6].setEdit(true);
        wrti[7] = new WorkflowRequestTableField();
        wrti[7].setFieldName("lxsj");
        wrti[7].setFieldValue((String) map.get("LXSJ"));
        wrti[7].setView(true);
        wrti[7].setEdit(true);
        wrti[8] = new WorkflowRequestTableField();
        wrti[8].setFieldName("xmkp");
        wrti[8].setFieldValue((String) map.get("XMKP"));
        wrti[8].setView(true);
        wrti[8].setEdit(true);
        wrti[9] = new WorkflowRequestTableField();
        wrti[9].setFieldName("shr");
        wrti[9].setFieldValue((String) map.get("SHR"));
        wrti[9].setView(true);
        wrti[9].setEdit(true);
        wrti[10] = new WorkflowRequestTableField();
        wrti[10].setFieldName("tbr");
        wrti[10].setFieldValue((String) map.get("TBR"));
        wrti[10].setView(true);
        wrti[10].setEdit(true);
        wrti[11] = new WorkflowRequestTableField();
        wrti[11].setFieldName("tbgs");
        wrti[11].setFieldValue((String) map.get("TBGS"));
        wrti[11].setView(true);
        wrti[11].setEdit(true);
        wrti[12] = new WorkflowRequestTableField();
        wrti[12].setFieldName("tbbm");
        wrti[12].setFieldValue((String) map.get("TBBM"));
        wrti[12].setView(true);
        wrti[12].setEdit(true);
        wrti[13] = new WorkflowRequestTableField();
        wrti[13].setFieldName("wjrq");
        wrti[13].setFieldValue((String) map.get("WJRQ"));
        wrti[13].setView(true);
        wrti[13].setEdit(true);
        wrti[14] = new WorkflowRequestTableField();
        wrti[14].setFieldName("yqsm");
        wrti[14].setFieldValue((String) map.get("YQSM"));
        wrti[14].setView(true);
        wrti[14].setEdit(true);
        WorkflowRequestTableRecord[] wrtri = new WorkflowRequestTableRecord[1];
        wrtri[0] = new WorkflowRequestTableRecord();
        wrtri[0].setWorkflowRequestTableFields(wrti);
        WorkflowMainTableInfo wmi = new WorkflowMainTableInfo();
        wmi.setRequestRecords(wrtri);
        WorkflowBaseInfo wbi = new WorkflowBaseInfo();
        wbi.setWorkflowId("1114");
        WorkflowRequestInfo wri = new WorkflowRequestInfo();
        wri.setCreatorId((String) map.get("ZRR"));
        wri.setRequestLevel("0");
        wri.setRequestName("完成流程触发");
        wri.setWorkflowMainTableInfo(wmi);
        wri.setWorkflowBaseInfo(wbi);
        WorkflowServiceImpl WorkflowServicePortTypeProxy = I18nContextUtil.getBean(WorkflowServiceImpl.class);
        // TODO 方法参数:1 类型发生变化 e9类型:int e10类型:Long
        //TODO custom 已处理 管理员id
        String requestid = WorkflowServicePortTypeProxy.doCreateWorkflowRequest(wri, 1046285013082546176L);
        logger_7213d31d.error("流程触发：" + requestid);
    }

    public void qxRw(String mxid, String ksrq, String jsrq) {
        RecordSet rs = I18nContextUtil.getBean(RecordSet.class);
        RecordSet rs1 = I18nContextUtil.getBean(RecordSet.class);
        String sql = "select mxid from e10_common.dbo.jhrwmx where pid='" + mxid + "'";
        String poolname_yg = CONN_TYPE.workflow.getType();
        // TODO E10执行sql方法第二参数必须指定源，默认使用流程源，单体客户无需修改，微服务/组合客户需根据查询表所属服务切换源，或使用外部数据源
        boolean b = rs.executeSql(sql, poolname_yg);
        logger_7213d31d.error("b--" + b);
        while (rs.next()) {
            String mxid2 = Util.null2String(rs.getString("mxid"));
            String poolname_xq = CONN_TYPE.workflow.getType();
            // TODO E10执行sql方法第二参数必须指定源，默认使用流程源，单体客户无需修改，微服务/组合客户需根据查询表所属服务切换源，或使用外部数据源
            boolean b1 = rs1.executeSql("update e10_common.dbo.JhrwMx set rwzt='已取消',yqsqrq='" + ksrq + "',yqjsrq='" + jsrq + "' where mxid='" + mxid2 + "' ", poolname_xq);
            logger_7213d31d.error("b1--" + b1);
            qxRw(mxid2, ksrq, jsrq);
        }
    }

    public void jhrwDate(String zd, String mxid) {
        Date nowDate = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String nowDateStr = sdf.format(nowDate);
        RecordSet rs = I18nContextUtil.getBean(RecordSet.class);
        String poolname_17 = CONN_TYPE.workflow.getType();
        // TODO E10执行sql方法第二参数必须指定源，默认使用流程源，单体客户无需修改，微服务/组合客户需根据查询表所属服务切换源，或使用外部数据源
        boolean b = rs.executeSql("update e10_common.dbo.JhrwMx set " + zd + "='" + nowDateStr + "' where mxid='" + mxid + "' ", poolname_17);
        logger_7213d31d.error("b--" + b);
    }

    public void yjxr(Map mainMap, String workflowid, String fklx, RequestInfo paramRequestInfo) throws Exception {
        RecordSet rs2 = I18nContextUtil.getBean(RecordSet.class);
        RecordSet rs1 = I18nContextUtil.getBean(RecordSet.class);
        String requestid = paramRequestInfo.getRequestid();
        String mxid = Util.null2String((String) mainMap.get("mxid"));
        Calendar cal = Calendar.getInstance();
        String currDate = Util.add0(cal.get(1), 4) + "-" + Util.add0(cal.get(2) + 1, 2) + "-" + Util.add0(cal.get(5), 2);
        String currTime = Util.add0(cal.getTime().getHours(), 2) + ":" + Util.add0(cal.getTime().getMinutes(), 2);
        String remark = paramRequestInfo.getRequestManager().getRemark();
        String userid = paramRequestInfo.getLastoperator();
        String fjid = "";
        /*
	String sql11="SELECT top 1 ANNEXDOCIDS FROM workflow_requestlog WHERE REQUESTID='"+requestid+"' AND  OPERATOR='"+userid+"'"+ " order by operatedate desc,operatetime desc ";
	rs2.execute(sql11);
	logger_7213d31d.error("sql11:" + sql11);
	if(rs2.next())
	{
		fjid = Util.null2String(rs2.getString("ANNEXDOCIDS")).replace(" ", "");
	}
	BaseBean bb = new BaseBean();
	bb.writeLog("getSigndocids--"+ paramRequestInfo.getRequestManager().getSigndocids());
	bb.writeLog("sql11--fjid--"+ sql11);
	bb.writeLog("sql11--fjid--"+ requestid + "----" + fjid + "----");
	*/
        remark = remark.replace("&nbsp;", "");
        remark = remark.replace("'", "''");
        if (!"".equals(remark) || !"".equals(fjid)) {
//            String sql1 = "insert into By_Worktaskfankui(worktaskid,begindate,Begintime,resourceid,description,Createrid,Requestid,fj,title) values('" + mxid + "'," + "'" + currDate + "'," + "'" + currTime + "'," + "'" + userid + "'," + "'" + remark + "'," + "'" + userid + "'," + "'" + requestid + "'," + "'" + fjid + "'," + "'" + fklx + "审批'" + ")";
//            String poolname_cs = CONN_TYPE.workflow.getType();
//            /* *TODO  检测到insert语句 -> E9插入sql迁移到E10需要特殊处理请参考文档进行调整  https://www.e-cology.com.cn/sp/doc/docShare/1027709301045985331?tenantKey=t7akvdnf84
// TODO E10执行sql方法第二参数必须指定源，默认使用流程源，单体客户无需修改，微服务/组合客户需根据查询表所属服务切换源，或使用外部数据源*/
//            long generate = IdGenerator.generate();
//            rs2.executeSql(sql1, poolname_cs,generate);
//            logger_7213d31d.error("sql1:" + sql1);
            String poolname = CONN_TYPE.hrm.getType();
            SimpleEmployee currentEmployee = HrmContextUtil.getCurrentUser();
            Long uf_CustomFlow_formId = commonDevUtil.getObjId("By_Worktaskfankui");
            logger_7213d31d.error("uf_CustomFlow_formId = " + uf_CustomFlow_formId);
            Long recodId = ebFormDataDubboService.addEmptyData(uf_CustomFlow_formId, currentEmployee);
            String sql3 = "update e10_common.dbo.By_Worktaskfankui set worktaskid='" + mxid + "',begindate=" + " '" + currDate + "',Begintime=" + " '" + currTime + "',resourceid=" + " '" + userid + "',description=" + " '" + remark + "',Createrid=" + " '" + userid + "',Requestid=" + " '" + requestid + "',fj=" + " '" + fjid + "',title=" + " '" + fklx + "审批' " + " where id  =  " + recodId + " and delete_type = 0 ";
            boolean b1 = rs2.executeSql(sql3, poolname);
            logger_7213d31d.error("sql3 = " + sql3);
            logger_7213d31d.error("添加 = " + b1);
            if (!fjid.equals("")) {
                logger_7213d31d.error("SET_WD_QX:");
                SET_WD_QX(fjid, mxid);
            }
        }
        String sql = "select id from e10_common.dbo.By_Worktaskfankui where worktaskid='" + mxid + "' and requestid = '" + requestid + "' and title like '" + fklx + "'";
        String poolname_ym = CONN_TYPE.workflow.getType();
        // TODO E10执行sql方法第二参数必须指定源，默认使用流程源，单体客户无需修改，微服务/组合客户需根据查询表所属服务切换源，或使用外部数据源
        boolean b = rs1.executeSql(sql, poolname_ym);
        logger_7213d31d.error("b--" + b);
        logger_7213d31d.error("sql:" + sql);
        if (!rs1.next()) {
            xrfksj(mainMap, fklx, paramRequestInfo);
        }
    }

    public void qzyjxr(Map mainMap, String fklx, RequestInfo paramRequestInfo) {
        RecordSet rs2 = I18nContextUtil.getBean(RecordSet.class);
        RecordSet rs1 = I18nContextUtil.getBean(RecordSet.class);
        String requestid = paramRequestInfo.getRequestid();
        String mxid = Util.null2String((String) mainMap.get("mxid"));
        String sql2 = "select remark,annexdocids,operatedatetime,operatetime,creator from wfc_requestlog as  workflow_requestlog where remark is not null and requestid='" + requestid + "'";
        String poolname_7x = CONN_TYPE.workflow.getType();
        // TODO E10执行sql方法第二参数必须指定源，默认使用流程源，单体客户无需修改，微服务/组合客户需根据查询表所属服务切换源，或使用外部数据源
        boolean b = rs1.executeSql(sql2, poolname_7x);
        logger_7213d31d.error("b:" + b);
        while (rs1.next()) {
            String remark = Util.null2String(rs1.getString("remark"));
            remark = remark.replace("&nbsp;", "");
            String fjid = Util.null2String(rs1.getString("annexdocids"));
            String date = Util.null2String(rs1.getString("operatedate"));
            String time = Util.null2String(rs1.getString("operatetime"));
            String userid = Util.null2String(rs1.getString("operator"));
//            String sql1 = "insert into By_Worktaskfankui(id,worktaskid,begindate,Begintime,resourceid,description,Createrid,Requestid,fj,title) values(By_Worktaskfankuid.Nextval,'" + mxid + "'," + "'" + date + "'," + "'" + time + "'," + "'" + userid + "'," + "'" + remark + "'," + "'" + userid + "'," + "'" + requestid + "'," + "'" + fjid + "'," + "'" + fklx + "签字意见'" + ")";
//            String poolname_c1_ps = CONN_TYPE.workflow.getType();
//            /* *TODO  检测到insert语句 -> E9插入sql迁移到E10需要特殊处理请参考文档进行调整  https://www.e-cology.com.cn/sp/doc/docShare/1027709301045985331?tenantKey=t7akvdnf84
// TODO E10执行sql方法第二参数必须指定源，默认使用流程源，单体客户无需修改，微服务/组合客户需根据查询表所属服务切换源，或使用外部数据源*/
//            long generate = IdGenerator.generate();
//            rs2.executeSql(sql1, poolname_c1_ps,generate);
            String poolname = CONN_TYPE.hrm.getType();
            SimpleEmployee currentEmployee = HrmContextUtil.getCurrentUser();
            Long uf_CustomFlow_formId = commonDevUtil.getObjId("By_Worktaskfankui");
            logger_7213d31d.error("uf_CustomFlow_formId = " + uf_CustomFlow_formId);
            Long recodId = ebFormDataDubboService.addEmptyData(uf_CustomFlow_formId, currentEmployee);
            String sql3 = "update e10_common.dbo.By_Worktaskfankui set worktaskid='" + mxid + "',begindate=" + " '" + date + "',Begintime=" + " '" + time + "',resourceid=" + " '" + userid + "',description=" + " '" + remark + "',Createrid=" + " '" + userid + "',Requestid=" + " '" + requestid + "',fj=" + " '" + fjid + "',title=" + " '" +fklx + "签字意见' " + " where id  = " + recodId + " and delete_type = 0 ";
            boolean b1 = rs2.executeSql(sql3, poolname);
            logger_7213d31d.error("sql3 = " + sql3);
            logger_7213d31d.error("添加 = " + b1);
        }
        Calendar cal = Calendar.getInstance();
        String currDate = Util.add0(cal.get(1), 4) + "-" + Util.add0(cal.get(2) + 1, 2) + "-" + Util.add0(cal.get(5), 2);
        String currTime = Util.add0(cal.getTime().getHours(), 2) + ":" + Util.add0(cal.getTime().getMinutes(), 2);
        String remark = paramRequestInfo.getRequestManager().getRemark();
        remark = remark.replace("&nbsp;", "");
        String fjid = "";
        String userid = paramRequestInfo.getLastoperator();
//        String sql1 = "insert into By_Worktaskfankui(id,worktaskid,begindate,Begintime,resourceid,description,Createrid,Requestid,fj,title) values(By_Worktaskfankuid.Nextval,'" + mxid + "'," + "'" + currDate + "'," + "'" + currTime + "'," + "'" + userid + "'," + "'" + remark + "'," + "'" + userid + "'," + "'" + requestid + "'," + "'" + fjid + "'," + "'" + fklx + "签字意见'" + ")";
//        String poolname_0l = CONN_TYPE.workflow.getType();
//        /* *TODO  检测到insert语句 -> E9插入sql迁移到E10需要特殊处理请参考文档进行调整  https://www.e-cology.com.cn/sp/doc/docShare/1027709301045985331?tenantKey=t7akvdnf84
// TODO E10执行sql方法第二参数必须指定源，默认使用流程源，单体客户无需修改，微服务/组合客户需根据查询表所属服务切换源，或使用外部数据源*/
//        long generate = IdGenerator.generate();
//        rs2.executeSql(sql1, poolname_0l,generate);
        String poolname = CONN_TYPE.hrm.getType();
        SimpleEmployee currentEmployee = HrmContextUtil.getCurrentUser();
        Long uf_CustomFlow_formId = commonDevUtil.getObjId("By_Worktaskfankui");
        logger_7213d31d.error("uf_CustomFlow_formId = " + uf_CustomFlow_formId);
        Long recodId = ebFormDataDubboService.addEmptyData(uf_CustomFlow_formId, currentEmployee);
        String sql3 = "update e10_common.dbo.By_Worktaskfankui set worktaskid='" + mxid + "',begindate=" + " '" + currDate + "',Begintime=" + " '" + currTime + "',resourceid=" + " '" + userid + "',description=" + " '" + remark + "',Createrid=" + " '" + userid + "',Requestid=" + " '" + requestid + "',fj=" + " '" + fjid + "',title="+ " '" + fklx + "签字意见' " + " where id  = " + recodId + " and delete_type = 0 ";
        boolean b1 = rs2.executeSql(sql3, poolname);
        logger_7213d31d.error("sql3 = " + sql3);
        logger_7213d31d.error("添加 = " + b1);
    }

    public void xrfksj(Map mainMap, String fklx, RequestInfo paramRequestInfo) throws Exception {
        RecordSet rs = I18nContextUtil.getBean(RecordSet.class);
        String requestid = paramRequestInfo.getRequestid();
        String mxid = Util.null2String((String) mainMap.get("mxid"));
        String fksm = Util.null2String((String) mainMap.get("yqsm")).replace("'", "''");
        String fj = Util.null2String((String) mainMap.get("fjsc")).replace(" ", "");
        Calendar cal = Calendar.getInstance();
        String currDate = Util.add0(cal.get(1), 4) + "-" + Util.add0(cal.get(2) + 1, 2) + "-" + Util.add0(cal.get(5), 2);
        String currTime = Util.add0(cal.getTime().getHours(), 2) + ":" + Util.add0(cal.getTime().getMinutes(), 2);
        /* TODO 返回值:sql1 类型发生变化 e9类型:int  e10类型:long
 TODO 返回值:sql1 类型发生变化 e9类型:int  e10类型:long*/
//        String sql1 = "insert into By_Worktaskfankui(worktaskid,begindate,Begintime,resourceid,description,Createrid,Requestid,fj,title) values('" + mxid + "'," + "'" + currDate + "'," + "'" + currTime + "'," + "'" + paramRequestInfo.getRequestManager().getCreater() + "'," + "'" + fksm + "'," + "'" + paramRequestInfo.getRequestManager().getCreater() + "'," + "'" + requestid + "'," + "'" + fj + "'," + "'" + fklx + "'" + ")";
//        String poolname_9g = CONN_TYPE.workflow.getType();
//        /* *TODO  检测到insert语句 -> E9插入sql迁移到E10需要特殊处理请参考文档进行调整  https://www.e-cology.com.cn/sp/doc/docShare/1027709301045985331?tenantKey=t7akvdnf84
// TODO E10执行sql方法第二参数必须指定源，默认使用流程源，单体客户无需修改，微服务/组合客户需根据查询表所属服务切换源，或使用外部数据源*/
//        long generate = IdGenerator.generate();
//        rs.executeSql(sql1, poolname_9g,generate);
//        logger_7213d31d.error("sql1:" + sql1);
        String poolname = CONN_TYPE.hrm.getType();
        SimpleEmployee currentEmployee = HrmContextUtil.getCurrentUser();
        Long uf_CustomFlow_formId = commonDevUtil.getObjId("By_Worktaskfankui");
        Long recodId = ebFormDataDubboService.addEmptyData(uf_CustomFlow_formId, currentEmployee);
        String sql3 = "update e10_common.dbo.By_Worktaskfankui set worktaskid='" + mxid + "',begindate=" + " '" + currDate + "',Begintime=" + " '" + currTime + "',resourceid=" + " '" + paramRequestInfo.getRequestManager().getCreater() + "',description=" + " '" + fksm + "',Createrid=" + " '" + paramRequestInfo.getRequestManager().getCreater() + "',Requestid=" + "'" + requestid + "',fj=" + " '" + fj + "',title=" + " '" + fklx + "签字意见' " + " where id  = " + recodId + " and delete_type = 0 ";
        boolean b1 = rs.executeSql(sql3, poolname);
        logger_7213d31d.error("sql3 = " + sql3);
        logger_7213d31d.error("添加 = " + b1);
        if (!fj.equals("")) {
            SET_WD_QX(fj, mxid);
        }
    }

    public void SET_WD_QX(String fj, String worktaskid) throws Exception {
        String ryid = "";
        RecordSet rs = I18nContextUtil.getBean(RecordSet.class);
        // String sql1 = " select ZRR||','||PHR||','||DDR||','||QRR||','||CSR||','||TBR as ryid from jhrwmx jh  start with jh.id = " + worktaskid + " connect   by   prior   jh.pid=jh.id ";
        String sql1 = "with temp (id,pid,zrr,phr,ddr,qrr,csr,tbr) as ( select id,pid,zrr,phr,ddr,qrr,csr,tbr from jhrwmx where id = " + worktaskid + " union all select a.id,a.pid,a.zrr,a.phr,a.ddr,a.qrr,a.csr,a.tbr from jhrwmx a inner join temp on a.id = temp.pid)" + " select (cast(zrr as varchar) + ','+ cast(phr as varchar) + ',' + cast(ddr as varchar) + ',' + cast(qrr as varchar) + ',' + cast(csr as varchar) + ',' + cast(tbr as varchar)) as ryid from temp ";
        String poolname_5q = CONN_TYPE.workflow.getType();
        // TODO E10执行sql方法第二参数必须指定源，默认使用流程源，单体客户无需修改，微服务/组合客户需根据查询表所属服务切换源，或使用外部数据源
        boolean b = rs.executeSql(sql1, poolname_5q);
        logger_7213d31d.error("b:" + b);
        logger_7213d31d.error("sql1:" + sql1);
        while (rs.next()) {
            ryid = ryid + Util.null2String(rs.getString("ryid")) + ",";
        }
        logger_7213d31d.error("ryid:" + ryid);
        ryid = ryid.replace(" ", "");
        String[] strarray = ryid.split(",");
        ArrayList<String> List = new ArrayList();
        String temp = "";
        for (int n = 0; n < strarray.length - 1; n++) {
            temp = Util.null2String(strarray[n] + "");
            if (temp != "") {
                if (!List.contains(temp)) {
                    List.add(temp);
                }
            }
        }
        /*
    for (int i = 0; i <= List.size() - 1; i++)
    {
      for (int j = List.size() - 1; j > i; j--)
      {
        if (List.get(j).equals(List.get(i)))
        {
          List.remove(j);
        }

      }

    }
	*/
        RecordSet rs1 = I18nContextUtil.getBean(RecordSet.class);
        String sql2 = "";
        logger_7213d31d.error("fj:" + fj);
        String[] fjids = fj.split(",");
        String fjid = "";
        for (int n = 0; n < fjids.length; n++) {
            logger_7213d31d.error("fjid:" + fj);
            fjid = fjids[n].replace(" ", "");
            if (fjid != "") {
                logger_7213d31d.error("docid:" + fjid);
                for (int m = 0; m <= List.size() - 1; m++) {
                    if (!List.get(m).equals("")) {
                        logger_7213d31d.error("strarray1[m]:" + List.get(m));
//                        sql2 = "insert into document_ph_doc_s01(docid,sharetype,seclevel,rolelevel,sharelevel,userid,downloadlevel,roleid,foralluser)values( " + fjid + ",1 , 0, 0 ,1 ," + List.get(m) + ", 0 ,0 , 0)";
//                        String poolname_jy = CONN_TYPE.workflow.getType();
//                        // TODO E10执行sql方法第二参数必须指定源，默认使用流程源，单体客户无需修改，微服务/组合客户需根据查询表所属服务切换源，或使用外部数据源
//                        rs1.executeSql(sql2, poolname_jy);
//                        logger_7213d31d.error("sql2:" + sql2);
                        String poolname = CONN_TYPE.hrm.getType();
                        SimpleEmployee currentEmployee = HrmContextUtil.getCurrentUser();
                        Long uf_CustomFlow_formId = commonDevUtil.getObjId("document_ph_doc_s01");
                        logger_7213d31d.error("uf_CustomFlow_formId = " + uf_CustomFlow_formId);
                        Long recodId = ebFormDataDubboService.addEmptyData(uf_CustomFlow_formId, currentEmployee);// 获取插入的空的建模id
                        String sql3 = "update e10_common.dbo.document_ph_doc_s01 set docid=" + fjid + ",sharetype=1,seclevel=0,rolelevel=0,sharelevel=1,userid=" + List.get(m) + ",downloadlevel=0,roleid=0,foralluser=0 where id  =  " + recodId + " and delete_type = 0 ";
                        boolean b1 = rs.executeSql(sql3, poolname);
                        logger_7213d31d.error("sql3 = " + sql3);
                        logger_7213d31d.error("添加 = " + b1);
                    }
                }
                DocViewer dv = I18nContextUtil.getBean(DocViewer.class);
                dv.setDocShareByDoc(fjid);
            }
        }
    }

    public void xrfksj2(Map mainMap, String fklx, RequestInfo paramRequestInfo, String id) {
        RecordSet rs = I18nContextUtil.getBean(RecordSet.class);
        String requestid = paramRequestInfo.getRequestid();
        String mxid = Util.null2String((String) mainMap.get("mxid"));
        String fksm = Util.null2String((String) mainMap.get("yqsm"));
        String fj = Util.null2String((String) mainMap.get("fjsc"));
        Calendar cal = Calendar.getInstance();
        String currDate = Util.add0(cal.get(1), 4) + "-" + Util.add0(cal.get(2) + 1, 2) + "-" + Util.add0(cal.get(5), 2);
        String currTime = Util.add0(cal.getTime().getHours(), 2) + ":" + Util.add0(cal.getTime().getMinutes(), 2);
        String sql1 = "update e10_common.dbo.By_Worktaskfankui set begindate='" + currDate + "'," + "Begintime='" + currTime + "'," + "description='" + fksm + "'," + "fj='" + fj + "' " + " where id='" + id + "'";
        String poolname_yj = CONN_TYPE.workflow.getType();
        // TODO E10执行sql方法第二参数必须指定源，默认使用流程源，单体客户无需修改，微服务/组合客户需根据查询表所属服务切换源，或使用外部数据源
        boolean b = rs.executeSql(sql1, poolname_yj);
        logger_7213d31d.error("b:" + b);
    }
}
