doum
3 天以前 7ec3683c8e41460f4bb0bd3a6677198742313e2b
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
package com.doumee.service.business.impl;
 
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.doumee.core.constants.ResponseStatus;
import com.doumee.core.exception.BusinessException;
import com.doumee.core.utils.Constants;
import com.doumee.core.utils.DateUtil;
import com.doumee.dao.business.YwConditionerUsageMapper;
import com.doumee.dao.business.YwElectricalDataMapper;
import com.doumee.dao.business.model.YwConditionerUsage;
import com.doumee.dao.business.model.YwElectricalData;
import com.doumee.dao.business.vo.DailyEnergyStatVO;
import com.doumee.service.business.YwWorkDeskEnergyService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
 
@Service
public class YwWorkDeskEnergyServiceImpl implements YwWorkDeskEnergyService {
 
    private static final int DAYS = 30;
    private static final DateTimeFormatter DATE_FMT = DateTimeFormatter.ofPattern("yyyy-MM-dd");
    private static final String READ_TIME_PATTERN = "yyyy-MM-dd HH:mm:ss";
 
    @Autowired
    private YwElectricalDataMapper ywElectricalDataMapper;
 
    @Autowired
    private YwConditionerUsageMapper ywConditionerUsageMapper;
 
    @Override
    public List<DailyEnergyStatVO> electricalDailyStats() {
        LocalDate end = LocalDate.now();
        LocalDate start = end.minusDays(DAYS - 1L);
        return buildElectricalDailyStats(start, end);
    }
 
