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

import cn.hutool.core.collection.CollUtil;
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.TestMeteringPointDTO;
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 GJB548C15Executor extends BaseExecutor implements IExperimentReviewRuleExecutor {
    @Override
    public String getProcessType() {
        return ExperimentReviewProcessType.GJB548C;
    }

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

    /**
     * 15
     *
     * 试验过程未缺失，并且都符合试验标准的要求，参数测试结果未超差
     *
     * 评定结果通过
     *
     * 辐照过程中备注栏有“已过辐照”字段；有100℃高温退火过程；最后一次测试通过，启动这条评定建议
     *
     * @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, condition3 = false;

        List<ExperimentRadiationProcess> experimentRadiationProcessList = experimentRadiationProcessService.getByExperimentId(
                experiment.getId());
        for (ExperimentRadiationProcess v : experimentRadiationProcessList) {
            if (StringUtils.contains(v.getComment(), "已过辐照")) {
                condition1 = true;
                break;
            }
        }
        List<ExperimentAnnealProcess> experimentAnnealProcessList = experimentAnnealProcessService.getByExperimentId(experiment.getId());
        for (ExperimentAnnealProcess experimentAnnealProcess : experimentAnnealProcessList) {
            if (StringUtils.contains(experimentAnnealProcess.getAnnealTemperature(), "100")) {
                condition2 = true;
            }
        }
        List<ExperimentTestProcess> experimentTestProcessList = experimentTestProcessService.getByExperimentId(experiment.getId());

        for (ExperimentTestProcess v : experimentTestProcessList) {
            List<TestMeteringPointDTO> testMeteringPointList = v.getTestMeteringPointList();
            TestMeteringPointDTO last = CollUtil.getLast(testMeteringPointList);
            if (!StringUtils.equals(last.getResult(), "合格")) {
                condition3 = true;
                break;
            }
        }
        if (condition1 && condition2 && condition3) {
            return ExperimentReviewResultEnum.FINAL_PASS;
        }
        return ExperimentReviewResultEnum.ITEM_PASS;
    }
}