package com.seeyon.apps.src_contract.ordinary.node;

import cn.hutool.log.Log;
import com.alibaba.fastjson.JSONObject;
import com.seeyon.apps.common.config.ICstConfigApi;
import com.seeyon.apps.common.plugin.vo.ConfigVo;
import com.seeyon.apps.common.workflow.constants.WorkFlowType;
import com.seeyon.apps.common.workflow.node.ACommonSuperNode;
import com.seeyon.apps.ext.workflow.vo.FormDataVo;
import com.seeyon.apps.ext.workflow.vo.SuperNodeContext;
import com.seeyon.apps.src_contract.lease.constants.ContractConstants;
import com.seeyon.apps.src_contract.lease.kit.PropKit;
import com.seeyon.apps.src_contract.lease.util.HeaderUtil;
import com.seeyon.apps.src_contract.lease.util.ProtUtil;
import com.seeyon.apps.src_contract.ordinary.param.OrdinaryContractParam;
import com.seeyon.cap4.form.bean.FormDataMasterBean;
import com.seeyon.ctp.common.AppContext;
import com.seeyon.ctp.common.ctpenumnew.manager.EnumManager;

public class OrdinaryStopContractNode  extends ACommonSuperNode {

	private static final Log log = Log.get(OrdinaryStopContractNode.class);
	protected ICstConfigApi cstConfigApi = (ICstConfigApi) AppContext.getBean("cstConfigApi");

	@Override
	public String getNodeId() {
		return "ordinaryStopContractNode";
	}

	@Override
	public String getNodeName() {
		return "OA一般合同终止BIP合同超级节点";
	}

	@Override
	public String getPluginId() {
		return ContractConstants.getPluginId();
	}

	@Override
	public String getFormParse() {
		return "json";
	}
	public ConfigVo getContractConfig() {
		return cstConfigApi.getConfig(getPluginId());
	}

	@Override
	public WorkFlowType[] getTypes() {
		return new WorkFlowType[] {WorkFlowType.superNode};
	}

	private  EnumManager enumManagerNew;
	
	public void setEnumManagerNew(EnumManager enumManagerNew) {
		this.enumManagerNew = enumManagerNew;
	}
	
	public EnumManager getEnumManagerNew() {
		if (enumManagerNew == null) {
			enumManagerNew = (EnumManager) AppContext.getBean("enumManagerNew");
		}
		return enumManagerNew;
	}

	@Override
	public SuperNodeContext proceed(String request, FormDataVo formDataVo, FormDataMasterBean formDataMasterBean) throws Exception {
		//进入超级审核节点
		log.info("进入流程"+getNodeId());
		ConfigVo configVo = getContractConfig();
		SuperNodeContext context = new SuperNodeContext();
		context.setNeedSave(true);
		try {
			OrdinaryContractParam ordinaryContractParam = new OrdinaryContractParam();
			JSONObject headerer = HeaderUtil.getHeader();
//			String ordinaryid = CAP4FormKit.getFieldStrValue(master, "合同类型");
			String ordinaryid = formDataVo.getFieldData("合同类型")!=null?formDataVo.getFieldData("合同类型").getDbValue()+"":"";
			String ordinary = getEnumManagerNew().getEnumItem(Long.parseLong(ordinaryid)).getValue();
			JSONObject res = new JSONObject();
			if("0".equals(ordinary)) {
				String url = PropKit.getProp(PropKit.TERMINATIONCONTRACTURL);
				context.setUrl(url);
				JSONObject jsonObject = ordinaryContractParam.getOrdinaryContractSKStopParam(formDataVo);
				context.setRequest(jsonObject.toString());
				res = ProtUtil.doPost(jsonObject,url, headerer);
				context.setResponse(res.toString());
			}else if ("1".equals(ordinary)) {
				String url = PropKit.getProp(PropKit.TERMINATIONFKCONTRACTURL);
				JSONObject jsonObject = ordinaryContractParam.getOrdinaryContractFKStopParam(formDataVo);
				context.setUrl(url);
				context.setRequest(jsonObject.toString());
				res = ProtUtil.doPost(jsonObject,url , headerer);
				context.setResponse(res.toString());
			}
			if(res.isEmpty()) {
				log.error("一般合同终止失败，请联系管理员处理");
				context.back("一般合同终止失败，请联系管理员处理");
				context.setErrMsg("一般合同终止失败，请联系管理员处理");
			}else {
				String  isStatus =  res.get("status").toString();
				String msg = res.get("msg").toString();
				if("1".equals(isStatus)) {
					log.info("推送BIP一般合同终止成功");
					context.success(msg);
					formDataVo.getNewFieldDataMap().put("BIP返回结果",msg);
				}else {
					log.info("推送BIP一般合同终止失败:"+res.get("msg").toString());
					context.back(msg);
					formDataVo.getNewFieldDataMap().put("BIP返回结果", "推送BIP一般合同终止失败:"+res.get("msg").toString());
				}
			}
		} catch (Exception e) {
			log.error("异常："+ e);
			context.back("异常："+ e);
			e.printStackTrace();
			formDataVo.getNewFieldDataMap().put("BIP返回结果", "推送BIP一般合同终止失败:"+e);
		}
		return context;
	}
}
