package com.customization.htxc.contract;

import com.engine.core.cfg.annotation.CommandDynamicProxy;
import com.engine.core.interceptor.AbstractCommandProxy;
import com.engine.core.interceptor.Command;
import com.engine.htxc.commonutil.log.CustomizeLog;
import com.engine.htxc.contract.service.impl.ContractServiceImpl;
import com.engine.workflow.cmd.requestForm.RequestSubmitCmd;
import com.engine.workflow.entity.requestForm.RequestOperationResultBean;
import org.apache.commons.lang3.StringUtils;
import weaver.general.Util;
import weaver.interfaces.htsc.comInfo.PropBean;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>总部合同审批CMD类</p>
 * 监听拟稿节点数据，保存将requestid插入建模
 * @author K1810015
 * @version 1.0.0
 * @ClassName ContractApprovalSaveCmd.java
 * @createTime 2022年08月31日 23:26:00
 */
@CommandDynamicProxy(target = RequestSubmitCmd.class, desc = "总部合同报销触发")
public class ContractApprovalSaveCmd extends AbstractCommandProxy<Map<String, Object>> {

    @Override
    public Map<String, Object> execute(Command<Map<String, Object>> command) {
        RequestSubmitCmd requestSubmitCmd = (RequestSubmitCmd) command;
        HttpServletRequest request = requestSubmitCmd.getRequest();
        PropBean pp = new PropBean();
        //总部审批流程 workflowid
        String approvalWorkflowId = pp.getPropName("constract.approval.workflowid");
        //获取代理对象
        String requestid = Util.null2String(request.getParameter("requestid"));
        String workflowid = Util.null2String(request.getParameter("workflowid"));
        String nodetype = Util.null2String(request.getParameter("nodetype"));
        String src = Util.null2String(request.getParameter("src"));
        //执行标准的业务处理
        Map<String, Object> result = nextExecute(command);
        RequestOperationResultBean resultBean = (RequestOperationResultBean) result.get("data");
        //流程处理结果
        String type = Util.null2String(resultBean.getType());
        //判断为拟稿节点
        if (StringUtils.isNotBlank(approvalWorkflowId)
                && workflowid.equals(approvalWorkflowId)
                && "0".equals(nodetype)) {
            CustomizeLog.printLog("查看参数："+type+"--"+src
            );
            if ("SUCCESS".equalsIgnoreCase(type) && "save".equalsIgnoreCase(src)) {
                 //保存 终止
                    String code = "";
                    CustomizeLog.printLog("合同审批流程创建节点");
                    if ("save".equalsIgnoreCase(src)) {
                        //保存的requestid未生成
                        Map<String, Object> submitParams = resultBean.getSubmitParams();
                        if ("-1".equals(requestid)) {
                            requestid = Util.null2String(submitParams.get("requestid"));
                        }
                        CustomizeLog.printLog("合同审批流程创建节点参数"+requestid);
                        if(Util.getIntValue(requestid)>0){
                            Map<String,Object> linkmap=new HashMap<>(1);
                            linkmap.put("requestid",requestid);
                            new ContractServiceImpl().linkMode(null,linkmap);
                        }

                    }
            }
        }
        return result;
    }
}
