package com.seeyon.apps.jync_fz.event;

import cn.hutool.log.Log;
import com.seeyon.apps.bip.constants.BipConstants;
import com.seeyon.apps.common.config.ICstConfigApi;
import com.seeyon.apps.common.workflow.constants.WorkFlowType;
import com.seeyon.apps.ext.workflow.vo.FormDataVo;
import com.seeyon.apps.ext.workflow.vo.WorkflowEventContext;
import com.seeyon.apps.jync_file.util.ParamNewUtil;
import com.seeyon.ctp.common.AppContext;

import com.alibaba.fastjson.JSONObject;
import com.seeyon.apps.jync_file.kit.PropKit;
import com.seeyon.apps.jync_file.util.HeaderUtil;
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.constants.ApplicationCategoryEnum;
import com.seeyon.ctp.workflow.event.AbstractWorkflowEvent;
import com.seeyon.ctp.workflow.event.WorkflowEventData;
import com.seeyon.ctp.workflow.event.WorkflowEventResult;

import java.util.Map;

//public class SheBaoFenZhangEvent extends ACommonWorkflowEvent {
public class SheBaoFenZhangEvent extends AbstractWorkflowEvent {

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

	public SheBaoFenZhangEvent() {
	}
	public ApplicationCategoryEnum getAppName() {
		return ApplicationCategoryEnum.form;
	}

	@Override
	public String getId() {
		return "sheBaoFenZhangEvent";
	}

	@Override
	public String getLabel() {
		return "OA社保分账单流程监听";
	}

	public WorkflowEventResult onBeforeStop(WorkflowEventData data) {
		WorkflowEventResult workflowEventResult = new WorkflowEventResult();
		System.out.println("进入终止操作");
		try {
			Map<String, Object> map = data.getBusinessData();
			FormDataMasterBean master = (FormDataMasterBean)map.get("formDataBean");
			JSONObject headerer = HeaderUtil.getHeader();
			ParamUtil paramUtil = new ParamUtil();
			log.info("全量参数："+master.getId()+master.getFieldBeanByFieldName("BIP单据编号")+master.getFormTable());
			System.out.println("直接输出field0065的数据："+master.getFieldValue("field0065"));
			JSONObject jsonObject = paramUtil.getSheBaoFenZhangStopParam(master);
			log.info("接口终止，参数记录：" + jsonObject);
			JSONObject res = ProtUtil.doPost(jsonObject, PropKit.getProp("sbfzCancelUrl"), headerer);
			if (res.isEmpty()) {
				workflowEventResult.setAlertMessage("请求失败");
				return workflowEventResult;
			} else {
				String status = res.getString("status");
				if (!"1".equals(status)) {
					workflowEventResult.setAlertMessage(res.getString("msg"));
					return workflowEventResult;
				} else {
					return null;
				}
			}
		} catch (Exception var10) {
			var10.printStackTrace();
			workflowEventResult.setAlertMessage("终止异常："+var10.getMessage());
			return workflowEventResult;
		}
	}


//	@Override
//	protected WorkflowEventContext proceed(String request, FormDataVo formDataVo, WorkFlowType workFlowType, FormDataMasterBean formDataMasterBean) throws Exception {
//		WorkflowEventContext context = new WorkflowEventContext();
////		ConfigVo configVo = cstConfigApi.getConfig(getPluginId());
//		context.setNeedSave(true);
//		System.out.println("进入终止操作");
//		try {
//			JSONObject headerer = HeaderUtil.getHeader();
//				ParamUtil paramUtil = new ParamUtil();
//				JSONObject jsonObject = paramUtil.getSheBaoFenZhangStopParam(formDataVo);
//				log.info("接口终止，参数记录："+jsonObject);
//				String url = PropKit.getProp(PropKit.SBFZCANCELURL);
//				context.setUrl(url);
//				context.setRequest(jsonObject.toString());
//				JSONObject res = ProtUtil.doPost(jsonObject, url, headerer);
//				context.setResponse(res.toString());
//				if(res.isEmpty()) {
//					context.setSuccess(false);
//					context.setErrMsg("请求失败");
//					return context;
//				}
//				String status = res.getString("status");
//				if(!"1".equals(status)) {
//				context.setSuccess(false);
//				context.setErrMsg(res.getString("msg"));
//				return context;
//			}
//		}catch (Exception e) {
//			context.setSuccess(false);
//			context.setErrMsg(e.getMessage());
//			return context;
//		}
//		context.setSuccess(true);
//		context.setErrMsg("请求成功");
//		return context;
//	}
}
