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

import com.alibaba.fastjson.JSON;
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.dto.RadiationDetailDTO;
import org.jeecg.modules.database.dto.RadiationMapDTO;
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 ESCC225002Executor extends BaseExecutor implements IExperimentReviewRuleExecutor {
    @Override
    public String getProcessType() {
        return ExperimentReviewProcessType.ESCC22500;
    }

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

    /**
     * 质子辐照过程中的通量过高
     *
     * 可能高估器件的抗辐射性能
     *
     * 通量过高可能导致器件温度上升，引起缺陷退火
     *
     * 考其他单位指定的质子测试指南
     *
     * 在试验信息中辐射源选择质子加速器，辐照过程中的实际注量率高于10E9 p/cm²/s，启动这条评定建议
     *
     * @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 radiationProcess : radiationProcessList) {
            String radiationSource = radiationProcess.getRadiationSource();
            if (StringUtils.contains(radiationSource, "质子加速器")) {
                condition1 = true;
            }
            String radiationDetail = radiationProcess.getRadiationDetail();
            RadiationMapDTO radiationMap = JSON.parseObject(radiationDetail, RadiationMapDTO.class);
            List<RadiationDetailDTO> list = radiationMap.getProton();

            Double maxMeasurementPoint = list.stream().map(obj -> obj.getActualInjectionRate()) // 获取point字段
                    .filter(point -> StringUtils.isNotBlank(point)).filter(str -> str.matches("-?\\d+(\\.\\d+)?")) // 只保留数字格式的字符串
                    .mapToDouble(Double::parseDouble) // 转换为double
                    .max().getAsDouble();
            if (maxMeasurementPoint > 10E9) {
                condition2 = true;
            }
        }
        if (condition1 && condition2) {
            return ExperimentReviewResultEnum.HIGH;
        }

        return ExperimentReviewResultEnum.ITEM_PASS;
    }
}