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

import cn.hutool.core.util.NumberUtil;
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.*;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author lise
 * @version GJB548C1Executor.java, v 0.1 2025年06月16日 20:57 lise
 */

@Component
public class GJB548C3Executor extends BaseExecutor  implements IExperimentReviewRuleExecutor {

    @Override
    public String getProcessType() {
        return ExperimentReviewProcessType.GJB548C;
    }

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

    /**
     * 3
     *
     * 选择剂量率高于规定剂量率进行辐照试验
     *
     * 可能高估器件的抗辐射性能
     *
     * 参见附件二知识库中83条目《双极器件的辐射试验-辐照剂量率》的解释
     *
     * 可参考 QJ10004A-2018的相关规定，分析剂量率选择在QJ10004A-2018的适用性
     *
     * 样片类型非模拟集成电路，样片名称中不含有双极的字段。但是实际剂量率大于300 rad (Si) /s，启动这条评定建议
     *
     * @param experiment
     * @param sampleInfo
     * @param experimentReview
     * @return
     */
    @Override
    public String execute(Experiment experiment, ExperimentSampleInfo sampleInfo, ExperimentReview experimentReview) {
        experimentService.fetchExperimentDetail(experiment);
        List<ExperimentRadiationProcess> radiationProcessList = experimentRadiationProcessService.getByExperimentId(experiment.getId());

        for (ExperimentRadiationProcess radiationProcess : radiationProcessList) {
            if (!StringUtils.contains(radiationProcess.getSampleInfo(), sampleInfo.getId())) {
                continue;
            }
            String radiationDetailJson = radiationProcess.getRadiationDetail();
            RadiationMapDTO radiationMap = JSON.parseObject(radiationDetailJson, RadiationMapDTO.class);
            List<RadiationDetailDTO> detailDTOList = radiationMap.assembleDetails();

            Double minRate = detailDTOList.stream().map(obj -> obj.getActualMeasurementRate()).filter(
                            point -> StringUtils.isNotBlank(point)).filter(str -> str.matches("-?\\d+(\\.\\d+)?")).mapToDouble(Double::parseDouble)
                    .max().orElse(0d);
            if (!sampleInfo.getSampleName().contains("双极") && !sampleInfo.getSampleType().equals("模拟集成电路") && NumberUtil.compare(
                    minRate, 300d) > 0) {
                return ExperimentReviewResultEnum.HIGH;
            }
        }

        return ExperimentReviewResultEnum.ITEM_PASS;
    }
}