package com.seeyon.apps.src_contract.lease.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.param.LeaseContractParam;
import com.seeyon.apps.src_contract.lease.util.HeaderUtil;
import com.seeyon.apps.src_contract.lease.util.ProtUtil;
import com.seeyon.cap4.form.bean.FormDataMasterBean;
import com.seeyon.ctp.common.AppContext;
import com.seeyon.ctp.common.ctpenumnew.manager.EnumManager;
/**
 * 合同收款超级节点
 * @author lenovo
 *
 */
public class LeaseContractProceedsNode  extends ACommonSuperNode {

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

	@Override
	public String getNodeId() {
		return "leaseContractProceedsNode";
	}
	@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);
		System.out.println("开始处理单据");
		String summaryId = formDataVo.getColSummary().getId()+"";;
		String bipfhjg = formDataVo.getFieldData("BIP返回结果")!=null?formDataVo.getFieldData("BIP返回结果").getStringValue():"";
		if(bipfhjg.contains("成功")){
			context.success("跳过:"+bipfhjg);
			formDataVo.getNewFieldDataMap().put("BIP返回结果", "跳过:"+bipfhjg);
			return context;
		}
		try {
			LeaseContractParam leaseContractParam = new LeaseContractParam();
//			String proceedsTypeId= CAP4FormKit.getFieldStrValue(master, "收款合同类型");
			String proceedsTypeId= formDataVo.getFieldData("收款合同类型")!=null?formDataVo.getFieldData("收款合同类型").getDbValue()+"":"";
			String proceedsType = getEnumManagerNew().getEnumItem(Long.parseLong(proceedsTypeId)).getValue();
			JSONObject jsonObject = new JSONObject();
			if("1".equals(proceedsType)) {
				jsonObject = leaseContractParam.getContractProceedsParam(formDataVo,summaryId);
			}else {
				jsonObject = leaseContractParam.getLeaseContractProceedsParam(formDataVo,summaryId);
			}
			JSONObject headerer = HeaderUtil.getHeader();
			String url = PropKit.getProp(PropKit.LEASEPROCEEDSURL);
			context.setUrl(url);
			context.setRequest(jsonObject.toString());
			JSONObject res = ProtUtil.doPost(jsonObject, url, headerer);
			context.setResponse(res.toString());
			if(!res.isEmpty()) {
				log.info("推送BIP收款合同失败");
				context.back("收款单生成失败，请联系管理员处理");
				formDataVo.getNewFieldDataMap().put("BIP返回结果", "收款单生成失败，请联系管理员处理");
			}
			String msg = res.get("msg").toString();
			String  isStatus =  res.get("status").toString();
			if("1".equals(isStatus)) {
				log.info("推送BIP收款单成功");
				context.success(msg);
				formDataVo.getNewFieldDataMap().put("BIP返回结果",res.get("msg"));
			}else {
				context.back(res.get("msg").toString());
				context.setErrMsg(res.get("msg").toString());
				log.info("推送BIP收款单失败"+res.get("msg").toString());
				formDataVo.getNewFieldDataMap().put("BIP返回结果",res.get("msg"));
			}
		} catch (Exception e) {
			log.error("异常："+ e);
			context.back("异常："+ e);
			context.setErrMsg("异常："+ e);
			e.printStackTrace();
			formDataVo.getNewFieldDataMap().put("BIP返回结果","异常："+ e);
		}
		return context;
	}

}
