package org.jeecg.modules.database.service.executor;

import jakarta.annotation.PostConstruct;
import org.jeecg.modules.database.constant.ExperimentReviewResultEnum;
import org.jeecg.modules.database.entity.*;
import org.jeecg.modules.database.service.IExperimentReviewRuleExecutor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.*;

@Component
public class ExecutorHolder {
    private Map<String, IExperimentReviewRuleExecutor> executorMap = new HashMap<>();
    @Autowired
    private List<IExperimentReviewRuleExecutor>        executorList;

    @PostConstruct
    public void init() {
        executorList.forEach(executor -> executorMap.put(executor.getProcessType() + executor.getPriority(), executor));
    }

    public String execute(String processType, Experiment experiment, ExperimentSampleInfo sampleInfo, ExperimentReview experimentReview) {
        IExperimentReviewRuleExecutor executor = executorMap.get(processType);
        if (executor == null) {
            return null;
        }
        try {
            return executor.execute(experiment, sampleInfo,experimentReview);
        } catch (Exception e) {
            return ExperimentReviewResultEnum.UNKNOWN;
        }
    }
}
