package com.seeyon.apps.src_contract.lease.node;

import java.util.Map;

import com.seeyon.apps.src_contract.lease.param.LeaseContractParam;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.alibaba.fastjson.JSONObject;
import com.seeyon.apps.src_contract.lease.kit.PropKit;
import com.seeyon.apps.src_contract.lease.kit.form.CAP4FormKit;
import com.seeyon.apps.src_contract.lease.util.HeaderUtil;
import com.seeyon.apps.src_contract.lease.util.ProtUtil;
import com.seeyon.cap4.form.api.FormDataApi4Cap4;
import com.seeyon.cap4.form.bean.FormDataMasterBean;
import com.seeyon.ctp.common.AppContext;
import com.seeyon.ctp.common.ctpenumnew.manager.EnumManager;
import com.seeyon.ctp.common.exceptions.BusinessException;
import com.seeyon.ctp.workflow.supernode.BaseSuperNodeAction;
import com.seeyon.ctp.workflow.supernode.SuperNodeResponse;
import com.seeyon.ctp.workflow.supernode.enums.SuperNodeEnums.RunAction;
import com.seeyon.v3x.services.form.FormFactory;

public class LeaseContractTerminationNode  extends BaseSuperNodeAction {

	private static final Log log = LogFactory.getLog(LeaseContractTerminationNode.class);
	private FormFactory formFactory;

	public FormFactory getFormFactory() {
		if (formFactory == null) {
			formFactory = (FormFactory) AppContext.getBean("formFactory");
		}
		return formFactory;
	}
	protected FormDataApi4Cap4 formDataApi4Cap4;

	public void setFormDataApi4Cap4(FormDataApi4Cap4 formDataApi4Cap4) {
		this.formDataApi4Cap4 = formDataApi4Cap4;
	}

	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 void cancelAction(String token, String activityId, Map<String, Object> params) throws BusinessException {
	}
	/**
	 * 节点动作撤销。
	 */
	public void cancelAction(String token, String activityId) {
		System.out.println("撤销");
	}

	@Override
	public SuperNodeResponse confirmAction(String token, String activityId, Map<String, Object> params)
			throws BusinessException {
		return executeAction(token, activityId, params);
	}

