cjl
15 hours ago 8991fc3fb5f7a050797d1d49726fedbfd06bf676
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
package com.moral.api.service.impl;
 
import com.moral.api.entity.RadarHourlySummary;
import com.moral.api.mapper.RadarHourlySummaryMapper;
import com.moral.api.service.HistorySecondRadarService;
import com.moral.api.service.RadarHourlySummaryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.moral.api.vo.*;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
 
/**
 * <p>
 * 雷达小时汇总表 服务实现类
 * </p>
 *
 * @author moral
 * @since 2026-09-08
 */
@Service
public class RadarHourlySummaryServiceImpl extends ServiceImpl<RadarHourlySummaryMapper, RadarHourlySummary> implements RadarHourlySummaryService {
 
    @Autowired
    private HistorySecondRadarService radarService;
 
    private static final double HEIGHT_STEP = 7.5;
    private static final int PEAK_SEARCH_LIMIT = 200;
 
    private static final DateTimeFormatter FMT = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:00:00");
    private static final DateTimeFormatter DT_FMT = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
 
 
 
    @Override
    public void processLastHour() {
        LocalDateTime now = LocalDateTime.now();
        LocalDateTime prev = now.minusHours(1).withMinute(0).withSecond(0).withNano(0);
        processHour(prev.format(FMT));
    }
 
    @Override
    public ComparisonResponse getComparison(String location, String time) {
        // 入参时间 → 前一个整点 = current 时刻
        LocalDateTime inputTime = LocalDateTime.parse(time, DT_FMT);
        LocalDateTime prevHour = inputTime.minusHours(1).withMinute(0).withSecond(0).withNano(0);
        String currentTime = prevHour.format(DT_FMT);
 
        // 30 天范围:currentTime(不含)往前推 30 天
        String endStr = currentTime;
        String startStr = prevHour.minusDays(30).format(DT_FMT);
 
        ComparisonResponse resp = new ComparisonResponse();
        resp.setLocation(location);
        resp.setTime(time);
 
        // current: 前一个整点的 a34004_drop_height
        RadarHourlySummaryVO cur = this.baseMapper.getByTime(currentTime);
        resp.setCurrent(cur != null && cur.getA34004DropHeight() != null ? cur.getA34004DropHeight().doubleValue() : 0.0);
 
        // avg30: 720 小时平均
        Double avg = this.baseMapper.getAvgDropHeightInRange(startStr, endStr);
        resp.setAvg30(avg != null ? avg : 0.0);
 
        // daily30: 按天分组,不含今天
        List<DailyDropHeightVO> rows = this.baseMapper.getDailyAvgDropHeight(startStr, endStr);
        if(CollectionUtils.isEmpty(rows)){
            List<DailyDropHeightVO> daily = new ArrayList<>();
            String todayStr = prevHour.toLocalDate().format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
            DailyDropHeightVO dropHeightVO  = new DailyDropHeightVO();
            dropHeightVO.setDate(todayStr);
            dropHeightVO.setDropHeight(0d);
            daily.add(dropHeightVO);
            resp.setDaily30(daily);
        }else {
            resp.setDaily30(rows);
        }
        return resp;
    }
 
    @Override
    public HourlyResponse getHourly5Days(String location, String startTime, String endTime) {
        HourlyResponse resp = new HourlyResponse();
        resp.setLocation(location);
        resp.setStartTime(startTime);
        resp.setEndTime(endTime);
 
        List<RadarHourlySummaryVO> list = this.baseMapper.queryHourlyByRange(startTime, endTime);
        List<HourlyItemVO> items = new ArrayList<>();
        for (RadarHourlySummaryVO s : list) {
            HourlyItemVO item = new HourlyItemVO();
            item.setTime(s.getTimeStr());
            item.setA34004DropHeight(s.getA34004DropHeight().doubleValue());
            items.add(item);
        }
        resp.setCount(items.size());
        resp.setHourly(items);
        return resp;
    }
 
