|
@@ -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;
|
|
|
@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 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());
|
|
|
+// String property = System.getProperty("user.dir");
|
|
|
+
|
|
|
+ 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(fx< 11.25 || fx>= 348.75)
|
|
|
+// {
|
|
|
+// //风向
|
|
|
+// proEconMeteorological.setDeg("北");
|
|
|
+// }else if(fx>= 11.25 && fx< 33.75)
|
|
|
+// {
|
|
|
+// proEconMeteorological.setDeg("东北北");
|
|
|
+// }else if(fx>= 33.75 && fx< 56.25)
|
|
|
+// {
|
|
|
+// proEconMeteorological.setDeg("东北");
|
|
|
+// }else if(fx>= 56.25 && fx< 78.75)
|
|
|
+// {
|
|
|
+// proEconMeteorological.setDeg("东北东");
|
|
|
+// }else if(fx>= 78.75 && fx< 101.25)
|
|
|
+// {
|
|
|
+// proEconMeteorological.setDeg("东");
|
|
|
+// }else if(fx>= 101.25 && fx< 123.75)
|
|
|
+// {
|
|
|
+// proEconMeteorological.setDeg("东南东");
|
|
|
+// }else if(fx>= 123.75 && fx< 146.25)
|
|
|
+// {
|
|
|
+// proEconMeteorological.setDeg("东南");
|
|
|
+// }else if(fx>= 146.25 && fx< 168.75)
|
|
|
+// {
|
|
|
+// proEconMeteorological.setDeg("东南南");
|
|
|
+// }else if(fx>= 168.75 && fx< 191.25)
|
|
|
+// {
|
|
|
+// proEconMeteorological.setDeg("南");
|
|
|
+// }
|
|
|
+// else if(fx>= 191.25 && fx< 213.75)
|
|
|
+// {
|
|
|
+// proEconMeteorological.setDeg("西南南");
|
|
|
+// }else if(fx>= 213.75 && fx< 236.25)
|
|
|
+// {
|
|
|
+// proEconMeteorological.setDeg("西南");
|
|
|
+// }else if(fx>= 236.25 && fx< 258.75)
|
|
|
+// {
|
|
|
+// proEconMeteorological.setDeg("西南西");
|
|
|
+// }else if(fx>= 258.75 && fx< 281.25)
|
|
|
+// {
|
|
|
+// proEconMeteorological.setDeg("西");
|
|
|
+// }
|
|
|
+// else if(fx>= 281.25 && fx< 303.75)
|
|
|
+// {
|
|
|
+// proEconMeteorological.setDeg("西北西");
|
|
|
+// }else if(fx>= 303.75 && fx< 326.25)
|
|
|
+// {
|
|
|
+// proEconMeteorological.setDeg("西北");
|
|
|
+// }else if(fx>= 326.25 && fx< 348.75)
|
|
|
+// {
|
|
|
+// proEconMeteorological.setDeg("西北北");
|
|
|
+// }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ 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());
|
|
|
+// String property = System.getProperty("user.dir");
|
|
|
+ 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));
|
|
|
}
|
|
|
}
|