13693261870
2024-10-31 f7c642eed8ae076553f95a1f0ac5fd2e7c31b6a1
src/main/java/com/se/simu/controller/WaterController.java
@@ -1,8 +1,14 @@
package com.se.simu.controller;
import com.se.simu.domain.LayerVo;
import com.se.simu.domain.po.SimuPo;
import com.se.simu.domain.vo.R;
import com.se.simu.helper.WebHelper;
import com.se.simu.service.SimuService;
import com.se.simu.service.WaterService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.GetMapping;
@@ -12,18 +18,21 @@
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
/**
 * 内涝控制器
 * 内涝管理
 *
 * @author WWW
 * @date   2024-07-16
 */
@Api(tags = "内涝管理")
@Slf4j
@RestController
@RequestMapping("/waterlogging")
public class WaterController {
public class WaterController extends BaseController {
    @Resource
    SimuService simuService;
    @Resource
    WaterService waterService;
@@ -33,11 +42,7 @@
    private final static long Y2000 = 949334400000L;
    @GetMapping("/getTime")
    public Object getTime() {
        return System.currentTimeMillis();
    }
    @ApiOperation(value = "获取元数据信息")
    @GetMapping("/{serviceName}/layer.json")
    public void getLayer(@PathVariable String serviceName, HttpServletResponse res) {
        try {
@@ -45,15 +50,16 @@
                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());
        }
    }
    @ApiOperation(value = "获取地形高度图")
    @GetMapping("/{serviceName}/terrain")
    public void getTerraMap(@PathVariable String serviceName, Integer width, Integer height, HttpServletResponse res) {
        try {
@@ -62,13 +68,15 @@
            }
            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());
        }
    }
    @ApiOperation(value = "获取水面高度图")
    @GetMapping("/{serviceName}/waterMap")
    public void getWaterMap(@PathVariable String serviceName, Integer width, Integer height, Long timestamp, HttpServletResponse res) {
        try {
@@ -76,14 +84,16 @@
                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());
        }
    }
    @ApiOperation(value = "获取水流向流速图")
    @GetMapping("/{serviceName}/flowMap")
    public void getFlowMap(@PathVariable String serviceName, Integer width, Integer height, Long timestamp, HttpServletResponse res) {
        try {
@@ -92,10 +102,51 @@
            }
            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());
        }
    }
    @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());
        }
    }
    @ApiOperation(value = "根据坐标查询积水深度")
    @GetMapping("/{serviceName}/getWaterHeight")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "serviceName", value = "服务名", dataType = "String", paramType = "path", example = "20241010095328"),
            @ApiImplicitParam(name = "x", value = "X", dataType = "double", paramType = "query", example = "116.6447998"),
            @ApiImplicitParam(name = "y", value = "Y", dataType = "double", paramType = "query", example = "39.8868915"),
            @ApiImplicitParam(name = "timestamp", value = "时间戳", dataType = "long", paramType = "query", example = "1730217660000")
    })
    public R<Double> getWaterHeight(@PathVariable String serviceName, double x, double y, long timestamp, HttpServletResponse res) {
        try {
            SimuPo simu = simuService.getSimuByServiceName(serviceName);
            if (null == simu) {
                return null;
            }
            // 根据服务名+时间戳+坐标,查询对应的积水深度
            return success(waterService.getWaterHeight(simu, x, y, timestamp));
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            return fail(ex);
        }
    }
@@ -124,26 +175,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 (WebHelper.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;
    }