区域气象数据同步修改:去除同步国家气象数据
This commit is contained in:
parent
cbc17435f1
commit
e418fa18ab
@ -1,11 +1,12 @@
|
|||||||
package com.southern.power.grid.service.impl;
|
package com.southern.power.grid.service.impl;
|
||||||
|
|
||||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
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.RegionalWeatherDataMapper;
|
||||||
import com.southern.power.grid.dao.RegionalWeatherStationMapper;
|
import com.southern.power.grid.dao.RegionalWeatherStationMapper;
|
||||||
import com.southern.power.grid.dao.WeatherSiteAreaConfigurationMapper;
|
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.RequiredArgsConstructor;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
@ -17,7 +18,6 @@ import java.math.RoundingMode;
|
|||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.util.concurrent.ConcurrentHashMap;
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
import java.util.stream.Stream;
|
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
@Slf4j
|
@Slf4j
|
||||||
@ -25,25 +25,22 @@ import java.util.stream.Stream;
|
|||||||
public class RegionalWeatherDataSyncService extends ServiceImpl<RegionalWeatherDataMapper, RegionalWeatherData> {
|
public class RegionalWeatherDataSyncService extends ServiceImpl<RegionalWeatherDataMapper, RegionalWeatherData> {
|
||||||
|
|
||||||
private final RegionalWeatherStationMapper regionalWeatherStationMapper;
|
private final RegionalWeatherStationMapper regionalWeatherStationMapper;
|
||||||
private final NationalWeatherStationMapper nationalWeatherStationMapper;
|
|
||||||
private final Map<String, String> weatherAreaMap = new ConcurrentHashMap<>();
|
|
||||||
private final WeatherSiteAreaConfigurationMapper weatherSiteAreaConfigurationMapper;
|
private final WeatherSiteAreaConfigurationMapper weatherSiteAreaConfigurationMapper;
|
||||||
|
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 {
|
||||||
List<RegionalWeatherStation> regionalWeatherStations = regionalWeatherStationMapper.getSyncData(dateTime);
|
List<RegionalWeatherStation> regionalWeatherStations = regionalWeatherStationMapper.getSyncData(dateTime);
|
||||||
List<NationalWeatherStation> nationalWeatherStations = nationalWeatherStationMapper.getSyncData(dateTime);
|
if (CollectionUtils.isEmpty(regionalWeatherStations)) {
|
||||||
if (CollectionUtils.isEmpty(regionalWeatherStations) && CollectionUtils.isEmpty(nationalWeatherStations)) {
|
|
||||||
log.warn("【区域天气数据同步任务】没有查询到天气数据:{}", dateTime);
|
log.warn("【区域天气数据同步任务】没有查询到天气数据:{}", dateTime);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
List<SyncWeatherDataVO> list = convertToSyncList(regionalWeatherStations, nationalWeatherStations);
|
List<RegionalWeatherData> dataList = convert(regionalWeatherStations, dateTime);
|
||||||
if (CollectionUtils.isEmpty(list)) {
|
if (CollectionUtils.isEmpty(dataList)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
List<RegionalWeatherData> dataList = convert(list, dateTime);
|
|
||||||
this.saveBatch(dataList, 700);
|
this.saveBatch(dataList, 700);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.error("定时同步区域气象数据失败:{}", e.getMessage(), 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()) {
|
if (list == null || list.isEmpty()) {
|
||||||
return Collections.emptyList();
|
return Collections.emptyList();
|
||||||
}
|
}
|
||||||
|
|
||||||
// 1. 按 orgCode 分组
|
// 1. 过滤掉未匹配区域编码的记录,按 orgCode 分组
|
||||||
Map<String, List<SyncWeatherDataVO>> groupMap =
|
Map<String, List<RegionalWeatherStation>> groupMap = list.stream()
|
||||||
list.stream().collect(Collectors.groupingBy(SyncWeatherDataVO::getOrgCode));
|
.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. 分组处理
|
// 2. 分组处理
|
||||||
List<RegionalWeatherData> result = new ArrayList<>();
|
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();
|
String orgCode = entry.getKey();
|
||||||
List<SyncWeatherDataVO> groupList = entry.getValue();
|
List<RegionalWeatherStation> groupList = entry.getValue();
|
||||||
|
|
||||||
int size = groupList.size();
|
int size = groupList.size();
|
||||||
|
|
||||||
@ -76,13 +75,13 @@ public class RegionalWeatherDataSyncService extends ServiceImpl<RegionalWeatherD
|
|||||||
BigDecimal minTemp = null;
|
BigDecimal minTemp = null;
|
||||||
BigDecimal maxWind = null;
|
BigDecimal maxWind = null;
|
||||||
|
|
||||||
for (SyncWeatherDataVO vo : groupList) {
|
for (RegionalWeatherStation station : groupList) {
|
||||||
|
|
||||||
BigDecimal hourlyPre = parse(vo.getHourlyPrecipitation());
|
BigDecimal hourlyPre = parse(station.getHourlyPrecipitation());
|
||||||
BigDecimal temp = parse(vo.getTemperature());
|
BigDecimal temp = parse(station.getTemperature());
|
||||||
BigDecimal maxT = parse(vo.getHourlyMaxTemperature());
|
BigDecimal maxT = parse(station.getHourlyMaxTemperature());
|
||||||
BigDecimal minT = parse(vo.getHourlyMinTemperature());
|
BigDecimal minT = parse(station.getHourlyMinTemperature());
|
||||||
BigDecimal wind = parse(vo.getExtremeWindSpeedHourly());
|
BigDecimal wind = parse(station.getExtremeWindSpeedHourly());
|
||||||
|
|
||||||
// 累加
|
// 累加
|
||||||
sumHourlyPrecipitation = sumHourlyPrecipitation.add(hourlyPre);
|
sumHourlyPrecipitation = sumHourlyPrecipitation.add(hourlyPre);
|
||||||
@ -184,79 +183,6 @@ public class RegionalWeatherDataSyncService extends ServiceImpl<RegionalWeatherD
|
|||||||
weatherAreaMap.putAll(tempMap);
|
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) {
|
private static String buildKey(String province, String city, String district) {
|
||||||
return String.join(":",
|
return String.join(":",
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user