package weaver.interfaces.htsc.itcglc.action;

import com.engine.htsc.itlxlc.util.RequestShareUtil;
import com.weaver.general.BaseBean;
import weaver.conn.RecordSet;
import weaver.general.Util;
import weaver.interfaces.htsc.comInfo.PropBean;
import weaver.interfaces.htsc.itcglc.util.WorkflowUtil;
import weaver.interfaces.htsc.util.HT_Util;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;
import weaver.workflow.workflow.WorkflowComInfo;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @Title: ecology-9-demo
 * @Company: 泛微软件
 * @author: weaver.Dusongsong
 * @version: 1.0
 * @CreateDate: 2020-09-24
 * @Description: 申请节点提交后删除明细表数据
 * @ModifyLog:
 *
 * 总部重要事项 拟稿节点提交执行
 **/

public class ItcglcCreateCgrwdAction extends BaseBean implements Action {

    @Override
    public String execute(RequestInfo requestInfo) {

        BaseBean bb = new BaseBean();
        int inok = 0;
        try{
            String requestid = requestInfo.getRequestid();
            System.out.println( "----ItcglcCreateCgrwdAction-------" + requestid );
            String tablename = requestInfo.getRequestManager().getBillTableName();//主表
            String cgsq_title = requestInfo.getRequestManager().getRequestname();
            inok = createRequest(requestid,tablename,cgsq_title);
            if(inok >=0){
                return Action.SUCCESS;
            }else{
                requestInfo.getRequestManager().setMessageid( "111111" );
                requestInfo.getRequestManager().setMessagecontent( "流程提交失败" );
                return Action.FAILURE_AND_CONTINUE;
            }
        } catch (Exception e) {
            e.printStackTrace();
            writeLog( "异常参考:" + e );
            requestInfo.getRequestManager().setMessageid( "111111" );
            requestInfo.getRequestManager().setMessagecontent( "流程提交失败" );
            return Action.FAILURE_AND_CONTINUE;
        }
    }

