package weaver.interfaces.htxc.tycgsq.action;

import com.ibm.icu.text.SimpleDateFormat;
import org.apache.commons.lang3.StringUtils;
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.soa.workflow.request.RequestInfo;

import java.util.*;

/**
 * @Title: ecology-9
 * @Company: 泛微软件
 * @author: K1810027
 * @version: 1.0
 * @CreateDate: 2022-09-16 09:25
 * @Description:It采购任务办结后触发采购任务单
 * @ModifyLog:
 **/
public class ItcgTriggerCgrwd {
    public boolean triggerFellow(RequestInfo requestInfo){
        //请求标题
        String requestname = requestInfo.getRequestManager().getRequestname();
        //请求ID
        String requestid = requestInfo.getRequestid();
        //表单名称
        String tablename = requestInfo.getRequestManager().getBillTableName();
//        String requestname = "1111221221212121";
//        String requestid = "5888896";
//        String tablename = "formtable_main_493";
        PropBean pb = new PropBean();
        String yjngr = pb.getPropName("cgd_yj_ngr");
        String fyjngr = pb.getPropName("cgd_fyj_ngr");
        String zxbmcode = pb.getActiveWorkflowIdByUnNumber("itcgrw_zxbm");
        String cgd_workflowId = pb.getActiveWorkflowIdByUnNumber("tycgrw");
        RecordSet rs = new RecordSet();
        RecordSet hrRs = new RecordSet();
        RecordSet rs1 = new RecordSet();
        String zbxmid = "";
        if (!"".equals(zxbmcode)) {
            String zxsql = " select id from hrmdepartment t where t.departmentcode='" + zxbmcode + "' ";
            rs.execute(zxsql);
            if (rs.next()) {
                zbxmid = Util.null2String(rs.getString("id"));
            }
        }
        String IsNextFlow = "0";

        HT_Util htutil = new HT_Util();
        String mainSql = "select * from "+tablename+ " where requestid = ?";
        rs.executeQuery(mainSql,requestid);
        if(rs.next()){
//            rs.getString("id");
//            jkzd1 = Util.null2String(rs.getString("jkzd1"));//
//            jkzd2 = Util.null2String(rs.getString("jkzd2"));//
//            jkzd3 = Util.null2String(rs.getString("jkzd3"));//
//            jkzd4 = Util.null2String(rs.getString("jkzd4"));//
            String itlxsq = Util.null2String(rs.getString("itlxsq"));//
            String xgcglc = Util.null2String(rs.getString("xgcglc"));//
            //立项类型
            String lxlx = rs.getString("lxlx");
            //立项来源
            String lxly = rs.getString("lxly");
            String zbcysxlc = rs.getString("zbcysxlc");
            String xmmc = Util.null2String(rs.getString("xmmc"));//
            String xmmczw = Util.null2String(rs.getString("xmmczw"));//
            String xmbh = Util.null2String(rs.getString("xmbh"));//
            String xmfzr = Util.null2String(rs.getString("xmfzr"));//
            String lcbh = Util.null2String(rs.getString("lcbh"));

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

            String dtSql = "";
            switch (lxlx){
                case "0":dtSql = "select * from "+tablename+"_dt5 where mainid = ?";break;
                case "1":dtSql = "select * from "+tablename+"_dt9 where mainid = ?";break;
                default:dtSql = "select * from "+tablename+"_dt10 where mainid = ?";break;
            }
            rs.executeQuery(dtSql,rs.getString("id"));
            while(rs.next()){
                String cglx = 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 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"));
                List<Map<String, String>> yjList = new ArrayList<Map<String, String>>();
                HashMap<String, String> 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);
                Map<String, String> requestMainMap = new HashMap<String, String>();
                requestMainMap.put("cffs", "1");
                requestMainMap.put("jjcd", "0");
                SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
                String format = dateFormat.format(new Date());
                requestMainMap.put("ngrq", format);
                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);

                requestMainMap.put("lxly", lxly);
                requestMainMap.put("lxlx", lxlx);
                requestMainMap.put("zbcysxlc", zbcysxlc);
                if(Objects.equals("1",cglx)){
                    Map<String, String> request = createRequest(yjngr);
                    //拟稿人
                    String ngr = request.get("id");
                    //所在部门
                    String szbm = request.get("departmentid");
                    //联系方式
                    String lxfs = request.get("telephone");
                    //部门负责人
                    String bmfzr = request.get("deptdirectors");

                    //所在机构
                    String szjg = htutil.getszjg(ngr);
                    requestMainMap.put("lxfs", lxfs);
                    requestMainMap.put("bmldsh", bmfzr);
                    requestMainMap.put("ngr", ngr);
                    if (!"".equals(szbm)) {
                        requestMainMap.put("szbm", szbm);
                    }
                    if (!"".equals(szjg)) {
                        requestMainMap.put("szjg", szjg);
                    }
                    String requestName = requestname + "的" + cgnr + "等采购任务单(" + lcbh + "-1等)";
                    WorkflowUtil workflowUtil = new WorkflowUtil();
                    String rwd_requestid = workflowUtil.WorkflowCreateByRequestMap(ngr, cgd_workflowId, requestName, IsNextFlow, requestMainMap);
                    if(StringUtils.isNotBlank(rwd_requestid) && Integer.parseInt(rwd_requestid) >0){
                        workflowUtil.insertTableDt5(rwd_requestid, cgd_workflowId, yjList);
                    }else{
                        return false;
                    }

                }else{
                    Map<String, String> request = createRequest(fyjngr);
                    //拟稿人
                    String ngr = request.get("id");
                    //所在部门
                    String szbm = request.get("departmentid");
                    //联系方式
                    String lxfs = request.get("telephone");
                    //部门负责人
                    String bmfzr = request.get("deptdirectors");

                    //所在机构
                    String szjg = htutil.getszjg(ngr);
                    requestMainMap.put("lxfs", lxfs);
                    requestMainMap.put("bmldsh", bmfzr);
                    requestMainMap.put("ngr", ngr);
                    if (!"".equals(szbm)) {
                        requestMainMap.put("szbm", szbm);
                    }
                    if (!"".equals(szjg)) {
                        requestMainMap.put("szjg", szjg);
                    }
                    String requestName = requestname + "的" + cgnr + "等采购任务单(" + lcbh + "-1等)";
                    WorkflowUtil workflowUtil = new WorkflowUtil();
                    String rwd_requestid = workflowUtil.WorkflowCreateByRequestMap(ngr, cgd_workflowId, requestName, IsNextFlow, requestMainMap);
                    if(StringUtils.isNotBlank(rwd_requestid) && Integer.parseInt(rwd_requestid) >0){
                        workflowUtil.insertTableDt5(rwd_requestid, cgd_workflowId, yjList);
                    }else{
                        return false;
                    }
                }
            }
        }
        return true;
    }

    public Map<String, String> createRequest(String ngr){
        RecordSet hrRs = new RecordSet();
        String hrmSql = " 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=? and h.belongto is null  ";
        hrRs.executeQuery(hrmSql,ngr);
        hrRs.next();
        HashMap<String, String> map = new HashMap<>();
        map.put("id",hrRs.getString("id"));
        map.put("departmentid",hrRs.getString("departmentid"));
        map.put("telephone",hrRs.getString("telephone"));
        map.put("deptdirectors",hrRs.getString("deptdirectors"));
        return map;
    }
}
