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.FieldDataVo;
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;

/**
 * 租赁合同收款超级节点
 * @author lenovo
 *
 */
public class CntractPaymentNode  extends ACommonSuperNode {

	private static final Log log = Log.get(CntractPaymentNode.class);

	protected ICstConfigApi cstConfigApi = (ICstConfigApi) AppContext.getBean("cstConfigApi");

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

	@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};
	}

	@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);
		String summaryId = formDataVo.getColSummary().getId()+"";
		FieldDataVo bipfhjgVo = formDataVo.getFieldData("BIP返回结果");
		String bipfhjg = "";
		if(bipfhjgVo!=null){
			bipfhjg = bipfhjgVo.getStringValue();
		}
		if(bipfhjg.contains("成功")){
			context.success("跳过:"+bipfhjg);
			formDataVo.getNewFieldDataMap().put("BIP返回结果", "跳过:"+bipfhjg);
			return context;
		}
		try {
			LeaseContractParam leaseContractParam = new LeaseContractParam();
			JSONObject jsonObject = leaseContractParam.getCntractPaymentNodeParam(formDataVo,summaryId);
			JSONObject headerer = HeaderUtil.getHeader();
			String url = PropKit.getProp(PropKit.HTPAYBILLSERVLETURL);
			context.setUrl(url);
			context.setRequest(jsonObject.toString());
			JSONObject res = ProtUtil.doPost(jsonObject,url , headerer);
			context.setResponse(res.toString());
//        	判断调用BIP是否返回数据
			if(res.isEmpty()) {
//				没有返回数据则设置调用错误，并返回接口
				context.back("付款单生成失败，请联系管理员处理");
				context.setErrMsg("付款单生成失败，请联系管理员处理");
				formDataVo.getNewFieldDataMap().put("BIP返回结果", "付款单生成失败，请联系管理员处理");
				return context;
			}
			String msg = res.get("msg").toString();
			String  isStatus =  res.get("status").toString();
			if("1".equals(isStatus)) {
//				System.out.println("推送BIP付款单成功");
				log.info("推送BIP付款单成功");
				context.success(msg);
				formDataVo.getNewFieldDataMap().put("BIP返回结果", res.get("msg"));
			}else {
				log.info("推送BIP付款单失败"+res.get("msg").toString());
				context.back(msg);
				context.setErrMsg(msg);
				formDataVo.getNewFieldDataMap().put("BIP返回结果", res.get("msg"));
			}
		} catch (Exception e) {
			log.error("异常："+ e);
			context.back("异常："+ e);
			context.setErrMsg("异常："+ e);
			e.printStackTrace();
		}
		return context;
	}
}