    public int createRequest(String requestid,String tablename,String cgsq_title){
        int isok = 0;
        BaseBean bb = new BaseBean();
        RecordSet rs = new RecordSet();
        RecordSet rs_ngr = new RecordSet();
        try {
            String mainid = "";
            String jkzd1 = "";
            String jkzd2 = "";
            String jkzd3 = "";
            String jkzd4 = "";
            String xmmc = "";
            String xmbh = "";
            String xmfzr = "";
            String lcbh = "";
            String xmmczw = "";
            String itlxsq = "";
            String xgcglc = "";

            String xglcoa = "";
            String sql = " select id,jkzd1,jkzd2,jkzd3,jkzd4,xmmc,xmbh,xmfzr,lcbh,xmmczw,itlxsq,xglcoa,xgcglc from " + tablename + " where requestid=" + requestid;
            bb.writeLog("sql:" + sql);
            rs.executeQuery(sql);
            if (rs.next()) {
                mainid = Util.null2String(rs.getString("id"));//mainid
                jkzd1 = Util.null2String(rs.getString("jkzd1"));//
                jkzd2 = Util.null2String(rs.getString("jkzd2"));//
                jkzd3 = Util.null2String(rs.getString("jkzd3"));//
                jkzd4 = Util.null2String(rs.getString("jkzd4"));//
                itlxsq = Util.null2String(rs.getString("itlxsq"));//
                xgcglc = Util.null2String(rs.getString("xgcglc"));//

                xmmc = Util.null2String(rs.getString("xmmc"));//
                xmmczw = Util.null2String(rs.getString("xmmczw"));//
                xmbh = Util.null2String(rs.getString("xmbh"));//
                xmfzr = Util.null2String(rs.getString("xmfzr"));//
                lcbh = Util.null2String(rs.getString("lcbh"));

                xglcoa = Util.null2String(rs.getString("xglcoa"));

                bb.writeLog("mainid:" + mainid);
                bb.writeLog("jkzd1:" + jkzd1);
                bb.writeLog("jkzd2:" + jkzd2);
                bb.writeLog("jkzd3:" + jkzd3);
                bb.writeLog("jkzd4:" + jkzd4);
                bb.writeLog("jkzd4:" + jkzd4);

                bb.writeLog("xmmc:" + xmmc);
                bb.writeLog("xmmczw:" + xmmczw);

                bb.writeLog("xmbh:" + xmbh);
                bb.writeLog("xmfzr:" + xmfzr);
                bb.writeLog("lcbh:" + lcbh);
            }

//            String itlxsq = "<div style=\"width:100%;\">" +
//                            " <div style=\"float:left;width:70%\">" +
//                            "       <a href=\""+jkzd4+"\" target=\"_blank\" style=\"font-size: 10pt;font-family: Microsoft YaHei;\">"+jkzd1+"</a>" +
//                            " </div>" +
//                            " <div style=\"float:right;color:#a9a8a6;font-size: 10pt;font-family: Microsoft YaHei;\">"+jkzd2+"</div>" +
//                            "</div>" ;

            bb.writeLog("itlxsq:" + itlxsq);
            String itcgsq = xgcglc +
                    " <div style=\"width:100%;line-height:30px;\">" +
                    " <div style=\"float:left;width:70%\">" +
                    "       <a class=\"xgcglc_link\" link=\"/htoa/workflow/request/ViewRequestForwardSPA.jsp?requestid=" + requestid + "&ismonitor=1\" style=\"font-size: 10pt;font-family: Microsoft YaHei;\">" + cgsq_title + "</a>" +
                    " </div>" +
                    " <div style=\"float:right;color:#a9a8a6;font-size: 10pt;font-family: Microsoft YaHei;\">" + lcbh + "</div>" +
                    " </div>";
            bb.writeLog("itcgsq:" + itcgsq);

            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
            String ngsj = sdf.format(new Date());

            WorkflowUtil WorkflowUtil = new WorkflowUtil();
            HT_Util htutil = new HT_Util();
            PropBean pb = new PropBean();
            String cgd_workflowId = pb.getActiveWorkflowIdByUnNumber("itcgrw");
            String zxbmcode = pb.getActiveWorkflowIdByUnNumber("itcgrw_zxbm");
            String yjngr = pb.getPropName("cgd_yj_ngr");
            String fyjngr = pb.getPropName("cgd_fyj_ngr");
            String zbxmid = "";
            if (!"".equals(zxbmcode)) {
                sql = " select id from hrmdepartment t where t.departmentcode='" + zxbmcode + "' ";
                rs.execute(sql);
                if (rs.next()) {
                    zbxmid = Util.null2String(rs.getString("id"));
                }
            }
            String IsNextFlow = "0";

            if (!"".equals(lcbh)) {
                lcbh = lcbh.substring(lcbh.indexOf("20"));
            }

            List<String> ngrList = new ArrayList<String>();
            if (!"".equals(mainid)) {
                List<Map<String, String>> yjList = new ArrayList<Map<String, String>>();
                Map<String, String> yjMap = null;
                String btcgnr = "";
                String yjsql = "select * from " + tablename + "_dt5 where mainid = " + mainid + " and cglx = 1 order by id ";
                bb.writeLog("yjsql:" + yjsql);
                rs.execute(yjsql);
                while (rs.next()) {
                    String cglx = Util.null2String(rs.getString("cglx"));
                    String cgfs = Util.null2String(rs.getString("cgfs"));
                    String ysbm = Util.null2String(rs.getString("ysbm"));
                    String ysje = Util.null2String(rs.getString("ysje"));
                    String sl = Util.null2String(rs.getString("sl"));
                    String cgfzr = Util.null2String(rs.getString("cgfzr"));
                    String cgnr = Util.null2String(rs.getString("cgnr"));
                    String cgzd = Util.null2String(rs.getString("cgzd"));
                    String ejcgzd = Util.null2String(rs.getString("ejcgzd"));
                    String gys = Util.null2String(rs.getString("gys"));
                    String bz = Util.null2String(rs.getString("bz"));
                    String cgsqgssm = Util.null2String(rs.getString("cgsqgssm"));
                    String appid = Util.null2String(rs.getString("appid"));
                    //String sjly = Util.null2String(rs.getString("sjly")) ;
                    String workFlowContents = Util.null2String(rs.getString("workFlowContents"));
                    String cgdbh = Util.null2String(rs.getString("cgdbh"));

                    String xmmcdt5 = Util.null2String(rs.getString("xmmc"));
                    String ysjexs = Util.null2String(rs.getString("ysjexs"));

                    btcgnr = cgnr;

                    yjMap = new HashMap<String, String>();
                    yjMap.put("cgdbh", cgdbh);
                    yjMap.put("cglx", cglx);
                    yjMap.put("cgfs", cgfs);
                    if (!"".equals(ysbm)) {
                        yjMap.put("ysbm", ysbm);
                    }

                    yjMap.put("ysje", ysje);
                    yjMap.put("sl", sl);
                    if (!"".equals(cgfzr)) {
                        yjMap.put("cgfzr", cgfzr);
                    }

                    if (!"".equals(cgnr)) {
                        yjMap.put("cgnr", cgnr);
                    }

                    yjMap.put("cgzd", cgzd);
                    yjMap.put("ejcgzd", ejcgzd);
                    yjMap.put("gys", gys);
                    yjMap.put("bz", bz);
                    yjMap.put("cgsqgssm", cgsqgssm);
                    yjMap.put("appid", appid);
                    yjMap.put("sjly", "0");

                    yjMap.put("ysjexs", ysjexs);
                    yjMap.put("xmmc", xmmcdt5);

                    yjMap.put("workFlowContents", workFlowContents);
                    yjList.add(yjMap);
                }
                bb.writeLog("yjList:" + yjList.size());
                if (yjList != null && yjList.size() > 0) {
                    String requestName = cgsq_title + "的" + btcgnr + "等采购任务单(" + lcbh + "-1等)";
                    String szbm = "";
                    String szjg = "";
                    String lxfs = "";
                    String ngr = "";


                    String bmfzr = "";
                    sql = " select h.id,h.departmentid,h.telephone,d.deptdirectors \n" +
                            "from hrmresource h \n" +
                            "left join hrmdepartmentdefined d on d.deptid = h.departmentid\n" +
                            "where h.workcode='" + yjngr + "' and h.belongto is null  ";
                    rs.execute(sql);
                    if (rs.next()) {
                        ngr = Util.null2String(rs.getString("id"));
                        ngrList.add(ngr);
                        szbm = Util.null2String(rs.getString("departmentid"));
                        lxfs = Util.null2String(rs.getString("telephone"));
                        bmfzr = Util.null2String(rs.getString("deptdirectors"));
                    }

                    if (!"".equals(ngr)) {
                        szjg = htutil.getszjg(ngr);
                    }
                    bb.writeLog("szjg:" + szjg);

                    Map<String, String> requestMainMap = new HashMap<String, String>();
                    requestMainMap.put("cffs", "1");
                    requestMainMap.put("jjcd", "0");

                    requestMainMap.put("ngr", ngr);
                    if (!"".equals(szbm)) {
                        requestMainMap.put("szbm", szbm);
                    }

                    if (!"".equals(szjg)) {
                        requestMainMap.put("szjg", szjg);
                    }

                    requestMainMap.put("lxfs", lxfs);
                    requestMainMap.put("bmldsh", bmfzr);
                    requestMainMap.put("ngrq", ngsj);
                    requestMainMap.put("xmmc", xmmc);
                    requestMainMap.put("xmmczw", xmmczw);
                    requestMainMap.put("xmbh", xmbh);

                    if (!"".equals(xmfzr)) {
                        requestMainMap.put("xmfzr", xmfzr);
                    }

                    requestMainMap.put("itlxsq", itlxsq);
                    requestMainMap.put("itcglc", itcgsq);
                    requestMainMap.put("flc", requestid);
                    requestMainMap.put("zbxm", zbxmid);

                    requestMainMap.put("shfs", "0");
                    requestMainMap.put("xglcoa", xglcoa);

                    String rwd_requestid = WorkflowUtil.WorkflowCreateByRequestMap(ngr, cgd_workflowId, requestName, IsNextFlow, requestMainMap);
                    if (Util.getIntValue(rwd_requestid) > 0) {
                        isok = 1 ;
                        bb.writeLog("yjList:" + yjList.size());
                        WorkflowUtil.insertTableDt5(rwd_requestid, cgd_workflowId, yjList);
                    }else {
                        isok = -2;
                    }

                    if (!"".equals(xglcoa)) {
                        try {
                            RequestShareUtil RequestShareUtil = new RequestShareUtil();
                            bb.writeLog("RequestShareUtil---" + xglcoa);
                            RequestShareUtil.addShare(ngr, xglcoa);
                        } catch (Exception e) {
                            bb.writeLog("e---" + e);
                        }
                    }
                }

                List<Map<String, String>> fyjList = new ArrayList<Map<String, String>>();
                Map<String, String> fyjMap = null;
                String fyjsql = "select * from " + tablename + "_dt5 where mainid = " + mainid + " and cglx != 1";
                bb.writeLog("yjsql:" + yjsql);
                rs.execute(fyjsql);
                while (rs.next()) {
                    String cglx = Util.null2String(rs.getString("cglx"));
                    String cgfs = Util.null2String(rs.getString("cgfs"));
                    String ysbm = Util.null2String(rs.getString("ysbm"));
                    String ysje = Util.null2String(rs.getString("ysje"));
                    String sl = Util.null2String(rs.getString("sl"));
                    String cgfzr = Util.null2String(rs.getString("cgfzr"));
                    String cgnr = Util.null2String(rs.getString("cgnr"));
                    String cgzd = Util.null2String(rs.getString("cgzd"));
                    String ejcgzd = Util.null2String(rs.getString("ejcgzd"));
                    String gys = Util.null2String(rs.getString("gys"));
                    String bz = Util.null2String(rs.getString("bz"));
                    String cgsqgssm = Util.null2String(rs.getString("cgsqgssm"));
                    String appid = Util.null2String(rs.getString("appid"));
                    //String sjly = Util.null2String(rs.getString("sjly"));
                    String workFlowContents = Util.null2String(rs.getString("workFlowContents"));
                    String cgdbh = Util.null2String(rs.getString("cgdbh"));

                    String xmmcdt5 = Util.null2String(rs.getString("xmmc"));
                    String ysjexs = Util.null2String(rs.getString("ysjexs"));

                    fyjMap = new HashMap<String, String>();
                    fyjMap.put("cgdbh", cgdbh);
                    fyjMap.put("cglx", cglx);
                    fyjMap.put("cgfs", cgfs);
                    if (!"".equals(ysbm)) {
                        fyjMap.put("ysbm", ysbm);
                    }

                    fyjMap.put("ysje", ysje);
                    if (!"".equals(sl)) {
                        fyjMap.put("sl", sl);
                    }

                    if (!"".equals(cgfzr)) {
                        fyjMap.put("cgfzr", cgfzr);
                    }

                    if (!"".equals(cgnr)) {
                        fyjMap.put("cgnr", cgnr);
                    }

                    fyjMap.put("cgzd", cgzd);
                    fyjMap.put("ejcgzd", ejcgzd);
                    fyjMap.put("gys", gys);
                    fyjMap.put("bz", bz);
                    fyjMap.put("cgsqgssm", cgsqgssm);
                    fyjMap.put("appid", appid);
                    fyjMap.put("sjly", "0");
                    fyjMap.put("workFlowContents", workFlowContents);

                    fyjMap.put("ysjexs", ysjexs);
                    fyjMap.put("xmmc", xmmcdt5);

                    fyjList.add(fyjMap);
                }

                for (int i = 0; i < fyjList.size(); i++) {
                    List<Map<String, String>> fyj_list = new ArrayList<Map<String, String>>();
                    Map<String, String> dataMap = fyjList.get(i);
                    fyj_list.add(dataMap);
                    Map<String, String> requestMainMap = new HashMap<String, String>();
                    requestMainMap.put("cffs", "1");
                    requestMainMap.put("jjcd", "0");

                    String szbm = "";
                    String szjg = "";
                    String lxfs = "";
                    String bmfzr = "";
                    String fyjngrid = "";
                    if (!"".equals(fyjngr)) {
                        sql = " select h.id,h.departmentid,h.telephone,d.deptdirectors \n" +
                                "from hrmresource h \n" +
                                "left join hrmdepartmentdefined d on d.deptid = h.departmentid\n" +
                                "where h.workcode='" + fyjngr + "' and h.belongto is null ";
                        rs_ngr.execute(sql);
                        if (rs_ngr.next()) {
                            fyjngrid = Util.null2String(rs_ngr.getString("id"));
                            szbm = Util.null2String(rs_ngr.getString("departmentid"));
                            lxfs = Util.null2String(rs_ngr.getString("telephone"));
                            bmfzr = Util.null2String(rs_ngr.getString("deptdirectors"));
                        }
                    }

                    if (!"".equals(fyjngrid)) {
                        szjg = htutil.getszjg(fyjngrid);
                    }

                    bb.writeLog("szjg:" + szjg);
                    String cgdbh = Util.null2String(dataMap.get("cgdbh"));
                    String cgnr = Util.null2String(dataMap.get("cgnr"));

                    if (!"".equals(lcbh)) {
                        lcbh = lcbh.substring(lcbh.indexOf("20"));
                    }
                    String requestName = cgsq_title + "的" + cgnr + "采购任务单(" + cgdbh + ")";
                    requestMainMap.put("ngr", fyjngrid);
                    requestMainMap.put("szbm", szbm);
                    requestMainMap.put("szjg", szjg);
                    requestMainMap.put("lxfs", lxfs);
                    requestMainMap.put("bmldsh", bmfzr);

                    requestMainMap.put("ngrq", ngsj);
                    requestMainMap.put("xmmc", xmmc);
                    requestMainMap.put("xmmczw", xmmczw);
                    requestMainMap.put("xmbh", xmbh);
                    requestMainMap.put("xmfzr", xmfzr);

                    requestMainMap.put("itlxsq", itlxsq);
                    requestMainMap.put("itcglc", itcgsq);
                    requestMainMap.put("flc", requestid);

                    requestMainMap.put("zbxm", zbxmid);
                    requestMainMap.put("shfs", "0");
                    requestMainMap.put("xglcoa", xglcoa);

                    String rwd_requestid = WorkflowUtil.WorkflowCreateByRequestMap(fyjngrid, cgd_workflowId, requestName, IsNextFlow, requestMainMap);
                    if (Util.getIntValue(rwd_requestid) > 0) {
                        bb.writeLog("fyj_list:" + fyj_list.size());
                        WorkflowUtil.insertTableDt5(rwd_requestid, cgd_workflowId, fyj_list);
                    }else{
                        isok = -2;
                    }

                    if (!"".equals(xglcoa)) {
                        try {
                            RequestShareUtil RequestShareUtil = new RequestShareUtil();
                            bb.writeLog("RequestShareUtil---" + xglcoa);
                            RequestShareUtil.addShare(fyjngrid, xglcoa);
                        } catch (Exception e) {
                            bb.writeLog("e---" + e);
                        }
                    }
                }


//                if((yjList ==null || yjList.size() == 0) && (fyjList == null || fyjList.size() ==0)){
//                    isok = -1000 ;
//                }else{
                    WorkflowComInfo workflowComInfo = new WorkflowComInfo();
                    workflowComInfo.removeWorkflowCache();
//                }
            }
        }catch (Exception e) {
            bb.writeLog("e---:"+e);
        }
        return isok;
    }

}
