fix(weather-sync): 增强区域天气数据同步的健壮性和日志记录
- 添加输入参数校验,确保 dateTime 不为空 - 懒加载天气区划配置,避免配置为空时跳过同步 - 在关键步骤添加详细日志,便于问题排查 - 插入数据前先清理同时间点的旧数据,避免重复 - 优化代码格式,提高可读性
This commit is contained in:
parent
8cfb78b8af
commit
55ad35338c
@ -12,6 +12,7 @@ import lombok.extern.slf4j.Slf4j;
|
|||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
import org.springframework.util.CollectionUtils;
|
import org.springframework.util.CollectionUtils;
|
||||||
|
import org.springframework.util.StringUtils;
|
||||||
|
|
||||||
import java.math.BigDecimal;
|
import java.math.BigDecimal;
|
||||||
import java.math.RoundingMode;
|
import java.math.RoundingMode;
|
||||||
@ -28,10 +29,18 @@ public class RegionalWeatherDataSyncService extends ServiceImpl<RegionalWeatherD
|
|||||||
private final WeatherSiteAreaConfigurationMapper weatherSiteAreaConfigurationMapper;
|
private final WeatherSiteAreaConfigurationMapper weatherSiteAreaConfigurationMapper;
|
||||||
private final Map<String, String> weatherAreaMap = new ConcurrentHashMap<>();
|
private final Map<String, String> weatherAreaMap = new ConcurrentHashMap<>();
|
||||||
|
|
||||||
|
|
||||||
@Transactional(rollbackFor = Exception.class)
|
@Transactional(rollbackFor = Exception.class)
|
||||||
public void syncWeatherData(String dateTime) {
|
public void syncWeatherData(String dateTime) {
|
||||||
try {
|
try {
|
||||||
|
if (!StringUtils.hasText(dateTime)) {
|
||||||
|
throw new IllegalArgumentException("dateTime不能为空");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (CollectionUtils.isEmpty(weatherAreaMap)) {
|
||||||
|
loadWeatherAreaConfig();
|
||||||
|
log.info("【区域天气数据同步任务】加载区划配置完成,配置数量:{}", weatherAreaMap.size());
|
||||||
|
}
|
||||||
|
|
||||||
List<RegionalWeatherStation> regionalWeatherStations = regionalWeatherStationMapper.getSyncData(dateTime);
|
List<RegionalWeatherStation> regionalWeatherStations = regionalWeatherStationMapper.getSyncData(dateTime);
|
||||||
if (CollectionUtils.isEmpty(regionalWeatherStations)) {
|
if (CollectionUtils.isEmpty(regionalWeatherStations)) {
|
||||||
log.warn("【区域天气数据同步任务】没有查询到天气数据:{}", dateTime);
|
log.warn("【区域天气数据同步任务】没有查询到天气数据:{}", dateTime);
|
||||||
@ -39,9 +48,14 @@ public class RegionalWeatherDataSyncService extends ServiceImpl<RegionalWeatherD
|
|||||||
}
|
}
|
||||||
List<RegionalWeatherData> dataList = convert(regionalWeatherStations, dateTime);
|
List<RegionalWeatherData> dataList = convert(regionalWeatherStations, dateTime);
|
||||||
if (CollectionUtils.isEmpty(dataList)) {
|
if (CollectionUtils.isEmpty(dataList)) {
|
||||||
|
log.warn("【区域天气数据同步任务】转换后无可入库数据:{},源数据条数:{}", dateTime, regionalWeatherStations.size());
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
log.info("【区域天气数据同步任务】准备写入:{},源数据条数:{},转换后条数:{}",
|
||||||
|
dateTime, regionalWeatherStations.size(), dataList.size());
|
||||||
|
this.lambdaUpdate().eq(RegionalWeatherData::getDataTime, dateTime).remove();
|
||||||
this.saveBatch(dataList, 700);
|
this.saveBatch(dataList, 700);
|
||||||
|
log.info("【区域天气数据同步任务】写入完成:{},写入条数:{}", dateTime, dataList.size());
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.error("定时同步区域气象数据失败:{}", e.getMessage(), e);
|
log.error("定时同步区域气象数据失败:{}", e.getMessage(), e);
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
@ -55,9 +69,10 @@ public class RegionalWeatherDataSyncService extends ServiceImpl<RegionalWeatherD
|
|||||||
|
|
||||||
// 1. 过滤掉未匹配区域编码的记录,按 orgCode 分组
|
// 1. 过滤掉未匹配区域编码的记录,按 orgCode 分组
|
||||||
Map<String, List<RegionalWeatherStation>> groupMap = list.stream()
|
Map<String, List<RegionalWeatherStation>> groupMap = list.stream()
|
||||||
.filter(item -> weatherAreaMap.containsKey(buildKey(item.getProvince(), item.getCity(), item.getDistrict())))
|
.filter(item -> weatherAreaMap
|
||||||
.collect(Collectors.groupingBy(item ->
|
.containsKey(buildKey(item.getProvince(), item.getCity(), item.getDistrict())))
|
||||||
weatherAreaMap.get(buildKey(item.getProvince(), item.getCity(), item.getDistrict()))));
|
.collect(Collectors.groupingBy(
|
||||||
|
item -> weatherAreaMap.get(buildKey(item.getProvince(), item.getCity(), item.getDistrict()))));
|
||||||
|
|
||||||
// 2. 分组处理
|
// 2. 分组处理
|
||||||
List<RegionalWeatherData> result = new ArrayList<>();
|
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();
|
return val == null ? "0" : val.setScale(2, RoundingMode.HALF_UP).toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 加载气象区划配置表
|
* 加载气象区划配置表
|
||||||
*
|
*
|
||||||
@ -163,7 +177,8 @@ public class RegionalWeatherDataSyncService extends ServiceImpl<RegionalWeatherD
|
|||||||
public void loadWeatherAreaConfig() {
|
public void loadWeatherAreaConfig() {
|
||||||
weatherAreaMap.clear();
|
weatherAreaMap.clear();
|
||||||
List<WeatherSiteAreaConfiguration> list = weatherSiteAreaConfigurationMapper.selectAll();
|
List<WeatherSiteAreaConfiguration> list = weatherSiteAreaConfigurationMapper.selectAll();
|
||||||
if (CollectionUtils.isEmpty(list)) return;
|
if (CollectionUtils.isEmpty(list))
|
||||||
|
return;
|
||||||
Map<String, String> tempMap = list.stream()
|
Map<String, String> tempMap = list.stream()
|
||||||
.filter(Objects::nonNull)
|
.filter(Objects::nonNull)
|
||||||
.filter(config -> config.getWeatherProvince() != null
|
.filter(config -> config.getWeatherProvince() != null
|
||||||
@ -176,20 +191,17 @@ public class RegionalWeatherDataSyncService extends ServiceImpl<RegionalWeatherD
|
|||||||
config.getWeatherCity(),
|
config.getWeatherCity(),
|
||||||
config.getWeatherDistrict()),
|
config.getWeatherDistrict()),
|
||||||
WeatherSiteAreaConfiguration::getDistrictCode,
|
WeatherSiteAreaConfiguration::getDistrictCode,
|
||||||
(oldValue, newValue) -> oldValue
|
(oldValue, newValue) -> oldValue));
|
||||||
));
|
|
||||||
|
|
||||||
// 全部放入你的成员变量 Map
|
// 全部放入你的成员变量 Map
|
||||||
weatherAreaMap.putAll(tempMap);
|
weatherAreaMap.putAll(tempMap);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private static String buildKey(String province, String city, String district) {
|
private static String buildKey(String province, String city, String district) {
|
||||||
return String.join(":",
|
return String.join(":",
|
||||||
safe(province),
|
safe(province),
|
||||||
safe(city),
|
safe(city),
|
||||||
safe(district)
|
safe(district));
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static String safe(String str) {
|
private static String safe(String str) {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user