package com.battery.business.service.impl; import java.time.Instant; import java.util.Date; import java.util.List; import java.util.Map; import org.springframework.stereotype.Service; import com.battery.business.mapper.TTestMapper; import com.battery.business.service.ITTestService; import com.battery.business.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) { tests.selectTestById(testId); return normalizeApiTimes(mapper.selectPersistedFinalRealtimeRecords(testId)); } /** 查询设备实际提供且尚存的工步记录。 */ @Override public List> steps(Long testId) { tests.selectTestById(testId); List> rows = normalizeApiTimes(mapper.selectPersistedFinalStepRecords(testId)); for (Map row : rows) { Object extension = row.get("extensionMeasurementsJson"); if (extension == null) continue; Map values = com.alibaba.fastjson2.JSON.parseObject(extension.toString()); for (String field : List.of("stepDuration", "startVoltage", "chargePeakVoltage", "dischargeValleyVoltage")) if (values.containsKey(field)) row.put(field, values.get(field)); } return rows; } /** 查询设备实际提供且尚存的循环记录。 */ @Override public List> cycles(Long testId) { tests.selectTestById(testId); return normalizeApiTimes(mapper.selectPersistedFinalCycleRecords(testId)); } private List> normalizeApiTimes(List> rows) { if (rows == null) return List.of(); for (Map row : rows) row.replaceAll((key, value) -> value instanceof Date date ? date.getTime() : value instanceof Instant instant ? instant.toEpochMilli() : value); return rows; } }