wangyifei
2024-10-30 3cf717d9c9cb1426f25bd5cd886ec20873058e39
获取降水曲线文件曲线图功能第一次提交
已修改3个文件
79 ■■■■■ 文件已修改
src/main/java/com/se/simu/controller/WaterController.java 17 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/se/simu/service/ResultService.java 38 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/se/simu/service/WaterService.java 24 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/se/simu/controller/WaterController.java
@@ -102,6 +102,23 @@
        }
    }
    @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());
        }
    }
    private boolean validate(String serviceName, HttpServletResponse res) {
        if (WebHelper.isEmpty(serviceName)) {
            return WebHelper.writeJson2Page(res, HttpStatus.BAD_REQUEST, "服务名不能为空");
src/main/java/com/se/simu/service/ResultService.java
@@ -23,6 +23,10 @@
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.List;
@@ -61,6 +65,7 @@
            copeWater(dto, layer);
            copeFlow(dto, layer);
            copeLayerJson(dto, layer);
            copeRainFallJson(dto, layer);
        } finally {
            File dir = new File(dto.getTemp());
            if (dir.exists()) {
@@ -525,4 +530,37 @@
        bw.close();
        fw.close();
    }
    /**
     * 处理降水曲线文件曲线图
     */
    public void copeRainFallJson(ResultDto dto, LayerDto layer) throws IOException, ParseException {
        String rainGageFilePath = config.getInPath() + File.separator + dto.getServiceName() + File.separator + "RainGage.dat";
        String filePath = dto.getOutPath() + File.separator + "rainfall.json";
        String line;
        Map<Long,Double> rainFallJsons = new LinkedHashMap<>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        BufferedReader br = new BufferedReader(new FileReader(rainGageFilePath));
            //处理第一行数据
            if ((line = br.readLine()) != null) {
                while ((line = br.readLine()) != null) {
                    // 处理每一行数据
                    String[] rainFall = line.split(" ");
                    if (rainFall.length < 7) continue;
                    String sdt = rainFall[1]+"-"+rainFall[2]+"-"+rainFall[3]+" "+rainFall[4]+":"+rainFall[5];
                    BigDecimal num = new BigDecimal(rainFall[6]);
                    rainFallJsons.put(sdf.parse(sdt).getTime()/1000,num.setScale(2, RoundingMode.HALF_UP).doubleValue());
                }
            }
        FileWriter fw = new FileWriter(filePath);
        BufferedWriter bw = new BufferedWriter(fw);
        bw.write(JSON.toJSONString(rainFallJsons));
        bw.close();
        fw.close();
    }
}
src/main/java/com/se/simu/service/WaterService.java
@@ -92,4 +92,28 @@
        return layer;
    }
    /**
     * 获取降水曲线文件曲线图
     */
    public byte[] getRainfall(String serviceName) {
        try {
            String filePath = config.getOutPath() + File.separator + serviceName + File.separator + "rainfall.json";
            File rainfall = new File(filePath);
            if (!rainfall.exists()) {
                return null;
            }
            byte[] bytes = new byte[(int) rainfall.length()];
            FileInputStream fs = new FileInputStream(filePath);
            fs.read(bytes);
            fs.close();
            return bytes;
        } catch (Exception ex) {
            return null;
        }
    }
}