|  |  |  | @ -21,6 +21,7 @@ import digital.laboratory.platform.inspection.service.TestRecordService; | 
			
		
	
		
			
				
					|  |  |  |  | import digital.laboratory.platform.inspection.utils.datafile.nps.NPSTestDetailDataStruct; | 
			
		
	
		
			
				
					|  |  |  |  | import digital.laboratory.platform.inspetion.api.entity.TestRecord; | 
			
		
	
		
			
				
					|  |  |  |  | import digital.laboratory.platform.sys.entity.Drug; | 
			
		
	
		
			
				
					|  |  |  |  | import lombok.extern.slf4j.Slf4j; | 
			
		
	
		
			
				
					|  |  |  |  | import org.springframework.stereotype.Service; | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  | import javax.annotation.Resource; | 
			
		
	
	
		
			
				
					|  |  |  | @ -37,6 +38,7 @@ import java.util.stream.Collectors; | 
			
		
	
		
			
				
					|  |  |  |  |  * @description 针对表【b_test_record_sampledata_expand(样本检验数据的扩展信息)】的数据库操作Service实现 | 
			
		
	
		
			
				
					|  |  |  |  |  * @createDate 2025-03-19 14:58:17 | 
			
		
	
		
			
				
					|  |  |  |  |  */ | 
			
		
	
		
			
				
					|  |  |  |  | @Slf4j | 
			
		
	
		
			
				
					|  |  |  |  | @Service | 
			
		
	
		
			
				
					|  |  |  |  | public class TestRecordSampledataExpandServiceImpl extends ServiceImpl<TestRecordSampleDataExpandMapper, TestRecordSampleDataExpand> | 
			
		
	
		
			
				
					|  |  |  |  |         implements TestRecordSampledataExpandService { | 
			
		
	
	
		
			
				
					|  |  |  | @ -148,42 +150,6 @@ public class TestRecordSampledataExpandServiceImpl extends ServiceImpl<TestRecor | 
			
		
	
		
			
				
					|  |  |  |  |         return testRecordSampleDataExpandList; | 
			
		
	
		
			
				
					|  |  |  |  |     } | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |     /** | 
			
		
	
		
			
				
					|  |  |  |  |      * 根据DTO去更新 | 
			
		
	
		
			
				
					|  |  |  |  |      * | 
			
		
	
		
			
				
					|  |  |  |  |      * @param updateDTO | 
			
		
	
		
			
				
					|  |  |  |  |      * @return | 
			
		
	
		
			
				
					|  |  |  |  |      */ | 
			
		
	
		
			
				
					|  |  |  |  |     @Override | 
			
		
	
		
			
				
					|  |  |  |  |     public Boolean updateByDTO(TestSampleDataExpandDTO updateDTO) { | 
			
		
	
		
			
				
					|  |  |  |  |         TestRecordSampleDataExpand testRecordSampleDataExpand = TestSampleDataExpandConverter.dtoToEntity(updateDTO); | 
			
		
	
		
			
				
					|  |  |  |  |         boolean update = super.updateById(testRecordSampleDataExpand); | 
			
		
	
		
			
				
					|  |  |  |  |         if (update) { | 
			
		
	
		
			
				
					|  |  |  |  |             TestRecordSampleDataExpand sampleDataExpand = super.getById(updateDTO.getId()); | 
			
		
	
		
			
				
					|  |  |  |  |             // 查询对应的基峰数据是否存在
 | 
			
		
	
		
			
				
					|  |  |  |  |             TestRecordSampleDataExpand basePeakData = super.getOne(Wrappers.<TestRecordSampleDataExpand>lambdaQuery() | 
			
		
	
		
			
				
					|  |  |  |  |                     .eq(TestRecordSampleDataExpand::getTestDataId, sampleDataExpand.getTestDataId()) | 
			
		
	
		
			
				
					|  |  |  |  |                     .eq(TestRecordSampleDataExpand::getBasePeak, Boolean.TRUE) | 
			
		
	
		
			
				
					|  |  |  |  |             ); | 
			
		
	
		
			
				
					|  |  |  |  |             if (!sampleDataExpand.getBasePeak() && basePeakData != null) { | 
			
		
	
		
			
				
					|  |  |  |  |                 // 获取标准物质的扩展信息,1 先查询对应的实验数据 2 根据实验数据的实验id以及化合物以及类型获取标准物质的实验数据信息 3 根据标准物质实验数据id取对应的扩展数据
 | 
			
		
	
		
			
				
					|  |  |  |  |                 TestRecordSampleData testRecordSampleData = testRecordSampleDataService.getById(sampleDataExpand.getTestDataId()); | 
			
		
	
		
			
				
					|  |  |  |  |                 TestRecord testRecord = testRecordService.getById(testRecordSampleData.getTestId()); | 
			
		
	
		
			
				
					|  |  |  |  |                 List<TestRecordSampleDataExpand> stdDataExpandList = null; | 
			
		
	
		
			
				
					|  |  |  |  |                 if (!testRecordSampleData.getSampleType().equals(TestRecordSampleDataConstant.SAMPLE_TYPE_STD)) { | 
			
		
	
		
			
				
					|  |  |  |  |                     TestRecordSampleData stdTestSampleData = testRecordSampleDataService.getOne(Wrappers.<TestRecordSampleData>lambdaQuery().eq(TestRecordSampleData::getTestId, testRecordSampleData.getTestId()).eq(TestRecordSampleData::getCompoundName, testRecordSampleData.getCompoundName()).eq(TestRecordSampleData::getSampleType, TestRecordSampleDataConstant.SAMPLE_TYPE_STD)); | 
			
		
	
		
			
				
					|  |  |  |  |                     stdDataExpandList = super.list(Wrappers.<TestRecordSampleDataExpand>lambdaQuery().eq(TestRecordSampleDataExpand::getTestDataId, stdTestSampleData.getId())); | 
			
		
	
		
			
				
					|  |  |  |  |                 } | 
			
		
	
		
			
				
					|  |  |  |  |                 if (sampleDataExpand.getPeakArea() != null && basePeakData.getPeakArea() != null) { | 
			
		
	
		
			
				
					|  |  |  |  |                     calculateIonAbundanceRatio(basePeakData, testRecord, stdDataExpandList, sampleDataExpand); | 
			
		
	
		
			
				
					|  |  |  |  |                     update = super.updateById(sampleDataExpand); | 
			
		
	
		
			
				
					|  |  |  |  |                 } | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |             } | 
			
		
	
		
			
				
					|  |  |  |  |         } | 
			
		
	
		
			
				
					|  |  |  |  |         return update; | 
			
		
	
		
			
				
					|  |  |  |  |     } | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |     /** | 
			
		
	
		
			
				
					|  |  |  |  |      * 根据DTO去批量更新 | 
			
		
	
		
			
				
					|  |  |  |  |      * | 
			
		
	
	
		
			
				
					|  |  |  | @ -194,35 +160,49 @@ public class TestRecordSampledataExpandServiceImpl extends ServiceImpl<TestRecor | 
			
		
	
		
			
				
					|  |  |  |  |     public Boolean updateBatchByDTO(List<TestSampleDataExpandDTO> dtoList) { | 
			
		
	
		
			
				
					|  |  |  |  |         List<TestRecordSampleDataExpand> expandList = TestSampleDataExpandConverter.dtoToEntityList(dtoList); | 
			
		
	
		
			
				
					|  |  |  |  |         if (super.updateBatchById(expandList)) { | 
			
		
	
		
			
				
					|  |  |  |  |             CompletableFuture.runAsync(() -> { | 
			
		
	
		
			
				
					|  |  |  |  |                 List<TestRecordSampleDataExpand> newExpandDatList = super.listByIds(expandList.stream().map(TestRecordSampleDataExpand::getId).collect(Collectors.toList())); | 
			
		
	
		
			
				
					|  |  |  |  | //            CompletableFuture.runAsync(() -> {
 | 
			
		
	
		
			
				
					|  |  |  |  |                 List<TestRecordSampleDataExpand> newExpandDatList = super.list(Wrappers.<TestRecordSampleDataExpand>lambdaQuery() | 
			
		
	
		
			
				
					|  |  |  |  |                         .in(TestRecordSampleDataExpand::getId, expandList.stream().map(TestRecordSampleDataExpand::getId).collect(Collectors.toList())) | 
			
		
	
		
			
				
					|  |  |  |  |                         .isNotNull(TestRecordSampleDataExpand::getPeakArea) | 
			
		
	
		
			
				
					|  |  |  |  |                 ); | 
			
		
	
		
			
				
					|  |  |  |  |                 Map<String, List<TestRecordSampleDataExpand>> expandDataGroupByDataId = newExpandDatList.stream().collect(Collectors.groupingBy(TestRecordSampleDataExpand::getTestDataId)); | 
			
		
	
		
			
				
					|  |  |  |  |                 List<TestRecordSampleDataExpand> calculateAfterResult = new ArrayList<>(); | 
			
		
	
		
			
				
					|  |  |  |  |                 expandDataGroupByDataId.forEach((key, value) -> { | 
			
		
	
		
			
				
					|  |  |  |  |                     log.info("Key: " + key + ", Value Size: " + value.size()); | 
			
		
	
		
			
				
					|  |  |  |  |                     // 查询对应的基峰数据是否存在
 | 
			
		
	
		
			
				
					|  |  |  |  |                     TestRecordSampleDataExpand basePeakData = value.stream().filter(TestRecordSampleDataExpand::getBasePeak).findFirst().orElse(null); | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |                     if (basePeakData != null) { | 
			
		
	
		
			
				
					|  |  |  |  |                         // 获取标准物质的扩展信息,1 先查询对应的实验数据 2 根据实验数据的实验id以及化合物以及类型获取标准物质的实验数据信息 3 根据标准物质实验数据id取对应的扩展数据
 | 
			
		
	
		
			
				
					|  |  |  |  |                         TestRecordSampleData testRecordSampleData = testRecordSampleDataService.getById(key); | 
			
		
	
		
			
				
					|  |  |  |  |                         boolean isSTDSample = testRecordSampleData.getSampleType().equals(TestRecordSampleDataConstant.SAMPLE_TYPE_STD); | 
			
		
	
		
			
				
					|  |  |  |  |                         TestRecord testRecord = testRecordService.getById(testRecordSampleData.getTestId()); | 
			
		
	
		
			
				
					|  |  |  |  |                         List<TestRecordSampleDataExpand> stdDataExpandList = null; | 
			
		
	
		
			
				
					|  |  |  |  |                         if (!testRecordSampleData.getSampleType().equals(TestRecordSampleDataConstant.SAMPLE_TYPE_STD)) { | 
			
		
	
		
			
				
					|  |  |  |  |                         if (!isSTDSample) { | 
			
		
	
		
			
				
					|  |  |  |  |                             TestRecordSampleData stdTestSampleData = testRecordSampleDataService.getOne(Wrappers.<TestRecordSampleData>lambdaQuery().eq(TestRecordSampleData::getTestId, testRecordSampleData.getTestId()).eq(TestRecordSampleData::getCompoundName, testRecordSampleData.getCompoundName()).eq(TestRecordSampleData::getSampleType, TestRecordSampleDataConstant.SAMPLE_TYPE_STD)); | 
			
		
	
		
			
				
					|  |  |  |  |                             stdDataExpandList = super.list(Wrappers.<TestRecordSampleDataExpand>lambdaQuery().eq(TestRecordSampleDataExpand::getTestDataId, stdTestSampleData.getId())); | 
			
		
	
		
			
				
					|  |  |  |  |                         } | 
			
		
	
		
			
				
					|  |  |  |  |                         List<TestRecordSampleDataExpand> finalStdDataExpandList = stdDataExpandList; | 
			
		
	
		
			
				
					|  |  |  |  |                         testRecordSampleData.setIsDetected(1); // 默认检出
 | 
			
		
	
		
			
				
					|  |  |  |  |                         value.forEach(sampleDataExpand -> { | 
			
		
	
		
			
				
					|  |  |  |  |                             calculateIonAbundanceRatio(basePeakData, testRecord, finalStdDataExpandList, sampleDataExpand); | 
			
		
	
		
			
				
					|  |  |  |  |                             if (!isSTDSample && testRecordSampleData.getIsDetected().equals(1) && sampleDataExpand.getIonAbundanceRatioWithinError().equals(TestRecordSampleDataConstant.NO)) { | 
			
		
	
		
			
				
					|  |  |  |  |                                 testRecordSampleData.setIsDetected(0); | 
			
		
	
		
			
				
					|  |  |  |  |                             } | 
			
		
	
		
			
				
					|  |  |  |  |                             calculateAfterResult.add(sampleDataExpand); | 
			
		
	
		
			
				
					|  |  |  |  |                         }); | 
			
		
	
		
			
				
					|  |  |  |  |                         testRecordSampleData.setWhetherCheckOut( | 
			
		
	
		
			
				
					|  |  |  |  |                                 (testRecordSampleData.getIsDetected().equals(1) ? TestRecordSampleDataConstant.CHECK_OUT : TestRecordSampleDataConstant.NOT_CHECK_OUT) | 
			
		
	
		
			
				
					|  |  |  |  |                                         + testRecordSampleData.getCompoundCnName() | 
			
		
	
		
			
				
					|  |  |  |  |                         ); | 
			
		
	
		
			
				
					|  |  |  |  |                         testRecordSampleDataService.updateById(testRecordSampleData); | 
			
		
	
		
			
				
					|  |  |  |  |                     } | 
			
		
	
		
			
				
					|  |  |  |  | 
 | 
			
		
	
		
			
				
					|  |  |  |  |                 }); | 
			
		
	
		
			
				
					|  |  |  |  |                 if (CollUtil.isNotEmpty(calculateAfterResult)) { | 
			
		
	
		
			
				
					|  |  |  |  |                     super.updateBatchById(calculateAfterResult); | 
			
		
	
		
			
				
					|  |  |  |  |                 } | 
			
		
	
		
			
				
					|  |  |  |  |             }); | 
			
		
	
		
			
				
					|  |  |  |  | //            });
 | 
			
		
	
		
			
				
					|  |  |  |  |             return Boolean.TRUE; | 
			
		
	
		
			
				
					|  |  |  |  |         } | 
			
		
	
		
			
				
					|  |  |  |  |         return Boolean.FALSE; | 
			
		
	
	
		
			
				
					|  |  |  | @ -238,7 +218,7 @@ public class TestRecordSampledataExpandServiceImpl extends ServiceImpl<TestRecor | 
			
		
	
		
			
				
					|  |  |  |  |      */ | 
			
		
	
		
			
				
					|  |  |  |  |     private void calculateIonAbundanceRatio(TestRecordSampleDataExpand basePeakData, TestRecord testRecord, List<TestRecordSampleDataExpand> finalStdDataExpandList, TestRecordSampleDataExpand sampleDataExpand) { | 
			
		
	
		
			
				
					|  |  |  |  |         if (sampleDataExpand.getPeakArea() != null && basePeakData.getPeakArea() != null) { | 
			
		
	
		
			
				
					|  |  |  |  |             sampleDataExpand.setIonAbundanceRatio(sampleDataExpand.getPeakArea().divide(basePeakData.getPeakArea(), 5, BigDecimal.ROUND_HALF_UP)); | 
			
		
	
		
			
				
					|  |  |  |  |             sampleDataExpand.setIonAbundanceRatio(sampleDataExpand.getPeakArea().divide(basePeakData.getPeakArea(), 5, BigDecimal.ROUND_HALF_UP).multiply(BigDecimal.valueOf(100))); | 
			
		
	
		
			
				
					|  |  |  |  |             if (CollUtil.isNotEmpty(finalStdDataExpandList)) { | 
			
		
	
		
			
				
					|  |  |  |  |                 finalStdDataExpandList.forEach(stdDataExpand -> { | 
			
		
	
		
			
				
					|  |  |  |  |                     if (stdDataExpand.getMassToChargeRatio().equals(sampleDataExpand.getMassToChargeRatio())) { | 
			
		
	
	
		
			
				
					|  |  |  | 
 |