From f31f0991c0d2036e563b886f57de4cf45d3c72cb Mon Sep 17 00:00:00 2001
From: dcb <xgybdcb@163.com>
Date: 星期二, 01 七月 2025 14:51:59 +0800
Subject: [PATCH] 实时模拟异步功能实现

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

diff --git a/src/main/java/com/se/nsl/controller/SimuController.java b/src/main/java/com/se/nsl/controller/SimuController.java
new file mode 100644
index 0000000..f289da9
--- /dev/null
+++ b/src/main/java/com/se/nsl/controller/SimuController.java
@@ -0,0 +1,257 @@
+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.*;
+import com.se.nsl.service.*;
+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.*;
+
+import javax.annotation.Resource;
+import java.util.List;
+
+@Api(tags = "03锛嶆帹婕旀ā鎷�")
+@Slf4j
+@RestController
+@RequestMapping("/simu")
+@SuppressWarnings("ALL")
+public class SimuController extends BaseController {
+    @Resource
+    SimuService simuService;
+
+    @Resource
+    ResolveService resolveService;
+
+    @Resource
+    RealTimeSimulationAsyncService rtsas;
+
+    @Resource
+    CrossSectionAnalysisService scas;
+
+    /**
+     * 鍒嗛〉鏌ヨ鎺ㄦ紨妯℃嫙
+     *
+     * @param pageNum  椤电爜
+     * @param pageSize 姣忛〉鏁伴噺
+     * @return 鍒嗛〉鍚庣殑鎺ㄦ紨妯℃嫙
+     */
+    @ApiOperation(value = "selectPage")
+    @GetMapping("/selectPage")
+    public R<Object> selectPage(SimuVo vo, Integer pageNum, Integer pageSize) {
+        try {
+            if (null == pageNum || pageNum < 1) pageNum = 1;
+            if (null == pageSize || pageSize < 1) pageSize = 10;
+            if (pageSize > 1000) pageSize = 1000;
+
+            IPage<Simu> paged = simuService.selectPage(vo, pageNum, pageSize);
+            if (null == paged) {
+                return success(null, 0);
+            }
+
+            return success(paged.getRecords(), paged.getTotal());
+        } catch (Exception ex) {
+            return fail(ex, null);
+        }
+    }
+
+    /**
+     * 鏍规嵁ID鎵归噺鍒犻櫎鎺ㄦ紨妯℃嫙
+     *
+     * @param ids 瑕佸垹闄ょ殑鎺ㄦ紨妯℃嫙ID鍒楄〃
+     * @return 鍒犻櫎鎴愬姛鐨勮褰曟暟
+     */
+    @ApiOperation(value = "deleteByIds")
+    @DeleteMapping("/deleteByIds")
+    public R<Object> deleteByIds(@RequestParam List<Integer> ids) {
+        try {
+            return success(simuService.deleteByIds(ids));
+        } catch (Exception ex) {
+            return fail(ex, null);
+        }
+    }
+
+    /**
+     * 鏂板鎺ㄦ紨妯℃嫙
+     *
+     * @param simu 鎺ㄦ紨妯℃嫙瀵硅薄
+     * @return 鏂板鎴愬姛鐨勮褰曟暟
+     */
+    @ApiOperation(value = "insert")
+    @PostMapping(value = "/insert", produces = "application/json; charset=UTF-8")
+    public R<Object> insert(@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瀵硅薄涓嶆槸澶氳竟褰�");
+
+            int rows = simuService.insert(simu);
+            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);
+        }
+    }
+
+    @ApiOperation(value = "start")
+    @GetMapping(value = "/start", produces = "application/json; charset=UTF-8")
+    public R<Object> start(Integer id) {
+        try {
+            if (null == id || id < 1) return fail("id涓虹┖");
+
+            Simu simu = simuService.selectById(id);
+            if (null == simu) return notFound("鏂规鎵句笉鍒�");
+            if (StringUtils.isEmpty(simu.getData())) return fail("鏂规鏁版嵁(JSON)涓虹┖");
+
+            SimuData data = JSON.parseObject(simu.getData(), SimuData.class);
+            if (null == data) return fail("鏂规鏁版嵁鏍煎紡(JSON)涓嶆纭�");
+
+            //if (simu.getStatus() != 0) return fail("鏂规姝e湪杩愯鎴栧凡瀹屾垚");
+            if (StringUtils.isEmpty(simu.getGeom())) return fail("鏂规鐨勫浘褰负绌�");
+
+            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) {
+                rtsas.startSimulation(simu);
+                return success(null, "瀹炴椂妯℃嫙浠诲姟宸叉彁浜�");
+            }
+            return fail("妯℃嫙绫诲瀷鏆備笉鏀寔");
+        } catch (Exception ex) {
+            return fail(ex, null);
+        }
+    }
+
+    /**
+     * 淇敼鎺ㄦ紨妯℃嫙
+     *
+     * @param simu 鎺ㄦ紨妯℃嫙瀵硅薄
+     * @return 淇敼鎴愬姛鐨勮褰曟暟
+     */
+    @ApiOperation(value = "updateById")
+    @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());
+        }
+    }
+
+    @ApiOperation(value = "crossSection")
+    @GetMapping("/crossSection")
+    public R<Object> crossSection(String serviceName, double[] startPoint, double[] endPoint) {
+        if (serviceName == null) {
+            return clientError("鏈嶅姟鍚嶄笉鑳戒负绌�");
+        }
+        if (startPoint == null) {
+            return clientError("璧风偣涓嶈兘涓虹┖");
+        }
+        if (startPoint.length < 2) {
+            return clientError("璧风偣鑷冲皯鍖呭惈x,y涓や釜鍊�");
+        }
+        if (endPoint == null) {
+            return clientError("缁堢偣涓嶈兘涓虹┖");
+        }
+        if (endPoint.length < 2) {
+            return clientError("缁堢偣鑷冲皯鍖呭惈x,y涓や釜鍊�");
+        }
+        List<CrossSectionAnalysisResult> result = scas.crossSectionAnalysis(serviceName, startPoint, endPoint);
+        return success(result);
+    }
+
+    @ApiOperation(value = "stop")
+    @GetMapping("/stop")
+    public R<Object> stop(Integer id) {
+        if (id == null) {
+            return fail("id涓嶈兘涓虹┖");
+        }
+        try {
+            rtsas.stopSimulation(id);
+        } catch (IllegalArgumentException e) {
+            return notFound(e.getMessage());
+        }
+        return success(null, "姝e湪鍋滄id涓�" + id + "鐨勬ā鎷熶换鍔�");
+    }
+
+    @ApiOperation(value = "results")
+    @GetMapping("/results")
+    public R<Object> querySimulationResult(Integer id) {
+        if (null == id || id < 1) return clientError("id涓嶈兘涓虹┖");
+        Simu simu = simuService.selectById(id);
+        if (simu == null) {
+            return clientError("鎵句笉鍒板搴旂殑鏈嶅姟");
+        }
+        String serviceName = simu.getServiceName();
+        if (serviceName == null) {
+            return fail("鎵句笉鍒板搴旂殑鏈嶅姟");
+        }
+        List<String> results = resolveService.simulationResults(serviceName);
+        return success(results, results.size());
+    }
+}

--
Gitblit v1.9.3