1、天气统计历史天气及当天天气实现
This commit is contained in:
parent
42c1674a98
commit
7055572d5b
|
|
@ -5,6 +5,10 @@ import com.ruoyi.common.core.domain.R;
|
||||||
import com.xjs.business.api.factory.RemoteWeatherFactory;
|
import com.xjs.business.api.factory.RemoteWeatherFactory;
|
||||||
import org.springframework.cloud.openfeign.FeignClient;
|
import org.springframework.cloud.openfeign.FeignClient;
|
||||||
import org.springframework.web.bind.annotation.GetMapping;
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestParam;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* rpc远程调用其他服务天气接口
|
* rpc远程调用其他服务天气接口
|
||||||
|
|
@ -18,4 +22,8 @@ public interface RemoteWeatherFeign {
|
||||||
|
|
||||||
@GetMapping("/weather/getWeatherForRPC")
|
@GetMapping("/weather/getWeatherForRPC")
|
||||||
R getWeatherForRPC() ;
|
R getWeatherForRPC() ;
|
||||||
|
|
||||||
|
@GetMapping("/weather/getHistoryWeatherForRPC")
|
||||||
|
R<Map<String, List>> getHistoryWeatherForRPC(@RequestParam("startDate")String startDate,
|
||||||
|
@RequestParam("endDate")String endDate);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -7,8 +7,12 @@ import org.slf4j.LoggerFactory;
|
||||||
import org.springframework.cloud.openfeign.FallbackFactory;
|
import org.springframework.cloud.openfeign.FallbackFactory;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 内部调用天气服务降级
|
* 内部调用天气服务降级
|
||||||
|
*
|
||||||
* @author xiejs
|
* @author xiejs
|
||||||
* @since 2022-01-16
|
* @since 2022-01-16
|
||||||
*/
|
*/
|
||||||
|
|
@ -19,6 +23,17 @@ public class RemoteWeatherFactory implements FallbackFactory<RemoteWeatherFeign>
|
||||||
@Override
|
@Override
|
||||||
public RemoteWeatherFeign create(Throwable cause) {
|
public RemoteWeatherFeign create(Throwable cause) {
|
||||||
log.error("api模块天气服务调用失败:{}", cause.getMessage());
|
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<Map<String, List>> getHistoryWeatherForRPC(String startDate, String endDate) {
|
||||||
|
return R.fail("获取统计天气服务调用失败" + cause.getMessage());
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,10 @@
|
||||||
|
import request from '@/utils/request'
|
||||||
|
|
||||||
|
//查询天气历史记录统计
|
||||||
|
export function getHistoryWeather(params) {
|
||||||
|
return request({
|
||||||
|
url: '/statistics/weatherstatistics/history',
|
||||||
|
method: 'get',
|
||||||
|
params: params,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
@ -1,10 +1,176 @@
|
||||||
<template>
|
<template>
|
||||||
|
<div v-loading="loading"
|
||||||
|
element-loading-text="拼命加载中"
|
||||||
|
element-loading-spinner="el-icon-loading"
|
||||||
|
element-loading-background="rgba(0, 0, 0, 0.8)">
|
||||||
|
|
||||||
|
<el-form :model="historyWeatherParams" ref="historyWeatherParams"
|
||||||
|
:inline="true"
|
||||||
|
label-width="70px"
|
||||||
|
style="margin-left: 150px">
|
||||||
|
<el-form-item label="">
|
||||||
|
<el-date-picker
|
||||||
|
v-model="daterangeCreateTime"
|
||||||
|
size="small"
|
||||||
|
style="width: 320px"
|
||||||
|
value-format="yyyy-MM-dd"
|
||||||
|
format="yyyy 年 MM 月 dd 日"
|
||||||
|
type="daterange"
|
||||||
|
range-separator="-"
|
||||||
|
start-placeholder="开始日期"
|
||||||
|
end-placeholder="结束日期"
|
||||||
|
:picker-options="pickerOptions"
|
||||||
|
@change="handleQuery"
|
||||||
|
></el-date-picker>
|
||||||
|
</el-form-item>
|
||||||
|
|
||||||
|
<el-form-item>
|
||||||
|
<el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
|
||||||
|
</el-form-item>
|
||||||
|
</el-form>
|
||||||
|
<div ref="historyChart" style="height: 280px;width: 100%;"></div>
|
||||||
|
|
||||||
|
<div ref="futureChart" style="height: 400px;width: 100%"></div>
|
||||||
|
|
||||||
|
</div>
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script>
|
<script>
|
||||||
|
|
||||||
|
import {getHistoryWeather} from "@/api/business/statistics/weatherstatistics";
|
||||||
|
|
||||||
|
// 引入 ECharts 主模块
|
||||||
|
var echarts = require('echarts/lib/echarts');
|
||||||
|
// 引入柱状图
|
||||||
|
require('echarts/lib/chart/line');
|
||||||
|
// 引入提示框和标题组件
|
||||||
|
require('echarts/lib/component/tooltip');
|
||||||
|
require('echarts/lib/component/title');
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
name: "WeatherStatistics"
|
name: "WeatherStatistics",
|
||||||
|
data() {
|
||||||
|
return {
|
||||||
|
historyWeatherData: {},
|
||||||
|
|
||||||
|
historyWeatherParams: {
|
||||||
|
startDate: null,
|
||||||
|
endDate: null,
|
||||||
|
},
|
||||||
|
//检查查询范围
|
||||||
|
daterangeCreateTime: [],
|
||||||
|
|
||||||
|
//遮罩层
|
||||||
|
loading: false,
|
||||||
|
|
||||||
|
//日期组件
|
||||||
|
pickerOptions: {
|
||||||
|
shortcuts: [{
|
||||||
|
text: '昨天',
|
||||||
|
onClick(picker) {
|
||||||
|
const end = new Date();
|
||||||
|
const start = new Date();
|
||||||
|
start.setTime(start.getTime() - 3600 * 1000 * 24 );
|
||||||
|
picker.$emit('pick', [start, end]);
|
||||||
|
}
|
||||||
|
},{
|
||||||
|
text: '最近一周',
|
||||||
|
onClick(picker) {
|
||||||
|
const end = new Date();
|
||||||
|
const start = new Date();
|
||||||
|
start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
|
||||||
|
picker.$emit('pick', [start, end]);
|
||||||
|
}
|
||||||
|
}, {
|
||||||
|
text: '最近一个月',
|
||||||
|
onClick(picker) {
|
||||||
|
const end = new Date();
|
||||||
|
const start = new Date();
|
||||||
|
start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
|
||||||
|
picker.$emit('pick', [start, end]);
|
||||||
|
}
|
||||||
|
}, {
|
||||||
|
text: '最近三个月',
|
||||||
|
onClick(picker) {
|
||||||
|
const end = new Date();
|
||||||
|
const start = new Date();
|
||||||
|
start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
|
||||||
|
picker.$emit('pick', [start, end]);
|
||||||
|
}
|
||||||
|
}]
|
||||||
|
},
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
created() {
|
||||||
|
this.getHistoryWeather()
|
||||||
|
},
|
||||||
|
|
||||||
|
|
||||||
|
methods: {
|
||||||
|
initHistory() {
|
||||||
|
let historyChart = echarts.init(this.$refs.historyChart)
|
||||||
|
historyChart.setOption({
|
||||||
|
title: {
|
||||||
|
text: '天气(单位℃)',
|
||||||
|
textStyle: {
|
||||||
|
color: '#541264',
|
||||||
|
fontWeight: '1000',
|
||||||
|
align: 'center',
|
||||||
|
},
|
||||||
|
left: "center",
|
||||||
|
},
|
||||||
|
tooltip: {},
|
||||||
|
xAxis: {
|
||||||
|
type: 'category',
|
||||||
|
data: this.historyWeatherData.reportTime,
|
||||||
|
},
|
||||||
|
yAxis: {
|
||||||
|
type: 'value'
|
||||||
|
},
|
||||||
|
series: [
|
||||||
|
{
|
||||||
|
data: this.historyWeatherData.temperature,
|
||||||
|
type: 'line',
|
||||||
|
smooth: true,
|
||||||
|
}
|
||||||
|
]
|
||||||
|
})
|
||||||
|
|
||||||
|
},
|
||||||
|
|
||||||
|
|
||||||
|
//获取历史天气
|
||||||
|
getHistoryWeather() {
|
||||||
|
this.loading = true
|
||||||
|
if (null != this.daterangeCreateTime && '' != this.daterangeCreateTime) {
|
||||||
|
this.historyWeatherParams.startDate = this.daterangeCreateTime[0];
|
||||||
|
this.historyWeatherParams.endDate = this.daterangeCreateTime[1];
|
||||||
|
}
|
||||||
|
getHistoryWeather(this.historyWeatherParams).then(res => {
|
||||||
|
this.loading = false
|
||||||
|
this.historyWeatherData = res.data;
|
||||||
|
this.initHistory()
|
||||||
|
}).catch(err => {
|
||||||
|
})
|
||||||
|
this.loading = false
|
||||||
|
},
|
||||||
|
|
||||||
|
/** 搜索按钮操作 */
|
||||||
|
handleQuery() {
|
||||||
|
this.getHistoryWeather();
|
||||||
|
},
|
||||||
|
/** 重置按钮操作 */
|
||||||
|
resetQuery() {
|
||||||
|
this.daterangeCreateTime = [];
|
||||||
|
this.historyWeatherParams.startDate = null
|
||||||
|
this.historyWeatherParams.endDate = null
|
||||||
|
this.handleQuery();
|
||||||
|
},
|
||||||
|
|
||||||
|
|
||||||
|
},
|
||||||
|
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -15,9 +15,11 @@ import lombok.extern.log4j.Log4j2;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.web.bind.annotation.GetMapping;
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestParam;
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -56,12 +58,22 @@ public class WeatherController {
|
||||||
|
|
||||||
|
|
||||||
@GetMapping("getWeatherForRPC")
|
@GetMapping("getWeatherForRPC")
|
||||||
@ApiOperation("远程调用获取天气信息ForRPC")
|
@ApiOperation("远程调用获取天气信息")
|
||||||
public R getWeatherForRPC() {
|
public R getWeatherForRPC() {
|
||||||
NowWeather nowWeather = weatherService.save();
|
NowWeather nowWeather = weatherService.save();
|
||||||
return Objects.nonNull(nowWeather.getCity()) ? R.ok() : R.fail();
|
return Objects.nonNull(nowWeather.getCity()) ? R.ok() : R.fail();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@GetMapping("getHistoryWeatherForRPC")
|
||||||
|
@ApiOperation("远程调用获取历史天气信息")
|
||||||
|
public R<Map<String, List>> getHistoryWeatherForRPC(@RequestParam("startDate")String startDate,
|
||||||
|
@RequestParam("endDate")String endDate) {
|
||||||
|
Map<String, List> map = weatherService.getHistoryWeather(startDate,endDate);
|
||||||
|
return R.ok(map);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* week类型转换
|
* week类型转换
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,9 @@ package com.xjs.weather.service;
|
||||||
import com.xjs.weather.domain.ForecastWeather;
|
import com.xjs.weather.domain.ForecastWeather;
|
||||||
import com.xjs.weather.domain.NowWeather;
|
import com.xjs.weather.domain.NowWeather;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 天气服务
|
* 天气服务
|
||||||
* @author xiejs
|
* @author xiejs
|
||||||
|
|
@ -29,4 +32,11 @@ public interface WeatherService {
|
||||||
ForecastWeather cacheForecastWeather();
|
ForecastWeather cacheForecastWeather();
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取历史天气
|
||||||
|
* @param startDate 开始时间
|
||||||
|
* @param endDate 结束时间
|
||||||
|
* @return key: value:
|
||||||
|
*/
|
||||||
|
Map<String, List> getHistoryWeather(String startDate, String endDate);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -9,14 +9,13 @@ import com.xjs.weather.domain.NowWeather;
|
||||||
import com.xjs.weather.factory.WeatherFactory;
|
import com.xjs.weather.factory.WeatherFactory;
|
||||||
import com.xjs.weather.mapper.NowWeatherMapper;
|
import com.xjs.weather.mapper.NowWeatherMapper;
|
||||||
import com.xjs.weather.service.WeatherService;
|
import com.xjs.weather.service.WeatherService;
|
||||||
|
import org.apache.commons.lang3.StringUtils;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
|
||||||
import javax.annotation.Resource;
|
import javax.annotation.Resource;
|
||||||
import java.util.Date;
|
import java.util.*;
|
||||||
import java.util.Objects;
|
|
||||||
import java.util.Optional;
|
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
import static com.xjs.consts.RedisConst.*;
|
import static com.xjs.consts.RedisConst.*;
|
||||||
|
|
@ -78,6 +77,25 @@ public class WeatherServiceImpl implements WeatherService {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Map<String, List> getHistoryWeather(String startDate, String endDate) {
|
||||||
|
List<NowWeather> weatherList = nowWeatherMapper.selectList(new QueryWrapper<NowWeather>()
|
||||||
|
.between("create_time", startDate, endDate));
|
||||||
|
|
||||||
|
ArrayList<String> dateTime = new ArrayList<>();
|
||||||
|
ArrayList<String> temperature = new ArrayList<>();
|
||||||
|
weatherList.forEach(weather ->{
|
||||||
|
dateTime.add(DateUtil.format(weather.getReporttime(),"MM-dd HH"));
|
||||||
|
temperature.add(weather.getTemperature());
|
||||||
|
});
|
||||||
|
|
||||||
|
Map<String, List> 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);
|
String dateTime = DateUtil.formatDateTime(reporttime);
|
||||||
NowWeather selectOne = nowWeatherMapper.selectOne(new QueryWrapper<NowWeather>().eq("reporttime", dateTime));
|
NowWeather selectOne = nowWeatherMapper.selectOne(new QueryWrapper<NowWeather>().eq("reporttime", dateTime));
|
||||||
if (Objects.isNull(selectOne)) {
|
if (Objects.isNull(selectOne)) {
|
||||||
|
if(StringUtils.isNotBlank(nowWeather.getTemperature()))
|
||||||
nowWeatherMapper.insert(nowWeather);
|
nowWeatherMapper.insert(nowWeather);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,9 +1,19 @@
|
||||||
package com.xjs.controller;
|
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.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.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestParam;
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 天气统计controller
|
* 天气统计controller
|
||||||
* @author xiejs
|
* @author xiejs
|
||||||
|
|
@ -13,4 +23,17 @@ import org.springframework.web.bind.annotation.RestController;
|
||||||
@RequestMapping("weatherstatistics")
|
@RequestMapping("weatherstatistics")
|
||||||
@Api(tags = "业务模块-天气记录统计")
|
@Api(tags = "业务模块-天气记录统计")
|
||||||
public class WeatherStatisticsController {
|
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<String, List> map = weatherStatisticsService.historyWeather(startDate, endDate);
|
||||||
|
return AjaxResult.success(map);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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<String, List> historyWeather(String startDate, String endDate);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -18,6 +18,7 @@ import java.util.Map;
|
||||||
* @since 2022-01-25
|
* @since 2022-01-25
|
||||||
*/
|
*/
|
||||||
@Service
|
@Service
|
||||||
|
@SuppressWarnings("all")
|
||||||
public class ApiStatisticsServiceImpl implements ApiStatisticsService {
|
public class ApiStatisticsServiceImpl implements ApiStatisticsService {
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
|
|
|
||||||
|
|
@ -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<String, List> 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();
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue