package com.seeyon.apps.src_generalmatter.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_generalmatter.constants.GeneralmatterConstants;

import com.alibaba.fastjson.JSONObject;
import com.seeyon.apps.src_generalmatter.unit.HeaderUtil;
import com.seeyon.apps.src_generalmatter.unit.ParamUtil;
import com.seeyon.apps.src_generalmatter.unit.ProtUtil;
import com.seeyon.cap4.form.bean.FormDataMasterBean;
import com.seeyon.ctp.common.AppContext;

public class GeneralmatterNode  extends ACommonSuperNode {

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

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

	@Override
	public String getNodeName() {
		return "OA通用事项审批完成超级节点";
	}


	@Override
	public String getPluginId() {
		return GeneralmatterConstants.getPluginId();
	}

	@Override
	public String getFormParse() {
		return "json";
	}
	public ConfigVo getGeneralmatterConfig() {
		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 = getGeneralmatterConfig();
		SuperNodeContext context = new SuperNodeContext();
		context.setNeedSave(true);
//    	封装推送接口参数调用接口
		try {
			JSONObject headerer = HeaderUtil.getHeader();
			ParamUtil paramUtil = new ParamUtil();
			JSONObject jsonObject = paramUtil.getGeneralmatterFinishParam(formDataVo);
//			String url = CAP4FormKit.getFieldStrValue(master, "来源推送地址");
			String url = formDataVo.getFieldData("来源推送地址")!=null?formDataVo.getFieldData("来源推送地址").getStringValue():"";
//			String ly = CAP4FormKit.getFieldStrValue(master, "来源系统");
			String ly = formDataVo.getFieldData("来源系统")!=null?formDataVo.getFieldData("来源系统").getStringValue():"";
			context.setUrl(url);
			context.setRequest(jsonObject.toString());
			JSONObject res = ProtUtil.doPost(jsonObject, url, headerer);
			context.setResponse(res.toString());
			if(res==null) {
				context.back("推送单据失败:接口调用失败，未正常返回结果");
				formDataVo.getNewFieldDataMap().put("NC返回结果", "推送单据失败:接口调用失败，未正常返回结果");
				return context;
			}
			String status = res.getString("status");
			String  msg =  res.get("msg").toString();
			if(!"1".equals(status)) {
				log.info("推送单据失败:"+msg);
				context.success("推送单据失败:"+msg);
				formDataVo.getNewFieldDataMap().put("NC返回结果", "推送单据失败:"+msg);
			}else {
				log.info( "推送"+ly+"单据成功:"+msg);
				context.back( "推送"+ly+"单据成功:"+msg);
				formDataVo.getNewFieldDataMap().put("NC返回结果", "推送"+ly+"单据成功:"+msg);
			}
		} catch (Exception e) {
			log.error("异常："+ e);
			context.back("异常："+ e);
			formDataVo.getNewFieldDataMap().put("NC返回结果", "推送单据失败:"+e);
			e.printStackTrace();
		}
		return context;
	}
}
