package com.customization.htsc.tgpt;

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.GlobatTools.JAVATools;
import weaver.interfaces.htsc.comInfo.PropBean;
import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 触发流程
 */
@CommandDynamicProxy(target = RequestSubmitCmd.class, desc = "流程提交处理业务")
public class TgptSaveDetail  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"));
        PropBean pb = new PropBean();
        String Ehd_workflowid = pb.getPropName("ehd_workflowid");
        JAVATools.writelog("投关平台增加明细表Ehd_workflowid"+Ehd_workflowid);
        String Ehd_nodeid = pb.getPropName("ehd_nodeid");
        JAVATools.writelog("投关平台增加明细表Ehd_nodeid"+Ehd_nodeid);
        String table = pb.getPropName("ehd_tablename");
        JAVATools.writelog("投关平台增加明细表table"+table);
        //执行标准的业务处理
        Map<String, Object> result = nextExecute(targetCommand);
        RequestOperationResultBean resultBean = (RequestOperationResultBean) result.get("data");
        String executeResult = resultBean.getType().name();
        RecordSet rs = new RecordSet();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        RecordSet rs2 = new RecordSet();
        if ("SUCCESS".equals(executeResult) && "submit".equals(src)) {
            if(workflowid.equals(Ehd_workflowid)&&Ehd_nodeid.equals(nodeid) ){
                String sql = "select * from "+table + " where requestid = '"+requestid+"'";
                JAVATools.writelog("投关平台增加明细表sql"+sql);
                rs.execute(sql);
                while(rs.next()){
                    String id = rs.getString("id");
                    String wthf = rs.getString("wthf");
                    String format = simpleDateFormat.format(new Date());
                    String insertSql = "insert into "+table+"_dt4 (mainid,xgxzry,wtda,hfsj) values ('"+id+"','"+operator+"','"+wthf+"','"+format+"')";
                    JAVATools.writelog("投关平台增加明细表insertSql"+insertSql);
                    rs2.execute(insertSql);
                }
            }

        }

        return result;
    }
}

