|
|
@ -25,10 +25,7 @@ import org.springframework.web.bind.annotation.*; |
|
|
|
|
|
|
|
import javax.annotation.Resource; |
|
|
|
import java.io.IOException; |
|
|
|
import java.util.ArrayList; |
|
|
|
import java.util.HashMap; |
|
|
|
import java.util.List; |
|
|
|
import java.util.Objects; |
|
|
|
import java.util.*; |
|
|
|
import java.util.concurrent.TimeUnit; |
|
|
|
import java.util.stream.Collectors; |
|
|
|
|
|
|
@ -224,11 +221,13 @@ public class WeatherForecastController extends BaseController { |
|
|
|
@PostMapping(value = "/currentWeatherAndForecastInformation") |
|
|
|
public AjaxResult currentWeatherAndForecastInformation(@RequestBody HashMap<String, Object> parameter) { |
|
|
|
|
|
|
|
//拿到获取到的桩号值并处理拆分
|
|
|
|
String stakeNum = String.valueOf(parameter.get("stakeNum")); |
|
|
|
String[] split = stakeNum.split("[|]"); |
|
|
|
|
|
|
|
StringBuilder numStake = new StringBuilder(); |
|
|
|
|
|
|
|
//循环执行查询周围最近的5的倍数的桩号(集团接口需求 桩号从K50+000开始 K55+000 K60+000……以此类推)
|
|
|
|
for (int i = UniversalEnum.ZERO.getNumber(); i < split.length; i++) { |
|
|
|
String[] split1 = split[i].split("[+]"); |
|
|
|
String k = split1[UniversalEnum.ZERO.getNumber()].replace(UniversalEnum.CAPITAL_K.getValue(), UniversalEnum.EMPTY_STRING.getValue()); |
|
|
@ -257,22 +256,28 @@ public class WeatherForecastController extends BaseController { |
|
|
|
if (response.body() != null) { |
|
|
|
|
|
|
|
JSONObject jsonResult = JSONObject.parseObject(response.body().string()); |
|
|
|
//判断请求是否成功 成功则判断data字段数组长度是否为0 否则获取redis中的缓存进行返回
|
|
|
|
if (jsonResult.getInteger("code") == UniversalEnum.TWO_HUNDRED.getNumber()) { |
|
|
|
JSONArray data = jsonResult.getJSONArray("data"); |
|
|
|
; |
|
|
|
JSONArray jsonArray = processingWeatherData(data); |
|
|
|
|
|
|
|
//判断返回data是否不为0 不为0则将数据进行返回 为0则获取redis中的缓存进行返回
|
|
|
|
if (jsonArray.size() != UniversalEnum.ZERO.getNumber()) { |
|
|
|
redisCache.deleteObject(UniversalEnum.CURRENT_WEATHER_AND_FORECAST_INFORMATION_SINGLE_WORD.getValue() + numStake); |
|
|
|
redisCache.setCacheObject(UniversalEnum.CURRENT_WEATHER_AND_FORECAST_INFORMATION_SINGLE_WORD.getValue() + numStake, jsonArray); |
|
|
|
return AjaxResult.success(jsonArray); |
|
|
|
} else { |
|
|
|
JSONArray cacheObject = redisCache.getCacheObject(UniversalEnum.CURRENT_WEATHER_AND_FORECAST_INFORMATION_SINGLE_WORD.getValue() + numStake); |
|
|
|
return AjaxResult.success(cacheObject); |
|
|
|
if (cacheObject != null) { |
|
|
|
return AjaxResult.success(cacheObject); |
|
|
|
} |
|
|
|
} |
|
|
|
} else { |
|
|
|
JSONArray cacheObject = redisCache.getCacheObject(UniversalEnum.CURRENT_WEATHER_AND_FORECAST_INFORMATION_SINGLE_WORD.getValue() + numStake); |
|
|
|
return AjaxResult.success(cacheObject); |
|
|
|
if (cacheObject != null) { |
|
|
|
return AjaxResult.success(cacheObject); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} catch (Exception ignored) { |
|
|
@ -280,8 +285,56 @@ public class WeatherForecastController extends BaseController { |
|
|
|
logger.error(String.valueOf(ignored)); |
|
|
|
|
|
|
|
} |
|
|
|
//如果出现错误直接返回redis缓存中的数据
|
|
|
|
JSONArray cacheObject = redisCache.getCacheObject(UniversalEnum.CURRENT_WEATHER_AND_FORECAST_INFORMATION_SINGLE_WORD.getValue() + numStake); |
|
|
|
return AjaxResult.success(cacheObject); |
|
|
|
//判断数据库中是否存在此桩号数据 存在直接返回 不存在则查询指定索引数据
|
|
|
|
if (cacheObject != null) { |
|
|
|
return AjaxResult.success(cacheObject); |
|
|
|
} else { |
|
|
|
//获取执行索引的数据
|
|
|
|
JSONArray cacheObject1 = redisCache.getCacheObject(UniversalEnum.CURRENT_WEATHER_AND_FORECAST_INFORMATION_DISTRICTS_AND_COUNTIES.getValue()); |
|
|
|
//获取桩号
|
|
|
|
String s = String.valueOf(numStake); |
|
|
|
//获取桩号指定字段
|
|
|
|
String replace = s.split(UniversalEnum.BARRE_PLUS.getValue())[UniversalEnum.ZERO.getNumber()].replace(UniversalEnum.CAPITAL_K.getValue(), UniversalEnum.EMPTY_STRING.getValue()); |
|
|
|
//将指定字段进行匹配相近的桩号指定字段
|
|
|
|
String s1 = queryCloseValue(Integer.parseInt(replace)); |
|
|
|
//指定字段替换为相近的桩号的指定字段 获取正确桩号
|
|
|
|
String replace1 = s.replace(replace, s1); |
|
|
|
//筛选 获取相近桩号的指定字段的数据
|
|
|
|
List<Object> collect = cacheObject1.stream().filter(item -> { |
|
|
|
JSONObject jsonObject = (JSONObject) JSON.toJSON(item); |
|
|
|
return Objects.equals(jsonObject.getString("stakeNum"), replace1); |
|
|
|
}).collect(Collectors.toList()); |
|
|
|
//JSONArray cacheObject2 = redisCache.getCacheObject(UniversalEnum.CURRENT_WEATHER_AND_FORECAST_INFORMATION_SINGLE_WORD.getValue() + replace1);
|
|
|
|
//返回筛选好的数据
|
|
|
|
return AjaxResult.success(collect); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
private static String queryCloseValue(int target) { |
|
|
|
int[] array = new int[UniversalEnum.EIGHT.getNumber()]; |
|
|
|
array[UniversalEnum.ZERO.getNumber()] = UniversalEnum.SIXTY.getNumber(); |
|
|
|
array[UniversalEnum.ONE.getNumber()] = UniversalEnum.ONE_HUNDRED_AND_FIVE.getNumber(); |
|
|
|
array[UniversalEnum.TWO.getNumber()] = UniversalEnum.ONE_HUNDRED_AND_FORTY_FIVE.getNumber(); |
|
|
|
array[UniversalEnum.THREE.getNumber()] = UniversalEnum.ONE_HUNDRED_AND_SIXTY_FIVE.getNumber(); |
|
|
|
array[UniversalEnum.FOUR.getNumber()] = UniversalEnum.ONE_HUNDRED_SEVENTY_FIVE.getNumber(); |
|
|
|
array[UniversalEnum.FIVE.getNumber()] = UniversalEnum.ONE_HUNDRED_AND_NINETY.getNumber(); |
|
|
|
array[UniversalEnum.SIX.getNumber()] = UniversalEnum.TWO_HUNDRED.getNumber(); |
|
|
|
array[UniversalEnum.SEVEN.getNumber()] = UniversalEnum.TWO_HUNDRED_AND_FIVE.getNumber(); |
|
|
|
//int target = 88;
|
|
|
|
int closest = array[UniversalEnum.ZERO.getNumber()]; |
|
|
|
int minDiff = Math.abs(array[UniversalEnum.ZERO.getNumber()] - target); |
|
|
|
|
|
|
|
for (int i = UniversalEnum.ONE.getNumber(); i < array.length; i++) { |
|
|
|
int diff = Math.abs(array[i] - target); |
|
|
|
if (diff < minDiff) { |
|
|
|
minDiff = diff; |
|
|
|
closest = array[i]; |
|
|
|
} |
|
|
|
} |
|
|
|
System.out.println(closest); |
|
|
|
return String.valueOf(closest); |
|
|
|
} |
|
|
|
|
|
|
|
public JSONArray processingWeatherData(JSONArray data) { |
|
|
@ -290,22 +343,27 @@ public class WeatherForecastController extends BaseController { |
|
|
|
|
|
|
|
JSONObject jsonObject = (JSONObject) JSON.toJSON(data.get(i)); |
|
|
|
String pre = jsonObject.getString("pre"); |
|
|
|
//判断降水量是否为0 为0判断湿度以及能见度 否则判断降水等级以及降水类型
|
|
|
|
if (Objects.equals(pre, UniversalEnum.ZERO.getValue()) || Double.parseDouble(pre) == UniversalEnum.ZERO.getNumber()) { |
|
|
|
|
|
|
|
String visLevel = jsonObject.getString("visLevel"); |
|
|
|
|
|
|
|
Double rhu = jsonObject.getDouble("rhu"); |
|
|
|
|
|
|
|
//判断湿度是否大于90 如果大于90咋返回阴天 否则判断能见度等级
|
|
|
|
if (rhu > UniversalEnum.NINETY.getNumber()) { |
|
|
|
jsonObject.put("weatherCondition", UniversalEnum.OVERCAST_SKY.getValue()); |
|
|
|
jsonObject.put("weatherConditionCode", UniversalEnum.OVERCAST_SKY.getNumber()); |
|
|
|
} else { |
|
|
|
//判断能见度等级为0则返回晴天
|
|
|
|
if (Objects.equals(visLevel, UniversalEnum.ONE.getValue())) { |
|
|
|
jsonObject.put("weatherCondition", UniversalEnum.CLEAR_WEATHER.getValue()); |
|
|
|
jsonObject.put("weatherConditionCode", UniversalEnum.CLEAR_WEATHER.getNumber()); |
|
|
|
// 判断能见度等级为2、3则返回多云
|
|
|
|
} else if (Objects.equals(visLevel, UniversalEnum.TWO.getValue()) || Objects.equals(visLevel, UniversalEnum.THREE.getValue())) { |
|
|
|
jsonObject.put("weatherCondition", UniversalEnum.CLOUDY.getValue()); |
|
|
|
jsonObject.put("weatherConditionCode", UniversalEnum.CLOUDY.getNumber()); |
|
|
|
// 判断能见度等级为4、5、6则返回阴天
|
|
|
|
} else if (Objects.equals(visLevel, UniversalEnum.FOUR.getValue()) || Objects.equals(visLevel, UniversalEnum.FIVE.getValue()) || Objects.equals(visLevel, UniversalEnum.SIX.getValue())) { |
|
|
|
jsonObject.put("weatherCondition", UniversalEnum.OVERCAST_SKY.getValue()); |
|
|
|
jsonObject.put("weatherConditionCode", UniversalEnum.OVERCAST_SKY.getNumber()); |
|
|
@ -316,6 +374,7 @@ public class WeatherForecastController extends BaseController { |
|
|
|
String preLevel = jsonObject.getString("preLevel"); |
|
|
|
String pph = jsonObject.getString("pph"); |
|
|
|
|
|
|
|
//判断降水等级
|
|
|
|
switch (preLevel) { |
|
|
|
case "0": |
|
|
|
jsonObject.put("weatherCondition", UniversalEnum.CLEAR_WEATHER.getValue()); |
|
|
@ -323,6 +382,7 @@ public class WeatherForecastController extends BaseController { |
|
|
|
break; |
|
|
|
case "1": |
|
|
|
case "2": |
|
|
|
//判断降水类型
|
|
|
|
if (Objects.equals(pph, UniversalEnum.ONE.getValue())) { |
|
|
|
jsonObject.put("weatherCondition", UniversalEnum.SPIT.getValue()); |
|
|
|
jsonObject.put("weatherConditionCode", UniversalEnum.SPIT.getNumber()); |
|
|
@ -341,6 +401,7 @@ public class WeatherForecastController extends BaseController { |
|
|
|
} |
|
|
|
break; |
|
|
|
case "3": |
|
|
|
//判断降水类型
|
|
|
|
if (Objects.equals(pph, UniversalEnum.ONE.getValue())) { |
|
|
|
jsonObject.put("weatherCondition", UniversalEnum.MODERATE_RAIN.getValue()); |
|
|
|
jsonObject.put("weatherConditionCode", UniversalEnum.MODERATE_RAIN.getNumber()); |
|
|
@ -359,6 +420,7 @@ public class WeatherForecastController extends BaseController { |
|
|
|
} |
|
|
|
break; |
|
|
|
case "4": |
|
|
|
//判断降水类型
|
|
|
|
if (Objects.equals(pph, UniversalEnum.ONE.getValue())) { |
|
|
|
jsonObject.put("weatherCondition", UniversalEnum.HEAVY_RAIN.getValue()); |
|
|
|
jsonObject.put("weatherConditionCode", UniversalEnum.HEAVY_RAIN.getNumber()); |
|
|
@ -377,6 +439,7 @@ public class WeatherForecastController extends BaseController { |
|
|
|
} |
|
|
|
break; |
|
|
|
case "5": |
|
|
|
//判断降水类型
|
|
|
|
if (Objects.equals(pph, UniversalEnum.ONE.getValue())) { |
|
|
|
jsonObject.put("weatherCondition", UniversalEnum.RAINSTORM.getValue()); |
|
|
|
jsonObject.put("weatherConditionCode", UniversalEnum.RAINSTORM.getNumber()); |
|
|
@ -395,6 +458,7 @@ public class WeatherForecastController extends BaseController { |
|
|
|
} |
|
|
|
break; |
|
|
|
case "6": |
|
|
|
//判断降水类型
|
|
|
|
if (Objects.equals(pph, UniversalEnum.ONE.getValue())) { |
|
|
|
jsonObject.put("weatherCondition", UniversalEnum.BIG_RAINSTORM.getValue()); |
|
|
|
jsonObject.put("weatherConditionCode", UniversalEnum.BIG_RAINSTORM.getNumber()); |
|
|
@ -413,6 +477,7 @@ public class WeatherForecastController extends BaseController { |
|
|
|
} |
|
|
|
break; |
|
|
|
case "7": |
|
|
|
//判断降水类型
|
|
|
|
if (Objects.equals(pph, UniversalEnum.ONE.getValue())) { |
|
|
|
jsonObject.put("weatherCondition", UniversalEnum.EXTREMELY_HEAVY_RAINSTORM.getValue()); |
|
|
|
jsonObject.put("weatherConditionCode", UniversalEnum.EXTREMELY_HEAVY_RAINSTORM.getNumber()); |
|
|
@ -434,6 +499,7 @@ public class WeatherForecastController extends BaseController { |
|
|
|
|
|
|
|
} |
|
|
|
JSONArray forecastList = jsonObject.getJSONArray("forecastList"); |
|
|
|
//判断是否存在预报内容 是则递归执行本方法 否则直接将整理好数据加入返回数组中
|
|
|
|
if (forecastList != null && forecastList.size() != UniversalEnum.ZERO.getNumber()) { |
|
|
|
|
|
|
|
jsonObject.put("forecastList", processingWeatherData(forecastList)); |
|
|
|