    @Override
    public void processHour(String hourStart) {
        String hourEnd = nextHour(hourStart);
        List<String> rawList = radarService.queryRawDataByHour(hourStart, hourEnd);
        if (rawList.isEmpty()) {
            return ;
        }
 
        // 提取 a34004 / a34002
        List<String> a34004List = new ArrayList<>();
        List<String> a34002List = new ArrayList<>();
        for (String json : rawList) {
            a34004List.add(extractJsonField(json, "a34004-Avg"));
            a34002List.add(extractJsonField(json, "a34002-Avg"));
        }
 
        double[] avg04 = averageByHeight(a34004List);
        double[] avg02 = averageByHeight(a34002List);
 
        R r04 = analyze(avg04);
        R r02 = analyze(avg02);
 
        RadarHourlySummary entity = new RadarHourlySummary();
        entity.setTime(LocalDateTime.parse(hourStart, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        entity.setRadarData(buildRadarJson(avg04, avg02));
        entity.setA34004PeakHeight(BigDecimal.valueOf(r04.peak));
        entity.setA34004DropHeight(BigDecimal.valueOf(r04.drop));
        entity.setA34002PeakHeight(BigDecimal.valueOf(r02.peak));
        entity.setA34002DropHeight(BigDecimal.valueOf(r02.drop));
 
        this.baseMapper.insert(entity);
 
    }
// ======================== 算法 ========================
 
    private double[] averageByHeight(List<String> rawList) {
        List<double[]> rows = new ArrayList<>();
        int minLen = Integer.MAX_VALUE;
        for (String s : rawList) {
            double[] arr = parseToArray(s);
            rows.add(arr);
            if (arr.length < minLen) minLen = arr.length;
        }
        double[] avg = new double[minLen];
        int n = rows.size();
        for (int i = 0; i < minLen; i++) {
            double sum = 0;
            for (double[] row : rows) sum += row[i];
            BigDecimal bd = new BigDecimal(String.valueOf(sum / n));
            BigDecimal resultBd = bd.setScale(2, RoundingMode.HALF_UP);
            avg[i] =resultBd.doubleValue();
        }
        return avg;
    }
 
    private R analyze(double[] values) {
        int searchEnd = Math.min(PEAK_SEARCH_LIMIT, values.length);
        int peakIdx = 0;
        double peakVal = values[0];
        for (int i = 1; i < searchEnd; i++) {
            if (values[i] > peakVal) { peakVal = values[i]; peakIdx = i; }
        }
        double peakHeight = peakIdx * HEIGHT_STEP;
        System.out.println("最大值 》》》》》》》"+values[peakIdx]+"  》》》》最大高度>>>>>"+peakHeight+"  》》》》最大高度位置>>>>>"+peakIdx);
        int dropIdx = -1;
        if(PEAK_SEARCH_LIMIT-peakIdx == 1){
            searchEnd = searchEnd+60;
        }
        double base = findBaseValue(values, peakIdx, searchEnd);
        for (int i = peakIdx + 1; i < searchEnd; i++) {
            if (values[i] <= base * 1.05) { dropIdx = i; break; }
        }
        double dropHeight = dropIdx > 0 ? dropIdx * HEIGHT_STEP : 0;
 
        System.out.println("最小值 》》》》》》》"+values[dropIdx]+"  》》》》最小高度>>>>>"+dropHeight+"  米 》》》》最小高度位置>>>>>"+dropIdx);
        return new R(peakHeight, dropHeight);
    }
 
    private double findBaseValue(double[] values, int peakIdx, int end) {
        List<Double> tail = new ArrayList<>();
        for (int i = peakIdx + 1; i < end; i++) tail.add(values[i]);
        if (tail.isEmpty()) return 0;
        Collections.sort(tail);
        if (tail.size() >= 2) {
            double a = tail.get(0), b = tail.get(1);
            if (b - a > 2.0 || (a > 0 && b / a > 1.5)) return b;
        }
        return tail.get(0);
    }
 
    // ======================== 工具 ========================
 
    private String extractJsonField(String json, String field) {
        String key = "\"" + field + "\"";
        int s = json.indexOf(key);
        if (s == -1) return "";
        s = json.indexOf(":", s) + 1;
        while (s < json.length() && (json.charAt(s) == ' ' || json.charAt(s) == '"')) s++;
        int e = json.indexOf("\"", s);
        if (e == -1) e = json.indexOf(",", s);
        if (e == -1) e = json.indexOf("}", s);
        if (e == -1) return "";
        while (e > s && json.charAt(e - 1) == '"') e--;
        return json.substring(s, e);
    }
 
    private double[] parseToArray(String raw) {
        if (raw == null || raw.isEmpty()) return new double[0];
        String[] parts = raw.split("\\*");
        double[] arr = new double[parts.length];
        for (int i = 0; i < parts.length; i++) {
            try { arr[i] = Double.parseDouble(parts[i]); }
            catch (NumberFormatException e) { arr[i] = 0; }
        }
        return arr;
    }
 
    private String buildRadarJson(double[] a04, double[] a02) {
        StringBuilder sb = new StringBuilder("{\"a34004-Avg\":[");
        for (int i = 0; i < a04.length; i++) {
            if (i > 0) sb.append(",");
            sb.append(String.format("%.6f", a04[i]));
        }
        sb.append("],\"a34002-Avg\":[");
        for (int i = 0; i < a02.length; i++) {
            if (i > 0) sb.append(",");
            sb.append(String.format("%.6f", a02[i]));
        }
        sb.append("]}");
        return sb.toString();
    }
 
    private String nextHour(String timeStr) {
        LocalDateTime time = LocalDateTime.parse(timeStr, FMT);
        LocalDateTime nextTime = time.plusHours(1);
        return nextTime.format(FMT);
    }
 
    static class R {
        double peak, drop;
        R(double peak, double drop) { this.peak = peak; this.drop = drop; }
    }
 
}