Browse Source

优化天气服务 天气情况判断逻辑

develop
zhaoxianglong 9 months ago
parent
commit
e21e6ff297
  1. 313
      zc-business/src/main/java/com/zc/business/controller/WeatherForecastController.java

313
zc-business/src/main/java/com/zc/business/controller/WeatherForecastController.java

@ -258,157 +258,9 @@ public class WeatherForecastController extends BaseController {
JSONObject jsonResult = JSONObject.parseObject(response.body().string());
if (jsonResult.getInteger("code") == UniversalEnum.TWO_HUNDRED.getNumber()) {
JSONArray data = jsonResult.getJSONArray("data");
JSONArray jsonArray = new JSONArray();
for (int i = UniversalEnum.ZERO.getNumber(); i < data.size(); i++) {
JSONObject jsonObject = (JSONObject) JSON.toJSON(data.get(i));
String pre = jsonObject.getString("pre");
if (Objects.equals(pre, UniversalEnum.ZERO.getValue())||Double.parseDouble(pre)==UniversalEnum.ZERO.getNumber()) {
String visLevel = jsonObject.getString("visLevel");
Double rhu = jsonObject.getDouble("rhu");
if (rhu > UniversalEnum.SIXTY.getNumber()) {
jsonObject.put("weatherCondition", UniversalEnum.OVERCAST_SKY.getValue());
jsonObject.put("weatherConditionCode", UniversalEnum.OVERCAST_SKY.getNumber());
} else {
if (Objects.equals(visLevel, UniversalEnum.ONE.getValue())) {
jsonObject.put("weatherCondition", UniversalEnum.CLEAR_WEATHER.getValue());
jsonObject.put("weatherConditionCode", UniversalEnum.CLEAR_WEATHER.getNumber());
} 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());
} 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());
}
}
} else {
String preLevel = jsonObject.getString("preLevel");
String pph = jsonObject.getString("pph");
switch (preLevel) {
case "0":
jsonObject.put("weatherCondition", UniversalEnum.CLEAR_WEATHER.getValue());
jsonObject.put("weatherConditionCode", UniversalEnum.CLEAR_WEATHER.getNumber());
break;
case "1":
case "2":
if (Objects.equals(pph, UniversalEnum.ONE.getValue())) {
jsonObject.put("weatherCondition", UniversalEnum.SPIT.getValue());
jsonObject.put("weatherConditionCode", UniversalEnum.SPIT.getNumber());
} else if (Objects.equals(pph, UniversalEnum.TWO.getValue())) {
jsonObject.put("weatherCondition", UniversalEnum.SLEET.getValue());
jsonObject.put("weatherConditionCode", UniversalEnum.SLEET.getNumber());
} else if (Objects.equals(pph, UniversalEnum.THREE.getValue())) {
jsonObject.put("weatherCondition", UniversalEnum.LIGHT_SNOW.getValue());
jsonObject.put("weatherConditionCode", UniversalEnum.LIGHT_SNOW.getNumber());
} else if (Objects.equals(pph, UniversalEnum.FOUR.getValue())) {
jsonObject.put("weatherCondition", UniversalEnum.FREEZING_RAIN.getValue());
jsonObject.put("weatherConditionCode", UniversalEnum.FREEZING_RAIN.getNumber());
} else if (Objects.equals(pph, UniversalEnum.ZERO.getValue())) {
jsonObject.put("weatherCondition", UniversalEnum.CLEAR_WEATHER.getValue());
jsonObject.put("weatherConditionCode", UniversalEnum.CLEAR_WEATHER.getNumber());
}
break;
case "3":
if (Objects.equals(pph, UniversalEnum.ONE.getValue())) {
jsonObject.put("weatherCondition", UniversalEnum.MODERATE_RAIN.getValue());
jsonObject.put("weatherConditionCode", UniversalEnum.MODERATE_RAIN.getNumber());
} else if (Objects.equals(pph, UniversalEnum.TWO.getValue())) {
jsonObject.put("weatherCondition", UniversalEnum.SLEET.getValue());
jsonObject.put("weatherConditionCode", UniversalEnum.SLEET.getNumber());
} else if (Objects.equals(pph, UniversalEnum.THREE.getValue())) {
jsonObject.put("weatherCondition", UniversalEnum.MODERATE_SNOW.getValue());
jsonObject.put("weatherConditionCode", UniversalEnum.MODERATE_SNOW.getNumber());
} else if (Objects.equals(pph, UniversalEnum.FOUR.getValue())) {
jsonObject.put("weatherCondition", UniversalEnum.FREEZING_RAIN.getValue());
jsonObject.put("weatherConditionCode", UniversalEnum.FREEZING_RAIN.getNumber());
} else if (Objects.equals(pph, UniversalEnum.ZERO.getValue())) {
jsonObject.put("weatherCondition", UniversalEnum.CLEAR_WEATHER.getValue());
jsonObject.put("weatherConditionCode", UniversalEnum.CLEAR_WEATHER.getNumber());
}
break;
case "4":
if (Objects.equals(pph, UniversalEnum.ONE.getValue())) {
jsonObject.put("weatherCondition", UniversalEnum.HEAVY_RAIN.getValue());
jsonObject.put("weatherConditionCode", UniversalEnum.HEAVY_RAIN.getNumber());
} else if (Objects.equals(pph, UniversalEnum.TWO.getValue())) {
jsonObject.put("weatherCondition", UniversalEnum.SLEET.getValue());
jsonObject.put("weatherConditionCode", UniversalEnum.SLEET.getNumber());
} else if (Objects.equals(pph, UniversalEnum.THREE.getValue())) {
jsonObject.put("weatherCondition", UniversalEnum.HEAVY_SNOW.getValue());
jsonObject.put("weatherConditionCode", UniversalEnum.HEAVY_SNOW.getNumber());
} else if (Objects.equals(pph, UniversalEnum.FOUR.getValue())) {
jsonObject.put("weatherCondition", UniversalEnum.FREEZING_RAIN.getValue());
jsonObject.put("weatherConditionCode", UniversalEnum.FREEZING_RAIN.getNumber());
} else if (Objects.equals(pph, UniversalEnum.ZERO.getValue())) {
jsonObject.put("weatherCondition", UniversalEnum.CLEAR_WEATHER.getValue());
jsonObject.put("weatherConditionCode", UniversalEnum.CLEAR_WEATHER.getNumber());
}
break;
case "5":
if (Objects.equals(pph, UniversalEnum.ONE.getValue())) {
jsonObject.put("weatherCondition", UniversalEnum.RAINSTORM.getValue());
jsonObject.put("weatherConditionCode", UniversalEnum.RAINSTORM.getNumber());
} else if (Objects.equals(pph, UniversalEnum.TWO.getValue())) {
jsonObject.put("weatherCondition", UniversalEnum.SLEET.getValue());
jsonObject.put("weatherConditionCode", UniversalEnum.SLEET.getNumber());
} else if (Objects.equals(pph, UniversalEnum.THREE.getValue())) {
jsonObject.put("weatherCondition", UniversalEnum.BLIZZARD.getValue());
jsonObject.put("weatherConditionCode", UniversalEnum.BLIZZARD.getNumber());
} else if (Objects.equals(pph, UniversalEnum.FOUR.getValue())) {
jsonObject.put("weatherCondition", UniversalEnum.FREEZING_RAIN.getValue());
jsonObject.put("weatherConditionCode", UniversalEnum.FREEZING_RAIN.getNumber());
} else if (Objects.equals(pph, UniversalEnum.ZERO.getValue())) {
jsonObject.put("weatherCondition", UniversalEnum.CLEAR_WEATHER.getValue());
jsonObject.put("weatherConditionCode", UniversalEnum.CLEAR_WEATHER.getNumber());
}
break;
case "6":
if (Objects.equals(pph, UniversalEnum.ONE.getValue())) {
jsonObject.put("weatherCondition", UniversalEnum.BIG_RAINSTORM.getValue());
jsonObject.put("weatherConditionCode", UniversalEnum.BIG_RAINSTORM.getNumber());
} else if (Objects.equals(pph, UniversalEnum.TWO.getValue())) {
jsonObject.put("weatherCondition", UniversalEnum.SLEET.getValue());
jsonObject.put("weatherConditionCode", UniversalEnum.SLEET.getNumber());
} else if (Objects.equals(pph, UniversalEnum.THREE.getValue())) {
jsonObject.put("weatherCondition", UniversalEnum.BIG_BLIZZARD.getValue());
jsonObject.put("weatherConditionCode", UniversalEnum.BIG_BLIZZARD.getNumber());
} else if (Objects.equals(pph, UniversalEnum.FOUR.getValue())) {
jsonObject.put("weatherCondition", UniversalEnum.FREEZING_RAIN.getValue());
jsonObject.put("weatherConditionCode", UniversalEnum.FREEZING_RAIN.getNumber());
} else if (Objects.equals(pph, UniversalEnum.ZERO.getValue())) {
jsonObject.put("weatherCondition", UniversalEnum.CLEAR_WEATHER.getValue());
jsonObject.put("weatherConditionCode", UniversalEnum.CLEAR_WEATHER.getNumber());
}
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());
} else if (Objects.equals(pph, UniversalEnum.TWO.getValue())) {
jsonObject.put("weatherCondition", UniversalEnum.SLEET.getValue());
jsonObject.put("weatherConditionCode", UniversalEnum.SLEET.getNumber());
} else if (Objects.equals(pph, UniversalEnum.THREE.getValue())) {
jsonObject.put("weatherCondition", UniversalEnum.HEAVY_SNOWSTORM.getValue());
jsonObject.put("weatherConditionCode", UniversalEnum.HEAVY_SNOWSTORM.getNumber());
} else if (Objects.equals(pph, UniversalEnum.FOUR.getValue())) {
jsonObject.put("weatherCondition", UniversalEnum.FREEZING_RAIN.getValue());
jsonObject.put("weatherConditionCode", UniversalEnum.FREEZING_RAIN.getNumber());
} else if (Objects.equals(pph, UniversalEnum.ZERO.getValue())) {
jsonObject.put("weatherCondition", UniversalEnum.CLEAR_WEATHER.getValue());
jsonObject.put("weatherConditionCode", UniversalEnum.CLEAR_WEATHER.getNumber());
}
break;
}
}
jsonArray.add(i, jsonObject);
}
;
JSONArray jsonArray = processingWeatherData(data);
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);
@ -429,6 +281,167 @@ public class WeatherForecastController extends BaseController {
return AjaxResult.error(UniversalEnum.REQUEST_FAILED.getValue());
}
public JSONArray processingWeatherData(JSONArray data) {
JSONArray jsonArray = new JSONArray();
for (int i = UniversalEnum.ZERO.getNumber(); i < data.size(); i++) {
JSONObject jsonObject = (JSONObject) JSON.toJSON(data.get(i));
String pre = jsonObject.getString("pre");
if (Objects.equals(pre, UniversalEnum.ZERO.getValue()) || Double.parseDouble(pre) == UniversalEnum.ZERO.getNumber()) {
String visLevel = jsonObject.getString("visLevel");
Double rhu = jsonObject.getDouble("rhu");
if (rhu > UniversalEnum.SIXTY.getNumber()) {
jsonObject.put("weatherCondition", UniversalEnum.OVERCAST_SKY.getValue());
jsonObject.put("weatherConditionCode", UniversalEnum.OVERCAST_SKY.getNumber());
} else {
if (Objects.equals(visLevel, UniversalEnum.ONE.getValue())) {
jsonObject.put("weatherCondition", UniversalEnum.CLEAR_WEATHER.getValue());
jsonObject.put("weatherConditionCode", UniversalEnum.CLEAR_WEATHER.getNumber());
} 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());
} 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());
}
}
} else {
String preLevel = jsonObject.getString("preLevel");
String pph = jsonObject.getString("pph");
switch (preLevel) {
case "0":
jsonObject.put("weatherCondition", UniversalEnum.CLEAR_WEATHER.getValue());
jsonObject.put("weatherConditionCode", UniversalEnum.CLEAR_WEATHER.getNumber());
break;
case "1":
case "2":
if (Objects.equals(pph, UniversalEnum.ONE.getValue())) {
jsonObject.put("weatherCondition", UniversalEnum.SPIT.getValue());
jsonObject.put("weatherConditionCode", UniversalEnum.SPIT.getNumber());
} else if (Objects.equals(pph, UniversalEnum.TWO.getValue())) {
jsonObject.put("weatherCondition", UniversalEnum.SLEET.getValue());
jsonObject.put("weatherConditionCode", UniversalEnum.SLEET.getNumber());
} else if (Objects.equals(pph, UniversalEnum.THREE.getValue())) {
jsonObject.put("weatherCondition", UniversalEnum.LIGHT_SNOW.getValue());
jsonObject.put("weatherConditionCode", UniversalEnum.LIGHT_SNOW.getNumber());
} else if (Objects.equals(pph, UniversalEnum.FOUR.getValue())) {
jsonObject.put("weatherCondition", UniversalEnum.FREEZING_RAIN.getValue());
jsonObject.put("weatherConditionCode", UniversalEnum.FREEZING_RAIN.getNumber());
} else if (Objects.equals(pph, UniversalEnum.ZERO.getValue())) {
jsonObject.put("weatherCondition", UniversalEnum.CLEAR_WEATHER.getValue());
jsonObject.put("weatherConditionCode", UniversalEnum.CLEAR_WEATHER.getNumber());
}
break;
case "3":
if (Objects.equals(pph, UniversalEnum.ONE.getValue())) {
jsonObject.put("weatherCondition", UniversalEnum.MODERATE_RAIN.getValue());
jsonObject.put("weatherConditionCode", UniversalEnum.MODERATE_RAIN.getNumber());
} else if (Objects.equals(pph, UniversalEnum.TWO.getValue())) {
jsonObject.put("weatherCondition", UniversalEnum.SLEET.getValue());
jsonObject.put("weatherConditionCode", UniversalEnum.SLEET.getNumber());
} else if (Objects.equals(pph, UniversalEnum.THREE.getValue())) {
jsonObject.put("weatherCondition", UniversalEnum.MODERATE_SNOW.getValue());
jsonObject.put("weatherConditionCode", UniversalEnum.MODERATE_SNOW.getNumber());
} else if (Objects.equals(pph, UniversalEnum.FOUR.getValue())) {
jsonObject.put("weatherCondition", UniversalEnum.FREEZING_RAIN.getValue());
jsonObject.put("weatherConditionCode", UniversalEnum.FREEZING_RAIN.getNumber());
} else if (Objects.equals(pph, UniversalEnum.ZERO.getValue())) {
jsonObject.put("weatherCondition", UniversalEnum.CLEAR_WEATHER.getValue());
jsonObject.put("weatherConditionCode", UniversalEnum.CLEAR_WEATHER.getNumber());
}
break;
case "4":
if (Objects.equals(pph, UniversalEnum.ONE.getValue())) {
jsonObject.put("weatherCondition", UniversalEnum.HEAVY_RAIN.getValue());
jsonObject.put("weatherConditionCode", UniversalEnum.HEAVY_RAIN.getNumber());
} else if (Objects.equals(pph, UniversalEnum.TWO.getValue())) {
jsonObject.put("weatherCondition", UniversalEnum.SLEET.getValue());
jsonObject.put("weatherConditionCode", UniversalEnum.SLEET.getNumber());
} else if (Objects.equals(pph, UniversalEnum.THREE.getValue())) {
jsonObject.put("weatherCondition", UniversalEnum.HEAVY_SNOW.getValue());
jsonObject.put("weatherConditionCode", UniversalEnum.HEAVY_SNOW.getNumber());
} else if (Objects.equals(pph, UniversalEnum.FOUR.getValue())) {
jsonObject.put("weatherCondition", UniversalEnum.FREEZING_RAIN.getValue());
jsonObject.put("weatherConditionCode", UniversalEnum.FREEZING_RAIN.getNumber());
} else if (Objects.equals(pph, UniversalEnum.ZERO.getValue())) {
jsonObject.put("weatherCondition", UniversalEnum.CLEAR_WEATHER.getValue());
jsonObject.put("weatherConditionCode", UniversalEnum.CLEAR_WEATHER.getNumber());
}
break;
case "5":
if (Objects.equals(pph, UniversalEnum.ONE.getValue())) {
jsonObject.put("weatherCondition", UniversalEnum.RAINSTORM.getValue());
jsonObject.put("weatherConditionCode", UniversalEnum.RAINSTORM.getNumber());
} else if (Objects.equals(pph, UniversalEnum.TWO.getValue())) {
jsonObject.put("weatherCondition", UniversalEnum.SLEET.getValue());
jsonObject.put("weatherConditionCode", UniversalEnum.SLEET.getNumber());
} else if (Objects.equals(pph, UniversalEnum.THREE.getValue())) {
jsonObject.put("weatherCondition", UniversalEnum.BLIZZARD.getValue());
jsonObject.put("weatherConditionCode", UniversalEnum.BLIZZARD.getNumber());
} else if (Objects.equals(pph, UniversalEnum.FOUR.getValue())) {
jsonObject.put("weatherCondition", UniversalEnum.FREEZING_RAIN.getValue());
jsonObject.put("weatherConditionCode", UniversalEnum.FREEZING_RAIN.getNumber());
} else if (Objects.equals(pph, UniversalEnum.ZERO.getValue())) {
jsonObject.put("weatherCondition", UniversalEnum.CLEAR_WEATHER.getValue());
jsonObject.put("weatherConditionCode", UniversalEnum.CLEAR_WEATHER.getNumber());
}
break;
case "6":
if (Objects.equals(pph, UniversalEnum.ONE.getValue())) {
jsonObject.put("weatherCondition", UniversalEnum.BIG_RAINSTORM.getValue());
jsonObject.put("weatherConditionCode", UniversalEnum.BIG_RAINSTORM.getNumber());
} else if (Objects.equals(pph, UniversalEnum.TWO.getValue())) {
jsonObject.put("weatherCondition", UniversalEnum.SLEET.getValue());
jsonObject.put("weatherConditionCode", UniversalEnum.SLEET.getNumber());
} else if (Objects.equals(pph, UniversalEnum.THREE.getValue())) {
jsonObject.put("weatherCondition", UniversalEnum.BIG_BLIZZARD.getValue());
jsonObject.put("weatherConditionCode", UniversalEnum.BIG_BLIZZARD.getNumber());
} else if (Objects.equals(pph, UniversalEnum.FOUR.getValue())) {
jsonObject.put("weatherCondition", UniversalEnum.FREEZING_RAIN.getValue());
jsonObject.put("weatherConditionCode", UniversalEnum.FREEZING_RAIN.getNumber());
} else if (Objects.equals(pph, UniversalEnum.ZERO.getValue())) {
jsonObject.put("weatherCondition", UniversalEnum.CLEAR_WEATHER.getValue());
jsonObject.put("weatherConditionCode", UniversalEnum.CLEAR_WEATHER.getNumber());
}
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());
} else if (Objects.equals(pph, UniversalEnum.TWO.getValue())) {
jsonObject.put("weatherCondition", UniversalEnum.SLEET.getValue());
jsonObject.put("weatherConditionCode", UniversalEnum.SLEET.getNumber());
} else if (Objects.equals(pph, UniversalEnum.THREE.getValue())) {
jsonObject.put("weatherCondition", UniversalEnum.HEAVY_SNOWSTORM.getValue());
jsonObject.put("weatherConditionCode", UniversalEnum.HEAVY_SNOWSTORM.getNumber());
} else if (Objects.equals(pph, UniversalEnum.FOUR.getValue())) {
jsonObject.put("weatherCondition", UniversalEnum.FREEZING_RAIN.getValue());
jsonObject.put("weatherConditionCode", UniversalEnum.FREEZING_RAIN.getNumber());
} else if (Objects.equals(pph, UniversalEnum.ZERO.getValue())) {
jsonObject.put("weatherCondition", UniversalEnum.CLEAR_WEATHER.getValue());
jsonObject.put("weatherConditionCode", UniversalEnum.CLEAR_WEATHER.getNumber());
}
break;
}
}
JSONArray forecastList = jsonObject.getJSONArray("forecastList");
if (forecastList != null && forecastList.size() != UniversalEnum.ZERO.getNumber()) {
jsonObject.put("forecastList",processingWeatherData(forecastList));
}
jsonArray.add(i, jsonObject);
}
return jsonArray;
}
public static int findNearestMultipleOfFive(int number) {
// 计算除以5的余数
int remainder = number % UniversalEnum.FIVE.getNumber();

Loading…
Cancel
Save