/*
 * 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.IExperimentReviewRuleExecutor;
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 QJ100041Executor extends BaseExecutor implements IExperimentReviewRuleExecutor {

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

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

    /**
     * 1
     *
     * 模拟器件未进行ELDRS试验
     *
     * 可能高估器件的抗辐射性能
     *
     * 参见附件二知识库中68条目《双极器件的低剂量率效应》的解释。
     *
     * a)已知器件设计不包含双极晶体管； b)已知器件设计不包含任何线性电路功能； c)业已证明，该器件类型或IC工艺，并未表现出器件参数ELDRS,而且也已证明影呴ELDRS 响应的变量对于特定的生产方工艺过程已处于受控状态
     *
     * 样片类型为模拟集成电路，样片名称中含有双极的字段。但是实际剂量率大于0.01 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)
                    .min().orElse(0d);
            if (sampleInfo.getSampleName().contains("双极") && sampleInfo.getSampleType().equals("模拟集成电路") && NumberUtil.compare(
                    minRate, 0.01d) > 0 && StringUtils.contains(radiationProcess.getComment(), "加速试验")) {
                return ExperimentReviewResultEnum.HIGH;
            }
        }

        return ExperimentReviewResultEnum.ITEM_PASS;
    }
}