From a5b6c6d16634f1fa14edf6fe5e7dd6080bef1772 Mon Sep 17 00:00:00 2001
From: yeg <1347747024@qq.com>
Date: 星期三, 02 九月 2026 12:42:49 +0800
Subject: [PATCH] 限制接收数据归属在测试开始后

---
 ruoyi-admin/src/test/java/com/ruoyi/system/service/impl/TestDataReceptionServiceImplTest.java |   76 ++++++++++++++++++++++++++++++++++++++
 ruoyi-system/src/main/java/com/ruoyi/system/service/impl/TestDataReceptionServiceImpl.java    |    3 +
 2 files changed, 79 insertions(+), 0 deletions(-)

diff --git a/ruoyi-admin/src/test/java/com/ruoyi/system/service/impl/TestDataReceptionServiceImplTest.java b/ruoyi-admin/src/test/java/com/ruoyi/system/service/impl/TestDataReceptionServiceImplTest.java
new file mode 100644
index 0000000..794525c
--- /dev/null
+++ b/ruoyi-admin/src/test/java/com/ruoyi/system/service/impl/TestDataReceptionServiceImplTest.java
@@ -0,0 +1,76 @@
+package com.ruoyi.system.service.impl;
+
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.anyString;
+import static org.mockito.ArgumentMatchers.anyInt;
+import static org.mockito.ArgumentMatchers.eq;
+import static org.mockito.ArgumentMatchers.isNull;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+import java.time.Instant;
+import java.util.Date;
+import java.util.List;
+import java.math.BigDecimal;
+import org.junit.jupiter.api.Test;
+import com.ruoyi.device.bts.BtsDevelopmentSimulator;
+import com.ruoyi.device.simulator.BtsSimulatorStateStore.BtsHistoryRecord;
+import com.ruoyi.system.domain.TTest;
+import com.ruoyi.system.domain.TTestBattery;
+import com.ruoyi.system.domain.TTestChannelBinding;
+import com.ruoyi.system.mapper.TTestMapper;
+import com.ruoyi.system.service.ITTestService;
+
+/** 接收服务只应把本次测试开始后的模拟器事实归入测试。 */
+class TestDataReceptionServiceImplTest
+{
+    @Test
+    void ignoresRecordsBeforeReceptionStart()
+    {
+        TTestMapper mapper = mock(TTestMapper.class);
+        ITTestService tests = mock(ITTestService.class);
+        BtsDevelopmentSimulator simulator = mock(BtsDevelopmentSimulator.class);
+        TTest test = test(Date.from(Instant.parse("2026-09-02T04:00:00Z")));
+        when(mapper.selectReceivingTestIds()).thenReturn(List.of(1L));
+        when(mapper.selectTestByIdForUpdate(1L)).thenReturn(test);
+        when(tests.selectTestById(1L)).thenReturn(test);
+        when(simulator.btsRecords(1, 1)).thenReturn(List.of(
+                record(1L, "2026-09-02T03:59:59Z"), record(2L, "2026-09-02T04:00:01Z")));
+        when(mapper.selectBtsSampleContentHash(any(), anyString(), any())).thenReturn(null);
+        when(mapper.insertBtsSample(any(), any(), any(), anyString(), any(), any(Date.class), anyString(),
+                any(Date.class), anyInt(), anyInt(), anyString(), anyString(), isNull(), any(BigDecimal.class),
+                any(BigDecimal.class), any(BigDecimal.class), any(BigDecimal.class), any(byte[].class))).thenReturn(1);
+
+        new TestDataReceptionServiceImpl(mapper, tests, simulator).captureDevelopmentCabinetRecords();
+
+        verify(mapper, times(1)).insertBtsSample(eq(1L), eq(2L), eq(3L), anyString(), eq(2L), any(Date.class),
+                anyString(), any(Date.class), anyInt(), anyInt(), anyString(), anyString(), isNull(),
+                any(BigDecimal.class), any(BigDecimal.class), any(BigDecimal.class), any(BigDecimal.class),
+                any(byte[].class));
+    }
+
+    private static TTest test(Date startedAt)
+    {
+        TTestChannelBinding binding = new TTestChannelBinding();
+        binding.setBindingId(2L);
+        binding.setChannelId(3L);
+        binding.setDevid(1);
+        binding.setChlid(1);
+        TTestBattery battery = new TTestBattery();
+        battery.setBinding(binding);
+        TTest test = new TTest();
+        test.setTestId(1L);
+        test.setReceptionStatus("RECEIVING");
+        test.setStartedAtUtc(startedAt);
+        test.setBatteries(List.of(battery));
+        return test;
+    }
+
+    private static BtsHistoryRecord record(long sequenceId, String deviceTime)
+    {
+        return new BtsHistoryRecord(sequenceId, Instant.parse(deviceTime), 1d, 2d, 3d, 4d,
+                1, 1, "恒流充电", "cc");
+    }
+}
diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/TestDataReceptionServiceImpl.java b/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/TestDataReceptionServiceImpl.java
index 874f0ce..e7d8555 100644
--- a/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/TestDataReceptionServiceImpl.java
+++ b/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/TestDataReceptionServiceImpl.java
@@ -51,6 +51,7 @@
 
     private void captureTest(TTest test)
     {
+        Date startedAt = test.getStartedAtUtc();
         for (TTestBattery battery : test.getBatteries() == null ? List.<TTestBattery>of() : test.getBatteries())
         {
             TTestChannelBinding binding = battery.getBinding();
@@ -60,6 +61,8 @@
             catch (IllegalArgumentException ignored) { continue; }
             for (BtsHistoryRecord record : records)
             {
+                // 模拟器保留通道历史;只接收本次测试开始后产生的设备时间记录,避免顺序测试串入旧事实。
+                if (startedAt != null && record.deviceTime().isBefore(startedAt.toInstant())) continue;
                 save(test, binding, record);
             }
         }

--
Gitblit v1.7.1