From 7055572d5b40adc98bcea240678fc1dc1721612b Mon Sep 17 00:00:00 2001 From: xjs <1294405880@qq.com> Date: Wed, 26 Jan 2022 11:32:58 +0800 Subject: [PATCH] =?UTF-8?q?1=E3=80=81=E5=A4=A9=E6=B0=94=E7=BB=9F=E8=AE=A1?= =?UTF-8?q?=E5=8E=86=E5=8F=B2=E5=A4=A9=E6=B0=94=E5=8F=8A=E5=BD=93=E5=A4=A9?= =?UTF-8?q?=E5=A4=A9=E6=B0=94=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../xjs/business/api/RemoteWeatherFeign.java | 8 + .../api/factory/RemoteWeatherFactory.java | 17 +- .../business/statistics/weatherstatistics.js | 10 ++ .../statistics/weatherstatistics/index.vue | 168 +++++++++++++++++- .../weather/controller/WeatherController.java | 14 +- .../xjs/weather/service/WeatherService.java | 10 ++ .../service/impl/WeatherServiceImpl.java | 25 ++- .../WeatherStatisticsController.java | 23 +++ .../xjs/service/WeatherStatisticsService.java | 21 +++ .../impl/ApiStatisticsServiceImpl.java | 1 + .../impl/WeatherStatisticsServiceImpl.java | 33 ++++ 11 files changed, 324 insertions(+), 6 deletions(-) create mode 100644 ruoyi-ui/src/api/business/statistics/weatherstatistics.js create mode 100644 xjs-business/xjs-business-statistics/src/main/java/com/xjs/service/WeatherStatisticsService.java create mode 100644 xjs-business/xjs-business-statistics/src/main/java/com/xjs/service/impl/WeatherStatisticsServiceImpl.java diff --git a/ruoyi-api/ruoyi-api-system/src/main/java/com/xjs/business/api/RemoteWeatherFeign.java b/ruoyi-api/ruoyi-api-system/src/main/java/com/xjs/business/api/RemoteWeatherFeign.java index 1bd28be7..ac0baa28 100644 --- a/ruoyi-api/ruoyi-api-system/src/main/java/com/xjs/business/api/RemoteWeatherFeign.java +++ b/ruoyi-api/ruoyi-api-system/src/main/java/com/xjs/business/api/RemoteWeatherFeign.java @@ -5,6 +5,10 @@ import com.ruoyi.common.core.domain.R; import com.xjs.business.api.factory.RemoteWeatherFactory; import org.springframework.cloud.openfeign.FeignClient; import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestParam; + +import java.util.List; +import java.util.Map; /** * rpc远程调用其他服务天气接口 @@ -18,4 +22,8 @@ public interface RemoteWeatherFeign { @GetMapping("/weather/getWeatherForRPC") R getWeatherForRPC() ; + + @GetMapping("/weather/getHistoryWeatherForRPC") + R> getHistoryWeatherForRPC(@RequestParam("startDate")String startDate, + @RequestParam("endDate")String endDate); } diff --git a/ruoyi-api/ruoyi-api-system/src/main/java/com/xjs/business/api/factory/RemoteWeatherFactory.java b/ruoyi-api/ruoyi-api-system/src/main/java/com/xjs/business/api/factory/RemoteWeatherFactory.java index ae7ec7aa..e3038175 100644 --- a/ruoyi-api/ruoyi-api-system/src/main/java/com/xjs/business/api/factory/RemoteWeatherFactory.java +++ b/ruoyi-api/ruoyi-api-system/src/main/java/com/xjs/business/api/factory/RemoteWeatherFactory.java @@ -7,8 +7,12 @@ import org.slf4j.LoggerFactory; import org.springframework.cloud.openfeign.FallbackFactory; import org.springframework.stereotype.Component; +import java.util.List; +import java.util.Map; + /** * 内部调用天气服务降级 + * * @author xiejs * @since 2022-01-16 */ @@ -19,6 +23,17 @@ public class RemoteWeatherFactory implements FallbackFactory @Override public RemoteWeatherFeign create(Throwable cause) { log.error("api模块天气服务调用失败:{}", cause.getMessage()); - return () -> R.fail("天气服务调用失败" + cause.getMessage()); + return new RemoteWeatherFeign() { + @Override + public R getWeatherForRPC() { + return R.fail("天气服务调用失败" + cause.getMessage()); + } + + @Override + public R> getHistoryWeatherForRPC(String startDate, String endDate) { + return R.fail("获取统计天气服务调用失败" + cause.getMessage()); + } + }; + } } diff --git a/ruoyi-ui/src/api/business/statistics/weatherstatistics.js b/ruoyi-ui/src/api/business/statistics/weatherstatistics.js new file mode 100644 index 00000000..562f166a --- /dev/null +++ b/ruoyi-ui/src/api/business/statistics/weatherstatistics.js @@ -0,0 +1,10 @@ +import request from '@/utils/request' + +//查询天气历史记录统计 +export function getHistoryWeather(params) { + return request({ + url: '/statistics/weatherstatistics/history', + method: 'get', + params: params, + }) +} diff --git a/ruoyi-ui/src/views/business/statistics/weatherstatistics/index.vue b/ruoyi-ui/src/views/business/statistics/weatherstatistics/index.vue index fe485824..eed8ec79 100644 --- a/ruoyi-ui/src/views/business/statistics/weatherstatistics/index.vue +++ b/ruoyi-ui/src/views/business/statistics/weatherstatistics/index.vue @@ -1,10 +1,176 @@ diff --git a/xjs-business/xjs-business-openapi/src/main/java/com/xjs/weather/controller/WeatherController.java b/xjs-business/xjs-business-openapi/src/main/java/com/xjs/weather/controller/WeatherController.java index 7a1b1d67..c46e79f6 100644 --- a/xjs-business/xjs-business-openapi/src/main/java/com/xjs/weather/controller/WeatherController.java +++ b/xjs-business/xjs-business-openapi/src/main/java/com/xjs/weather/controller/WeatherController.java @@ -15,9 +15,11 @@ import lombok.extern.log4j.Log4j2; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import java.util.List; +import java.util.Map; import java.util.Objects; /** @@ -56,12 +58,22 @@ public class WeatherController { @GetMapping("getWeatherForRPC") - @ApiOperation("远程调用获取天气信息ForRPC") + @ApiOperation("远程调用获取天气信息") public R getWeatherForRPC() { NowWeather nowWeather = weatherService.save(); return Objects.nonNull(nowWeather.getCity()) ? R.ok() : R.fail(); } + @GetMapping("getHistoryWeatherForRPC") + @ApiOperation("远程调用获取历史天气信息") + public R> getHistoryWeatherForRPC(@RequestParam("startDate")String startDate, + @RequestParam("endDate")String endDate) { + Map map = weatherService.getHistoryWeather(startDate,endDate); + return R.ok(map); + } + + + /** * week类型转换 diff --git a/xjs-business/xjs-business-openapi/src/main/java/com/xjs/weather/service/WeatherService.java b/xjs-business/xjs-business-openapi/src/main/java/com/xjs/weather/service/WeatherService.java index add607ab..63e1f368 100644 --- a/xjs-business/xjs-business-openapi/src/main/java/com/xjs/weather/service/WeatherService.java +++ b/xjs-business/xjs-business-openapi/src/main/java/com/xjs/weather/service/WeatherService.java @@ -3,6 +3,9 @@ package com.xjs.weather.service; import com.xjs.weather.domain.ForecastWeather; import com.xjs.weather.domain.NowWeather; +import java.util.List; +import java.util.Map; + /** * 天气服务 * @author xiejs @@ -29,4 +32,11 @@ public interface WeatherService { ForecastWeather cacheForecastWeather(); + /** + * 获取历史天气 + * @param startDate 开始时间 + * @param endDate 结束时间 + * @return key: value: + */ + Map getHistoryWeather(String startDate, String endDate); } diff --git a/xjs-business/xjs-business-openapi/src/main/java/com/xjs/weather/service/impl/WeatherServiceImpl.java b/xjs-business/xjs-business-openapi/src/main/java/com/xjs/weather/service/impl/WeatherServiceImpl.java index feaee4d5..99a1053f 100644 --- a/xjs-business/xjs-business-openapi/src/main/java/com/xjs/weather/service/impl/WeatherServiceImpl.java +++ b/xjs-business/xjs-business-openapi/src/main/java/com/xjs/weather/service/impl/WeatherServiceImpl.java @@ -9,14 +9,13 @@ import com.xjs.weather.domain.NowWeather; import com.xjs.weather.factory.WeatherFactory; import com.xjs.weather.mapper.NowWeatherMapper; import com.xjs.weather.service.WeatherService; +import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import javax.annotation.Resource; -import java.util.Date; -import java.util.Objects; -import java.util.Optional; +import java.util.*; import java.util.concurrent.TimeUnit; import static com.xjs.consts.RedisConst.*; @@ -78,6 +77,25 @@ public class WeatherServiceImpl implements WeatherService { } } + @Override + public Map getHistoryWeather(String startDate, String endDate) { + List weatherList = nowWeatherMapper.selectList(new QueryWrapper() + .between("create_time", startDate, endDate)); + + ArrayList dateTime = new ArrayList<>(); + ArrayList temperature = new ArrayList<>(); + weatherList.forEach(weather ->{ + dateTime.add(DateUtil.format(weather.getReporttime(),"MM-dd HH")); + temperature.add(weather.getTemperature()); + }); + + Map listMap = new HashMap<>(); + listMap.put("reportTime", dateTime); + listMap.put("temperature", temperature); + + return listMap; + } + /** * 校验当前天气数据数据库是否存在 @@ -89,6 +107,7 @@ public class WeatherServiceImpl implements WeatherService { String dateTime = DateUtil.formatDateTime(reporttime); NowWeather selectOne = nowWeatherMapper.selectOne(new QueryWrapper().eq("reporttime", dateTime)); if (Objects.isNull(selectOne)) { + if(StringUtils.isNotBlank(nowWeather.getTemperature())) nowWeatherMapper.insert(nowWeather); } } diff --git a/xjs-business/xjs-business-statistics/src/main/java/com/xjs/controller/WeatherStatisticsController.java b/xjs-business/xjs-business-statistics/src/main/java/com/xjs/controller/WeatherStatisticsController.java index 64ba0830..150dde41 100644 --- a/xjs-business/xjs-business-statistics/src/main/java/com/xjs/controller/WeatherStatisticsController.java +++ b/xjs-business/xjs-business-statistics/src/main/java/com/xjs/controller/WeatherStatisticsController.java @@ -1,9 +1,19 @@ package com.xjs.controller; +import com.ruoyi.common.core.web.domain.AjaxResult; +import com.ruoyi.common.security.annotation.RequiresPermissions; +import com.xjs.service.WeatherStatisticsService; import io.swagger.annotations.Api; +import io.swagger.annotations.ApiOperation; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; +import java.util.List; +import java.util.Map; + /** * 天气统计controller * @author xiejs @@ -13,4 +23,17 @@ import org.springframework.web.bind.annotation.RestController; @RequestMapping("weatherstatistics") @Api(tags = "业务模块-天气记录统计") public class WeatherStatisticsController { + + @Autowired + private WeatherStatisticsService weatherStatisticsService; + + @GetMapping("history") + @ApiOperation("统计历史天气") + @RequiresPermissions("statistics:weatherstatistics:list") + public AjaxResult historyWeather(@RequestParam(value = "startDate",required = false)String startDate, + @RequestParam(value="endDate",required = false)String endDate) { + Map map = weatherStatisticsService.historyWeather(startDate, endDate); + return AjaxResult.success(map); + } + } diff --git a/xjs-business/xjs-business-statistics/src/main/java/com/xjs/service/WeatherStatisticsService.java b/xjs-business/xjs-business-statistics/src/main/java/com/xjs/service/WeatherStatisticsService.java new file mode 100644 index 00000000..a9be8dfc --- /dev/null +++ b/xjs-business/xjs-business-statistics/src/main/java/com/xjs/service/WeatherStatisticsService.java @@ -0,0 +1,21 @@ +package com.xjs.service; + +import java.util.List; +import java.util.Map; + +/** + * 天气统计service接口 + * @author xiejs + * @since 2022-01-26 + */ +public interface WeatherStatisticsService { + + /** + * 获取历史天气 + * @param startDate 开始时间 + * @param endDate 结束时间 + * @return key: value: + */ + Map historyWeather(String startDate, String endDate); + +} diff --git a/xjs-business/xjs-business-statistics/src/main/java/com/xjs/service/impl/ApiStatisticsServiceImpl.java b/xjs-business/xjs-business-statistics/src/main/java/com/xjs/service/impl/ApiStatisticsServiceImpl.java index 2221c73d..fbd558cd 100644 --- a/xjs-business/xjs-business-statistics/src/main/java/com/xjs/service/impl/ApiStatisticsServiceImpl.java +++ b/xjs-business/xjs-business-statistics/src/main/java/com/xjs/service/impl/ApiStatisticsServiceImpl.java @@ -18,6 +18,7 @@ import java.util.Map; * @since 2022-01-25 */ @Service +@SuppressWarnings("all") public class ApiStatisticsServiceImpl implements ApiStatisticsService { @Autowired diff --git a/xjs-business/xjs-business-statistics/src/main/java/com/xjs/service/impl/WeatherStatisticsServiceImpl.java b/xjs-business/xjs-business-statistics/src/main/java/com/xjs/service/impl/WeatherStatisticsServiceImpl.java new file mode 100644 index 00000000..baa9b047 --- /dev/null +++ b/xjs-business/xjs-business-statistics/src/main/java/com/xjs/service/impl/WeatherStatisticsServiceImpl.java @@ -0,0 +1,33 @@ +package com.xjs.service.impl; + +import cn.hutool.core.date.DateUtil; +import com.xjs.business.api.RemoteWeatherFeign; +import com.xjs.service.WeatherStatisticsService; +import org.apache.commons.lang3.StringUtils; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.util.List; +import java.util.Map; + +/** + * 天气统计service接口实现 + * @author xiejs + * @since 2022-01-26 + */ +@Service +public class WeatherStatisticsServiceImpl implements WeatherStatisticsService { + + @Autowired + private RemoteWeatherFeign remoteWeatherFeign; + + + @Override + public Map historyWeather(String startDate, String endDate) { + if (StringUtils.isEmpty(startDate) || StringUtils.isEmpty(endDate)) { + startDate = DateUtil.today() + " 00:00:00"; + endDate = DateUtil.today() + " 23:59:59"; + } + return remoteWeatherFeign.getHistoryWeatherForRPC(startDate, endDate).getData(); + } +}