Ver Fonte

fix(water): stabilize monitoring trend and export permissions (#8)

Kazerin há 4 dias atrás
pai
commit
0b86e9960f

+ 19 - 5
pipe-network-service/zksy-system/src/main/java/com/zksy/base/service/impl/MonitoringServiceImpl.java

@@ -82,7 +82,8 @@ public class MonitoringServiceImpl implements MonitoringService {
 
             FlowMonitoringVO vo = new FlowMonitoringVO();
             fillBaseFields(vo, device, typeMap.get(device.getEquipmentTypeId()));
-            if (radar != null) {
+            if (radar != null && (telemetry == null
+                    || compareTime(radar.getCreateTime(), telemetry.getCreateTime()) >= 0)) {
                 vo.setDataSource(RADAR_SOURCE);
                 vo.setInstantFlow(radar.getMeter1InstantFlow());
                 vo.setFlowSpeed(radar.getFlowSpeed());
@@ -197,7 +198,7 @@ public class MonitoringServiceImpl implements MonitoringService {
         List<RadarData> radarList = radarDataMapper.selectList(
                 new LambdaQueryWrapper<RadarData>()
                         .eq(RadarData::getDeviceCode, code)
-                        .ge(RadarData::getCreateTime, start)
+                        .ge(start != null, RadarData::getCreateTime, start)
                         .le(endTime != null, RadarData::getCreateTime, endTime)
                         .orderByAsc(RadarData::getCreateTime));
         if (CollUtil.isNotEmpty(radarList)) {
@@ -214,7 +215,7 @@ public class MonitoringServiceImpl implements MonitoringService {
         List<TelemetryData> telemetryList = telemetryDataMapper.selectList(
                 new LambdaQueryWrapper<TelemetryData>()
                         .eq(TelemetryData::getDeviceCode, code)
-                        .ge(TelemetryData::getCreateTime, start)
+                        .ge(start != null, TelemetryData::getCreateTime, start)
                         .le(endTime != null, TelemetryData::getCreateTime, endTime)
                         .orderByAsc(TelemetryData::getCreateTime));
         for (TelemetryData data : telemetryList) {
@@ -238,7 +239,7 @@ public class MonitoringServiceImpl implements MonitoringService {
         List<FirefightingPressure> list = firefightingPressureMapper.selectList(
                 new LambdaQueryWrapper<FirefightingPressure>()
                         .eq(FirefightingPressure::getTelemeteringStation, device.getEquipmentCode())
-                        .ge(FirefightingPressure::getCreateTime, start)
+                        .ge(start != null, FirefightingPressure::getCreateTime, start)
                         .le(endTime != null, FirefightingPressure::getCreateTime, endTime)
                         .orderByAsc(FirefightingPressure::getCreateTime));
 
@@ -265,7 +266,7 @@ public class MonitoringServiceImpl implements MonitoringService {
         List<NoiseInfo> list = noiseInfoMapper.selectList(
                 new LambdaQueryWrapper<NoiseInfo>()
                         .eq(NoiseInfo::getEncode, device.getEquipmentCode())
-                        .ge(NoiseInfo::getCreateTime, start)
+                        .ge(start != null, NoiseInfo::getCreateTime, start)
                         .le(endTime != null, NoiseInfo::getCreateTime, endTime)
                         .orderByAsc(NoiseInfo::getCreateTime));
 
@@ -368,6 +369,19 @@ public class MonitoringServiceImpl implements MonitoringService {
         return result;
     }
 
+    private int compareTime(LocalDateTime left, LocalDateTime right) {
+        if (left == null && right == null) {
+            return 0;
+        }
+        if (left == null) {
+            return -1;
+        }
+        if (right == null) {
+            return 1;
+        }
+        return left.compareTo(right);
+    }
+
     private LocalDateTime getTime(Object value) {
         if (value instanceof RadarData) return ((RadarData) value).getCreateTime();
         if (value instanceof TelemetryData) return ((TelemetryData) value).getCreateTime();

+ 62 - 0
pipe-network-service/zksy-system/src/test/java/com/zksy/base/service/impl/MonitoringServiceImplTest.java

@@ -24,8 +24,10 @@ import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.Test;
 import org.apache.ibatis.builder.MapperBuilderAssistant;
 import org.springframework.test.util.ReflectionTestUtils;
+import org.mockito.invocation.InvocationOnMock;
 
 import java.time.LocalDateTime;
+import java.util.Objects;
 import java.util.Collection;
 import java.util.Arrays;
 import java.util.Collections;
@@ -114,6 +116,28 @@ class MonitoringServiceImplTest {
         assertEquals(BASE_TIME.minusHours(1), row.getMonitorTime());
     }
 
+    @Test
+    void flowPaginationUsesTheNewestQualifyingMeasurementAcrossDataSources() {
+        when(equipmentBaseMapper.selectList(any())).thenReturn(Collections.singletonList(
+                device("flow-1", "WS-FLOW-1", "流量监测点1", "flow-type", BASE_TIME)
+        ));
+        when(radarDataMapper.selectList(any())).thenReturn(Collections.singletonList(
+                radar("WS-FLOW-1", "30", BASE_TIME.minusHours(2))
+        ));
+        when(telemetryDataMapper.selectList(any())).thenReturn(Collections.singletonList(
+                telemetry("WS-FLOW-1", "35", BASE_TIME.minusHours(1))
+        ));
+
+        Page<FlowMonitoringVO> page = service.getFlowMonitorPage(
+                1, 10, null, null, null, null, null, 20D, 40D);
+
+        assertEquals(1, page.getTotal());
+        FlowMonitoringVO row = page.getRecords().get(0);
+        assertEquals("遥测终端", row.getDataSource());
+        assertEquals("35", row.getInstantFlow());
+        assertEquals(BASE_TIME.minusHours(1), row.getMonitorTime());
+    }
+
     @Test
     void pressurePaginationTotalsFollowMeasurementFiltering() {
         when(equipmentBaseMapper.selectList(any())).thenReturn(Arrays.asList(
@@ -291,6 +315,27 @@ class MonitoringServiceImplTest {
         assertEquals("20", trend.get(1).get("instantFlow"));
     }
 
+    @Test
+    void trendQueriesWithOnlyAnEndTimeDoNotBindNullStartTime() {
+        EquipmentBase flowDevice = device("flow-1", "WS-FLOW-1", "流量监测点1", "flow-type", BASE_TIME);
+        EquipmentBase pressureDevice = device("pressure-1", "WS-PR-1", "压力监测点1", "pressure-type", BASE_TIME);
+        EquipmentBase leakageDevice = device("leakage-1", "WS-LK-1", "漏失监测点1", "leakage-type", BASE_TIME);
+        when(equipmentBaseMapper.selectById("flow-1")).thenReturn(flowDevice);
+        when(equipmentBaseMapper.selectById("pressure-1")).thenReturn(pressureDevice);
+        when(equipmentBaseMapper.selectById("leakage-1")).thenReturn(leakageDevice);
+
+        when(radarDataMapper.selectList(any())).thenAnswer(invocation -> recordsWithoutNullBounds(
+                invocation, Collections.singletonList(radar("WS-FLOW-1", "10", BASE_TIME.minusDays(3)))));
+        when(pressureMapper.selectList(any())).thenAnswer(invocation -> recordsWithoutNullBounds(
+                invocation, Collections.singletonList(pressure("WS-PR-1", 0.2D, BASE_TIME.minusDays(3)))));
+        when(noiseMapper.selectList(any())).thenAnswer(invocation -> recordsWithoutNullBounds(
+                invocation, Collections.singletonList(noise("WS-LK-1", 2D, BASE_TIME.minusDays(3)))));
+
+        assertEquals(1, service.getFlowTrend("flow-1", null, BASE_TIME).size());
+        assertEquals(1, service.getPressureTrend("pressure-1", null, BASE_TIME).size());
+        assertEquals(1, service.getLeakageTrend("leakage-1", null, BASE_TIME).size());
+    }
+
     @Test
     void requestedPageBeyondMeasurementResultsKeepsTheFilteredTotal() {
         when(equipmentBaseMapper.selectList(any())).thenReturn(Collections.singletonList(
@@ -326,6 +371,14 @@ class MonitoringServiceImplTest {
         return values;
     }
 
+    private static <T> List<T> recordsWithoutNullBounds(InvocationOnMock invocation, List<T> records) {
+        @SuppressWarnings("unchecked")
+        LambdaQueryWrapper<T> wrapper = invocation.getArgument(0);
+        assertTrue(wrapper.getParamNameValuePairs().values().stream().noneMatch(Objects::isNull),
+                "trend query must not bind a null start time");
+        return records;
+    }
+
     private static EquipmentType type(String id, String typeId, String name, String parentTypeId) {
         EquipmentType type = new EquipmentType();
         type.setId(id);
@@ -355,6 +408,15 @@ class MonitoringServiceImplTest {
         return data;
     }
 
+    private static TelemetryData telemetry(String code, String flow, LocalDateTime time) {
+        TelemetryData data = new TelemetryData();
+        data.setDeviceCode(code);
+        data.setMeter1InstantFlow(flow);
+        data.setFlowSpeed("1.1");
+        data.setCreateTime(time);
+        return data;
+    }
+
     private static FirefightingPressure pressure(String code, Double value, LocalDateTime time) {
         FirefightingPressure data = new FirefightingPressure();
         data.setTelemeteringStation(code);