From 59b432c883011119649c283cc9d4c1d357599802 Mon Sep 17 00:00:00 2001
From: dcb <xgybdcb@163.com>
Date: 星期一, 30 六月 2025 15:13:56 +0800
Subject: [PATCH] 对于没有不能获取到数据的雨量计,使用其他雨量计数据填充

---
 src/main/java/com/se/nsl/service/RealTimeSimulationService.java |  176 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++--
 1 files changed, 169 insertions(+), 7 deletions(-)

diff --git a/src/main/java/com/se/nsl/service/RealTimeSimulationService.java b/src/main/java/com/se/nsl/service/RealTimeSimulationService.java
index 404d4c3..9c1622b 100644
--- a/src/main/java/com/se/nsl/service/RealTimeSimulationService.java
+++ b/src/main/java/com/se/nsl/service/RealTimeSimulationService.java
@@ -237,11 +237,16 @@
         List<String> newLines = new ArrayList<>();
         newLines.add(title);
         Map<String, Integer> deviceInfoMap = deviceInfos.stream().collect(Collectors.toMap(DeviceInfo::getId, DeviceInfo::getMappingId));
+        List<RainRecord> rainRecords = new ArrayList<>();
         for (RainGauge gauge : gauges) {
             String id = gauge.getId();
             Integer mappingId = deviceInfoMap.getOrDefault(id, 0);
-            String l = getRainGaugeResult(gauge, mappingId, currentTime);
-            newLines.add(l);
+            RainRecord rr = getRainGaugeResult(gauge, mappingId, currentTime);
+            rainRecords.add(rr);
+        }
+        fillEmptyValue(rainRecords); //濉厖娌℃湁鍊肩殑闆ㄩ噺璁�
+        for (RainRecord rr : rainRecords) {
+            newLines.add(rr.toString());
         }
         File newDatFile = new File(serviceNameDir, "rainfall_" + currentTime + ".dat");
         if (!newDatFile.exists()) newDatFile.createNewFile();
@@ -249,7 +254,52 @@
         return newDatFile;
     }
 
-    private String getRainGaugeResult(RainGauge gauge,int stationId, long currentTime) throws JsonProcessingException {
+    //灏嗛洦閲忚涓┖鍊硷紙intensity涓�-1锛夌殑閮ㄥ垎濉厖鍊�
+    private void fillEmptyValue(List<RainRecord> rainRecords) {
+        double[] rainValues = rainRecords.stream().mapToDouble(r -> r.getIntensity()).toArray();
+        double[] forwardValues = forwardFill(rainValues);
+        double[] backwordValues = backwordFill(rainValues);
+        for (int i = 0; i < rainRecords.size(); i++) {
+            RainRecord rr = rainRecords.get(i);
+            double filledValue = forwardValues[i] != -1 ? forwardValues[i] : backwordValues[i];
+            rr.setIntensity(filledValue);
+        }
+        //濡傛灉閮芥槸-1锛屽垯璁剧疆涓�0
+        for (RainRecord rr : rainRecords) {
+            double intensity = rr.getIntensity();
+            if (intensity == -1) {
+                rr.setIntensity(0);
+            }
+        }
+    }
+
+    private double[] forwardFill(double[] rainValues) {
+        double[] result = Arrays.copyOf(rainValues, rainValues.length);
+        double lastValid = -1D;
+        for (int i = 0; i < result.length; i++) {
+            if (result[i] != -1) {
+                lastValid = result[i];
+            } else if (lastValid != -1) {
+                result[i] = lastValid;
+            }
+        }
+        return result;
+    }
+
+    private double[] backwordFill(double[] rainValues) {
+        double[] result = Arrays.copyOf(rainValues, rainValues.length);
+        double lastValid = -1D;
+        for (int i = result.length - 1; i >= 0; i--) {
+            if (result[i] != -1) {
+                lastValid = result[i];
+            } else if (lastValid != -1) {
+                result[i] = lastValid;
+            }
+        }
+        return result;
+    }
+
+    private RainRecord getRainGaugeResult(RainGauge gauge,int stationId, long currentTime) throws JsonProcessingException {
         double lon = gauge.getX();
         double lat = gauge.getY();
         LocalDateTime dateTime = TimeFormatUtil.toDate(currentTime);
@@ -263,8 +313,9 @@
         String startTime = TimeFormatUtil.formatTime(someMinutesAgo, YYYY_MM_DD_HH_MM_SS);
         String endTime = TimeFormatUtil.formatTime(currentTime, YYYY_MM_DD_HH_MM_SS);
         double intensity = getIntensityByDeviceId(id, startTime, endTime); //淇濈暀鎸囧畾浣嶆暟灏忔暟
-        return String.format("%s %s %s %s %s %s %s %s %s",
-                stationId, lon, lat, year, month, day, hour, minute, String.format("%.6f", intensity));
+//        return String.format("%s %s %s %s %s %s %s %s %s",
+//                stationId, lon, lat, year, month, day, hour, minute, String.format("%.6f", intensity));
+        return new RainRecord(stationId, lon, lat, year, month, day, hour, minute, intensity);
     }
 
     //鏍规嵁闆ㄩ噺璁℃爣璇嗘煡璇㈤洦閲忔暟鎹�
@@ -285,9 +336,8 @@
         if (!pageData.isEmpty()) {
             return pageData.get(0).get("value").asDouble();
         }
-        return 0;
+        return -1D;
     }
