dcb
2025-07-09 e53252b99e7b49b435b7a6ee3eab21ae1bd7a055
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
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 io.swagger.v3.oas.annotations.Parameter;
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字符串不是WKT格式");
 
            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("方案正在运行或已完成");
            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字符串不是WKT格式");
 
            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(@Parameter(description = "方案id,示例:50") Integer id,
                                  @Parameter(description = "时间戳,示例:1751552400000") Long time,
                                  @Parameter(description = "起点坐标,示例:116.59049537485063,40.564178548127686") String startPoint,
                                  @Parameter(description = "终点坐标,示例:116.5901406492509,40.56499045715429") String endPoint) {
        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("找不到对应的服务");
        }
        if (startPoint == null) {
            return clientError("起点不能为空");
        }
        if (endPoint == null) {
            return clientError("终点不能为空");
        }
        String[] sp = startPoint.split(",");
        double[] s = new double[] {Double.parseDouble(sp[0]), Double.parseDouble(sp[1])};
        String[] ep = endPoint.split(",");
        double[] e = new double[] {Double.parseDouble(ep[0]), Double.parseDouble(ep[1])};
        try {
            if (time == null) {
                List<CrossSectionAnalysisResult> result = scas.crossSectionAnalysis(serviceName, s, e);
                return success(result, result.size());
            } else {
                CrossSectionAnalysisResult res = scas.crossSectionAnalysis(serviceName, s, e, time);
                return success(res, 1);
            }
        } catch (IllegalArgumentException ex) {
            return fail(ex.getMessage(), null);
        }
    }
 
    @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, "正在停止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());
    }
}