package cn.axzo.workflow.core.engine.cmd;


import cn.axzo.workflow.common.exception.WorkflowEngineException;
import lombok.extern.slf4j.Slf4j;
import org.flowable.common.engine.impl.interceptor.Command;
import org.flowable.common.engine.impl.interceptor.CommandContext;

import static cn.axzo.workflow.common.code.BpmnInstanceRespCode.ENGINE_EXEC_EXCEPTION;

/**
 * 抽象的命令，用于将在 Command 中执行的逻辑所抛出的非 WorkflowEngineException 的异常都包装为 WorkflowEngineException
 *
 * @author wangli
 * @since 2024/7/1 13:59
 */
@Slf4j
public abstract class AbstractCommand<T> implements Command<T> {

    public abstract String paramToJsonString();

    @Override
    public T execute(CommandContext commandContext) {
        try {
            return executeInternal(commandContext);
        } catch (WorkflowEngineException e) {
            throw e;
        } catch (Exception e) {
            log.warn(e.getMessage(), e);
            throw new WorkflowEngineException(ENGINE_EXEC_EXCEPTION, e, e.getMessage());
        }
    }

    /**
     * 实现该方法，默认就捕获了 command 一些引擎内部异常
     *
     * @param commandContext
     * @return
     */
    public T executeInternal(CommandContext commandContext) {
        return null;
    }
}
