From 043d1bd4860bbee636b3935e94a71246091cbf3f Mon Sep 17 00:00:00 2001
From: dcb <xgybdcb@163.com>
Date: 星期二, 24 六月 2025 15:29:24 +0800
Subject: [PATCH] 实时模拟功能实现

---
 src/main/java/com/se/nsl/controller/SimuController.java |   85 ++++++++++++++++++++++++++++++++++++++----
 1 files changed, 77 insertions(+), 8 deletions(-)

diff --git a/src/main/java/com/se/nsl/controller/SimuController.java b/src/main/java/com/se/nsl/controller/SimuController.java
index f4df136..f4d0076 100644
--- a/src/main/java/com/se/nsl/controller/SimuController.java
+++ b/src/main/java/com/se/nsl/controller/SimuController.java
@@ -1,16 +1,20 @@
 package com.se.nsl.controller;
 
 import com.alibaba.fastjson.JSON;
+import com.alibaba.fastjson.JSONObject;
 import com.baomidou.mybatisplus.core.metadata.IPage;
 import com.se.nsl.domain.po.Simu;
 import com.se.nsl.domain.po.SimuData;
-import com.se.nsl.domain.vo.R;
-import com.se.nsl.domain.vo.SimuVo;
+import com.se.nsl.domain.vo.*;
+import com.se.nsl.service.RealTimeSimulationService;
 import com.se.nsl.service.ResolveService;
 import com.se.nsl.service.SimuService;
+import com.se.nsl.utils.SimulateType;
 import io.swagger.annotations.Api;
 import io.swagger.annotations.ApiOperation;
 import lombok.extern.slf4j.Slf4j;
+import org.gdal.ogr.Geometry;
+import org.gdal.ogr.ogr;
 import org.springframework.util.StringUtils;
 import org.springframework.web.bind.annotation.*;
 
@@ -28,6 +32,9 @@
 
     @Resource
     ResolveService resolveService;
+
+    @Resource
+    RealTimeSimulationService rts;
 
     /**
      * 鍒嗛〉鏌ヨ鎺ㄦ紨妯℃嫙
@@ -85,10 +92,25 @@
 
             SimuData data = JSON.parseObject(simu.getData(), SimuData.class);
             if (null == data) return fail("data鏁版嵁鏍煎紡(JSON)涓嶆纭�");
+            if (StringUtils.isEmpty(simu.getGeom())) return fail("geom瀛楃涓蹭笉鏄疻KT鏍煎紡");
+
+            Geometry geom = Geometry.CreateFromWkt(simu.getGeom());
+            if (!(geom.GetGeometryType() == ogr.wkbMultiPolygon || geom.GetGeometryType() == ogr.wkbPolygon))
+                return fail("geom瀵硅薄涓嶆槸澶氳竟褰�");
 
             int rows = simuService.insert(simu);
-
-            return success(rows);
+            System.out.println(String.format("id:%s", simu.getId()));
+            Integer type = data.getType();
+            SimulateType simulateType = SimulateType.of(type);
+            if (simulateType == SimulateType.HISTORY) {
+                if (rows > 0 && (null == data.getRainfalls() || data.getRainfalls().size() < 2)) {
+                    resolveService.createRainfall(simu);
+                    simuService.updateById(simu);
+                }
+            }
+            JSONObject json = new JSONObject();
+            json.put("id", simu.getId());
+            return success(json);
         } catch (Exception ex) {
             return fail(ex, null);
         }
@@ -107,11 +129,19 @@
             SimuData data = JSON.parseObject(simu.getData(), SimuData.class);
             if (null == data) return fail("鏂规鏁版嵁鏍煎紡(JSON)涓嶆纭�");
 
-            if (simu.getStatus() != 0) return fail("鏂规姝e湪杩愯鎴栧凡瀹屾垚");
+            //if (simu.getStatus() != 0) return fail("鏂规姝e湪杩愯鎴栧凡瀹屾垚");
+            if (StringUtils.isEmpty(simu.getGeom())) return fail("鏂规鐨勫浘褰负绌�");
 
-            int rows = resolveService.start(simu);
-
-            return success("ok");
+            Short type = simu.getType();
+            SimulateType simulateType = SimulateType.of(type);
+            if (simulateType == SimulateType.HISTORY) {
+                int rows = resolveService.start(simu);
+                return success("ok");
+            } else if (simulateType == SimulateType.REAL_TIME) {
+                String s = rts.realTimeSimulate(simu);
+                return success(s);
+            }
+            return fail("妯℃嫙绫诲瀷鏆備笉鏀寔");
         } catch (Exception ex) {
             return fail(ex, null);
         }
@@ -127,9 +157,48 @@
     @PutMapping(value = "/updateById", produces = "application/json; charset=UTF-8")
     public R<Object> updateById(@RequestBody Simu simu) {
         try {
+            if (StringUtils.isEmpty(simu.getData())) return fail("data涓虹┖");
+
+            SimuData data = JSON.parseObject(simu.getData(), SimuData.class);
+            if (null == data) return fail("data鏁版嵁鏍煎紡(JSON)涓嶆纭�");
+            if (StringUtils.isEmpty(simu.getGeom())) return fail("geom瀛楃涓蹭笉鏄疻KT鏍煎紡");
+
+            Geometry geom = Geometry.CreateFromWkt(simu.getGeom());
+            if (!(geom.GetGeometryType() == ogr.wkbMultiPolygon || geom.GetGeometryType() == ogr.wkbPolygon))
+                return fail("geom瀵硅薄涓嶆槸澶氳竟褰�");
+
+            if (null == data.getRainfalls() || data.getRainfalls().size() < 2) resolveService.createRainfall(simu);
+
             return success(simuService.updateById(simu));
         } catch (Exception ex) {
             return fail(ex, null);
         }
     }
+
+    @ApiOperation(value = "position")
+    @GetMapping("/position")
+    public R<Object> queryByPosition(double lon, double lat, Long time, String serviceName) {
+        if (lon > 180 || lon < -180) {
+            return clientError("缁忓害鑼冨洿搴旇鍦�-180鍒�180");
+        }
+        if (lat > 90 || lat < -90) {
+            return clientError("绾害鑼冨洿搴旇鍦�-90鍒�90");
+        }
+        if (serviceName == null || serviceName.trim().isEmpty()) {
+            return clientError("鏈嶅姟鍚嶄笉鑳戒负绌�");
+        }
+        if (time != null) {
+            SimuResult result = simuService.queryByPosition(lon, lat, time, serviceName);
+            if (result == null) {
+                return notFound("鏈煡鎵惧埌鐩稿叧鏁版嵁");
+            }
+            return success(result, 1);
+        } else {
+            List<SimuResult> simuResults = simuService.queryByPosition(lon, lat, serviceName);
+            if (simuResults.isEmpty()) {
+                return notFound("鏈煡鎵惧埌鐩稿叧鏁版嵁");
+            }
+            return success(simuResults, simuResults.size());
+        }
+    }
 }

--
Gitblit v1.9.3