1、天气统计未来天气统计实现
This commit is contained in:
parent
7055572d5b
commit
0d1664c88e
|
|
@ -26,4 +26,7 @@ public interface RemoteWeatherFeign {
|
|||
@GetMapping("/weather/getHistoryWeatherForRPC")
|
||||
R<Map<String, List>> getHistoryWeatherForRPC(@RequestParam("startDate")String startDate,
|
||||
@RequestParam("endDate")String endDate);
|
||||
|
||||
@GetMapping("/weather/getFutureWeatherForRPC")
|
||||
R<Map<String, List<String>>> getFutureWeatherForRPC();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -31,7 +31,12 @@ public class RemoteWeatherFactory implements FallbackFactory<RemoteWeatherFeign>
|
|||
|
||||
@Override
|
||||
public R<Map<String, List>> getHistoryWeatherForRPC(String startDate, String endDate) {
|
||||
return R.fail("获取统计天气服务调用失败" + cause.getMessage());
|
||||
return R.fail("获取统计历史天气服务调用失败" + cause.getMessage());
|
||||
}
|
||||
|
||||
@Override
|
||||
public R<Map<String, List<String>>> getFutureWeatherForRPC() {
|
||||
return R.fail("获取统计未来天气服务调用失败" + cause.getMessage());
|
||||
}
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -8,3 +8,11 @@ export function getHistoryWeather(params) {
|
|||
params: params,
|
||||
})
|
||||
}
|
||||
|
||||
//查询未来天气统计
|
||||
export function getFutureWeather() {
|
||||
return request({
|
||||
url: '/statistics/weatherstatistics/future',
|
||||
method: 'get',
|
||||
})
|
||||
}
|
||||
|
|
|
|||
|
|
@ -112,9 +112,7 @@ export default {
|
|||
|
||||
//查询API历史记录统计
|
||||
getStatisticsHistoryApi() {
|
||||
this.loading=true
|
||||
getStatisticsHistoryApi().then(res => {
|
||||
this.loading=false
|
||||
this.historyApiData = res.data
|
||||
this.initHistory()
|
||||
})
|
||||
|
|
@ -122,9 +120,13 @@ export default {
|
|||
|
||||
//查询API当天记录统计
|
||||
getStatisticsTodayApi() {
|
||||
this.loading=true
|
||||
getStatisticsTodayApi().then(res => {
|
||||
this.loading=false
|
||||
this.todayApiData = res.data
|
||||
this.initToday()
|
||||
}).catch(err =>{
|
||||
this.loading=false
|
||||
})
|
||||
},
|
||||
|
||||
|
|
|
|||
|
|
@ -28,16 +28,16 @@
|
|||
<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="historyChart" style="height:350px;width: 100%;"></div>
|
||||
|
||||
<div ref="futureChart" style="height: 400px;width: 100%"></div>
|
||||
<div ref="futureChart" style="height: 350px;width: 100%;margin-top: 20px"></div>
|
||||
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script>
|
||||
|
||||
import {getHistoryWeather} from "@/api/business/statistics/weatherstatistics";
|
||||
import {getHistoryWeather, getFutureWeather} from "@/api/business/statistics/weatherstatistics";
|
||||
|
||||
// 引入 ECharts 主模块
|
||||
var echarts = require('echarts/lib/echarts');
|
||||
|
|
@ -52,6 +52,7 @@ export default {
|
|||
data() {
|
||||
return {
|
||||
historyWeatherData: {},
|
||||
futureWeatherData: {},
|
||||
|
||||
historyWeatherParams: {
|
||||
startDate: null,
|
||||
|
|
@ -70,10 +71,10 @@ export default {
|
|||
onClick(picker) {
|
||||
const end = new Date();
|
||||
const start = new Date();
|
||||
start.setTime(start.getTime() - 3600 * 1000 * 24 );
|
||||
start.setTime(start.getTime() - 3600 * 1000 * 24);
|
||||
picker.$emit('pick', [start, end]);
|
||||
}
|
||||
},{
|
||||
}, {
|
||||
text: '最近一周',
|
||||
onClick(picker) {
|
||||
const end = new Date();
|
||||
|
|
@ -104,6 +105,7 @@ export default {
|
|||
|
||||
created() {
|
||||
this.getHistoryWeather()
|
||||
this.getFutureWeather()
|
||||
},
|
||||
|
||||
|
||||
|
|
@ -126,7 +128,10 @@ export default {
|
|||
data: this.historyWeatherData.reportTime,
|
||||
},
|
||||
yAxis: {
|
||||
type: 'value'
|
||||
type: 'value',
|
||||
axisLabel: {
|
||||
formatter: '{value} °C'
|
||||
}
|
||||
},
|
||||
series: [
|
||||
{
|
||||
|
|
@ -139,23 +144,118 @@ export default {
|
|||
|
||||
},
|
||||
|
||||
initFuture() {
|
||||
let futureChart = echarts.init(this.$refs.futureChart)
|
||||
futureChart.setOption({
|
||||
title: {
|
||||
text: '预报天气(单位℃)',
|
||||
textStyle: {
|
||||
color: '#541264',
|
||||
fontWeight: '1000',
|
||||
align: 'center',
|
||||
},
|
||||
left: "center",
|
||||
},
|
||||
tooltip: {
|
||||
trigger: 'axis'
|
||||
},
|
||||
legend: {},
|
||||
toolbox: {
|
||||
show: true,
|
||||
feature: {
|
||||
dataZoom: {
|
||||
yAxisIndex: 'none'
|
||||
},
|
||||
dataView: {readOnly: false},
|
||||
magicType: {type: ['line', 'bar']},
|
||||
restore: {},
|
||||
saveAsImage: {}
|
||||
}
|
||||
},
|
||||
xAxis: {
|
||||
type: 'category',
|
||||
boundaryGap: false,
|
||||
data: this.futureWeatherData.dateWeek
|
||||
},
|
||||
yAxis: {
|
||||
type: 'value',
|
||||
axisLabel: {
|
||||
formatter: '{value} °C'
|
||||
}
|
||||
},
|
||||
series: [
|
||||
{
|
||||
name: '最高温度',
|
||||
type: 'line',
|
||||
data: this.futureWeatherData.maxTemperature,
|
||||
markPoint: {
|
||||
data: [
|
||||
{type: 'max', name: 'Max'},
|
||||
{type: 'min', name: 'Min'}
|
||||
]
|
||||
},
|
||||
markLine: {
|
||||
data: [{type: 'average', name: 'Avg'}]
|
||||
}
|
||||
},
|
||||
{
|
||||
name: '最低温度',
|
||||
type: 'line',
|
||||
data: this.futureWeatherData.minTemperature,
|
||||
markPoint: {
|
||||
data: [{name: '周最低', value: -2, xAxis: 1, yAxis: -1.5}]
|
||||
},
|
||||
markLine: {
|
||||
data: [
|
||||
{type: 'average', name: 'Avg'},
|
||||
[
|
||||
{
|
||||
symbol: 'none',
|
||||
x: '90%',
|
||||
yAxis: 'max'
|
||||
},
|
||||
{
|
||||
symbol: 'circle',
|
||||
label: {
|
||||
position: 'start',
|
||||
formatter: 'Max'
|
||||
},
|
||||
type: 'max',
|
||||
name: '最高点'
|
||||
}
|
||||
]
|
||||
]
|
||||
}
|
||||
}
|
||||
]
|
||||
})
|
||||
},
|
||||
|
||||
//获取未来天气
|
||||
getFutureWeather() {
|
||||
this.loading = true
|
||||
getFutureWeather().then(res => {
|
||||
this.loading = false
|
||||
this.futureWeatherData = res.data
|
||||
this.initFuture()
|
||||
}).catch(err => {
|
||||
this.loading = false
|
||||
})
|
||||
},
|
||||
|
||||
//获取历史天气
|
||||
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();
|
||||
|
|
@ -168,7 +268,6 @@ export default {
|
|||
this.handleQuery();
|
||||
},
|
||||
|
||||
|
||||
},
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -56,6 +56,7 @@ public class WeatherController {
|
|||
return AjaxResult.success(forecastWeather);
|
||||
}
|
||||
|
||||
//----------------------内部远程调用rpc----------------------------
|
||||
|
||||
@GetMapping("getWeatherForRPC")
|
||||
@ApiOperation("远程调用获取天气信息")
|
||||
|
|
@ -72,6 +73,13 @@ public class WeatherController {
|
|||
return R.ok(map);
|
||||
}
|
||||
|
||||
@GetMapping("getFutureWeatherForRPC")
|
||||
@ApiOperation("远程调用获取未来天气信息")
|
||||
R<Map<String, List<String>>> getFutureWeatherForRPC() {
|
||||
Map<String, List<String>> map = weatherService.getFutureWeather();
|
||||
return R.ok(map);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -39,4 +39,10 @@ public interface WeatherService {
|
|||
* @return key: value:
|
||||
*/
|
||||
Map<String, List> getHistoryWeather(String startDate, String endDate);
|
||||
|
||||
/**
|
||||
* 获取未来天气
|
||||
* @return
|
||||
*/
|
||||
Map<String, List<String>> getFutureWeather();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,13 +1,17 @@
|
|||
package com.xjs.weather.service.impl;
|
||||
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import cn.hutool.core.date.DateUtil;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.ruoyi.common.redis.service.RedisService;
|
||||
import com.xjs.exception.ApiException;
|
||||
import com.xjs.exception.BusinessException;
|
||||
import com.xjs.weather.domain.ForecastWeather;
|
||||
import com.xjs.weather.domain.IPInfoVo;
|
||||
import com.xjs.weather.domain.NowWeather;
|
||||
import com.xjs.weather.factory.WeatherFactory;
|
||||
import com.xjs.weather.mapper.NowWeatherMapper;
|
||||
import com.xjs.weather.service.IPService;
|
||||
import com.xjs.weather.service.WeatherService;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
|
@ -37,6 +41,8 @@ public class WeatherServiceImpl implements WeatherService {
|
|||
private NowWeatherMapper nowWeatherMapper;
|
||||
@Autowired
|
||||
private RedisService redisService;
|
||||
@Autowired
|
||||
private IPService ipService;
|
||||
|
||||
|
||||
@Override
|
||||
|
|
@ -72,20 +78,29 @@ public class WeatherServiceImpl implements WeatherService {
|
|||
if (Objects.nonNull(forecastWeather)) {
|
||||
redisService.setCacheObject(FORECAST_WEATHER, forecastWeather, FORECAST_WHEATHER_EXPIRE, TimeUnit.MINUTES);
|
||||
return forecastWeather;
|
||||
}else {
|
||||
} else {
|
||||
throw new BusinessException("获取预报天气数据为空");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, List> getHistoryWeather(String startDate, String endDate) {
|
||||
IPInfoVo ipApiData = null;
|
||||
String city = "";
|
||||
try {
|
||||
ipApiData = ipService.getIPApiData();
|
||||
city = ipApiData.getCity();
|
||||
} catch (ApiException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
List<NowWeather> weatherList = nowWeatherMapper.selectList(new QueryWrapper<NowWeather>()
|
||||
.likeRight(Objects.nonNull(ipApiData), "city", city)
|
||||
.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"));
|
||||
weatherList.forEach(weather -> {
|
||||
dateTime.add(DateUtil.format(weather.getReporttime(), "MM-dd HH"));
|
||||
temperature.add(weather.getTemperature());
|
||||
});
|
||||
|
||||
|
|
@ -96,6 +111,28 @@ public class WeatherServiceImpl implements WeatherService {
|
|||
return listMap;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, List<String>> getFutureWeather() {
|
||||
ForecastWeather forecastWeather = this.cacheForecastWeather();
|
||||
ArrayList<String> date = new ArrayList<>();
|
||||
ArrayList<String> week = new ArrayList<>();
|
||||
ArrayList<String> minTemperature = new ArrayList<>();
|
||||
ArrayList<String> maxTemperature = new ArrayList<>();
|
||||
|
||||
forecastWeather.getCasts().forEach(casts -> {
|
||||
date.add(casts.getDate());
|
||||
week.add(casts.getWeek());
|
||||
minTemperature.add(casts.getNighttemp());
|
||||
maxTemperature.add(casts.getDaytemp());
|
||||
});
|
||||
Map<String, List<String>> listMap = new HashMap<>();
|
||||
listMap.put("date", date);
|
||||
listMap.put("week", week);
|
||||
listMap.put("minTemperature", minTemperature);
|
||||
listMap.put("maxTemperature", maxTemperature);
|
||||
return listMap;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 校验当前天气数据数据库是否存在
|
||||
|
|
@ -105,10 +142,10 @@ public class WeatherServiceImpl implements WeatherService {
|
|||
private void checkExistSave(NowWeather nowWeather) {
|
||||
Date reporttime = nowWeather.getReporttime();
|
||||
String dateTime = DateUtil.formatDateTime(reporttime);
|
||||
NowWeather selectOne = nowWeatherMapper.selectOne(new QueryWrapper<NowWeather>().eq("reporttime", dateTime));
|
||||
if (Objects.isNull(selectOne)) {
|
||||
if(StringUtils.isNotBlank(nowWeather.getTemperature()))
|
||||
nowWeatherMapper.insert(nowWeather);
|
||||
List<NowWeather> nowWeatherList = nowWeatherMapper.selectList(new QueryWrapper<NowWeather>().eq("reporttime", dateTime));
|
||||
if (CollUtil.isEmpty(nowWeatherList)) {
|
||||
if (StringUtils.isNotBlank(nowWeather.getTemperature()))
|
||||
nowWeatherMapper.insert(nowWeather);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -36,4 +36,12 @@ public class WeatherStatisticsController {
|
|||
return AjaxResult.success(map);
|
||||
}
|
||||
|
||||
@GetMapping("future")
|
||||
@ApiOperation("统计未来天气")
|
||||
@RequiresPermissions("statistics:weatherstatistics:list")
|
||||
public AjaxResult futureWeather() {
|
||||
Map<String, List<String>> map = weatherStatisticsService.futureWeather();
|
||||
return AjaxResult.success(map);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -18,4 +18,10 @@ public interface WeatherStatisticsService {
|
|||
*/
|
||||
Map<String, List> historyWeather(String startDate, String endDate);
|
||||
|
||||
/**
|
||||
* 获取未来天气
|
||||
* @return map
|
||||
*/
|
||||
Map<String, List<String>> futureWeather();
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,12 +3,15 @@ package com.xjs.service.impl;
|
|||
import cn.hutool.core.date.DateUtil;
|
||||
import com.xjs.business.api.RemoteWeatherFeign;
|
||||
import com.xjs.service.WeatherStatisticsService;
|
||||
import com.xjs.utils.WeekUtils;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* 天气统计service接口实现
|
||||
|
|
@ -30,4 +33,31 @@ public class WeatherStatisticsServiceImpl implements WeatherStatisticsService {
|
|||
}
|
||||
return remoteWeatherFeign.getHistoryWeatherForRPC(startDate, endDate).getData();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, List<String>> futureWeather() {
|
||||
Map<String, List<String>> map = remoteWeatherFeign.getFutureWeatherForRPC().getData();
|
||||
List<String> weekList = map.get("week");
|
||||
List<String> collect = weekList.stream().map(WeekUtils::weekConvert).collect(Collectors.toList());
|
||||
|
||||
//合并时间和星期
|
||||
ArrayList<String> dateWeek = new ArrayList<>();
|
||||
List<String> date = map.get("date");
|
||||
for (int i = 0; i < collect.size(); i++) {
|
||||
for (int j = 0; j < date.size(); j++) {
|
||||
String dates = DateUtil.parse(date.get(i)).toString("MM-dd");
|
||||
if (i == j) {
|
||||
String value = dates+"("+collect.get(j)+")";
|
||||
dateWeek.add(value);
|
||||
}
|
||||
}
|
||||
}
|
||||
map.put("dateWeek", dateWeek);
|
||||
|
||||
//移除不需要的属性
|
||||
map.remove("date");
|
||||
map.remove("week");
|
||||
|
||||
return map;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue