From 3cf717d9c9cb1426f25bd5cd886ec20873058e39 Mon Sep 17 00:00:00 2001
From: wangyifei <1522824457@qq.com>
Date: 星期三, 30 十月 2024 15:57:06 +0800
Subject: [PATCH] 获取降水曲线文件曲线图功能第一次提交

---
 src/main/java/com/se/simu/service/ResultService.java      |   38 +++++++++++++++++++
 src/main/java/com/se/simu/controller/WaterController.java |   17 ++++++++
 src/main/java/com/se/simu/service/WaterService.java       |   24 ++++++++++++
 3 files changed, 79 insertions(+), 0 deletions(-)

diff --git a/src/main/java/com/se/simu/controller/WaterController.java b/src/main/java/com/se/simu/controller/WaterController.java
index 6d25649..cbeaa71 100644
--- a/src/main/java/com/se/simu/controller/WaterController.java
+++ b/src/main/java/com/se/simu/controller/WaterController.java
@@ -102,6 +102,23 @@
         }
     }
 
+    @ApiOperation(value = "鑾峰彇闄嶆按鏇茬嚎鏂囦欢鏇茬嚎鍥�")
+    @GetMapping("/{serviceName}/rainfall.json")
+    public void getRainfall(@PathVariable String serviceName, HttpServletResponse res) {
+        try {
+            if (!validate(serviceName, res)) {
+                return;
+            }
+
+            byte[] bytes = waterService.getRainfall(serviceName);
+
+            WebHelper.writeBytes(bytes, res);
+        } catch (Exception ex) {
+            log.error(ex.getMessage(), ex);
+            WebHelper.writeStr2Page(res, HttpStatus.INTERNAL_SERVER_ERROR, ex.getMessage());
+        }
+    }
+
     private boolean validate(String serviceName, HttpServletResponse res) {
         if (WebHelper.isEmpty(serviceName)) {
             return WebHelper.writeJson2Page(res, HttpStatus.BAD_REQUEST, "鏈嶅姟鍚嶄笉鑳戒负绌�");
diff --git a/src/main/java/com/se/simu/service/ResultService.java b/src/main/java/com/se/simu/service/ResultService.java
index 347cf72..e1da3df 100644
--- a/src/main/java/com/se/simu/service/ResultService.java
+++ b/src/main/java/com/se/simu/service/ResultService.java
@@ -23,6 +23,10 @@
 import java.awt.*;
 import java.awt.image.BufferedImage;
 import java.io.*;
+import java.math.BigDecimal;
+import java.math.RoundingMode;
+import java.text.ParseException;
+import java.text.SimpleDateFormat;
 import java.util.*;
 import java.util.List;
 
@@ -61,6 +65,7 @@
             copeWater(dto, layer);
             copeFlow(dto, layer);
             copeLayerJson(dto, layer);
+            copeRainFallJson(dto, layer);
         } finally {
             File dir = new File(dto.getTemp());
             if (dir.exists()) {
@@ -525,4 +530,37 @@
         bw.close();
         fw.close();
     }
+
+    /**
+     * 澶勭悊闄嶆按鏇茬嚎鏂囦欢鏇茬嚎鍥� 
+     */
+    public void copeRainFallJson(ResultDto dto, LayerDto layer) throws IOException, ParseException {
+        String rainGageFilePath = config.getInPath() + File.separator + dto.getServiceName() + File.separator + "RainGage.dat";
+        String filePath = dto.getOutPath() + File.separator + "rainfall.json";
+
+        String line;
+        Map<Long,Double> rainFallJsons = new LinkedHashMap<>();
+        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
+
+        BufferedReader br = new BufferedReader(new FileReader(rainGageFilePath));
+            //澶勭悊绗竴琛屾暟鎹�
+            if ((line = br.readLine()) != null) {
+                while ((line = br.readLine()) != null) {
+                    // 澶勭悊姣忎竴琛屾暟鎹�
+                    String[] rainFall = line.split(" ");
+
+                    if (rainFall.length < 7) continue;
+
+                    String sdt = rainFall[1]+"-"+rainFall[2]+"-"+rainFall[3]+" "+rainFall[4]+":"+rainFall[5];
+                    BigDecimal num = new BigDecimal(rainFall[6]);
+                    rainFallJsons.put(sdf.parse(sdt).getTime()/1000,num.setScale(2, RoundingMode.HALF_UP).doubleValue());
+                }
+            }
+
+        FileWriter fw = new FileWriter(filePath);
+        BufferedWriter bw = new BufferedWriter(fw);
+        bw.write(JSON.toJSONString(rainFallJsons));
+        bw.close();
+        fw.close();
+    }
 }
diff --git a/src/main/java/com/se/simu/service/WaterService.java b/src/main/java/com/se/simu/service/WaterService.java
index e2ea59d..d49d75a 100644
--- a/src/main/java/com/se/simu/service/WaterService.java
+++ b/src/main/java/com/se/simu/service/WaterService.java
@@ -92,4 +92,28 @@
 
         return layer;
     }
+
+    /**
+     * 鑾峰彇闄嶆按鏇茬嚎鏂囦欢鏇茬嚎鍥�
+     */
+    public byte[] getRainfall(String serviceName) {
+        try {
+            String filePath = config.getOutPath() + File.separator + serviceName + File.separator + "rainfall.json";
+
+            File rainfall = new File(filePath);
+            if (!rainfall.exists()) {
+                return null;
+            }
+
+            byte[] bytes = new byte[(int) rainfall.length()];
+
+            FileInputStream fs = new FileInputStream(filePath);
+            fs.read(bytes);
+            fs.close();
+
+            return bytes;
+        } catch (Exception ex) {
+            return null;
+        }
+    }
 }

--
Gitblit v1.9.3