NowWeatherQuery.java 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. package com.persagy.functions.weather.business.v2020;
  2. import java.util.Arrays;
  3. import java.util.HashMap;
  4. import java.util.HashSet;
  5. import java.util.List;
  6. import java.util.Map;
  7. import java.util.Set;
  8. import javax.annotation.Resource;
  9. import org.springframework.stereotype.Service;
  10. import org.springframework.util.StringUtils;
  11. import com.persagy.core.service.BaseBusinessService;
  12. import com.persagy.core.service.BusinessService;
  13. import com.persagy.ems.pojo.wz.HourRecordData;
  14. import com.persagy.framework.util.ConfigUtil;
  15. import com.persagy.framework.util.WServiceUtil;
  16. import com.persagy.functions.weather.constant.Const;
  17. import com.persagy.functions.weather.service.HourRecordService;
  18. @Service("NowWeatherQuery")
  19. @SuppressWarnings({"unchecked", "rawtypes"})
  20. public class NowWeatherQuery extends BaseBusinessService<Map> implements BusinessService {
  21. private static Set<String> typeSet = new HashSet<>(Arrays.asList("all", "weather", "air"));
  22. @Resource
  23. private HourRecordService hourDataService;
  24. @Override
  25. public void handle(Map dto, List content) throws Exception {
  26. String cityName = (String) dto.get("cityName");
  27. String projectId = (String) dto.get("projectId");
  28. String cityCode = (String) dto.get("cityCode");
  29. String dataType = (String) dto.get("dataType");
  30. if (StringUtils.isEmpty(projectId) && StringUtils.isEmpty(cityCode) && StringUtils.isEmpty(cityName)) {
  31. throw new Exception(Const.ErrorTag.paramMis);
  32. }
  33. if(StringUtils.isEmpty(dataType)) {
  34. throw new Exception(Const.ErrorTag.paramMis);
  35. }
  36. if(!typeSet.contains(dataType)) {
  37. throw new Exception(Const.ErrorTag.paramUnrecognized + ":dataType=" + dataType);
  38. }
  39. Map<String, Object> resultMap = new HashMap<>();
  40. String location = ConfigUtil.getHourCity(cityName, cityCode, projectId);
  41. if(null != location) {
  42. HourRecordData obj = hourDataService.getPresent(location);
  43. if(null != obj) {
  44. if("all".equals(dataType) || "air".equals(dataType)) {
  45. resultMap.put("no2", obj.getNo2());
  46. resultMap.put("pm25", obj.getPm25());
  47. resultMap.put("o3", obj.getO3());
  48. resultMap.put("so2", obj.getSo2());
  49. resultMap.put("last_update", obj.getLastUpdateAir());
  50. resultMap.put("aqi", obj.getAqi());
  51. resultMap.put("pm10", obj.getPm10());
  52. resultMap.put("primary_pollutant", obj.getPrimary_pollutant());
  53. resultMap.put("co", obj.getCo());
  54. resultMap.put("quality", obj.getQuality());
  55. }
  56. if("all".equals(dataType) || "weather".equals(dataType)) {
  57. resultMap.put("code", obj.getCode());
  58. resultMap.put("visibility", obj.getVisibility());
  59. resultMap.put("wind_direction", obj.getWind_direction());
  60. resultMap.put("pressure", obj.getPressure());
  61. resultMap.put("clouds", obj.getClouds());
  62. resultMap.put("feels_like", obj.getFeels_like());
  63. resultMap.put("dew_point", obj.getDew_point());
  64. resultMap.put("wind_scale", obj.getWind_scale());
  65. resultMap.put("last_update", obj.getLastUpdateNow());
  66. resultMap.put("temperature", obj.getTemperature());
  67. resultMap.put("humidity", obj.getHumidity());
  68. resultMap.put("wind_direction_degree", obj.getWind_direction_degree());
  69. resultMap.put("wind_speed", obj.getWind_speed());
  70. resultMap.put("text", obj.getText());
  71. }
  72. }
  73. }
  74. if(resultMap.isEmpty()) {
  75. location = ConfigUtil.getLocation(cityName, cityCode, projectId);
  76. if("all".equals(dataType) || "air".equals(dataType)) {
  77. try {
  78. Map<String, Object> dataMap = WServiceUtil.getNowAir(location);
  79. if (!dataMap.isEmpty()) {
  80. resultMap.putAll(dataMap);
  81. }
  82. } catch (Exception e) {
  83. e.printStackTrace();
  84. }
  85. }
  86. if("all".equals(dataType) || "weather".equals(dataType)) {
  87. try {
  88. Map<String, Object> dataMap = WServiceUtil.getNowWeather(location);
  89. if (!dataMap.isEmpty()) {
  90. resultMap.putAll(dataMap);
  91. }
  92. } catch (Exception e) {
  93. e.printStackTrace();
  94. }
  95. }
  96. }
  97. content.add(resultMap);
  98. }
  99. }