fix(weather-sync): 增强区域天气数据同步的健壮性和日志记录

- 添加输入参数校验,确保 dateTime 不为空
- 懒加载天气区划配置,避免配置为空时跳过同步
- 在关键步骤添加详细日志,便于问题排查
- 插入数据前先清理同时间点的旧数据,避免重复
- 优化代码格式,提高可读性
This commit is contained in:
yufengshuo 2026-03-31 11:05:54 +08:00
parent 8cfb78b8af
commit 55ad35338c

View File

@ -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<RegionalWeatherD
private final WeatherSiteAreaConfigurationMapper weatherSiteAreaConfigurationMapper;
private final Map<String, String> 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<RegionalWeatherStation> regionalWeatherStations = regionalWeatherStationMapper.getSyncData(dateTime);
if (CollectionUtils.isEmpty(regionalWeatherStations)) {
log.warn("【区域天气数据同步任务】没有查询到天气数据:{}", dateTime);
@ -39,9 +48,14 @@ public class RegionalWeatherDataSyncService extends ServiceImpl<RegionalWeatherD
}
List<RegionalWeatherData> 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<RegionalWeatherD
// 1. 过滤掉未匹配区域编码的记录 orgCode 分组
Map<String, List<RegionalWeatherStation>> 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<RegionalWeatherData> result = new ArrayList<>();
@ -155,7 +170,6 @@ public class RegionalWeatherDataSyncService extends ServiceImpl<RegionalWeatherD
return val == null ? "0" : val.setScale(2, RoundingMode.HALF_UP).toString();
}
/**
* 加载气象区划配置表
*
@ -163,7 +177,8 @@ public class RegionalWeatherDataSyncService extends ServiceImpl<RegionalWeatherD
public void loadWeatherAreaConfig() {
weatherAreaMap.clear();
List<WeatherSiteAreaConfiguration> list = weatherSiteAreaConfigurationMapper.selectAll();
if (CollectionUtils.isEmpty(list)) return;
if (CollectionUtils.isEmpty(list))
return;
Map<String, String> tempMap = list.stream()
.filter(Objects::nonNull)
.filter(config -> config.getWeatherProvince() != null
@ -176,20 +191,17 @@ public class RegionalWeatherDataSyncService extends ServiceImpl<RegionalWeatherD
config.getWeatherCity(),
config.getWeatherDistrict()),
WeatherSiteAreaConfiguration::getDistrictCode,
(oldValue, newValue) -> 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) {