package com.ruoyi.system.service.impl; import java.util.List; import java.util.LinkedHashMap; import java.util.Map; import java.util.HashMap; import com.alibaba.fastjson2.JSON; import org.springframework.stereotype.Service; import com.ruoyi.common.exception.ServiceException; import com.ruoyi.system.domain.TTestDataOverride; import com.ruoyi.system.mapper.TTestMapper; import com.ruoyi.system.service.ITTestService; import com.ruoyi.system.service.ITestDataQueryService; /** 测试数据查询模块;实现不访问设备或模拟器历史。 */ @Service public class TestDataQueryServiceImpl implements ITestDataQueryService { private final TTestMapper mapper; private final ITTestService tests; public TestDataQueryServiceImpl(TTestMapper mapper, ITTestService tests) { this.mapper = mapper; this.tests = tests; } @Override public List> records(Long testId) { requireTest(testId); return applyEffectiveData(testId, "REALTIME", mapper.selectPersistedRecords(testId)); } @Override public List> steps(Long testId) { requireTest(testId); return applyEffectiveData(testId, "STEP", mapper.selectPersistedSteps(testId)); } @Override public List> cycles(Long testId) { requireTest(testId); return applyEffectiveData(testId, "CYCLE", mapper.selectPersistedCycles(testId)); } /** 将最新人工覆盖应用到设备事实,删除只影响当前有效视图,不抹除原始采样。 */ private List> applyEffectiveData(Long testId, String dataType, List> source) { List overrides = mapper.selectDataOverrides(testId, dataType); if (source == null || source.isEmpty() || overrides == null || overrides.isEmpty()) return source == null ? List.of() : source; Map byIdentity = new HashMap<>(); for (TTestDataOverride override : overrides) if (override != null && override.getStableIdentity() != null) byIdentity.put(override.getStableIdentity(), override); List> effective = new java.util.ArrayList<>(source.size()); for (Map row : source) { String identity = stableIdentity(dataType, row); TTestDataOverride override = byIdentity.get(identity); if (override != null && Boolean.TRUE.equals(override.getDeleted())) continue; Map copy = new LinkedHashMap<>(row); if (override != null && override.getFieldValues() != null && !override.getFieldValues().isBlank()) { try { Map values = JSON.parseObject(override.getFieldValues(), Map.class); if (values != null) copy.putAll(values); } catch (RuntimeException ignored) { // 覆盖已由修订服务校验;解析异常时保留原始事实,避免查询整体失败。 } } effective.add(copy); } return effective; } private String stableIdentity(String dataType, Map row) { String deviceId = value(row, "deviceId"); String channelId = value(row, "channelId"); String cycleNo = value(row, "cycleNo"); if ("REALTIME".equals(dataType)) return deviceId + ":" + channelId + ":" + value(row, "sequenceId"); if ("STEP".equals(dataType)) return "step:" + deviceId + ":" + channelId + ":" + cycleNo + ":" + value(row, "stepNo"); return "cycle:" + deviceId + ":" + channelId + ":" + cycleNo; } private String value(Map row, String key) { Object value = row == null ? null : row.get(key); return value == null ? "" : String.valueOf(value); } private void requireTest(Long testId) { if (testId == null || tests.selectTestById(testId) == null) throw new ServiceException("测试不存在"); } }