/*
 * Ant Group
 * Copyright (c) 2004-2025 All Rights Reserved.
 */
package org.jeecg.modules.database.service.executor;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.modules.database.constant.ExperimentReviewProcessType;
import org.jeecg.modules.database.constant.ExperimentReviewResultEnum;
import org.jeecg.modules.database.entity.*;
import org.jeecg.modules.database.service.IExperimentReviewRuleExecutor;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author lise
 */

@Component
public class ESCC225001Executor extends BaseExecutor implements IExperimentReviewRuleExecutor {
    @Override
    public String getProcessType() {
        return ExperimentReviewProcessType.ESCC22500;
    }

    @Override
    public int getPriority() {
        return 1;
    }

    /**
     * 质子退火过程中的偏置未采用无偏置条件
     *
     * 可能低估或高估器件的抗辐射性能
     *
     * 不同偏置下产生不同数量、类型的辐射损伤缺陷
     *
     * 测试计划中另有规定
     *
     * 在试验信息中辐射源选择质子加速器，试验数据中的偏置条件选择了静态偏置或动态偏置，启动这条评定建议
     *
     * @param experiment
     * @param sampleInfo
     * @param experimentReview
     * @return
     */
    @Override
    public String execute(Experiment experiment, ExperimentSampleInfo sampleInfo, ExperimentReview experimentReview) {
        experimentService.fetchExperimentDetail(experiment);
        boolean condition1 = false, condition2 = false;

        List<ExperimentRadiationProcess> radiationProcessList = experimentRadiationProcessService.getByExperimentId(experiment.getId());
        for (ExperimentRadiationProcess experimentRadiationProcess : radiationProcessList) {
            String radiationSource = experimentRadiationProcess.getRadiationSource();
            if (StringUtils.contains(radiationSource, "质子加速器")) {
                condition1 = true;
                break;
            }
        }
        List<ExperimentAnnealProcess> experimentAnnealProcessList = experimentAnnealProcessService.getByExperimentId(experiment.getId());
        for (ExperimentAnnealProcess experimentAnnealProcess : experimentAnnealProcessList) {
            String deviationCondition = experimentAnnealProcess.getDeviationCondition();
            JSONObject jsonObject = JSON.parseObject(deviationCondition);
            if (jsonObject.containsKey("offsetCondition")) {
                String offsetCondition = jsonObject.getString("offsetCondition");
                if (StringUtils.contains(offsetCondition, "dtpz") || StringUtils.contains(offsetCondition, "jtpz")) {
                    condition2 = true;
                    break;
                }
            }
        }
        if (condition1 && condition2) {
            return ExperimentReviewResultEnum.UNKNOWN;
        }
        return ExperimentReviewResultEnum.ITEM_PASS;
    }
}