1、天气统计未来天气统计实现

pull/254/head
xjs 4 years ago
parent 7055572d5b
commit 0d1664c88e

@ -26,4 +26,7 @@ public interface RemoteWeatherFeign {
@GetMapping("/weather/getHistoryWeatherForRPC") @GetMapping("/weather/getHistoryWeatherForRPC")
R<Map<String, List>> getHistoryWeatherForRPC(@RequestParam("startDate")String startDate, R<Map<String, List>> getHistoryWeatherForRPC(@RequestParam("startDate")String startDate,
@RequestParam("endDate")String endDate); @RequestParam("endDate")String endDate);
@GetMapping("/weather/getFutureWeatherForRPC")
R<Map<String, List<String>>> getFutureWeatherForRPC();
} }

@ -31,7 +31,12 @@ public class RemoteWeatherFactory implements FallbackFactory<RemoteWeatherFeign>
@Override @Override
public R<Map<String, List>> getHistoryWeatherForRPC(String startDate, String endDate) { 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, params: params,
}) })
} }
//查询未来天气统计
export function getFutureWeather() {
return request({
url: '/statistics/weatherstatistics/future',
method: 'get',
})
}

@ -112,9 +112,7 @@ export default {
//API //API
getStatisticsHistoryApi() { getStatisticsHistoryApi() {
this.loading=true
getStatisticsHistoryApi().then(res => { getStatisticsHistoryApi().then(res => {
this.loading=false
this.historyApiData = res.data this.historyApiData = res.data
this.initHistory() this.initHistory()
}) })
@ -122,9 +120,13 @@ export default {
//API //API
getStatisticsTodayApi() { getStatisticsTodayApi() {
this.loading=true
getStatisticsTodayApi().then(res => { getStatisticsTodayApi().then(res => {
this.loading=false
this.todayApiData = res.data this.todayApiData = res.data
this.initToday() this.initToday()
}).catch(err =>{
this.loading=false
}) })
}, },

@ -28,16 +28,16 @@
<el-button icon="el-icon-refresh" size="mini" @click="resetQuery"></el-button> <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"></el-button>
</el-form-item> </el-form-item>
</el-form> </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> </div>
</template> </template>
<script> <script>
import {getHistoryWeather} from "@/api/business/statistics/weatherstatistics"; import {getHistoryWeather, getFutureWeather} from "@/api/business/statistics/weatherstatistics";
// ECharts // ECharts
var echarts = require('echarts/lib/echarts'); var echarts = require('echarts/lib/echarts');
@ -52,6 +52,7 @@ export default {
data() { data() {
return { return {
historyWeatherData: {}, historyWeatherData: {},
futureWeatherData: {},
historyWeatherParams: { historyWeatherParams: {
startDate: null, startDate: null,
@ -70,10 +71,10 @@ export default {
onClick(picker) { onClick(picker) {
const end = new Date(); const end = new Date();
const start = 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]); picker.$emit('pick', [start, end]);
} }
},{ }, {
text: '最近一周', text: '最近一周',
onClick(picker) { onClick(picker) {
const end = new Date(); const end = new Date();
@ -104,6 +105,7 @@ export default {
created() { created() {
this.getHistoryWeather() this.getHistoryWeather()
this.getFutureWeather()
}, },
@ -126,7 +128,10 @@ export default {
data: this.historyWeatherData.reportTime, data: this.historyWeatherData.reportTime,
}, },
yAxis: { yAxis: {
type: 'value' type: 'value',
axisLabel: {
formatter: '{value} °C'
}
}, },
series: [ 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() { getHistoryWeather() {
this.loading = true
if (null != this.daterangeCreateTime && '' != this.daterangeCreateTime) { if (null != this.daterangeCreateTime && '' != this.daterangeCreateTime) {
this.historyWeatherParams.startDate = this.daterangeCreateTime[0]; this.historyWeatherParams.startDate = this.daterangeCreateTime[0];
this.historyWeatherParams.endDate = this.daterangeCreateTime[1]; this.historyWeatherParams.endDate = this.daterangeCreateTime[1];
} }
getHistoryWeather(this.historyWeatherParams).then(res => { getHistoryWeather(this.historyWeatherParams).then(res => {
this.loading = false
this.historyWeatherData = res.data; this.historyWeatherData = res.data;
this.initHistory() this.initHistory()
}).catch(err => {
}) })
this.loading = false
}, },
/** 搜索按钮操作 */ /** 搜索按钮操作 */
handleQuery() { handleQuery() {
this.getHistoryWeather(); this.getHistoryWeather();
@ -168,7 +268,6 @@ export default {
this.handleQuery(); this.handleQuery();
}, },
}, },
} }

@ -56,6 +56,7 @@ public class WeatherController {
return AjaxResult.success(forecastWeather); return AjaxResult.success(forecastWeather);
} }
//----------------------内部远程调用rpc----------------------------
@GetMapping("getWeatherForRPC") @GetMapping("getWeatherForRPC")
@ApiOperation("远程调用获取天气信息") @ApiOperation("远程调用获取天气信息")
@ -72,6 +73,13 @@ public class WeatherController {
return R.ok(map); 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: * @return key: value:
*/ */
Map<String, List> getHistoryWeather(String startDate, String endDate); Map<String, List> getHistoryWeather(String startDate, String endDate);
/**
*
* @return
*/
Map<String, List<String>> getFutureWeather();
} }

@ -1,13 +1,17 @@
package com.xjs.weather.service.impl; package com.xjs.weather.service.impl;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil; import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.redis.service.RedisService; import com.ruoyi.common.redis.service.RedisService;
import com.xjs.exception.ApiException;
import com.xjs.exception.BusinessException; import com.xjs.exception.BusinessException;
import com.xjs.weather.domain.ForecastWeather; import com.xjs.weather.domain.ForecastWeather;
import com.xjs.weather.domain.IPInfoVo;
import com.xjs.weather.domain.NowWeather; 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.IPService;
import com.xjs.weather.service.WeatherService; import com.xjs.weather.service.WeatherService;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
@ -37,6 +41,8 @@ public class WeatherServiceImpl implements WeatherService {
private NowWeatherMapper nowWeatherMapper; private NowWeatherMapper nowWeatherMapper;
@Autowired @Autowired
private RedisService redisService; private RedisService redisService;
@Autowired
private IPService ipService;
@Override @Override
@ -72,20 +78,29 @@ public class WeatherServiceImpl implements WeatherService {
if (Objects.nonNull(forecastWeather)) { if (Objects.nonNull(forecastWeather)) {
redisService.setCacheObject(FORECAST_WEATHER, forecastWeather, FORECAST_WHEATHER_EXPIRE, TimeUnit.MINUTES); redisService.setCacheObject(FORECAST_WEATHER, forecastWeather, FORECAST_WHEATHER_EXPIRE, TimeUnit.MINUTES);
return forecastWeather; return forecastWeather;
}else { } else {
throw new BusinessException("获取预报天气数据为空"); throw new BusinessException("获取预报天气数据为空");
} }
} }
@Override @Override
public Map<String, List> getHistoryWeather(String startDate, String endDate) { 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>() List<NowWeather> weatherList = nowWeatherMapper.selectList(new QueryWrapper<NowWeather>()
.likeRight(Objects.nonNull(ipApiData), "city", city)
.between("create_time", startDate, endDate)); .between("create_time", startDate, endDate));
ArrayList<String> dateTime = new ArrayList<>(); ArrayList<String> dateTime = new ArrayList<>();
ArrayList<String> temperature = new ArrayList<>(); ArrayList<String> temperature = new ArrayList<>();
weatherList.forEach(weather ->{ weatherList.forEach(weather -> {
dateTime.add(DateUtil.format(weather.getReporttime(),"MM-dd HH")); dateTime.add(DateUtil.format(weather.getReporttime(), "MM-dd HH"));
temperature.add(weather.getTemperature()); temperature.add(weather.getTemperature());
}); });
@ -96,6 +111,28 @@ public class WeatherServiceImpl implements WeatherService {
return listMap; 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) { private void checkExistSave(NowWeather nowWeather) {
Date reporttime = nowWeather.getReporttime(); Date reporttime = nowWeather.getReporttime();
String dateTime = DateUtil.formatDateTime(reporttime); String dateTime = DateUtil.formatDateTime(reporttime);
NowWeather selectOne = nowWeatherMapper.selectOne(new QueryWrapper<NowWeather>().eq("reporttime", dateTime)); List<NowWeather> nowWeatherList = nowWeatherMapper.selectList(new QueryWrapper<NowWeather>().eq("reporttime", dateTime));
if (Objects.isNull(selectOne)) { if (CollUtil.isEmpty(nowWeatherList)) {
if(StringUtils.isNotBlank(nowWeather.getTemperature())) if (StringUtils.isNotBlank(nowWeather.getTemperature()))
nowWeatherMapper.insert(nowWeather); nowWeatherMapper.insert(nowWeather);
} }
} }

@ -36,4 +36,12 @@ public class WeatherStatisticsController {
return AjaxResult.success(map); 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); 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 cn.hutool.core.date.DateUtil;
import com.xjs.business.api.RemoteWeatherFeign; import com.xjs.business.api.RemoteWeatherFeign;
import com.xjs.service.WeatherStatisticsService; import com.xjs.service.WeatherStatisticsService;
import com.xjs.utils.WeekUtils;
import org.apache.commons.lang3.StringUtils; 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 java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.stream.Collectors;
/** /**
* service * service
@ -30,4 +33,31 @@ public class WeatherStatisticsServiceImpl implements WeatherStatisticsService {
} }
return remoteWeatherFeign.getHistoryWeatherForRPC(startDate, endDate).getData(); 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…
Cancel
Save