package org.security.flow.business.listener.execution;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.flowable.common.engine.api.delegate.Expression;
import org.flowable.engine.delegate.DelegateExecution;
import org.flowable.engine.delegate.ExecutionListener;
import org.security.flow.core.constant.ProcessConstant;
import org.security.ht.feign.IHtClient;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.SpringUtil;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * @Author: chenchao
 * @Create: 2022/8/26
 * @Description:
 */
@Component
@Slf4j
@Data
public class FishedExecutionListener implements ExecutionListener {

	private Expression serviceBeanName;

	@Override
	public void notify(DelegateExecution execution) {
		log.info(">>>>>>>>>>>> 监听器启动");
		IHtClient htClient = SpringUtil.getBean(IHtClient.class);
		if (null == htClient) {
			throw new RuntimeException("找不到HtClient Bean！");
		}
		Boolean pass = (Boolean) execution.getVariable("pass");
		Map<String, Object> variables = execution.getVariables();
		R result = htClient.afterFlowFinished(
			(String) serviceBeanName.getValue(execution),
			(String) execution.getVariable(ProcessConstant.BUSINESS_ID_KEY),
			pass, variables);
		if (!result.isSuccess()) {
			throw new RuntimeException("远程调用完成处理失败！");
		}
		log.info(">>>>>>>>>>>> 监听器执行完成");
	}

}
