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

import cn.hutool.log.Log;
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.alibaba.fastjson.JSONObject;
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 OrdinaryContractNode  extends ACommonSuperNode {

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

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

	@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) {
		//进入超级审核节点
		log.info("进入流程"+getNodeId());
		ConfigVo configVo = getContractConfig();
		SuperNodeContext context = new SuperNodeContext();
		context.setNeedSave(true);
		String summaryId = formDataVo.getColSummary().getId()+"";
		try {
			OrdinaryContractParam ordinaryContractParam = new OrdinaryContractParam();
			JSONObject jsonObject = new JSONObject();
			JSONObject res = new JSONObject();
			String typeId = formDataVo.getFieldData("收付款合同属性")!=null?formDataVo.getFieldData("收付款合同属性").getDbValue()+"":"";
//			String typeId = CAP4FormKit.getFieldStrValue(master, "收付款合同属性");
			String typeValue = getEnumManagerNew().getEnumItem(Long.parseLong(typeId)).getValue();
			JSONObject headerer = HeaderUtil.getHeader();
			String url = "";
			if("1".equals(typeValue)) {
				jsonObject = ordinaryContractParam.getOrdinaryPaymentContractParam(formDataVo,summaryId);
				log.info("付款");
				url = PropKit.getProp(PropKit.PAYMENTCONTRACTURL);
				context.setUrl(url);
				context.setRequest(jsonObject.toString());
				res = ProtUtil.doPost(jsonObject, url, headerer);
				context.setResponse(res.toString());
			}else if("2".equals(typeValue)) {
				jsonObject = ordinaryContractParam.getOrdinaryCollectionContractParam(formDataVo,summaryId);
				url = PropKit.getProp(PropKit.RECEIVECONTRACTURL);
				context.setUrl(url);
				context.setRequest(jsonObject.toString());
				log.info("收款");
				res = ProtUtil.doPost(jsonObject, PropKit.getProp(PropKit.RECEIVECONTRACTURL), headerer);
				context.setResponse(res.toString());
			}else{
				log.info("不涉及收付款业务，跳过推送BIP处理");
				context.success("不涉及收付款业务，跳过推送BIP处理");
				formDataVo.getNewFieldDataMap().put("BIP返回结果", "不涉及收付款业务，跳过推送BIP处理");
			}
			if(res.isEmpty()) {
				log.info("一般合同创建失败，请联系管理员处理");
				context.back("一般合同创建失败，请联系管理员处理");
				formDataVo.getNewFieldDataMap().put("BIP返回结果", "一般合同创建失败，请联系管理员处理");
			}else {
				String  isStatus =  res.get("status").toString();
				String msg = res.get("msg").toString();
				if("1".equals(isStatus)) {
					log.info("推送BIP一般合同成功");
					log.info(msg);
					context.success(msg);
					formDataVo.getNewFieldDataMap().put("BIP返回结果", msg);
				}else {
					log.info("推送BIP一般合同失败:"+res.get("msg").toString());
					context.back("推送BIP一般合同失败:"+res.get("msg").toString());
					formDataVo.getNewFieldDataMap().put("BIP返回结果", "推送BIP一般合同失败:"+res.get("msg").toString());
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			log.info( "推送BIP一般合同失败:"+e);
			context.back( "推送BIP一般合同失败:"+e);
			formDataVo.getNewFieldDataMap().put("BIP返回结果",  "推送BIP一般合同失败:"+e);
		}
		return context;
	}
}
