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

import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.flowable.common.engine.api.delegate.Expression;
import org.flowable.engine.delegate.TaskListener;
import org.flowable.task.service.delegate.DelegateTask;
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;

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

	private Expression serviceBeanName;
	private Expression state;

	@Override
	public void notify(DelegateTask delegateTask) {
		log.info(">>>>>>>>>>>> " + delegateTask.getName() + "监听器启动");
		IHtClient htClient = SpringUtil.getBean(IHtClient.class);
		if (null == htClient) {
			throw new RuntimeException("找不到HtClient Bean！");
		}
		R result = htClient.updateStateById(
			(String) serviceBeanName.getValue(delegateTask),
			(String) delegateTask.getVariable(ProcessConstant.BUSINESS_ID_KEY),
			(String) state.getValue(delegateTask));
		if (!result.isSuccess()) {
			throw new RuntimeException("远程调用表单状态更新失败！");
		}
		log.info(">>>>>>>>>>>> " + delegateTask.getName() + "监听器执行完成");
	}
}
