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.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.ProtUtil;
import com.seeyon.cap4.form.bean.FormDataMasterBean;
import com.seeyon.ctp.common.AppContext;

public class XinChouFenZhangNode   extends ACommonSuperNode {

//	private static final Log log = LogFactory.getLog(XinChouFenZhangNode.class);
	private static Log log = Log.get(XinChouFenZhangNode.class);
	protected ICstConfigApi cstConfigApi = (ICstConfigApi) AppContext.getBean("cstConfigApi");
	@Override
	public String getNodeId() {
		return "xinChouFenzhangNode";
	}
	@Override
	public String getNodeName() {
		return "薪酬分账审批单超级节点";
	}
	@Override
	public String getPluginId() {
		return BipConstants.getPluginId();
	}
	@Override
	public String getFormParse() {
		return "json";
	}
//	public ConfigVo getMemberMamageConfig() {
//		return cstConfigApi.getConfig(getPluginId());
//	}

	@Override
	public WorkFlowType[] getTypes() {
		return new WorkFlowType[] {WorkFlowType.superNode};
	}
	@Override
	public SuperNodeContext proceed(String request, FormDataVo formDataVo, FormDataMasterBean formDataMasterBean) {
		//进入超级审核节点
//		SuperNodeResponse response = new SuperNodeResponse();
		SuperNodeContext context = new SuperNodeContext();
		context.setNeedSave(true);
		System.out.println("开始处理单据XinChouFenZhangNode");
//    	封装推送接口参数调用接口
		try {
			JSONObject headerer = HeaderUtil.getHeader();
			ParamNewUtil paramNewUtil = new ParamNewUtil();
			ColSummary summary = formDataVo.getColSummary();
			JSONObject jsonObject = paramNewUtil.getXinChouFenZhangParam(formDataVo,summary.getId()+"");
			context.setRequest(jsonObject.toString());
			if(jsonObject.size()==0) {
				JSONObject endJsonObject = paramNewUtil.getXinChouFenZhangEndParam(formDataVo);
				log.info("接口流程结束，参数记录："+endJsonObject);
				JSONObject EndRes = ProtUtil.doPost(endJsonObject, PropKit.getProp(PropKit.XCFZCANCELURL), headerer);
				if(EndRes.isEmpty()) {
					context.back("接口调用异常，流程状态改变失败");
					context.setErrMsg("接口调用异常，流程状态改变失败");
				}
				String status = EndRes.getString("status");
				if(!"1".equals(status)) {
					context.back("流程状态改变失败："+EndRes.get("msg").toString());
					context.setErrMsg("流程状态改变失败："+EndRes.get("msg").toString());
				}else {
					context.success("流程状态改变成功");
				}
				return context;
			}
			JSONObject res = ProtUtil.doPost(jsonObject, PropKit.getProp(PropKit.TRANSFORMURL), headerer);
			if(res==null) {
				context.back("接口调用失败，未正常返回结果");
				context.setErrMsg("接口调用失败，未正常返回结果");
				return context;
			}
			String  isStatus =  res.get("status").toString();
			String  msg =  res.get("msg").toString();
			if("1".equals(isStatus)) {
				context.success("推送BIP成功:"+msg);

				Map<String, List<FormDataVo>> subFormMaps = formDataVo.getSubFormMap();
				List<FormDataVo> subFormMap = subFormMaps.get("分账明细表");
				String[] nos = msg.split("【");
				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.getXinChouFenZhangEndParam(formDataVo);
				log.info("接口流程结束，参数记录："+endJsonObject);
				JSONObject EndRes = ProtUtil.doPost(endJsonObject, PropKit.getProp(PropKit.XCFZCANCELURL), headerer);
				if(EndRes.isEmpty()) {
					context.back("流程状态改变失败");
					context.setErrMsg("流程状态改变失败");
					formDataVo.getNewFieldDataMap().put("BIP返回结果", "流程状态改变失败");
				}
				String status = EndRes.getString("status");
				if(!"1".equals(status)) {
					context.back("流程状态改变失败"+EndRes.getString("msg"));
					context.setErrMsg("流程状态改变失败"+EndRes.getString("msg"));
					formDataVo.getNewFieldDataMap().put("BIP返回结果", "流程状态改变失败"+EndRes.getString("msg"));
				}else {
					context.success(msg);
				}
			}else {
				context.back(msg);
				context.setErrMsg(msg);
				formDataVo.getNewFieldDataMap().put("BIP返回结果", msg);
			}
		} catch (Exception e) {
			e.printStackTrace();
			context.back("异常："+e);
			context.setErrMsg("异常："+e);
			formDataVo.getNewFieldDataMap().put("BIP返回结果", "异常："+e);
		}
		return context;
	}

}
