package weaver.interfaces.htxc.tycgsq.action;

import com.ibm.icu.text.SimpleDateFormat;
import org.apache.commons.lang.StringUtils;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.interfaces.htsc.GlobatTools.JAVATools;
import weaver.interfaces.htsc.comInfo.PropBean;
import weaver.interfaces.htsc.itcglc.util.WorkflowUtil;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;

import java.util.Date;
import java.util.HashMap;
/**
 * @Title: ecology-9
 * @Company: 泛微软件
 * @author: K1810027
 * @version: 1.0
 * @CreateDate: 2022-08-30 10:26
 * @Description: 这个action是用来触发通用采购任务单的，明细表4有几个就触发几条，创建人是当前的采购负责人
 * @ModifyLog:
 **/

public class TycgsqEndingTriggerWorkflow extends BaseBean implements Action {

    @Override
    public String execute(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";
        RecordSet rs = new RecordSet();
        RecordSet deptRs = new RecordSet();
        RecordSet updateRs = new RecordSet();
        PropBean pb = new PropBean();
        String tycgrwdWorkflowid = pb.getPropName("tycgrwd_workflowid");
        String tycgrwdTablename = pb.getPropName("tycgrwd_tablename");
        try{
            String mainSql = "select * from " + tablename + " where requestid = "+requestid;
            rs.execute(mainSql);
            rs.next();
            //拟稿人
            String ngr = rs.getString("ngr");
            //项目名称
            String xmmcoa = rs.getString("xmmcoa");
            String id = rs.getString("id");
            //项目编号
            String xmbh = rs.getString("xmbh");
            //项目负责人
            String xmfzr = rs.getString("xmfzr");
            String szbm = rs.getString("szbm");
            //联系方式
            String lxfs = rs.getString("lxfs");
            //流程编号
            String lcbh = rs.getString("lcbh");
            String lxlx = rs.getString("lxlx");
            String xgcglc = Util.null2String(rs.getString("xgcglc"));//
//            String selectSql = "select * from "+tablename+"_dt5 where mainid = "+id;
            String itlxsq = Util.null2String(rs.getString("itlxsq"));//

            //立项来源
            String lxly = rs.getString("lxly");
            String zbcysxlc = rs.getString("zbcysxlc");
            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,id);
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
            String format = dateFormat.format(new Date());
            HashMap<String, String> request = new HashMap<>();
            request.put("xmmc",xmmcoa);
            request.put("xmbh",xmbh);
//            request.put("xmfzr",xmfzr);

            request.put("ngrq",format);
            request.put("jjcd","0");
            request.put("szbm",szbm);
            request.put("lxfs",lxfs);
            request.put("flc",requestid);
            request.put("lxly",lxly);
            request.put("lxlx",lxlx);
            request.put("itlxsq",itlxsq);
            request.put("zbcysxlc",zbcysxlc);
            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>" +
                    " <span style=\"float:right;color:#a9a8a6;font-size: 10pt;font-family: Microsoft YaHei;\">" + lcbh + "</span>" +
                    " </div>" +

                    " </div>";
            request.put("itcglc", itcgsq);
            String deptsql = "select * from hrmdepartment where id ="+ szbm;
            deptRs.execute(deptsql);
            deptRs.next();
            String supdepid = deptRs.getString("supdepid");//所在机构
            request.put("szjg", supdepid);
            while(rs.next()){
                HashMap<String, String> map = new HashMap<>();
                map.put("cgzd", Util.null2String(rs.getString("cgzd")));
                map.put("ejcgzd", Util.null2String(rs.getString("cgtk")));
                map.put("cgdbh", Util.null2String(rs.getString("cgdbh")));
                map.put("cglx", Util.null2String(rs.getString("cglx")));
                map.put("cgfs", Util.null2String(rs.getString("cgfs")));
                map.put("ysbm", Util.null2String(rs.getString("ysbm")));
                map.put("ysje", Util.null2String(rs.getString("ysje")));
                map.put("cgnr", Util.null2String(rs.getString("cgnr")));
                map.put("gys", Util.null2String(rs.getString("gys")));
                map.put("bz", Util.null2String(rs.getString("bz")));
                map.put("cgsqgssm", Util.null2String(rs.getString("cgsqgssm")));
                map.put("cgsl", Util.null2String(rs.getString("sl")));
                map.put("cgfzr", Util.null2String(rs.getString("cgfzr")));
                map.put("xmmc", Util.null2String(rs.getString("xmmc")));
                map.put("xmid", Util.null2String(rs.getString("jmid")));
                request.put("ngr",Util.null2String(rs.getString("cgfzr")));
                if(StringUtils.isNotBlank(szbm)){
                    String sql = "select * from hrmdepartmentdefined where deptid = "+szbm;
                    deptRs.execute(sql);
                    if(deptRs.next()){
                        //bmldsh
                        String deptdirectors = deptRs.getString("deptdirectors");
                        if(!deptdirectors.contains(",") && StringUtils.isNotBlank(deptdirectors)){
                            request.put("bmldsh", deptdirectors);
                        }
                    }
                }
                String requestName = requestname + "的"+rs.getString("cgnr")+"等采购任务单";
                request.put("bt",requestName);
                WorkflowUtil workflowUtil = new WorkflowUtil();
                String triggerRequestid = workflowUtil.WorkflowCreateByRequestMap(Util.null2String(rs.getString("cgfzr")), tycgrwdWorkflowid, requestName, "0", request);
//                System.out.println(triggerRequestid);
                if(StringUtils.isNotBlank(triggerRequestid) && Integer.parseInt(triggerRequestid)>0){
                    String updateSql = "update "+tycgrwdTablename +" set xmfzr = '"+xmfzr+"',tycglc = '"+requestid+"',tycgsqmc = '"+requestname+"',tycgsqlcbh = '"+lcbh+"' where requestid = "+triggerRequestid;
                    updateRs.execute(updateSql);
                    insertDt5(triggerRequestid,map,tycgrwdTablename);
                }
            }
        }catch (Exception e){
        }
        return Action.SUCCESS;
    }

    public void insertDt5(String triggerRequestid, HashMap<String, String> map, String tycgrwdTablename) {
        RecordSet rs = new RecordSet();
        String sql = "select * from " +tycgrwdTablename + " where requestid = "+triggerRequestid;
        rs.execute(sql);
        if(rs.next()){
            String id = rs.getString("id");
            String insertSql = "insert into "+tycgrwdTablename+"_dt5 (cgzd,ejcgzd,cgdbh,cglx,cgfs,ysbm,ysje,cgnr,gys,bz,cgsqgssm,sl,cgfzr,xmmc,xmid,mainid) values (" +
                    "'"+map.get("cgzd")+"','"+map.get("ejcgzd")+"','"+map.get("cgdbh")+"','"
                    +map.get("cglx")+"','"+map.get("cgfs")+"','"+map.get("ysbm")+"','"+map.get("ysje")+"','"
                    +map.get("cgnr")+"','"+map.get("gys")+"','"+map.get("bz")+"','"+map.get("cgsqgssm")+"','"+map.get("cgsl")
                    +"','"+map.get("cgfzr")+"','"+map.get("xmmc")+"','"+map.get("xmid")+"','"+id+"')";
            JAVATools.writelog("通用采购任务单insertSql"+insertSql);
            rs.execute(insertSql);
        }
    }
}
