|
@@ -10,22 +10,13 @@
|
|
|
*/
|
|
|
package com.ruoyi.ems.service.impl;
|
|
|
|
|
|
-import com.alibaba.fastjson2.JSON;
|
|
|
-import com.alibaba.fastjson2.JSONArray;
|
|
|
-import com.alibaba.fastjson2.JSONObject;
|
|
|
import com.huashe.common.domain.model.WeatherForecast;
|
|
|
import com.huashe.common.domain.model.WeatherRt;
|
|
|
-import com.huashe.common.exception.Assert;
|
|
|
-import com.huashe.common.utils.HttpUtils;
|
|
|
import com.ruoyi.ems.mapper.AdmWeatherForecastMapper;
|
|
|
import com.ruoyi.ems.mapper.AdmWeatherRtMapper;
|
|
|
import com.ruoyi.ems.service.IWeatherService;
|
|
|
-import org.apache.commons.collections4.CollectionUtils;
|
|
|
-import org.apache.commons.lang3.StringUtils;
|
|
|
-import org.apache.http.client.utils.URIBuilder;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
-import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
@@ -50,21 +41,17 @@ public class WeatherServiceImpl implements IWeatherService {
|
|
|
@Resource
|
|
|
private AdmWeatherForecastMapper weatherForecastMapper;
|
|
|
|
|
|
- @Value("${weather.api-key}")
|
|
|
- private String apiKey;
|
|
|
-
|
|
|
- @Value("${weather.rt.api-addr}")
|
|
|
- private String weatherRtAddr;
|
|
|
-
|
|
|
- @Value("${weather.forecast.api-addr}")
|
|
|
- private String weatherForecastAddr;
|
|
|
-
|
|
|
@Override
|
|
|
public WeatherRt getWeatherRt(String adcode) {
|
|
|
return weatherRtMapper.getNewByAdcode(adcode);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
+ public void mergeRt(WeatherRt weatherRt) {
|
|
|
+ weatherRtMapper.merge(weatherRt);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
public List<WeatherRt> getWeatherHis(String adcode, String startTime, String endTime) {
|
|
|
return weatherRtMapper.getByTime(adcode, startTime, endTime);
|
|
|
}
|
|
@@ -75,57 +62,12 @@ public class WeatherServiceImpl implements IWeatherService {
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public void collectRt(String adcode) {
|
|
|
- try {
|
|
|
- URIBuilder uriBuilder = new URIBuilder(weatherRtAddr);
|
|
|
- uriBuilder.addParameter("adcode", adcode).addParameter("apiKey", apiKey);
|
|
|
- String res = HttpUtils.doGet(uriBuilder);
|
|
|
- log.debug("get res:\r\n{}", res);
|
|
|
-
|
|
|
- JSONObject jsonObject = JSON.parseObject(res);
|
|
|
-
|
|
|
- String code = jsonObject.getString("code");
|
|
|
- Assert.isTrue(StringUtils.equals(code, "0"), -1, jsonObject.getString("message"));
|
|
|
-
|
|
|
- String data = jsonObject.getString("data");
|
|
|
-
|
|
|
- if (data != null) {
|
|
|
- WeatherRt weatherRt = JSONObject.parseObject(data, WeatherRt.class);
|
|
|
- weatherRtMapper.merge(weatherRt);
|
|
|
- }
|
|
|
- }
|
|
|
- catch (Exception e) {
|
|
|
- log.error("collectRt fail!", e);
|
|
|
- }
|
|
|
+ public int deleteForecastByAdcode(String adcode) {
|
|
|
+ return weatherForecastMapper.deleteByAdcode(adcode);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public void collectForecast(String adcode) {
|
|
|
- try {
|
|
|
- URIBuilder uriBuilder = new URIBuilder(weatherForecastAddr);
|
|
|
- uriBuilder.addParameter("adcode", adcode).addParameter("apiKey", apiKey);
|
|
|
- String res = HttpUtils.doGet(uriBuilder);
|
|
|
- log.debug("get res:\r\n{}", res);
|
|
|
-
|
|
|
- JSONObject jsonObject = JSON.parseObject(res);
|
|
|
-
|
|
|
- String code = jsonObject.getString("code");
|
|
|
- Assert.isTrue(StringUtils.equals(code, "0"), -1, jsonObject.getString("message"));
|
|
|
-
|
|
|
- JSONArray data = jsonObject.getJSONArray("data");
|
|
|
- Assert.notNull(data, -1, "weather list is null.");
|
|
|
-
|
|
|
- if (!data.isEmpty()) {
|
|
|
- List<WeatherForecast> forecastList = JSON.parseArray(data.toString(), WeatherForecast.class);
|
|
|
-
|
|
|
- if (CollectionUtils.isNotEmpty(forecastList)) {
|
|
|
- weatherForecastMapper.deleteByAdcode(adcode);
|
|
|
- weatherForecastMapper.insertBatch(forecastList);
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- catch (Exception e) {
|
|
|
- log.error("collectForecast fail!", e);
|
|
|
- }
|
|
|
+ public void insertForecastBacth(List<WeatherForecast> forecasts) {
|
|
|
+ weatherForecastMapper.insertBatch(forecasts);
|
|
|
}
|
|
|
}
|