    @Override
    public String refreshElectricalDailyStatsForRange(String readTimeBegin, String readTimeEnd) {
        if (StringUtils.isBlank(readTimeBegin) || StringUtils.isBlank(readTimeEnd)) {
            throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(), "抄表时间段不能为空");
        }
        LocalDate start = parseDateTime(readTimeBegin.trim()).minusDays(1);
        LocalDate end = parseDateTime(readTimeEnd.trim()).plusDays(1);
        if (start.isAfter(end)) {
            throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(), "抄表时间段无效");
        }
        buildElectricalDailyStats(start, end);
        return "已刷新每日电量/电费统计(" + start.format(DATE_FMT) + " ~ " + end.format(DATE_FMT) + ")";
    }
 
    @Override
    public List<DailyEnergyStatVO> conditionerDailyStats() {
        LocalDate end = LocalDate.now();
        LocalDate start = end.minusDays(DAYS - 1L);
        Map<String, DailyEnergyStatVO> bucket = initDailyBucket(start, end);
 
        List<YwConditionerUsage> rows = ywConditionerUsageMapper.selectList(new QueryWrapper<YwConditionerUsage>().lambda()
                .eq(YwConditionerUsage::getIsdeleted, Constants.ZERO)
                .ge(YwConditionerUsage::getUsageDate, java.sql.Date.valueOf(start))
                .le(YwConditionerUsage::getUsageDate, java.sql.Date.valueOf(end)));
 
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        for (YwConditionerUsage row : rows) {
            if (row.getUsageDate() == null) {
                continue;
            }
            String dayKey = sdf.format(row.getUsageDate());
            if (!bucket.containsKey(dayKey)) {
                continue;
            }
            DailyEnergyStatVO stat = bucket.get(dayKey);
            stat.setTotalKwh(stat.getTotalKwh().add(nullToZero(row.getSumDl())));
            stat.setTotalFee(stat.getTotalFee().add(nullToZero(row.getSumDf())));
        }
        return normalizeBucket(bucket);
    }
 
    private List<DailyEnergyStatVO> buildElectricalDailyStats(LocalDate start, LocalDate end) {
        LocalDate queryStart = start.minusDays(1L);
        Map<String, DailyEnergyStatVO> bucket = initDailyBucket(start, end);
        List<YwElectricalData> rows = loadElectricalRowsForStats(queryStart, end);
 
        SimpleDateFormat dayFmt = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat readTimeFmt = new SimpleDateFormat(READ_TIME_PATTERN);
        Map<String, Map<String, MeterDayReading>> meterDayLatest = new HashMap<>();
 
        for (YwElectricalData row : rows) {
            String meterKey = resolveMeterKey(row);
            if (StringUtils.isBlank(meterKey)) {
                continue;
            }
            String dayKey = resolveElectricalDayKey(row, dayFmt);
            if (StringUtils.isBlank(dayKey)) {
                continue;
            }
            BigDecimal totalEnergy = resolveTotalEnergy(row);
            if (totalEnergy.compareTo(BigDecimal.ZERO) < 0) {
                continue;
            }
            long readingTime = resolveReadingTime(row, readTimeFmt);
            MeterDayReading snapshot = new MeterDayReading(totalEnergy, parseDecimal(row.getDqdj()), readingTime);
            upsertLatestReading(meterDayLatest, meterKey, dayKey, snapshot);
        }
 
        for (LocalDate day = start; !day.isAfter(end); day = day.plusDays(1)) {
            String todayKey = day.format(DATE_FMT);
            String yesterdayKey = day.minusDays(1).format(DATE_FMT);
            DailyEnergyStatVO stat = bucket.get(todayKey);
            for (Map<String, MeterDayReading> dayReadings : meterDayLatest.values()) {
                MeterDayReading todayReading = dayReadings.get(todayKey);
                MeterDayReading yesterdayReading = dayReadings.get(yesterdayKey);
                if (todayReading == null || yesterdayReading == null) {
                    continue;
                }
                BigDecimal usage = todayReading.totalEnergy.subtract(yesterdayReading.totalEnergy);
                if (usage.compareTo(BigDecimal.ZERO) <= 0) {
                    continue;
                }
                stat.setTotalKwh(stat.getTotalKwh().add(usage));
                stat.setTotalFee(stat.getTotalFee().add(usage.multiply(todayReading.price)));
            }
        }
        return normalizeBucket(bucket);
    }
 
    private List<YwElectricalData> loadElectricalRowsForStats(LocalDate queryStart, LocalDate end) {
        String queryStartStr = queryStart.format(DATE_FMT) + " 00:00:00";
        String queryEndStr = end.format(DATE_FMT) + " 23:59:59";
        return ywElectricalDataMapper.selectList(new QueryWrapper<YwElectricalData>().lambda()
                .eq(YwElectricalData::getIsdeleted, Constants.ZERO)
                .and(w -> w.and(w1 -> w1.isNotNull(YwElectricalData::getAddTime)
                                .ne(YwElectricalData::getAddTime, "")
                                .ge(YwElectricalData::getAddTime, queryStartStr)
                                .le(YwElectricalData::getAddTime, queryEndStr))
                        .or(w2 -> w2.and(w3 -> w3.isNull(YwElectricalData::getAddTime)
                                        .or().eq(YwElectricalData::getAddTime, ""))
                                .ge(YwElectricalData::getCreateDate, java.sql.Date.valueOf(queryStart))
                                .le(YwElectricalData::getCreateDate, java.sql.Date.valueOf(end.plusDays(1))))));
    }
 
    private static LocalDate parseDateTime(String text) {
        if (text.length() >= 10) {
            try {
                return LocalDate.parse(text.substring(0, 10), DATE_FMT);
            } catch (DateTimeParseException ignored) {
            }
        }
        try {
            Date date = DateUtil.StringToDate(text, READ_TIME_PATTERN);
            if (date != null) {
                return date.toInstant().atZone(java.time.ZoneId.systemDefault()).toLocalDate();
            }
        } catch (Exception ignored) {
        }
        throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(), "抄表时间格式不正确");
    }
 
    private static void upsertLatestReading(Map<String, Map<String, MeterDayReading>> meterDayLatest,
                                            String meterKey,
                                            String dayKey,
                                            MeterDayReading snapshot) {
        Map<String, MeterDayReading> dayMap = meterDayLatest.computeIfAbsent(meterKey, key -> new HashMap<>());
        MeterDayReading existing = dayMap.get(dayKey);
        if (existing == null || snapshot.readingTime >= existing.readingTime) {
            dayMap.put(dayKey, snapshot);
        }
    }
 
    private static String resolveMeterKey(YwElectricalData row) {
        if (StringUtils.isNotBlank(row.getAddress())) {
            return row.getAddress().trim();
        }
        if (StringUtils.isNotBlank(row.getDeviceId())) {
            return "dev:" + row.getDeviceId().trim();
        }
        if (StringUtils.isNotBlank(row.getMid())) {
            return "mid:" + row.getMid().trim();
        }
        return null;
    }
 
    private static BigDecimal resolveTotalEnergy(YwElectricalData row) {
        BigDecimal total = parseDecimal(row.getZhygzdl());
        if (total.compareTo(BigDecimal.ZERO) <= 0) {
            total = parseDecimal(row.getZyje());
        }
        return total;
    }
 
    private static long resolveReadingTime(YwElectricalData row, SimpleDateFormat readTimeFmt) {
        if (StringUtils.isNotBlank(row.getAddTime())) {
            String addTime = row.getAddTime().trim();
            if (addTime.matches("\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2}")) {
                try {
                    return readTimeFmt.parse(addTime).getTime();
                } catch (ParseException ignored) {
                    // fallback to createDate
                }
            }
        }
        Date createDate = row.getCreateDate();
        return createDate == null ? 0L : createDate.getTime();
    }
 
    private static List<DailyEnergyStatVO> normalizeBucket(Map<String, DailyEnergyStatVO> bucket) {
        List<DailyEnergyStatVO> list = new ArrayList<>(bucket.values());
        for (DailyEnergyStatVO stat : list) {
            stat.setTotalKwh(stat.getTotalKwh().setScale(2, RoundingMode.HALF_UP));
            stat.setTotalFee(stat.getTotalFee().setScale(2, RoundingMode.HALF_UP));
        }
        return list;
    }
 
    private static Map<String, DailyEnergyStatVO> initDailyBucket(LocalDate start, LocalDate end) {
        Map<String, DailyEnergyStatVO> bucket = new LinkedHashMap<>();
        for (LocalDate d = start; !d.isAfter(end); d = d.plusDays(1)) {
            DailyEnergyStatVO vo = new DailyEnergyStatVO();
            vo.setStatDate(d.format(DATE_FMT));
            vo.setTotalKwh(BigDecimal.ZERO.setScale(2, RoundingMode.HALF_UP));
            vo.setTotalFee(BigDecimal.ZERO.setScale(2, RoundingMode.HALF_UP));
            bucket.put(vo.getStatDate(), vo);
        }
        return bucket;
    }
 
    private static String resolveElectricalDayKey(YwElectricalData row, SimpleDateFormat sdf) {
        if (StringUtils.isNotBlank(row.getAddTime())) {
            String addTime = row.getAddTime().trim();
            if (addTime.length() >= 10) {
                return addTime.substring(0, 10);
            }
        }
        Date createDate = row.getCreateDate();
        return createDate == null ? null : sdf.format(createDate);
    }
 
    private static BigDecimal parseDecimal(String val) {
        if (StringUtils.isBlank(val)) {
            return BigDecimal.ZERO;
        }
        try {
            return new BigDecimal(val.trim());
        } catch (NumberFormatException e) {
            return BigDecimal.ZERO;
        }
    }
 
    private static BigDecimal nullToZero(BigDecimal val) {
        return val == null ? BigDecimal.ZERO : val;
    }
 
    private static class MeterDayReading {
        private final BigDecimal totalEnergy;
        private final BigDecimal price;
        private final long readingTime;
 
        private MeterDayReading(BigDecimal totalEnergy, BigDecimal price, long readingTime) {
            this.totalEnergy = totalEnergy;
            this.price = price;
            this.readingTime = readingTime;
        }
    }
}