11
13693261870
2024-09-25 6017965892db5b414d6788d140c5eab46450250a
src/main/java/com/se/simu/controller/WaterController.java
@@ -1,7 +1,5 @@
package com.se.simu.controller;
import com.se.simu.domain.LayerVo;
import com.se.simu.helper.StringHelper;
import com.se.simu.helper.WebHelper;
import com.se.simu.service.WaterService;
import io.swagger.annotations.Api;
@@ -15,10 +13,9 @@
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
/**
 * 内涝控制器
 * 内涝管理
 *
 * @author WWW
 * @date   2024-07-16
@@ -37,12 +34,6 @@
    private final static long Y2000 = 949334400000L;
    @ApiOperation(value = "获取当前时间")
    @GetMapping("/getTime")
    public Object getTime() {
        return System.currentTimeMillis();
    }
    @ApiOperation(value = "获取元数据信息")
    @GetMapping("/{serviceName}/layer.json")
    public void getLayer(@PathVariable String serviceName, HttpServletResponse res) {
@@ -51,9 +42,9 @@
                return;
            }
            LayerVo layer = waterService.getLayer(serviceName);
            byte[] bytes = waterService.getLayerJson(serviceName);
            WebHelper.writeJson2Page(res, HttpStatus.OK, layer);
            WebHelper.writeBytes(bytes, res);
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            WebHelper.writeStr2Page(res, HttpStatus.INTERNAL_SERVER_ERROR, ex.getMessage());
@@ -69,7 +60,8 @@
            }
            String file = waterService.getTerraMap(serviceName, width, height);
            writeFile(file, res);
            WebHelper.writePng(file, res);
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            WebHelper.writeStr2Page(res, HttpStatus.INTERNAL_SERVER_ERROR, ex.getMessage());
@@ -84,8 +76,9 @@
                return;
            }
            String file = waterService.getWaterMap(serviceName, width, height);
            writeFile(file, res);
            String file = waterService.getWaterMap(serviceName, width, height, timestamp);
            WebHelper.writePng(file, res);
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            WebHelper.writeStr2Page(res, HttpStatus.INTERNAL_SERVER_ERROR, ex.getMessage());
@@ -101,7 +94,8 @@
            }
            String file = waterService.getFlowMap(serviceName, width, height, timestamp);
            writeFile(file, res);
            WebHelper.writePng(file, res);
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            WebHelper.writeStr2Page(res, HttpStatus.INTERNAL_SERVER_ERROR, ex.getMessage());
@@ -109,7 +103,7 @@
    }
    private boolean validate(String serviceName, HttpServletResponse res) {
        if (StringHelper.isEmpty(serviceName)) {
        if (WebHelper.isEmpty(serviceName)) {
            return WebHelper.writeJson2Page(res, HttpStatus.BAD_REQUEST, "服务名不能为空");
        }
@@ -124,7 +118,7 @@
     * 验证
     */
    private boolean validate(String serviceName, Integer width, Integer height, Long timestamp, HttpServletResponse res) {
        if (StringHelper.isEmpty(serviceName)) {
        if (WebHelper.isEmpty(serviceName)) {
            return WebHelper.writeJson2Page(res, HttpStatus.BAD_REQUEST, "服务名不能为空");
        }
        if (null == width || width < MIN_SIZE || width > MAX_SIZE) {
@@ -133,26 +127,9 @@
        if (null == height || height < MIN_SIZE || height > MAX_SIZE) {
            return WebHelper.writeJson2Page(res, HttpStatus.BAD_REQUEST, "图像高度不能为空且取值范围为" + MIN_SIZE + "~" + MAX_SIZE + "之间");
        }
        if (null == timestamp || timestamp < Y2000) {
            return WebHelper.writeJson2Page(res, HttpStatus.BAD_REQUEST, "时间不能为空且大于2000年");
        if (null == timestamp || timestamp < 0) {
            return WebHelper.writeJson2Page(res, HttpStatus.BAD_REQUEST, "时间不能为空且大于0");
        }
        return true;
    }
    /**
     * 写文件
     */
    private boolean writeFile(String path, HttpServletResponse res) throws Exception {
        if (StringHelper.isEmpty(path)) {
            return WebHelper.writeJson2Page(res, HttpStatus.INTERNAL_SERVER_ERROR, "生成PNG失败");
        }
        File file = new File(path);
        if (!file.exists() || file.isDirectory()) {
            return WebHelper.writeJson2Page(res, HttpStatus.INTERNAL_SERVER_ERROR, "PNG文件找不到");
        }
        WebHelper.download(path, file.getName(), res);
        return true;
    }