-
 
     private String callBat(String cmd) {
         try {
@@ -309,4 +359,116 @@
         }
     }
 
+    static class RainRecord {
+        private int stationId;
+        private double lon;
+        private double lat;
+        private  int year;
+        private int month;
+        private int day;
+        private int hour;
+        private int minute;
+        private double intensity; //-1浠h〃娌℃湁鏁板��
+
+        public RainRecord() {}
+
+        public RainRecord(int stationId, double lon, double lat, int year, int month,
+                          int day, int hour, int minute, double intensity) {
+            this.stationId = stationId;
+            this.lon = lon;
+            this.lat = lat;
+            this.year = year;
+            this.month = month;
+            this.day = day;
+            this.hour = hour;
+            this.minute = minute;
+            this.intensity = intensity;
+        }
+
+        public int getStationId() {
+            return stationId;
+        }
+
+        public void setStationId(int stationId) {
+            this.stationId = stationId;
+        }
+
+        public double getLon() {
+            return lon;
+        }
+
+        public void setLon(double lon) {
+            this.lon = lon;
+        }
+
+        public double getLat() {
+            return lat;
+        }
+
+        public void setLat(double lat) {
+            this.lat = lat;
+        }
+
+        public int getYear() {
+            return year;
+        }
+
+        public void setYear(int year) {
+            this.year = year;
+        }
+
+        public int getMonth() {
+            return month;
+        }
+
+        public void setMonth(int month) {
+            this.month = month;
+        }
+
+        public int getDay() {
+            return day;
+        }
+
+        public void setDay(int day) {
+            this.day = day;
+        }
+
+        public int getHour() {
+            return hour;
+        }
+
+        public void setHour(int hour) {
+            this.hour = hour;
+        }
+
+        public int getMinute() {
+            return minute;
+        }
+
+        public void setMinute(int minute) {
+            this.minute = minute;
+        }
+
+        public double getIntensity() {
+            return intensity;
+        }
+
+        public void setIntensity(double intensity) {
+            this.intensity = intensity;
+        }
+
+        @Override
+        public String toString() {
+            return "" + stationId +
+                    " " + lon +
+                    " " + lat +
+                    " " + year +
+                    " " + month +
+                    " " + day +
+                    " " + hour +
+                    " " + minute +
+                    " " + String.format("%.6f", intensity);
+        }
+    }
+
 }

--
Gitblit v1.9.3