cjl
9 hours ago bcc076d63c5c2ff473adb5ac15ea242559086000
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
package com.moral.api.service.impl;
 
import com.fasterxml.jackson.databind.ObjectMapper;
import com.moral.api.entity.HistorySecondRadar;
import com.moral.api.mapper.HistorySecondRadarMapper;
import com.moral.api.pojo.query.radar.RadarListCond;
import com.moral.api.pojo.vo.radar.HistorySecondRadarListVo;
import com.moral.api.pojo.vo.radar.RadarListVo;
import com.moral.api.service.HistorySecondRadarService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
 
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.List;
 
/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author moral
 * @since 2026-05-14
 */
@Service
public class HistorySecondRadarServiceImpl extends ServiceImpl<HistorySecondRadarMapper, HistorySecondRadar> implements HistorySecondRadarService {
    private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
    private static final double HEIGHT_INTERVAL = 7.5;
    @Override
    public RadarListVo radarList(RadarListCond cond) {
        RadarListVo radarListVo = new RadarListVo();
        List<String> time = new ArrayList<>();
        List<List<double[]>> pm25 = new ArrayList<>();
        List<List<double[]>> pm10 = new ArrayList<>();
        List<String> highList = buildHighList(cond.getHigh2());
        List<String> highList1 = buildHighList(cond.getHigh1());
        List<HistorySecondRadarListVo> list = this.baseMapper.listRadar(cond);
        for (HistorySecondRadarListVo row : list) {
            time.add(row.getTimeStr());
            double[] pm25Values = parseValues(row.getRadarData(), "a34004-Avg");
            double[] pm10Values = parseValues(row.getRadarData(), "a34002-Avg");
            pm25.add(buildPointList(pm25Values, highList.size(),highList1.size()));
            pm10.add(buildPointList(pm10Values, highList.size(),highList1.size()));
        }
        radarListVo.setPm25(pm25);
        radarListVo.setTime(time);
        radarListVo.setPm10(pm10);
 
       /* List<List<double[]>> pm25 = new ArrayList<>();
        List<List<double[]>> pm10 = new ArrayList<>();
        for(HistorySecondRadarListVo ot : list){
            pm10.add(convertSingleRecordToJson(ot.getRadarDataPm10(),cond.getHigh1(),cond.getHigh2()));
            pm25.add(convertSingleRecordToJson(ot.getRadarDataPm25(),cond.getHigh1(),cond.getHigh2()));
            time.add(ot.getTimeStr());
        }
        radarListVo.setPm25(pm25);
        radarListVo.setTime(time);
        radarListVo.setPm10(pm10);*/
        return radarListVo;
    }
    private static List<double[]> buildPointList(double[] values, int pointCount, int minPointCount) {
        List<double[]> pointList = new ArrayList<double[]>(pointCount);
        int count = Math.min(pointCount, values.length);
        int minCount = Math.min(minPointCount, values.length);
        for (int i = minCount; i < count; i++) {
            pointList.add(new double[]{values[i]});
        }
        return pointList;
    }
    private static List<String> buildHighList(int MAX_HEIGHT) {
        List<String> highList = new ArrayList<String>();
        int count = (int) (MAX_HEIGHT / HEIGHT_INTERVAL) + 1;
        for (int i = 0; i < count; i++) {
            highList.add(formatHeight(i * HEIGHT_INTERVAL));
        }
        return highList;
    }
    private static double[] parseValues(String json, String key) {
        if (json == null) {
            return new double[0];
        }
 
        String keyToken = "\"" + key + "\":";
        int keyIndex = json.indexOf(keyToken);
        if (keyIndex < 0) {
            return new double[0];
        }
 
        int bracketStart = json.indexOf('[', keyIndex + keyToken.length());
        int bracketEnd = json.indexOf(']', bracketStart + 1);
        if (bracketStart < 0 || bracketEnd < 0 || bracketEnd - bracketStart <= 1) {
            return new double[0];
        }
 
        String body = json.substring(bracketStart + 1, bracketEnd).trim();
        if (body.isEmpty()) {
            return new double[0];
        }
 
        String[] parts = body.split(",");
        double[] values = new double[parts.length];
        for (int i = 0; i < parts.length; i++) {
            values[i] = Double.parseDouble(parts[i].trim());
        }
        return values;
    }
 
    private static String formatHeight(double height) {
        return BigDecimal.valueOf(height).stripTrailingZeros().toPlainString();
    }
    private List<double[]> convertSingleRecordToJson(String rawData,int high1,int high2){
        if (rawData == null || rawData.isEmpty()) {
            return new ArrayList<>();
        }
        try {
            // 3. 分割字符串
            String[] valuesStr = rawData.split("\\*");
 
            // 4. 预分配列表大小,假设约2000个数据,减少扩容
            List<double[]> seriesData = new ArrayList<>(valuesStr.length);
 
            for (int i = 10; i < valuesStr.length; i++) {
                // 简单的空值检查,避免 NumberFormatException
                if (valuesStr[i].isEmpty()) continue;
                double height = (i + 1) * HEIGHT_INTERVAL;
                if(height> high1 && height < high2){
                    double pm25Value = Double.parseDouble(valuesStr[i]);
                    pm25Value = new BigDecimal(Double.toString(pm25Value))
                            .setScale(1, RoundingMode.HALF_UP)
                            .doubleValue();
                    // 使用 double 数组存储 [浓度, 高度],比对象更节省内存
                    seriesData.add(new double[]{pm25Value, height});
                }
            }
            // 5. 序列化为 JSON
            return seriesData;
 
        } catch (Exception e) {
            // 生产环境中建议记录日志,而不是打印堆栈
            System.err.println("处理数据出错: " + e.getMessage());
            return new ArrayList<>();
        }
    }
}