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

import org.flowable.common.engine.impl.interceptor.Command;
import org.flowable.common.engine.impl.interceptor.CommandContext;
import org.flowable.engine.RepositoryService;
import org.flowable.engine.impl.cfg.ProcessEngineConfigurationImpl;
import org.flowable.engine.impl.util.CommandContextUtil;
import org.flowable.engine.repository.Model;
import org.flowable.engine.repository.ProcessDefinition;

/**
 * 通过流程定义ID获取流程模型
 *
 * @author wangli
 * @since 2025-04-19 10:17
 */
public class CustomGetModelByDefinitionIdCmd implements Command<Model> {
    private final String processDefinitionId;

    public CustomGetModelByDefinitionIdCmd(String processDefinitionId) {
        this.processDefinitionId = processDefinitionId;
    }

    @Override
    public Model execute(CommandContext commandContext) {
        ProcessEngineConfigurationImpl processEngineConfiguration = CommandContextUtil
                .getProcessEngineConfiguration(commandContext).getProcessEngineConfiguration();
        RepositoryService repositoryService = processEngineConfiguration.getRepositoryService();
        ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery()
                .processDefinitionId(processDefinitionId)
                .singleResult();

        // 获取部署ID
        String deploymentId = processDefinition.getDeploymentId();

        // 通过部署ID查询模型
        Model model = repositoryService.createModelQuery()
                .deploymentId(deploymentId)
                .singleResult();
        return model;
    }
}
