package com.customization.htxc.swtp;

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.general.Util;
import weaver.interfaces.htsc.comInfo.PropBean;

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

/**
 * 如果流程被退回拟稿，则将采购项目dt1中的流程状态置为已终止（2）
 */
@CommandDynamicProxy(target = RequestSubmitCmd.class, desc = "流程提交处理业务")
public class StopUpdateStatus 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"));
        //执行标准的业务处理
        Map<String, Object> result = nextExecute(targetCommand);
        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();
        PropBean pb = new PropBean();
        String zbqsTable = pb.getPropName("swtp_table");
        String unnumber = rs.getString("unnumber");
        sql = "select * from workflow_requestbase where requestid = "+requestid;
        rs.execute(sql);
        rs.next();
        String isstopped = rs.getString("isstopped");
        if("SUCCESS".equals(executeResult) && "1".equals(isstopped)){
            if(Objects.equals("swtp",unnumber) ){
                String mainSql = "select * from " + zbqsTable+" where requestid = "+requestid;
                rs.execute(mainSql);
                if(rs.next()){
                    String xmid = rs.getString("jmid");
                    String upsql = "update uf_cgxq_dt1 set lczt = 2 where mainid = "+xmid + " and lcmc = "+ requestid;
                    rs.execute(upsql);
                }
            }
        }
        return result;
    }
}
