package digital.laboratory.platform.inspection.service.impl;

import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import digital.laboratory.platform.inspection.constant.TestRecordSampleDataConstant;
import digital.laboratory.platform.inspection.dto.HairSewageDataDto;
import digital.laboratory.platform.inspection.dto.NPSCaseTestDataDto;
import digital.laboratory.platform.inspection.entity.TestRecordSampleDataExpand;
import digital.laboratory.platform.inspection.mapper.TestRecordSampledataExpandMapper;
import digital.laboratory.platform.inspection.service.TestRecordSampledataExpandService;
import digital.laboratory.platform.inspection.utils.datafile.nps.NPSTestDetailDataStruct;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author ChenJiangBao
* @description 针对表【b_test_record_sampledata_expand(样本检验数据的扩展信息)】的数据库操作Service实现
* @createDate 2025-03-19 14:58:17
*/
@Service
public class TestRecordSampledataExpandServiceImpl extends ServiceImpl<TestRecordSampledataExpandMapper, TestRecordSampleDataExpand>
    implements TestRecordSampledataExpandService{

    /**
     * 封装处理毛发案件、任务等检验扩展数据信息
     * @param hairSewageDataDto
     * @param testDataId
     * @return
     */
    public Collection<? extends TestRecordSampleDataExpand> saveHairSewageInspectExpData(HairSewageDataDto hairSewageDataDto, String testDataId) {
        List<TestRecordSampleDataExpand> hairSewageInspectExpDataList = new ArrayList<>();

        // 设置基峰的扩展数据
        if (!TestRecordSampleDataConstant.isInvalidValue(hairSewageDataDto.getPeakAreaUp())) {
            hairSewageInspectExpDataList.add(createTestRecordSampleDataExpand(testDataId, Boolean.TRUE,
                    hairSewageDataDto.getPeakAreaUp(), null, null, null, null));
        }

        if (!TestRecordSampleDataConstant.isInvalidValue(hairSewageDataDto.getPeakAreaDown())) {
            hairSewageInspectExpDataList.add(createTestRecordSampleDataExpand(testDataId, Boolean.FALSE,
                    hairSewageDataDto.getPeakAreaDown(), hairSewageDataDto.getIonAbundanceRatio(),
                    hairSewageDataDto.getIonAbundanceRatioWithinError(), hairSewageDataDto.getWhetherCheckOut(), null));
        }

        return hairSewageInspectExpDataList;
    }

    /**
     * 封装处理nps案件检验扩展数据信息
     *
     * @param npsCaseTestDataDto
     * @param testDataId
     * @return
     */
    @Override
    public Collection<? extends TestRecordSampleDataExpand> saveNPSInspectExpData(NPSCaseTestDataDto npsCaseTestDataDto, String testDataId) {
        // 检验数据
        List<NPSTestDetailDataStruct> npsTestSampleDataList = npsCaseTestDataDto.getTestSampleDataList();

        List<TestRecordSampleDataExpand> testRecordSampleDataExpandList = npsTestSampleDataList.stream().map(item -> {
            return createTestRecordSampleDataExpand(
                    testDataId,
                    item.getIsBasePeak() == 1 ? Boolean.TRUE : Boolean.FALSE,
                    item.getArea(),
                    item.getAbundanceRatio(),
                    item.getAbundanceRatioError(),
                    item.getWithinError(),
                    null
            );
        }).collect(Collectors.toList());

        return testRecordSampleDataExpandList;
    }


    /**
     * 根据提供的参数创建一个TestRecordSampleDataExpand对象。
     *
     * @param testDataId 测试数据ID
     * @param isBasePeak 是否为基本峰
     * @param peakArea 峰面积
     * @param ionAbundanceRatio 离子丰度比
     * @param ionAbundanceRatioError 离子丰度比误差
     * @param ionAbundanceRatioWithinError 离子丰度比是否在误差范围内
     * @param someOtherValue 其他值
     * @return 创建的TestRecordSampleDataExpand对象
     */
    private TestRecordSampleDataExpand createTestRecordSampleDataExpand(
            String testDataId,
            Boolean isBasePeak,
            Double peakArea,
            Double ionAbundanceRatio,
            Double ionAbundanceRatioError,
            String ionAbundanceRatioWithinError,
            Double someOtherValue
    ) {
        TestRecordSampleDataExpand testRecordSampleDataExpand = new TestRecordSampleDataExpand();
        testRecordSampleDataExpand.setTestDataId(testDataId);
        testRecordSampleDataExpand.setId(IdWorker.getIdStr());
        testRecordSampleDataExpand.setBasePeak(isBasePeak);
        testRecordSampleDataExpand.setPeakArea(BigDecimal.valueOf(peakArea));  // 注意: BigDecimal.valueOf(double)

        if (ionAbundanceRatio != null) {
            testRecordSampleDataExpand.setIonAbundanceRatio(BigDecimal.valueOf(ionAbundanceRatio));
        }

        if (ionAbundanceRatioError != null) {
            testRecordSampleDataExpand.setIonAbundanceRatioError(BigDecimal.valueOf(ionAbundanceRatioError));
        }

        if (ionAbundanceRatioWithinError != null) {
            testRecordSampleDataExpand.setIonAbundanceRatioWithinError(ionAbundanceRatioWithinError);
        }

        // 如果有其他需要设置的值，可以继续扩展
        if (someOtherValue != null) {
            // 处理其他值的设置逻辑
        }

        return testRecordSampleDataExpand;
    }

}