	@Override
	public SuperNodeResponse executeAction(String token, String activityId, Map<String, Object> params)
			throws BusinessException {
		//进入超级审核节点
		SuperNodeResponse response = new SuperNodeResponse();
		System.out.println("开始处理单据");
		Map<String, Object> data = getFormData(params);
		FormDataMasterBean master = (FormDataMasterBean) data.get("formDataBean");
		String bipfhjg = CAP4FormKit.getFieldStrValue(master, "BIP返回结果");
		if (bipfhjg.contains("推送BIP合同终止状态成功")) {
			CAP4FormKit.setCellValue(master, "BIP返回结果", "跳过:" + bipfhjg);
			response.setReturnCode(RunAction.FORWARD.getKey());
			response.setSuccess(true);
			response.setReturnMsg("跳过:" + bipfhjg);
			response.setAddComment(false);
			return response;
		}
		try {
			String contractType =  CAP4FormKit.getFieldStrValue(master, "合同类型文本");
			LeaseContractParam leaseContractParam = new LeaseContractParam();
			JSONObject headerer = HeaderUtil.getHeader();
//			System.out.println(headerer);
			JSONObject res = new JSONObject();
			if("租赁合同".equals(contractType)) {
				JSONObject jsonObject = leaseContractParam.getLeaseContractTerminationParam(master);
				res = ProtUtil.doPost(jsonObject, PropKit.getProp(PropKit.LEASRTERMINATIONURL), headerer);
			}else if ("一般付款合同".equals(contractType)){
				JSONObject jsonObject = leaseContractParam.getfkContractTerminationParam(master);
				res = ProtUtil.doPost(jsonObject, PropKit.getProp(PropKit.LEASRTERMINATIONFKURL), headerer);
			}else if("一般合同".equals(contractType)) {
				JSONObject jsonObject = leaseContractParam.getskContractTerminationParam(master);
				res = ProtUtil.doPost(jsonObject, PropKit.getProp(PropKit.LEASRTERMINATIONURL), headerer);
			}else{
				response.setReturnCode(RunAction.BACK.getKey());
				response.setSuccess(false);
				response.setReturnMsg("请检查数据是否正确");
				response.setAddComment(false);
				log.info("请检查数据是否正确");
				return response;
			}
			if(res.isEmpty()) {
				response.setReturnCode(RunAction.BACK.getKey());
				response.setSuccess(false);
				response.setReturnMsg("合同终止状态失败，请联系管理员处理");
				response.setAddComment(false);
				log.info("合同终止状态失败，请联系管理员处理");
			}else {
				String  isStatus =  res.get("status").toString();
				String msg = res.get("msg").toString();
				if("1".equals(isStatus)) {
					CAP4FormKit.setCellValue(master, "BIP返回结果", "推送BIP合同终止状态成功:"+res.get("msg"));
//					System.out.println("推送BIP合同终止状态成功");
					log.info("推送BIP合同终止状态成功");
					response.setReturnCode(RunAction.FORWARD.getKey());
					response.setSuccess(true);
					response.setReturnMsg(msg);
					response.setAddComment(false);
				}else {
					CAP4FormKit.setCellValue(master, "BIP返回结果", "推送BIP合同终止状态失败:"+res.get("msg").toString());
					response.setReturnCode(RunAction.BACK.getKey());
					response.setSuccess(false);
					response.setReturnMsg(msg);
					response.setAddComment(false);
					log.info("推送BIP合同终止状态失败:"+res.get("msg").toString());
				}
			}
		} catch (Exception e) {
			log.error("异常："+ e);
			response.setReturnCode(RunAction.BACK.getKey());
			response.setSuccess(false);
			response.setReturnMsg("异常："+ e);
			response.setAddComment(false);
			CAP4FormKit.setCellValue(master, "BIP返回结果", "推送BIP合同终止状态失败:"+e);
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		formDataApi4Cap4.insertOrUpdateMasterData(master, false);
		return response;
	}

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

	@Override
	public String getNodeName() {
		return "OA合同终止同步BIP合同终止状态超级节点";
	}

	@Override
	public int getOrder() {
		return 0;
	}

	/**
	 * 获取表单数据
	 * @param params
	 * @return
	 */
	@SuppressWarnings("unchecked")
	private Map<String, Object> getFormData(Map<String, Object> params) {
		return (Map<String, Object>) params.get("CTP_FORM_DATA");
	}


//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.alibaba.fastjson.JSONObject;
//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;
//
//public class LeaseContractTerminationNode  extends ACommonSuperNode {
//
//	private static final Log log = Log.get(LeaseContractTerminationNode.class);
//	protected ICstConfigApi cstConfigApi = (ICstConfigApi) AppContext.getBean("cstConfigApi");
//	@Override
//	public String getNodeId() {
//		return "leaseContractTerminationNode";
//	}
//	@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);
////		String bipfhjg = CAP4FormKit.getFieldStrValue(master, "BIP返回结果");
//		String bipfhjg = formDataVo.getFieldData("BIP返回结果")!=null?formDataVo.getFieldData("BIP返回结果").getStringValue():"";
//		if(bipfhjg.contains("推送BIP合同终止状态成功")){
//			context.success("跳过:"+bipfhjg);
//			formDataVo.getNewFieldDataMap().put("BIP返回结果", "跳过:"+bipfhjg);
//			return context;
//		}
//		try {
////			String contractType =  CAP4FormKit.getFieldStrValue(master, "合同类型文本");
//			String contractType =  formDataVo.getFieldData("合同类型文本")!=null?formDataVo.getFieldData("合同类型文本").getStringValue():"";
//			LeaseContractParam leaseContractParam = new LeaseContractParam();
//			JSONObject headerer = HeaderUtil.getHeader();
//			System.out.println(headerer);
//			JSONObject res = new JSONObject();
//			String url = "";
//			if("租赁合同".equals(contractType)) {
//				url = PropKit.getProp(PropKit.LEASRTERMINATIONURL);
//				context.setUrl(url);
//				JSONObject jsonObject = leaseContractParam.getLeaseContractTerminationParam(formDataVo);
//				context.setRequest(jsonObject.toString());
//				res = ProtUtil.doPost(jsonObject, url, headerer);
//				context.setResponse(res.toString());
//			}else if ("一般付款合同".equals(contractType)){
//				url = PropKit.getProp(PropKit.LEASRTERMINATIONFKURL);
//				context.setUrl(url);
//				JSONObject jsonObject = leaseContractParam.getfkContractTerminationParam(formDataVo);
//				context.setRequest(jsonObject.toString());
//				res = ProtUtil.doPost(jsonObject, url, headerer);
//				context.setResponse(res.toString());
//			}else if("一般合同".equals(contractType)) {
//				url = PropKit.getProp(PropKit.LEASRTERMINATIONURL);
//				context.setUrl(url);
//				JSONObject jsonObject = leaseContractParam.getskContractTerminationParam(formDataVo);
//				context.setRequest(jsonObject.toString());
//				res = ProtUtil.doPost(jsonObject, url, headerer);
//				context.setResponse(res.toString());
//			}else{
//				log.info("请检查数据是否正确");
//				context.back("请检查数据是否正确");
//				context.setResponse("请检查数据是否正确");
//				context.setErrMsg("请检查数据是否正确");
//				return context;
//			}
//			if(res.isEmpty()) {
//				log.info("合同终止状态失败，请联系管理员处理");
//				context.back("合同终止状态失败，请联系管理员处理");
//				context.setResponse("合同终止状态失败，请联系管理员处理");
//				context.setErrMsg("合同终止状态失败，请联系管理员处理");
//			}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返回结果", "推送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) {
//			e.printStackTrace();
//			log.info("异常："+ e);
//			context.back("异常："+ e);
//			formDataVo.getNewFieldDataMap().put("BIP返回结果", "推送BIP合同终止状态失败:"+e);
//		}
//		return context;
//	}

}
