|
@@ -2,9 +2,16 @@ 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.core.toolkit.StringUtils;
|
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
+import com.hankcs.hanlp.dictionary.py.Pinyin;
|
|
|
+import com.hankcs.hanlp.dictionary.py.PinyinDictionary;
|
|
|
import lombok.AllArgsConstructor;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
+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;
|
|
@@ -20,11 +27,13 @@ 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.Duration;
|
|
|
import java.time.Instant;
|
|
|
import java.time.LocalDateTime;
|
|
|
import java.time.ZoneId;
|
|
|
+import java.time.format.DateTimeFormatter;
|
|
|
import java.util.*;
|
|
|
|
|
|
@Slf4j
|
|
@@ -179,7 +188,7 @@ public class WeatherInfoServiceImpl extends ServiceImpl<WeatherInfoMapper, Weath
|
|
|
weatherInfo = new WeatherInfo();
|
|
|
GregorianCalendar calendar = new GregorianCalendar();
|
|
|
calendar.setTime(new Date());
|
|
|
- calendar.add(Calendar.DATE,1);
|
|
|
+// calendar.add(Calendar.DATE,1);
|
|
|
weatherInfo.setRecordTime(calendar.getTime());
|
|
|
}
|
|
|
Date recordTime = weatherInfo.getRecordTime();
|
|
@@ -199,20 +208,36 @@ public class WeatherInfoServiceImpl extends ServiceImpl<WeatherInfoMapper, Weath
|
|
|
Duration duration = Duration.between(plainTime, ContractTime);
|
|
|
if (duration.toDays() > 0){
|
|
|
List<WeatherInfo> list = new ArrayList<>();
|
|
|
+ Map<String, Map<String, String>> weatherMap = null;
|
|
|
+ List<Pinyin> py = PinyinDictionary.convertToPinyin(projectContractArea.getCounty().substring(0, projectContractArea.getCounty().length() - 1));
|
|
|
+ StringBuilder county = new StringBuilder();
|
|
|
+ for (int i = 0; i < py.size(); i++) {
|
|
|
+ county.append(py.get(i).getPinyinWithoutTone());
|
|
|
+ }
|
|
|
+ List<Pinyin> py2 = PinyinDictionary.convertToPinyin(projectContractArea.getCity().substring(0, projectContractArea.getCity().length() - 1));
|
|
|
+ StringBuilder city = new StringBuilder();
|
|
|
+ for (int i = 0; i < py2.size(); i++) {
|
|
|
+ city.append(py2.get(i).getPinyinWithoutTone());
|
|
|
+ }
|
|
|
while (Duration.between(plainTime,ContractTime).toDays() != 0){
|
|
|
- //获取天气信息(百度天气)
|
|
|
- Map<String,String> weatherMap = BaiduApiUtil.getTodayWeather(projectContractArea.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);
|
|
|
- WeatherInfo newWeather = new WeatherInfo(String.valueOf(projectContractArea.getId()), weatherMap.get("weather"), aver.toString(), weatherMap.get("high"), weatherMap.get("low"), weatherMap.get("windLevel"));
|
|
|
- newWeather.setRecordTime(Date.from( plainTime.atZone( ZoneId.systemDefault()).toInstant()));
|
|
|
-// this.save(newWeather);
|
|
|
- list.add(newWeather);
|
|
|
- log.info("历史天气已经同步完成!contractAreaId:" + projectContractArea.getId() + ",syncTime:" + plainTime);
|
|
|
- } else {
|
|
|
- log.info("获取历史天气失败!contractAreaId:" + projectContractArea.getId() + ",syncTime:" + plainTime);
|
|
|
+ //判断集合中是否存在当前天气,如果不存在则获取
|
|
|
+ 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")));
|
|
|
+ if (weatherMap == null || weatherMap.get(plainTime.format(DateTimeFormatter.ofPattern("yyyy年MM月dd日"))) == null){
|
|
|
+ log.info("获取历史天气失败!contractAreaId:" + projectContractArea.getId() + ",syncTime:" + plainTime);
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
+ Map<String, String> day = weatherMap.get(plainTime.format(DateTimeFormatter.ofPattern("yyyy年MM月dd日")));
|
|
|
+ //计算平均气温
|
|
|
+ BigDecimal aver = (new BigDecimal(day.get("high")).add(new BigDecimal(day.get("low")))).divide(new BigDecimal("2"), 1, BigDecimal.ROUND_HALF_UP);
|
|
|
+ WeatherInfo newWeather = new WeatherInfo(String.valueOf(projectContractArea.getId()), day.get("weather"), aver.toString(), day.get("high"), day.get("low"), day.get("windLevel"));
|
|
|
+ newWeather.setRecordTime(Date.from( plainTime.atZone( ZoneId.systemDefault()).toInstant()));
|
|
|
+ list.add(newWeather);
|
|
|
+ log.info("历史天气已经同步完成!contractAreaId:" + projectContractArea.getId() + ",syncTime:" + plainTime);
|
|
|
plainTime = plainTime.plusDays(1);
|
|
|
}
|
|
|
this.saveBatch(list);
|
|
@@ -222,4 +247,36 @@ public class WeatherInfoServiceImpl extends ServiceImpl<WeatherInfoMapper, Weath
|
|
|
|
|
|
}
|
|
|
|
|
|
+ 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<>();
|
|
|
+ Document document = null;
|
|
|
+ try {
|
|
|
+ document = Jsoup.connect(html).get();
|
|
|
+ Element body = document.body();
|
|
|
+ Elements table = body.getElementsByTag("table");
|
|
|
+ Elements trs = table.select("tr");
|
|
|
+ //判断网页是否存在
|
|
|
+ if (trs.get(0).select("td").text().contains("本站目前")){
|
|
|
+ System.out.println("网页错误");
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ for (int i = 1; i < trs.size(); i++) {
|
|
|
+ Element tr = trs.get(i);
|
|
|
+ Elements tds = tr.select("td");
|
|
|
+
|
|
|
+ Map<String,String> m = new HashMap<>();
|
|
|
+ m.put("weather",tds.get(1).text().split("/")[0].trim());
|
|
|
+ String[] split = tds.get(2).text().split("/");
|
|
|
+ m.put("low",split[0].substring(0,split[0].length()-2));
|
|
|
+ m.put("high",split[1].substring(0,split[1].length()-1).trim());
|
|
|
+ m.put("windLevel",tds.get(3).text().substring(tds.get(3).text().length()-2));
|
|
|
+ map.put(tds.get(0).text(),m);
|
|
|
+ }
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return map;
|
|
|
+ }
|
|
|
+
|
|
|
}
|