package com.seeyon.apps.jync_fz.event;

import cn.hutool.log.Log;
import com.alibaba.fastjson.JSONObject;
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.common.workflow.event.ACommonWorkflowEvent;
import com.seeyon.apps.ext.workflow.vo.FormDataVo;
import com.seeyon.apps.ext.workflow.vo.WorkflowEventContext;
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.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 NewSheBaoFenZhangEvent extends ACommonWorkflowEvent {

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

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

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

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

	@Override
	public String getFormParse() {
		return "json";
	}


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

	@Override
	protected WorkflowEventContext proceed(String request, FormDataVo formDataVo, WorkFlowType workFlowType, FormDataMasterBean formDataMasterBean) throws Exception {
		WorkflowEventContext context = new WorkflowEventContext();
		context.setNeedSave(true);
		System.out.println("进入终止操作");
		try {
			JSONObject headerer = HeaderUtil.getHeader();
			ParamNewUtil paramNewUtil = new ParamNewUtil();
			JSONObject jsonObject = paramNewUtil.getSheBaoFenZhangStopParam(formDataVo);
			log.info("接口终止，参数记录："+jsonObject);
			String url = PropKit.getProp(PropKit.SBFZCANCELURL);
			context.setUrl(url);
			log.info("映射参数："+request);
			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;
	}


}
