package com.seeyon.apps.src_generalmatter.event;

import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.alibaba.fastjson.JSONObject;
import com.seeyon.apps.src_generalmatter.kit.form.CAP4FormKit;
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.constants.ApplicationCategoryEnum;
import com.seeyon.ctp.workflow.event.AbstractWorkflowEvent;
import com.seeyon.ctp.workflow.event.WorkflowEventData;
import com.seeyon.ctp.workflow.event.WorkflowEventResult;

public class GeneralmatterEvent  extends AbstractWorkflowEvent{

private static final Log log = LogFactory.getLog(GeneralmatterEvent.class);
	
	@Override
	public String getId() {
		return "generalmatterEvent";
	}

	@Override
	public String getLabel() {
		return "OA通用事项审批终止流程监听";
	}
	
	@Override
	public ApplicationCategoryEnum getAppName() {
		return ApplicationCategoryEnum.form;
	}
	
	@Override
	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();
			JSONObject jsonObject = paramUtil.getGeneralmatterStopParam(master);
			String url = CAP4FormKit.getFieldStrValue(master, "来源推送地址");
			log.info("接口终止，参数记录："+jsonObject);
			JSONObject res = ProtUtil.doPost(jsonObject, url, headerer);
			if(res.isEmpty()) {
				workflowEventResult.setAlertMessage("请求失败");
	            return workflowEventResult;
			}
			String status = res.getString("status");
			if(!"1".equals(status)) {
				workflowEventResult.setAlertMessage(res.getString("msg"));
	            return workflowEventResult;
			}
		}catch (Exception e) {
			workflowEventResult.setAlertMessage(e.getMessage());
	    	return workflowEventResult;
		}
		return null;
	}
	
}
