package cn.axzo.workflow.core.service.support.forecast;

import cn.axzo.workflow.core.service.support.ExpressionConditionCmd;
import lombok.AllArgsConstructor;
import org.flowable.engine.ManagementService;
import org.flowable.engine.RuntimeService;
import org.flowable.engine.impl.cfg.ProcessEngineConfigurationImpl;
import org.springframework.stereotype.Component;

/**
 * 基于 Flowable 引擎能力的表达式评估器
 *
 * @author wangli
 * @since 2023/10/10 10:41
 */
@Component
@AllArgsConstructor
public class FlowableConditionEvaluator {

    private final ManagementService managementService;
    private final RuntimeService runtimeService;
    private final ProcessEngineConfigurationImpl processEngineConfiguration;

    /**
     * 计算表达式真假, 需结合引擎能力
     *
     * @param exp               条件表达式
     * @param variables         流程变量
     * @param processInstanceId 流程实例 ID (可与 variables 参入二选一, 如果都传,则直接用实例 ID 去引擎中查真实的变量表)
     * @return true Or false
     */
    public Boolean conditionOn(String exp, String processInstanceId) {
        return managementService.executeCommand(new ExpressionConditionCmd(runtimeService,
                processEngineConfiguration, processInstanceId, exp));
    }

}
