From e21e6ff297d1becb0b74ae10f0f3cd55947e06b8 Mon Sep 17 00:00:00 2001 From: zhaoxianglong Date: Wed, 17 Jul 2024 19:13:05 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E5=A4=A9=E6=B0=94=E6=9C=8D?= =?UTF-8?q?=E5=8A=A1=20=E5=A4=A9=E6=B0=94=E6=83=85=E5=86=B5=E5=88=A4?= =?UTF-8?q?=E6=96=AD=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/WeatherForecastController.java | 313 +++++++++--------- 1 file changed, 163 insertions(+), 150 deletions(-) diff --git a/zc-business/src/main/java/com/zc/business/controller/WeatherForecastController.java b/zc-business/src/main/java/com/zc/business/controller/WeatherForecastController.java index 553eb249..9cfc7927 100644 --- a/zc-business/src/main/java/com/zc/business/controller/WeatherForecastController.java +++ b/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();