|
@@ -1,7 +1,6 @@
|
|
|
package org.springblade.business.service.impl;
|
|
|
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
-import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
import com.hankcs.hanlp.dictionary.py.Pinyin;
|
|
|
import com.hankcs.hanlp.dictionary.py.PinyinDictionary;
|
|
@@ -13,27 +12,33 @@ import org.jsoup.Jsoup;
|
|
|
import org.jsoup.nodes.Document;
|
|
|
import org.jsoup.nodes.Element;
|
|
|
import org.jsoup.select.Elements;
|
|
|
-import org.springblade.common.utils.BaiduApiUtil;
|
|
|
-import org.springblade.core.tool.utils.DateUtil;
|
|
|
import org.springblade.business.entity.WeatherInfo;
|
|
|
import org.springblade.business.mapper.WeatherInfoMapper;
|
|
|
import org.springblade.business.service.WeatherInfoService;
|
|
|
-//import org.springframework.scheduling.annotation.Scheduled;
|
|
|
+import org.springblade.common.utils.BaiduApiUtil;
|
|
|
+import org.springblade.common.utils.YiKeYunApiUtils;
|
|
|
+import org.springblade.core.mp.support.Condition;
|
|
|
+import org.springblade.core.tool.utils.DateUtil;
|
|
|
import org.springblade.core.tool.utils.Func;
|
|
|
-import org.springblade.manager.entity.ContractInfo;
|
|
|
import org.springblade.manager.entity.ProjectContractArea;
|
|
|
import org.springblade.manager.entity.ProjectInfo;
|
|
|
import org.springblade.manager.feign.ProjectClient;
|
|
|
import org.springblade.manager.feign.ProjectContractAreaClient;
|
|
|
import org.springblade.manager.vo.ContractInfoVO;
|
|
|
-import org.springframework.scheduling.annotation.Async;
|
|
|
+import org.springframework.dao.DataAccessException;
|
|
|
+import org.springframework.jdbc.core.JdbcTemplate;
|
|
|
+import org.springframework.jdbc.core.ResultSetExtractor;
|
|
|
import org.springframework.scheduling.annotation.Scheduled;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
-import org.springblade.core.mp.support.Condition;
|
|
|
|
|
|
import java.io.IOException;
|
|
|
import java.math.BigDecimal;
|
|
|
-import java.time.*;
|
|
|
+import java.sql.ResultSet;
|
|
|
+import java.sql.SQLException;
|
|
|
+import java.time.Duration;
|
|
|
+import java.time.LocalDate;
|
|
|
+import java.time.LocalDateTime;
|
|
|
+import java.time.ZoneId;
|
|
|
import java.time.format.DateTimeFormatter;
|
|
|
import java.util.*;
|
|
|
import java.util.stream.Collectors;
|
|
@@ -47,6 +52,8 @@ public class WeatherInfoServiceImpl extends ServiceImpl<WeatherInfoMapper, Weath
|
|
|
|
|
|
private final ProjectClient projectClient;
|
|
|
|
|
|
+ private final JdbcTemplate jdbcTemplate;
|
|
|
+
|
|
|
/**
|
|
|
* 根据所选年份获取当年所有天气台账
|
|
|
*/
|
|
@@ -114,7 +121,7 @@ public class WeatherInfoServiceImpl extends ServiceImpl<WeatherInfoMapper, Weath
|
|
|
public void syncWeatherInfo() {
|
|
|
//获取所有合同段的定位信息
|
|
|
List<ProjectContractArea> areaList = this.projectContractAreaClient.queryAllContractArea();
|
|
|
-
|
|
|
+ Map<String, Map<String, String>> cachedWeatherMap = new HashMap<>();
|
|
|
for (ProjectContractArea area : areaList) {
|
|
|
try {
|
|
|
//校验当前区域是否已经获取当天日期(手动补填或自动获取)
|
|
@@ -128,8 +135,14 @@ public class WeatherInfoServiceImpl extends ServiceImpl<WeatherInfoMapper, Weath
|
|
|
log.info("今日的天气已经同步完成!contractAreaId:" + area.getId() + ",syncTime:" + DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
|
|
|
continue;
|
|
|
}
|
|
|
- //获取天气信息(百度天气)
|
|
|
- Map<String, String> weatherMap = BaiduApiUtil.getTodayWeather(area.getCity_code());
|
|
|
+ Map<String, String> weatherMap = cachedWeatherMap.get(area.getCity_code());
|
|
|
+ if (weatherMap == null) {
|
|
|
+ //获取天气信息(百度天气)
|
|
|
+ weatherMap = YiKeYunApiUtils.getTodayWeatherByAdcode(area.getCity_code());
|
|
|
+ if (weatherMap == null) {
|
|
|
+ weatherMap = BaiduApiUtil.getTodayWeather(area.getCity_code());
|
|
|
+ }
|
|
|
+ }
|
|
|
if (weatherMap != null) {
|
|
|
//计算平均气温
|
|
|
BigDecimal aver = (new BigDecimal(weatherMap.get("high")).add(new BigDecimal(weatherMap.get("low")))).divide(new BigDecimal("2"), 1, BigDecimal.ROUND_HALF_UP);
|
|
@@ -231,13 +244,17 @@ public class WeatherInfoServiceImpl extends ServiceImpl<WeatherInfoMapper, Weath
|
|
|
while (Duration.between(plainTime, ContractTime).toDays() != 0) {
|
|
|
//判断集合中是否存在当前天气,如果不存在则获取
|
|
|
if (weatherMap == null || weatherMap.get(plainTime.format(DateTimeFormatter.ofPattern("yyyy年MM月dd日"))) == null) {
|
|
|
- weatherMap = this.getWeather(county.toString(), plainTime.format(DateTimeFormatter.ofPattern("yyyyMM")));
|
|
|
- if (weatherMap == null || weatherMap.get(plainTime.format(DateTimeFormatter.ofPattern("yyyy年MM月dd日"))) == null) {
|
|
|
- weatherMap = this.getWeather(city.toString(), plainTime.format(DateTimeFormatter.ofPattern("yyyyMM")));
|
|
|
- county = city;
|
|
|
+ String month = plainTime.format(DateTimeFormatter.ofPattern("yyyyMM"));
|
|
|
+ weatherMap = this.getHistoryWeather(projectContractArea, month);
|
|
|
+ if (weatherMap == null) {
|
|
|
+ weatherMap = this.getWeather(county.toString(), month);
|
|
|
if (weatherMap == null || weatherMap.get(plainTime.format(DateTimeFormatter.ofPattern("yyyy年MM月dd日"))) == null) {
|
|
|
- log.info("获取历史天气失败!contractAreaId:" + projectContractArea.getId() + ",syncTime:" + plainTime);
|
|
|
- break;
|
|
|
+ weatherMap = this.getWeather(city.toString(), month);
|
|
|
+ county = city;
|
|
|
+ if (weatherMap == null || weatherMap.get(plainTime.format(DateTimeFormatter.ofPattern("yyyy年MM月dd日"))) == null) {
|
|
|
+ log.info("获取历史天气失败!contractAreaId:" + projectContractArea.getId() + ",syncTime:" + plainTime);
|
|
|
+ break;
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
}
|
|
@@ -322,13 +339,16 @@ public class WeatherInfoServiceImpl extends ServiceImpl<WeatherInfoMapper, Weath
|
|
|
Map<String, List<LocalDate>> map = missDate.stream().collect(Collectors.groupingBy(l -> l.format(DateTimeFormatter.ofPattern("yyyyMM"))));
|
|
|
for (String s : map.keySet()) {
|
|
|
//获取整月天气
|
|
|
- weatherMap = this.getWeather(county.toString(), s);
|
|
|
- if (weatherMap == null || weatherMap.size() == 0) {
|
|
|
- weatherMap = this.getWeather(city.toString(), s);
|
|
|
- county = city;
|
|
|
+ weatherMap = this.getHistoryWeather(area, s);
|
|
|
+ if (weatherMap == null) {
|
|
|
+ weatherMap = this.getWeather(county.toString(), s);
|
|
|
if (weatherMap == null || weatherMap.size() == 0) {
|
|
|
- log.info("获取历史天气失败:" + area.getCity()+"-"+area.getCounty() + ",syncTime:" + s);
|
|
|
- break;
|
|
|
+ weatherMap = this.getWeather(city.toString(), s);
|
|
|
+ county = city;
|
|
|
+ if (weatherMap == null || weatherMap.size() == 0) {
|
|
|
+ log.info("获取历史天气失败:" + area.getCity()+"-"+area.getCounty() + ",syncTime:" + s);
|
|
|
+ break;
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
//为遗漏日期设置天气
|
|
@@ -384,6 +404,33 @@ public class WeatherInfoServiceImpl extends ServiceImpl<WeatherInfoMapper, Weath
|
|
|
System.out.println("9999");
|
|
|
}
|
|
|
|
|
|
+ public Map<String, Map<String, String>> getHistoryWeather(ProjectContractArea area, String month) {
|
|
|
+ String county = area.getCounty();
|
|
|
+ if (county.length() > 2) {
|
|
|
+ county = county.substring(0, county.length() - 1);
|
|
|
+ }
|
|
|
+ String city = area.getCity();
|
|
|
+ if (city.length() > 2) {
|
|
|
+ city = city.substring(0, city.length() - 1);
|
|
|
+ }
|
|
|
+ Integer count = jdbcTemplate.queryForObject("select count(*) from m_yikeyun_weather_city where cityZh like '" + county + "%'", Integer.class);
|
|
|
+ String cityId = null;
|
|
|
+ if (count == null || count == 0) {
|
|
|
+ return null;
|
|
|
+ } else if (count > 1) {
|
|
|
+ cityId = jdbcTemplate.queryForObject("select id from m_yikeyun_weather_city where cityZh like '" + county + "%' and leaderZh like '" + city + "%' limit 1", String.class);
|
|
|
+ } else {
|
|
|
+ cityId = jdbcTemplate.queryForObject("select id from m_yikeyun_weather_city where cityZh like '" + county + "%'", String.class);
|
|
|
+ }
|
|
|
+ if (cityId != null) {
|
|
|
+ Map<String, Map<String, String>> historyWeather = YiKeYunApiUtils.getHistoryWeather(cityId, Integer.parseInt(month.substring(0, 4)), Integer.parseInt(month.substring(4, 6)));
|
|
|
+ return historyWeather;
|
|
|
+ } else {
|
|
|
+ log.info("获取历史天气失败:" + area.getCity()+"-"+area.getCounty());
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
public Map<String, Map<String, String>> getWeather(String city, String month) {
|
|
|
String html = "http://www.tianqihoubao.com/lishi/" + city + "/month/" + month + ".html";
|
|
|
Map<String, Map<String, String>> map = new HashMap<>();
|