区域气象数据同步修改:去除同步国家气象数据
This commit is contained in:
parent
cbc17435f1
commit
e418fa18ab
@ -1,11 +1,12 @@
|
||||
package com.southern.power.grid.service.impl;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.southern.power.grid.dao.NationalWeatherStationMapper;
|
||||
import com.southern.power.grid.dao.RegionalWeatherDataMapper;
|
||||
import com.southern.power.grid.dao.RegionalWeatherStationMapper;
|
||||
import com.southern.power.grid.dao.WeatherSiteAreaConfigurationMapper;
|
||||
import com.southern.power.grid.entity.*;
|
||||
import com.southern.power.grid.entity.RegionalWeatherData;
|
||||
import com.southern.power.grid.entity.RegionalWeatherStation;
|
||||
import com.southern.power.grid.entity.WeatherSiteAreaConfiguration;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Service;
|
||||
@ -17,7 +18,6 @@ import java.math.RoundingMode;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
@Service
|
||||
@Slf4j
|
||||
@ -25,25 +25,22 @@ import java.util.stream.Stream;
|
||||
public class RegionalWeatherDataSyncService extends ServiceImpl<RegionalWeatherDataMapper, RegionalWeatherData> {
|
||||
|
||||
private final RegionalWeatherStationMapper regionalWeatherStationMapper;
|
||||
private final NationalWeatherStationMapper nationalWeatherStationMapper;
|
||||
private final Map<String, String> weatherAreaMap = new ConcurrentHashMap<>();
|
||||
private final WeatherSiteAreaConfigurationMapper weatherSiteAreaConfigurationMapper;
|
||||
private final Map<String, String> weatherAreaMap = new ConcurrentHashMap<>();
|
||||
|
||||
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public void syncWeatherData(String dateTime) {
|
||||
try {
|
||||
List<RegionalWeatherStation> regionalWeatherStations = regionalWeatherStationMapper.getSyncData(dateTime);
|
||||
List<NationalWeatherStation> nationalWeatherStations = nationalWeatherStationMapper.getSyncData(dateTime);
|
||||
if (CollectionUtils.isEmpty(regionalWeatherStations) && CollectionUtils.isEmpty(nationalWeatherStations)) {
|
||||
if (CollectionUtils.isEmpty(regionalWeatherStations)) {
|
||||
log.warn("【区域天气数据同步任务】没有查询到天气数据:{}", dateTime);
|
||||
return;
|
||||
}
|
||||
List<SyncWeatherDataVO> list = convertToSyncList(regionalWeatherStations, nationalWeatherStations);
|
||||
if (CollectionUtils.isEmpty(list)) {
|
||||
List<RegionalWeatherData> dataList = convert(regionalWeatherStations, dateTime);
|
||||
if (CollectionUtils.isEmpty(dataList)) {
|
||||
return;
|
||||
}
|
||||
List<RegionalWeatherData> dataList = convert(list, dateTime);
|
||||
this.saveBatch(dataList, 700);
|
||||
} catch (Exception e) {
|
||||
log.error("定时同步区域气象数据失败:{}", e.getMessage(), e);
|
||||
@ -51,21 +48,23 @@ public class RegionalWeatherDataSyncService extends ServiceImpl<RegionalWeatherD
|
||||
}
|
||||
}
|
||||
|
||||
public List<RegionalWeatherData> convert(List<SyncWeatherDataVO> list, String prevHourDate) {
|
||||
public List<RegionalWeatherData> convert(List<RegionalWeatherStation> list, String prevHourDate) {
|
||||
if (list == null || list.isEmpty()) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
// 1. 按 orgCode 分组
|
||||
Map<String, List<SyncWeatherDataVO>> groupMap =
|
||||
list.stream().collect(Collectors.groupingBy(SyncWeatherDataVO::getOrgCode));
|
||||
// 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()))));
|
||||
|
||||
// 2. 分组处理
|
||||
List<RegionalWeatherData> result = new ArrayList<>();
|
||||
|
||||
for (Map.Entry<String, List<SyncWeatherDataVO>> entry : groupMap.entrySet()) {
|
||||
for (Map.Entry<String, List<RegionalWeatherStation>> entry : groupMap.entrySet()) {
|
||||
String orgCode = entry.getKey();
|
||||
List<SyncWeatherDataVO> groupList = entry.getValue();
|
||||
List<RegionalWeatherStation> groupList = entry.getValue();
|
||||
|
||||
int size = groupList.size();
|
||||
|
||||
@ -76,13 +75,13 @@ public class RegionalWeatherDataSyncService extends ServiceImpl<RegionalWeatherD
|
||||
BigDecimal minTemp = null;
|
||||
BigDecimal maxWind = null;
|
||||
|
||||
for (SyncWeatherDataVO vo : groupList) {
|
||||
for (RegionalWeatherStation station : groupList) {
|
||||
|
||||
BigDecimal hourlyPre = parse(vo.getHourlyPrecipitation());
|
||||
BigDecimal temp = parse(vo.getTemperature());
|
||||
BigDecimal maxT = parse(vo.getHourlyMaxTemperature());
|
||||
BigDecimal minT = parse(vo.getHourlyMinTemperature());
|
||||
BigDecimal wind = parse(vo.getExtremeWindSpeedHourly());
|
||||
BigDecimal hourlyPre = parse(station.getHourlyPrecipitation());
|
||||
BigDecimal temp = parse(station.getTemperature());
|
||||
BigDecimal maxT = parse(station.getHourlyMaxTemperature());
|
||||
BigDecimal minT = parse(station.getHourlyMinTemperature());
|
||||
BigDecimal wind = parse(station.getExtremeWindSpeedHourly());
|
||||
|
||||
// 累加
|
||||
sumHourlyPrecipitation = sumHourlyPrecipitation.add(hourlyPre);
|
||||
@ -184,79 +183,6 @@ public class RegionalWeatherDataSyncService extends ServiceImpl<RegionalWeatherD
|
||||
weatherAreaMap.putAll(tempMap);
|
||||
}
|
||||
|
||||
public List<SyncWeatherDataVO> convertToSyncList(
|
||||
List<RegionalWeatherStation> regionalList,
|
||||
List<NationalWeatherStation> nationalList) {
|
||||
|
||||
Stream<SyncWeatherDataVO> regionalStream = regionalList.stream()
|
||||
.map(item -> buildVO(
|
||||
item.getProvince(),
|
||||
item.getCity(),
|
||||
item.getDistrict(),
|
||||
item.getDataTime(),
|
||||
item.getTemperature(),
|
||||
item.getHourlyMaxTemperature(),
|
||||
item.getHourlyMinTemperature(),
|
||||
item.getHourlyPrecipitation(),
|
||||
item.getDailyPrecipitation(),
|
||||
item.getExtremeWindSpeedHourly(),
|
||||
weatherAreaMap
|
||||
))
|
||||
.filter(Objects::nonNull);
|
||||
|
||||
Stream<SyncWeatherDataVO> nationalStream = nationalList.stream()
|
||||
.map(item -> buildVO(
|
||||
item.getProvince(),
|
||||
item.getCity(),
|
||||
item.getDistrict(),
|
||||
item.getDataTime(),
|
||||
item.getTemperature(),
|
||||
item.getHourlyMaxTemperature(),
|
||||
item.getHourlyMinTemperature(),
|
||||
item.getHourlyPrecipitation(),
|
||||
item.getDailyPrecipitation(),
|
||||
item.getExtremeWindSpeedHourly(),
|
||||
weatherAreaMap
|
||||
))
|
||||
.filter(Objects::nonNull);
|
||||
|
||||
return Stream.concat(regionalStream, nationalStream)
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
private static SyncWeatherDataVO buildVO(
|
||||
String province,
|
||||
String city,
|
||||
String district,
|
||||
String dataTime,
|
||||
String temperature,
|
||||
String hourlyMaxTemperature,
|
||||
String hourlyMinTemperature,
|
||||
String hourlyPrecipitation,
|
||||
String dailyPrecipitation,
|
||||
String extremeWindSpeedHourly,
|
||||
Map<String, String> weatherAreaMap) {
|
||||
|
||||
String key = buildKey(province, city, district);
|
||||
String orgCode = weatherAreaMap.get(key);
|
||||
|
||||
// 没匹配上区域编码,直接丢弃
|
||||
if (orgCode == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
SyncWeatherDataVO vo = new SyncWeatherDataVO();
|
||||
vo.setOrgCode(orgCode);
|
||||
vo.setDataTime(dataTime);
|
||||
vo.setTemperature(temperature);
|
||||
vo.setHourlyMaxTemperature(hourlyMaxTemperature);
|
||||
vo.setHourlyMinTemperature(hourlyMinTemperature);
|
||||
vo.setHourlyPrecipitation(hourlyPrecipitation);
|
||||
vo.setDailyPrecipitation(dailyPrecipitation);
|
||||
vo.setExtremeWindSpeedHourly(extremeWindSpeedHourly);
|
||||
|
||||
return vo;
|
||||
}
|
||||
|
||||
private static String buildKey(String province, String city, String district) {
|
||||
return String.join(":",
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user