package com.seeyon.apps.jync_fz.node;

import java.util.List;
import java.util.Map;

import cn.hutool.log.Log;
import com.seeyon.apps.bip.constants.BipConstants;
import com.seeyon.apps.collaboration.po.ColSummary;
import com.seeyon.apps.common.config.ICstConfigApi;
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.FieldDataVo;
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;
import com.seeyon.ctp.common.ctpenumnew.manager.EnumManager;


public class SheBaoFenZhangNode   extends ACommonSuperNode {

	private static Log log = Log.get(SheBaoFenZhangNode.class);
	protected ICstConfigApi cstConfigApi = (ICstConfigApi) AppContext.getBean("cstConfigApi");
	@Override
	public String getNodeId() {
		return "sheBaoFenZhangNode";
	}
	@Override
	public String getNodeName() {
		return "社保分账审批单超级节点";
	}
	@Override
	public String getPluginId() {
		return BipConstants.getPluginId();
	}
	@Override
	public String getFormParse() {
		return "json";
	}

	@Override
	public WorkFlowType[] getTypes() {
		return new WorkFlowType[] {WorkFlowType.superNode};
	}

//	public ConfigVo getMemberMamageConfig() {
//		return cstConfigApi.getConfig(getPluginId());
//	}

	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 {
//	进入超级审核节点
		SuperNodeContext context = new SuperNodeContext();
		context.setNeedSave(true);
		log.info("开始处理单据sheBaoFenZhangNode");
//    	封装推送接口参数调用接口
		try {
			JSONObject headerer = HeaderUtil.getHeader();
			ParamNewUtil paramNewUtil = new ParamNewUtil();
			ColSummary summary = formDataVo.getColSummary();
			JSONObject jsonObject = paramNewUtil.getSheBaoFenZhangParam(formDataVo,summary.getId()+"");
			context.setRequest(jsonObject.toString());
			if(jsonObject.size() == 0 ) {
				JSONObject endJsonObject = paramNewUtil.getSheBaoFenZhangEndParam(formDataVo);
				context.setRequest(endJsonObject.toString());
				log.info("接口流程结束，参数记录："+endJsonObject);
				String url = PropKit.getProp(PropKit.SBFZCANCELURL);
				context.setUrl(url);
				JSONObject EndRes = ProtUtil.doPost(endJsonObject, url, headerer);
				log.info("接口流程结束，返回值记录："+EndRes);
				if(EndRes.isEmpty() || !"2".equals(EndRes.getString("status"))) {
					context.setResponse(EndRes.toString());
					context.back("流程状态改变失败");
					context.setErrMsg("流程状态改变失败");
					return context;
				}
			}
			String url = PropKit.getProp(PropKit.TRANSFORMURL);
//			String url = PropKit.getProp(PropKit.SBFZCANCELURL);
			context.setUrl(url);
			log.info("接口流程结束，参数记录："+jsonObject);
			JSONObject res = ProtUtil.doPost(jsonObject,url , headerer);
			log.info("接口流程结束，返回值记录："+res);
			context.setResponse(res.toString());
			if(res == null) {
				context.back("接口调用失败，未正常返回结果");
				context.setErrMsg("接口调用失败，未正常返回结果");
				formDataVo.getNewFieldDataMap().put("BIP返回结果", "推送BIP失败");
				return context;
			}
			String  isStatus =  res.get("status").toString();
			String  msg =  res.get("msg").toString();
			if("1".equals(isStatus)) {
				context.success(msg);
				Map<String, List<FormDataVo>> subFormMaps = formDataVo.getSubFormMap();
				List<FormDataVo> subFormMap = subFormMaps.get("分账明细表");
				String[] nos = msg.split("【");
				FieldDataVo isbearidVo = formDataVo.getFieldData("支付公司是否成本承担公司");
				String isbearid = "";
				if(isbearidVo != null){
					isbearid = isbearidVo.getStringValue();
				}
				String isbear = "";
				if("".equals(isbearid)){
					isbear = "否";
				}else {
					isbear =  isbearid;
				}
				FieldDataVo isunitidVo = formDataVo.getFieldData("是否同一收款单位");
				String isunitid = "";
				if(isunitidVo!=null){
					isunitid = isunitidVo.getStringValue();
				}
				String isunit = "";
				if("".equals(isunit)){
					isunit = "是";
				}else {
					isunit =  isunitid;
				}
				if(!("是".equals(isunit) && "否".equals(isbear))){
					int i = 0;
					for (FormDataVo subFormDataVo : subFormMap) {
						i++;
						String[] bipNo = nos[i].split("BIP单据号:");
						subFormDataVo.getNewFieldDataMap().put("BIP单据号", bipNo[1].substring(0,bipNo[1].length()-3));
					}
				}
				formDataVo.getNewFieldDataMap().put("BIP返回结果", "推送BIP成功:"+msg);
				JSONObject endJsonObject = paramNewUtil.getSheBaoFenZhangEndParam(formDataVo);
				log.info("接口流程结束，参数记录："+endJsonObject);
				String endurl = PropKit.getProp(PropKit.SBFZCANCELURL);
				JSONObject EndRes = ProtUtil.doPost(endJsonObject, endurl, headerer);
				log.info("接口流程结束，返回值记录："+EndRes);
				context.setResponse(res.toString());
				if(EndRes.isEmpty() || !"2".equals(EndRes.getString("status"))){
					context.back("流程状态改变失败");
					context.setErrMsg("流程状态改变失败");
				}
			}else {
				context.back(msg);
				context.setErrMsg(msg);
			}
		} catch (Exception e) {
			log.error(e.getMessage(),e);
			context.back("异常："+e.getMessage());
			context.setErrMsg("异常："+e.getMessage());
		}
		return context;


	}

}