package com.seeyon.apps.jync_fz.node;

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

import cn.hutool.log.Log;
import com.alibaba.fastjson.JSONObject;
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.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.ProtUtil;
import com.seeyon.cap4.form.bean.FormDataMasterBean;
import com.seeyon.ctp.common.AppContext;

public class TongYongFenZhangNode extends ACommonSuperNode {

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

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

	@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) throws Exception {
		//进入超级审核节点
		System.out.println("开始处理单据TongYongFenZhangNode");
		SuperNodeContext context = new SuperNodeContext();
		context.setNeedSave(true);
		FieldDataVo isVo = formDataVo.getFieldData("是否推送NC");
		String is = "";
		if(isVo!=null){
			is = isVo.getStringValue();
		}
		if(!"1".equals(is)) {
			context.success("不推送BIP付款单，跳过处理！");
			return context;
		}
//    	封装推送接口参数调用接口
		try {
			JSONObject headerer = HeaderUtil.getHeader();
			ParamNewUtil paramNewUtil = new ParamNewUtil();
			ColSummary colSummary = formDataVo.getColSummary();
			JSONObject jsonObject = paramNewUtil.getTongYongFenZhangParam(formDataVo,colSummary.getId()+"");
			context.setRequest(jsonObject.toString());
			String url = PropKit.getProp(PropKit.TRANSFORMURL);
			context.setUrl(url);
			JSONObject res = ProtUtil.doPost(jsonObject,url, 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(msg);
				Map<String, List<FormDataVo>> subFormMaps = formDataVo.getSubFormMap();
				List<FormDataVo> subFormMap = subFormMaps.get("分账明细表");
				String[] nos = msg.split("【");
				int i = 0;
				for (FormDataVo subFormDataVo : subFormMap) {
					FieldDataVo numVo = subFormDataVo.getFieldData("分账原币金额");
					String num = "";
					if(numVo!=null){
						num = numVo.getStringValue();
					}
					if("0".equals(num)) {
						FieldDataVo fieldDataVo = subFormDataVo.getFieldData("分账原币金额");
						log.info("分账原币金额为0，跳过处理单据号回写："+ fieldDataVo.getStringValue());
						continue;
					}else {
						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);
			}else {
				context.back("推送BIP失败:"+msg);
				context.setErrMsg("推送BIP失败:"+msg);
				formDataVo.getNewFieldDataMap().put("BIP返回结果", "推送BIP成功:"+msg);
			}
		} catch (Exception e) {
			e.printStackTrace();
			context.back("OA处理异常:"+e);
			context.setErrMsg("OA处理异常:"+e);
			formDataVo.getNewFieldDataMap().put("BIP返回结果", "OA处理异常:"+e);
		}
		log.info("返回通用分账超级节点数据");
		return context;
	}

}
