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.common.workflow.event.ACommonWorkflowEvent;

import com.alibaba.fastjson.JSONObject;
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;

public class XinChouFenZhangEvent extends ACommonWorkflowEvent {

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

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

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

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

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

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

	@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.getXinChouFenZhangStopParam(formDataVo);
			log.info("接口终止，参数记录："+jsonObject);
			String url = PropKit.getProp(PropKit.XCFZCANCELURL);
			context.setUrl(url);
			context.setRequest(jsonObject.toString());
			JSONObject res = ProtUtil.doPost(jsonObject, PropKit.getProp(PropKit.XCFZCANCELURL), 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;
	}
}
