package com.seeyon.apps.jybip.node;

import cn.hutool.log.Log;
import com.seeyon.apps.bip.constants.BipConstants;
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.jync_file.kit.PropKit;
import com.seeyon.apps.jync_file.util.HeaderUtil;
import com.seeyon.apps.jync_file.util.ParamNewUtil;
import com.seeyon.apps.jync_file.util.ParamUtil;
import com.seeyon.apps.jync_file.util.ProtUtil;
import com.seeyon.cap4.form.bean.FormDataMasterBean;
import com.seeyon.ctp.common.AppContext;

public class TongyongshukuanNode extends ACommonSuperNode {
	private static final Log log = Log.get(TongyongshukuanNode.class);
	protected ICstConfigApi cstConfigApi = (ICstConfigApi) AppContext.getBean("cstConfigApi");

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

	@Override
	public String getNodeName() {
		return "OA通用收款同步BIP收款单超级节点";
	}

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

	@Override
	public String getFormParse() {
		return "json";
	}
	public ConfigVo getBipConfig() {
		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 = getBipConfig();
		SuperNodeContext context = new SuperNodeContext();
		context.setNeedSave(true);
		String summaryId = formDataVo.getColSummary().getId()+"";
		try {
			ParamNewUtil paramNewUtil = new ParamNewUtil();
			JSONObject jsonObject = paramNewUtil.getTongyongshoukuanParam(formDataVo,summaryId);
			JSONObject headerer = HeaderUtil.getHeader();
			log.info(headerer.toString());
			String url = PropKit.getProp(PropKit.GENERALBILLURL);
			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("BIP收款单创建失败，请联系管理员处理");
			}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返回结果", res.get("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;
	}
}
