|
@@ -1,18 +1,39 @@
|
|
|
package com.gyee.meteorological.service.collection;
|
|
|
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.alibaba.fastjson.TypeReference;
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
+import com.gyee.common.model.StringUtils;
|
|
|
+import com.gyee.common.util.CommonUtils;
|
|
|
import com.gyee.common.util.DateUtils;
|
|
|
+import com.gyee.common.util.JSONUtils;
|
|
|
import com.gyee.common.util.RestTemplateUtil;
|
|
|
import com.gyee.meteorological.init.CacheContext;
|
|
|
+import com.gyee.meteorological.model.*;
|
|
|
import com.gyee.meteorological.model.auto.Coordinate;
|
|
|
+import com.gyee.meteorological.model.auto.ProEconCoordinate;
|
|
|
+import com.gyee.meteorological.model.auto.ProEconMeteorological;
|
|
|
+import com.gyee.meteorological.model.auto.ProEconMeteorologicalfourdays;
|
|
|
import com.gyee.meteorological.service.RedisService;
|
|
|
+import com.gyee.meteorological.service.auto.IProEconCoordinateService;
|
|
|
+import com.gyee.meteorological.service.auto.IProEconMeteorologicalService;
|
|
|
+import com.gyee.meteorological.service.auto.IProEconMeteorologicalfourdaysService;
|
|
|
import org.apache.commons.logging.Log;
|
|
|
import org.apache.commons.logging.LogFactory;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
+import org.springframework.boot.system.ApplicationHome;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.util.ResourceUtils;
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
-import java.util.Arrays;
|
|
|
+import java.io.File;
|
|
|
+import java.io.FileInputStream;
|
|
|
+import java.io.FileNotFoundException;
|
|
|
+import java.io.IOException;
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
+import java.util.*;
|
|
|
import java.util.List;
|
|
|
+import java.util.stream.Collectors;
|
|
|
|
|
|
|
|
|
* @ClassName : CollectionService
|
|
@@ -24,8 +45,14 @@ import java.util.List;
|
|
|
public class CollectionService {
|
|
|
|
|
|
private Log log = LogFactory.getLog(CollectionService.class);
|
|
|
+
|
|
|
+
|
|
|
@Resource
|
|
|
- private RedisService redisService;
|
|
|
+ private IProEconMeteorologicalService proEconMeteorologicalService;
|
|
|
+ @Resource
|
|
|
+ private IProEconMeteorologicalfourdaysService proEconMeteorologicalfourdaysService;
|
|
|
+ @Resource
|
|
|
+ private IProEconCoordinateService proEconCoordinateService;
|
|
|
|
|
|
@Value("${meteorological.model}")
|
|
|
private String model;
|
|
@@ -34,21 +61,339 @@ public class CollectionService {
|
|
|
@Value("${meteorological.url}")
|
|
|
private String url;
|
|
|
|
|
|
- public void saveMeteorologicalData(){
|
|
|
- List<Coordinate> coordinateList = CacheContext.coordinateList;
|
|
|
-
|
|
|
- coordinateList.stream().forEach(c->{
|
|
|
- String[] models = model.split(",");
|
|
|
- Arrays.stream(models).forEach(m->{
|
|
|
- long startTime = System.currentTimeMillis();
|
|
|
- String restUrl = url+m+"?units=metric&lat="+c.getLat()+"&lon="+c.getLon()+"&units=metric&appid="+weatherkey;
|
|
|
- String s = RestTemplateUtil.get(restUrl, null);
|
|
|
- redisService.set((c.getForeignkeyid()+"_"+m).toUpperCase(),s);
|
|
|
- long endTime = System.currentTimeMillis();
|
|
|
- log.info((c.getForeignkeyid()+"_"+m).toUpperCase()+"保存成功,用时"+ (endTime-startTime) + "毫秒,当前时间为"+DateUtils.parseLongToDate(endTime));
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ public void saveMeteorological() throws IOException {
|
|
|
+ List<ProEconCoordinate> proEconCoordinates = proEconCoordinateService.list().stream().filter(i->i.getIsOpen()!=null && i.getIsOpen().equals(1)).collect(Collectors.toList());
|
|
|
+
|
|
|
+
|
|
|
+ long startTime = System.currentTimeMillis();
|
|
|
+ proEconCoordinates.stream().forEach(c->{
|
|
|
+ String restUrl = url+"weather?units=metric&land=zh_cn&lat="+c.getLat()+"&lon="+c.getLon()+"&appid="+weatherkey;
|
|
|
+ String weatherStr = RestTemplateUtil.get(restUrl, null);
|
|
|
+ WeatherReal weatherReal = JSONObject.parseObject(weatherStr, new TypeReference<WeatherReal>() {
|
|
|
});
|
|
|
+ ProEconMeteorological proEconMeteorological = new ProEconMeteorological();
|
|
|
+ proEconMeteorological.setId(CommonUtils.getUUID());
|
|
|
+ proEconMeteorological.setWindpowerstationId(c.getWindpowerstationId());
|
|
|
+
|
|
|
+ if(null!=weatherReal.getMain()) {
|
|
|
+ Main main=weatherReal.getMain();
|
|
|
+
|
|
|
+ double visibility=weatherReal.getVisibility();
|
|
|
+ proEconMeteorological.setVisibility(visibility);
|
|
|
+
|
|
|
+ proEconMeteorological.setTemp(main.getTemp());
|
|
|
+
|
|
|
+ proEconMeteorological.setTempMax(main.getTemp_max());
|
|
|
+
|
|
|
+ proEconMeteorological.setTempMin(main.getTemp_min());
|
|
|
+
|
|
|
+ proEconMeteorological.setPressure(main.getPressure());
|
|
|
+
|
|
|
+ proEconMeteorological.setHumidity(main.getHumidity());
|
|
|
+
|
|
|
+ proEconMeteorological.setSeaLevel(main.getSea_level());
|
|
|
+
|
|
|
+ proEconMeteorological.setGrndLevel(main.getGrnd_level());
|
|
|
+ }
|
|
|
+ if(null!=weatherReal.getWeather() && !weatherReal.getWeather().isEmpty()){
|
|
|
+ Weather weather=weatherReal.getWeather().get(0);
|
|
|
+
|
|
|
+ String icon=weather.getIcon();
|
|
|
+
|
|
|
+ if (StringUtils.notEmp(icon))
|
|
|
+ {
|
|
|
+ if(icon.equals("01d") || icon.equals("01n"))
|
|
|
+ {
|
|
|
+
|
|
|
+ proEconMeteorological.setWeather("晴");
|
|
|
+
|
|
|
+ }else if(icon.equals("02d") || icon.equals("02n"))
|
|
|
+ {
|
|
|
+ proEconMeteorological.setWeather("少云");
|
|
|
+ }else if(icon.equals("03d") || icon.equals("03n"))
|
|
|
+ {
|
|
|
+ proEconMeteorological.setWeather("多云");
|
|
|
+ }else if(icon.equals("04d") || icon.equals("04n"))
|
|
|
+ {
|
|
|
+ proEconMeteorological.setWeather("阴");
|
|
|
+ }else if(icon.equals("09d") || icon.equals("09n"))
|
|
|
+ {
|
|
|
+ proEconMeteorological.setWeather("阵雨");
|
|
|
+ }else if(icon.equals("10d") || icon.equals("10n"))
|
|
|
+ {
|
|
|
+ proEconMeteorological.setWeather("雨");
|
|
|
+ }else if(icon.equals("11d") || icon.equals("11n"))
|
|
|
+ {
|
|
|
+ proEconMeteorological.setWeather("雷雨");
|
|
|
+ }else if(icon.equals("13d") || icon.equals("13n"))
|
|
|
+ {
|
|
|
+ proEconMeteorological.setWeather("雪");
|
|
|
+ }else if(icon.equals("50d") || icon.equals("50n"))
|
|
|
+ {
|
|
|
+ proEconMeteorological.setWeather("雾");
|
|
|
+ }else
|
|
|
+ {
|
|
|
+ proEconMeteorological.setWeather("晴");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if(null!=weatherReal.getWind()) {
|
|
|
+ Wind wind=weatherReal.getWind();
|
|
|
+
|
|
|
+ proEconMeteorological.setSpeed(wind.getSpeed());
|
|
|
+ proEconMeteorological.setGust(wind.getGust());
|
|
|
+ if(StringUtils.notEmp(wind.getDeg()))
|
|
|
+ {
|
|
|
+ double fx=wind.getDeg();
|
|
|
+
|
|
|
+ proEconMeteorological.setDeg(fx);
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ if(null!=weatherReal.getClouds()) {
|
|
|
+ Clouds clouds=weatherReal.getClouds();
|
|
|
+
|
|
|
+ proEconMeteorological.setClouds(clouds.getAll());
|
|
|
+ }
|
|
|
+ if(null!=weatherReal.getSys())
|
|
|
+ {
|
|
|
+ Sys sys=weatherReal.getSys();
|
|
|
+ if(StringUtils.notEmp(sys.getSunrise()))
|
|
|
+ {
|
|
|
+ Date date=new Date(sys.getSunrise()*1000);
|
|
|
+ SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
|
|
|
+
|
|
|
+ proEconMeteorological.setSunrise(sdf.format(date));
|
|
|
+
|
|
|
+ }
|
|
|
+ if(StringUtils.notEmp(sys.getSunset()))
|
|
|
+ {
|
|
|
+ Date date=new Date(sys.getSunset()*1000);
|
|
|
+ SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
|
|
|
+
|
|
|
+ proEconMeteorological.setSunset(sdf.format(date));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ Date date=DateUtils.parseLongToDate(weatherReal.getDt()*1000);
|
|
|
+ proEconMeteorological.setDatetime(date);
|
|
|
+
|
|
|
+ QueryWrapper<ProEconMeteorological> qw = new QueryWrapper<>();
|
|
|
+ qw.eq("windpowerstation_id",c.getWindpowerstationId());
|
|
|
+ qw.eq("datetime",date);
|
|
|
+ List<ProEconMeteorological> list = proEconMeteorologicalService.list(qw);
|
|
|
+ if (StringUtils.isEmpty(list)){
|
|
|
+ proEconMeteorologicalService.save(proEconMeteorological);
|
|
|
+ }
|
|
|
+
|
|
|
});
|
|
|
+ long endTime = System.currentTimeMillis();
|
|
|
+ log.info("保存成功,用时"+ (endTime-startTime) + "毫秒,当前时间为"+DateUtils.parseLongToDate(endTime));
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ public void saveMeteorologicalFiveDay(){
|
|
|
|
|
|
- ;
|
|
|
+ List<ProEconCoordinate> proEconCoordinates = proEconCoordinateService.list().stream().filter(i->i.getIsOpen()!=null && i.getIsOpen().equals(1)).collect(Collectors.toList());
|
|
|
+
|
|
|
+ QueryWrapper<ProEconMeteorologicalfourdays> qw = new QueryWrapper<>();
|
|
|
+ qw.ge("datetime",DateUtils.getCurrentDate());
|
|
|
+ proEconMeteorologicalfourdaysService.remove(qw);
|
|
|
+
|
|
|
+ long startTime = System.currentTimeMillis();
|
|
|
+ List<ProEconMeteorologicalfourdays> resultList = new ArrayList<>();
|
|
|
+ proEconCoordinates.stream().forEach(c->{
|
|
|
+ String restUrl = url+"forecast?units=metric&land=zh_cn&lat="+c.getLat()+"&lon="+c.getLon()+"&appid="+weatherkey;
|
|
|
+ String weatherStr = RestTemplateUtil.get(restUrl, null);
|
|
|
+ JsonRootBean jsonRootBean = JSONObject.parseObject(weatherStr, new TypeReference<JsonRootBean>() {
|
|
|
+ });
|
|
|
+ String sunriseString = null;
|
|
|
+ String sunsetString = null;
|
|
|
+ if(null!=jsonRootBean && null!=jsonRootBean.getCity()){
|
|
|
+ City city = jsonRootBean.getCity();
|
|
|
+ if(StringUtils.notEmp(city.getSunrise()))
|
|
|
+ {
|
|
|
+ Date sunrise=new Date(city.getSunrise()*1000);
|
|
|
+ SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
|
|
|
+ sunriseString = sdf.format(sunrise);
|
|
|
+ }
|
|
|
+ if(StringUtils.notEmp(city.getSunset()))
|
|
|
+ {
|
|
|
+ Date sunset=new Date(city.getSunset()*1000);
|
|
|
+ SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
|
|
|
+ sunsetString = sdf.format(sunset);
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if(null!=jsonRootBean && null!=jsonRootBean.getList() && !jsonRootBean.getList().isEmpty()){
|
|
|
+ for(com.gyee.meteorological.model.List weatherReal:jsonRootBean.getList()){
|
|
|
+ ProEconMeteorologicalfourdays proEconMeteorological = new ProEconMeteorologicalfourdays();
|
|
|
+ Date date=DateUtils.parseLongToDate(weatherReal.getDt()*1000);
|
|
|
+ proEconMeteorological.setId(CommonUtils.getUUID());
|
|
|
+ proEconMeteorological.setDatetime(date);
|
|
|
+ proEconMeteorological.setWindpowerstationId(c.getWindpowerstationId());
|
|
|
+ proEconMeteorological.setVisibility(weatherReal.getVisibility());
|
|
|
+
|
|
|
+ proEconMeteorological.setSunrise(sunriseString);
|
|
|
+
|
|
|
+ proEconMeteorological.setSunset(sunsetString);
|
|
|
+ if(null!=weatherReal.getMain())
|
|
|
+ {
|
|
|
+ Main main=weatherReal.getMain();
|
|
|
+ proEconMeteorological.setTemp(main.getTemp());
|
|
|
+ proEconMeteorological.setTempMax(main.getTemp_max());
|
|
|
+ proEconMeteorological.setTempMin(main.getTemp_min());
|
|
|
+ proEconMeteorological.setPressure(main.getPressure());
|
|
|
+ proEconMeteorological.setHumidity(main.getHumidity());
|
|
|
+ proEconMeteorological.setSeaLevel(main.getSea_level());
|
|
|
+ proEconMeteorological.setGrndLevel(main.getGrnd_level());
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ if(null!=weatherReal.getClouds())
|
|
|
+ {
|
|
|
+
|
|
|
+ proEconMeteorological.setClouds(weatherReal.getClouds().getAll());
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ if(null!=weatherReal.getWind())
|
|
|
+ {
|
|
|
+ Wind wind=weatherReal.getWind();
|
|
|
+ proEconMeteorological.setSpeed(wind.getSpeed());
|
|
|
+
|
|
|
+ if(StringUtils.notEmp(wind.getDeg()))
|
|
|
+ {
|
|
|
+ double fx=wind.getDeg();
|
|
|
+ proEconMeteorological.setDeg(fx);
|
|
|
+ }
|
|
|
+ if(StringUtils.notEmp(wind.getGust()))
|
|
|
+ {
|
|
|
+ double zf=wind.getGust();
|
|
|
+ proEconMeteorological.setGust(zf);
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ if(null!=weatherReal.getWeather() && !weatherReal.getWeather().isEmpty())
|
|
|
+ {
|
|
|
+ Weather weather=weatherReal.getWeather().get(0);
|
|
|
+
|
|
|
+ String icon=weather.getIcon();
|
|
|
+
|
|
|
+ if (StringUtils.notEmp(icon))
|
|
|
+ {
|
|
|
+ if(icon.equals("01d") || icon.equals("01n"))
|
|
|
+ {
|
|
|
+ proEconMeteorological.setWeather("晴");
|
|
|
+ }else if(icon.equals("02d") || icon.equals("02n"))
|
|
|
+ {
|
|
|
+ proEconMeteorological.setWeather("少云");
|
|
|
+ }else if(icon.equals("03d") || icon.equals("03n"))
|
|
|
+ {
|
|
|
+ proEconMeteorological.setWeather("多云");
|
|
|
+ }else if(icon.equals("04d") || icon.equals("04n"))
|
|
|
+ {
|
|
|
+ proEconMeteorological.setWeather("阴");
|
|
|
+ }else if(icon.equals("09d") || icon.equals("09n"))
|
|
|
+ {
|
|
|
+ proEconMeteorological.setWeather("阵雨");
|
|
|
+ }else if(icon.equals("10d") || icon.equals("10n"))
|
|
|
+ {
|
|
|
+ proEconMeteorological.setWeather("雨");
|
|
|
+ }else if(icon.equals("11d") || icon.equals("11n"))
|
|
|
+ {
|
|
|
+ proEconMeteorological.setWeather("雷雨");
|
|
|
+ }else if(icon.equals("13d") || icon.equals("13n"))
|
|
|
+ {
|
|
|
+ proEconMeteorological.setWeather("雪");
|
|
|
+ }else if(icon.equals("50d") || icon.equals("50n"))
|
|
|
+ {
|
|
|
+ proEconMeteorological.setWeather("雾");
|
|
|
+ }else
|
|
|
+ {
|
|
|
+ proEconMeteorological.setWeather("晴");
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+ resultList.add(proEconMeteorological);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ });
|
|
|
+ proEconMeteorologicalfourdaysService.saveBatch(resultList);
|
|
|
+ long endTime = System.currentTimeMillis();
|
|
|
+ log.info("保存成功,用时"+ (endTime-startTime) + "毫秒,当前时间为"+DateUtils.parseLongToDate(endTime));
|
|
|
}
|
|
|
}
|