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;

public class LeaseContractNode  extends ACommonSuperNode {

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

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

	@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()+"";
		String bipfhjg = formDataVo.getFieldData("BIP返回结果").getStringValue();
        if(bipfhjg.contains("成功")){
        	context.success("跳过:"+bipfhjg);
			formDataVo.getNewFieldDataMap().put("BIP返回结果", "跳过:"+bipfhjg);
			return context;
		}
        try {
        	LeaseContractParam leaseContractParam = new LeaseContractParam();
        	JSONObject jsonObject = leaseContractParam.getLeaseContractParam(formDataVo,summaryId);
        	JSONObject headerer = HeaderUtil.getHeader();
        	log.info(headerer.toString());
        	String url = PropKit.getProp(PropKit.LEASEURL);
        	context.setUrl(url);
        	context.setRequest(jsonObject.toString());
        	JSONObject res = ProtUtil.doPost(jsonObject,url , headerer);
        	context.setResponse(res.toString());
			if(res.isEmpty()) {
				context.back("租赁合同创建失败，请联系管理员处理");
				context.setErrMsg("租赁合同创建失败，请联系管理员处理");
				log.info("租赁合同创建失败，请联系管理员处理");
			}else {
				String  isStatus =  res.get("status").toString();
				String msg = res.get("msg").toString();
				if("1".equals(isStatus)) {
					formDataVo.getNewFieldDataMap().put("BIP返回结果", res.get("msg"));
					log.info("推送BIP收款合同成功");
					context.success(msg);
				}else {
					context.back(msg);
					context.setErrMsg(msg);
					log.info("推送BIP收款合同失败:"+res.get("msg").toString());
					formDataVo.getNewFieldDataMap().put("BIP返回结果", "推送BIP收款合同失败:"+res.get("msg").toString());
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			log.error("异常："+ e);
			context.back("异常："+ e);
			context.setErrMsg("异常："+ e);
			formDataVo.getNewFieldDataMap().put("BIP返回结果", "推送BIP收款合同失败:"+e);
		}
		return  context;
    }

}
