package com.seeyon.apps.src_contract.lease.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.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.alibaba.fastjson.JSONObject;
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 UpdateLeaseContractNode extends ACommonSuperNode {

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

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

	@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 = CAP4FormKit.getFieldStrValue(master, "BIP返回结果");
		String bipfhjg = formDataVo.getFieldData("BIP返回结果")!=null?formDataVo.getFieldData("BIP返回结果").getStringValue():"";
		if(bipfhjg.contains("成功")){
			context.success("跳过:"+bipfhjg);
			formDataVo.getNewFieldDataMap().put("BIP返回结果", "跳过:"+bipfhjg);
			return context;
		}
		try {
			JSONObject jsonObject = LeaseContractParam.getUpdateLeaseContractParam(formDataVo,summaryId);
			JSONObject headerer = HeaderUtil.getHeader();
			log.info(headerer.toString());
			String url = PropKit.getProp(PropKit.UPDATELEASEURL);
			context.setUrl(url);
			context.setRequest(jsonObject.toString());
			JSONObject res = ProtUtil.doPost(jsonObject,url , headerer);
			context.setResponse(res.toString());
			String  isStatus =  res.get("status").toString();
			if("1".equals(isStatus)) {
				System.out.println("推送BIP收款合同成功");
				log.info("推送BIP收款合同成功");
			}else {
				String msg = res.get("msg").toString();
				context.back(msg);
				context.setErrMsg(msg);
				log.info("推送BIP收款合同失败"+res.get("msg").toString());
			}
		} catch (Exception e) {
			log.error("异常："+ e);
			e.printStackTrace();
			context.back("异常："+ e);
			context.setErrMsg("异常："+ e);

		}
		return context;
	}
}
