From 55ad35338c17a6be2da9151badcabb4b7d46fecf Mon Sep 17 00:00:00 2001 From: yufengshuo Date: Tue, 31 Mar 2026 11:05:54 +0800 Subject: [PATCH] =?UTF-8?q?fix(weather-sync):=20=E5=A2=9E=E5=BC=BA?= =?UTF-8?q?=E5=8C=BA=E5=9F=9F=E5=A4=A9=E6=B0=94=E6=95=B0=E6=8D=AE=E5=90=8C?= =?UTF-8?q?=E6=AD=A5=E7=9A=84=E5=81=A5=E5=A3=AE=E6=80=A7=E5=92=8C=E6=97=A5?= =?UTF-8?q?=E5=BF=97=E8=AE=B0=E5=BD=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 添加输入参数校验,确保 dateTime 不为空 - 懒加载天气区划配置,避免配置为空时跳过同步 - 在关键步骤添加详细日志,便于问题排查 - 插入数据前先清理同时间点的旧数据,避免重复 - 优化代码格式,提高可读性 --- .../impl/RegionalWeatherDataSyncService.java | 34 +++++++++++++------ 1 file changed, 23 insertions(+), 11 deletions(-) diff --git a/src/main/java/com/southern/power/grid/service/impl/RegionalWeatherDataSyncService.java b/src/main/java/com/southern/power/grid/service/impl/RegionalWeatherDataSyncService.java index 2496be4..1664860 100644 --- a/src/main/java/com/southern/power/grid/service/impl/RegionalWeatherDataSyncService.java +++ b/src/main/java/com/southern/power/grid/service/impl/RegionalWeatherDataSyncService.java @@ -12,6 +12,7 @@ import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.util.CollectionUtils; +import org.springframework.util.StringUtils; import java.math.BigDecimal; import java.math.RoundingMode; @@ -28,10 +29,18 @@ public class RegionalWeatherDataSyncService extends ServiceImpl weatherAreaMap = new ConcurrentHashMap<>(); - @Transactional(rollbackFor = Exception.class) public void syncWeatherData(String dateTime) { try { + if (!StringUtils.hasText(dateTime)) { + throw new IllegalArgumentException("dateTime不能为空"); + } + + if (CollectionUtils.isEmpty(weatherAreaMap)) { + loadWeatherAreaConfig(); + log.info("【区域天气数据同步任务】加载区划配置完成,配置数量:{}", weatherAreaMap.size()); + } + List regionalWeatherStations = regionalWeatherStationMapper.getSyncData(dateTime); if (CollectionUtils.isEmpty(regionalWeatherStations)) { log.warn("【区域天气数据同步任务】没有查询到天气数据:{}", dateTime); @@ -39,9 +48,14 @@ public class RegionalWeatherDataSyncService extends ServiceImpl dataList = convert(regionalWeatherStations, dateTime); if (CollectionUtils.isEmpty(dataList)) { + log.warn("【区域天气数据同步任务】转换后无可入库数据:{},源数据条数:{}", dateTime, regionalWeatherStations.size()); return; } + log.info("【区域天气数据同步任务】准备写入:{},源数据条数:{},转换后条数:{}", + dateTime, regionalWeatherStations.size(), dataList.size()); + this.lambdaUpdate().eq(RegionalWeatherData::getDataTime, dateTime).remove(); this.saveBatch(dataList, 700); + log.info("【区域天气数据同步任务】写入完成:{},写入条数:{}", dateTime, dataList.size()); } catch (Exception e) { log.error("定时同步区域气象数据失败:{}", e.getMessage(), e); throw new RuntimeException(e); @@ -55,9 +69,10 @@ public class RegionalWeatherDataSyncService extends ServiceImpl> groupMap = list.stream() - .filter(item -> weatherAreaMap.containsKey(buildKey(item.getProvince(), item.getCity(), item.getDistrict()))) - .collect(Collectors.groupingBy(item -> - weatherAreaMap.get(buildKey(item.getProvince(), item.getCity(), item.getDistrict())))); + .filter(item -> weatherAreaMap + .containsKey(buildKey(item.getProvince(), item.getCity(), item.getDistrict()))) + .collect(Collectors.groupingBy( + item -> weatherAreaMap.get(buildKey(item.getProvince(), item.getCity(), item.getDistrict())))); // 2. 分组处理 List result = new ArrayList<>(); @@ -155,7 +170,6 @@ public class RegionalWeatherDataSyncService extends ServiceImpl list = weatherSiteAreaConfigurationMapper.selectAll(); - if (CollectionUtils.isEmpty(list)) return; + if (CollectionUtils.isEmpty(list)) + return; Map tempMap = list.stream() .filter(Objects::nonNull) .filter(config -> config.getWeatherProvince() != null @@ -176,20 +191,17 @@ public class RegionalWeatherDataSyncService extends ServiceImpl oldValue - )); + (oldValue, newValue) -> oldValue)); // 全部放入你的成员变量 Map weatherAreaMap.putAll(tempMap); } - private static String buildKey(String province, String city, String district) { return String.join(":", safe(province), safe(city), - safe(district) - ); + safe(district)); } private static String safe(String str) {