Browse Source

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

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

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

@ -258,12 +258,36 @@ 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 = 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);
return AjaxResult.success(jsonArray);
} else {
JSONArray cacheObject = redisCache.getCacheObject(UniversalEnum.CURRENT_WEATHER_AND_FORECAST_INFORMATION_SINGLE_WORD.getValue() + numStake);
return AjaxResult.success(cacheObject);
}
} else {
return AjaxResult.error(jsonResult.getInteger("code"), UniversalEnum.REQUEST_FAILED.getValue());
}
}
} catch (Exception ignored) {
logger.error(String.valueOf(ignored));
}
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()) {
if (Objects.equals(pre, UniversalEnum.ZERO.getValue()) || Double.parseDouble(pre) == UniversalEnum.ZERO.getNumber()) {
String visLevel = jsonObject.getString("visLevel");
@ -406,29 +430,18 @@ public class WeatherForecastController extends BaseController {
}
}
jsonArray.add(i, jsonObject);
}
;
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);
}
} else {
return AjaxResult.error(jsonResult.getInteger("code"), UniversalEnum.REQUEST_FAILED.getValue());
}
}
} catch (Exception ignored) {
JSONArray forecastList = jsonObject.getJSONArray("forecastList");
if (forecastList != null && forecastList.size() != UniversalEnum.ZERO.getNumber()) {
logger.error(String.valueOf(ignored));
jsonObject.put("forecastList",processingWeatherData(forecastList));
}
jsonArray.add(i, jsonObject);
}
return AjaxResult.error(UniversalEnum.REQUEST_FAILED.getValue());
return jsonArray;
}
public static int findNearestMultipleOfFive(int number) {
// 计算除以5的余数
int remainder = number % UniversalEnum.FIVE.getNumber();

Loading…
Cancel
Save