package com.customization.htxc.zbhb;

import com.engine.core.cfg.annotation.CommandDynamicProxy;
import com.engine.core.interceptor.AbstractCommandProxy;
import com.engine.core.interceptor.Command;
import com.engine.workflow.cmd.requestForm.RequestSubmitCmd;
import com.engine.workflow.entity.requestForm.RequestOperationResultBean;
import weaver.conn.RecordSet;
import weaver.conn.RecordSetTrans;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.interfaces.htsc.comInfo.PropBean;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;
import java.util.Objects;

/**
 * 将招标请示的数据保存到采购项目明细表一
 */
@CommandDynamicProxy(target = RequestSubmitCmd.class, desc = "流程提交处理业务")
public class InsertCgsqxmDt1Cmd extends AbstractCommandProxy<Map<String, Object>> {

    @Override
    public Map<String, Object> execute(Command<Map<String, Object>> targetCommand) {
        RequestSubmitCmd requestSubmitCmd = (RequestSubmitCmd) targetCommand;
        HttpServletRequest request = requestSubmitCmd.getRequest();//从request中可以取到参数，不用以前的方法了
        String requestid = Util.null2String(request.getParameter("requestid"));
        String workflowid = Util.null2String(request.getParameter("workflowid"));
        String nodetype = Util.null2String(request.getParameter("nodetype"));
        String nodeid = Util.null2String(request.getParameter("nodeid"));
        String src = Util.null2String(request.getParameter("src"));
        String remark = Util.HTMLtoTxt(Util.null2String(request.getParameter("remark"))).trim();
        String operator = Util.null2String(request.getParameter("f_weaver_belongto_userid"));
        String requestname = Util.null2String(request.getParameter("requestname"));
        BaseBean bb = new BaseBean();
        bb.writeLog("招标汇报拟稿src"+src);
        bb.writeLog("招标汇报拟稿requestname"+requestname);
        //执行标准的业务处理
        Map<String, Object> result = nextExecute(targetCommand);
        try{

            RequestOperationResultBean resultBean = (RequestOperationResultBean) result.get("data");
            String executeResult = resultBean.getType().name();
            RecordSet rs = new RecordSet();
            String sql = "select * from workflow_base where id = "+workflowid;
            rs.execute(sql);
            rs.next();
            String unnumber = rs.getString("unnumber");
            bb.writeLog("招标汇报拟稿unnumber"+unnumber);
            PropBean pb = new PropBean();
            String zbqsTable = pb.getPropName("zbhb_table");
            String nodeUnNumber = pb.getNodeUnNumberByid(nodeid);
            bb.writeLog("招标汇报拟稿nodeUnNumber"+nodeUnNumber);
            String zbqsNg = pb.getPropName("zbhb_ng");
            bb.writeLog("招标汇报拟稿zbqs_ng"+zbqsNg);
            if("SUCCESS".equals(executeResult) && "submit".equals(src)){
                if(Objects.equals("zbhb",unnumber)  && Objects.equals(nodeUnNumber,zbqsNg)){
                    RecordSetTrans rst = new RecordSetTrans();
                    String mainSql = "select * from " + zbqsTable+" where requestid = "+requestid;
                    rs.execute(mainSql);
                    if(rs.next()){
                        String xmid = rs.getString("jmid");
                        String ngr = rs.getString("ngr");
                        String szbm = rs.getString("szbm");
                        String ngrq = rs.getString("ngrq");
                        bb.writeLog("招标汇报拟稿jmid"+xmid);
                        String selectSql = "select * from uf_cgxq_dt1 where lcmc = "+requestid;
                        rs.execute(selectSql);
                        if(!rs.next()){
                            RecordSet rs1 = new RecordSet();
                            String selectByRequsetId = "select * from uf_cgxq_dt1 where lcmc = ?";
                            rs1.executeQuery(selectByRequsetId,requestid);
                            if(!rs1.next()) {
                                String dt1Sql = "insert into uf_cgxq_dt1 (mainid,lcmc,lclx,lczt,sqr,sqbm,sqsj)values('" + xmid + "','" + requestid + "','" + 1 + "','" + 0 + "','" + ngr + "','" + szbm + "','" + ngrq + "')";
                                bb.writeLog("招标汇报拟稿dt1Sql" + dt1Sql);
                                try {
                                    rst.execute(dt1Sql);
                                } catch (Exception e) {
                                    e.printStackTrace();
                                }
                            }
                        }
                    }
                }
            }

        }catch (Exception e){

        }
        return result;
    }
}

