| | |
| | | private ProvinceMapper provinceMapper; |
| | | |
| | | @Override |
| | | public void insertRealWeather() throws ParseException { |
| | | public int insertRealWeather() throws ParseException { |
| | | Calendar c = Calendar.getInstance(); |
| | | SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH"); |
| | | SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); |
| | | Date now=sdf1.parse(sdf.format(c.getTime())+":00:00"); |
| | | Date now = sdf1.parse(sdf.format(c.getTime()) + ":00:00"); |
| | | RestTemplate restTemplate = new RestTemplate(); |
| | | List<MonitorPoint> monitorPointList = monitorPointMapper.getMonitorPointList(); |
| | | Set<Map<String, Object>> hashSet = new HashSet<>(); |
| | | List<Map<String, Object>> resultList = new ArrayList<>(); |
| | | for (MonitorPoint monitorPoint : monitorPointList) { |
| | | Map<String, Object> hashMap1 = new HashMap<>(); |
| | | Map<String, Object> hashMap = new HashMap<>(); |
| | | Integer areaCode = monitorPoint.getAreaCode(); |
| | | Integer cityCode = monitorPoint.getCityCode(); |
| | | Integer provinceCode = monitorPoint.getProvinceCode(); |
| | | String parentName = ""; |
| | | String name1 = ""; |
| | | Integer code = 0; |
| | | if (areaCode != null) { |
| | | Area area = areaMapper.getAreaByAreaCode(areaCode); |
| | | City city = cityMapper.getCityByCityCode(cityCode); |
| | | name1 = area.getAreaName(); |
| | | parentName = city.getCityName(); |
| | | code = areaCode; |
| | | if ("市辖区".equals(name1)) { |
| | | name1 = parentName; |
| | | parentName = provinceMapper.getProvinceByProvinceCode(provinceCode).getProvinceName(); |
| | | code = cityCode; |
| | | } |
| | | } else { |
| | | City city = cityMapper.getCityByCityCode(cityCode); |
| | | name1 = city.getCityName(); |
| | | parentName = provinceMapper.getProvinceByProvinceCode(provinceCode).getProvinceName(); |
| | | code = cityCode; |
| | | if ("市辖区".equals(name1)) { |
| | | name1 = parentName; |
| | | parentName = ""; |
| | | code = provinceCode; |
| | | } |
| | | } |
| | | hashMap.put("name1", name1); |
| | | hashMap.put("parentName", parentName); |
| | | hashMap.put("cityCode", code); |
| | | hashSet.add(hashMap); |
| | | hashMap1.put("monitorPointId", monitorPoint.getId()); |
| | | hashMap1.put("name", name1); |
| | | resultList.add(hashMap1); |
| | | } |
| | | |
| | | Map<String,Object> kunShanMap=new HashMap<>(); |
| | | kunShanMap.put("name1","苏州市"); |
| | | kunShanMap.put("parentName","江苏省"); |
| | | kunShanMap.put("cityCode",320500); |
| | | hashSet.add(kunShanMap); |
| | | |
| | | List<Element> elements = Dom4jUtils.readDocument(); |
| | | String cityID = "101190404"; |
| | | for (Map<String, Object> map : hashSet) { |
| | |
| | | } |
| | | } |
| | | } |
| | | |
| | | int count = 0; |
| | | for (Map<String, Object> map : hashSet) { |
| | | String id = map.get("cityID").toString(); |
| | | Map<String, Object> dataMap = restTemplate.getForObject("https://api.heweather.net/v7/weather/now?key=da05c6c4852d4f7aa3364a9236ee9e26&gzip=n&location={1}", Map.class, id); |
| | | if (dataMap == null) { |
| | | dataMap = new HashMap<>(); |
| | | return count; |
| | | } |
| | | String json = JSONObject.toJSONString(dataMap); |
| | | dataMap = (Map<String, Object>) JSONObject.parse(json); |
| | | Map<String, Object> nowMap = (Map<String, Object>) dataMap.get("now"); |
| | | map.put("data",nowMap); |
| | | map.put("data", nowMap); |
| | | } |
| | | |
| | | for (Map<String, Object> resultMap : resultList) { |
| | | Map<String,Object> jsonMap=new HashMap<>(); |
| | | for (Map<String, Object> hashMap : hashSet) { |
| | | if(hashMap.get("name1").equals(resultMap.get("name"))){ |
| | | Map<String, Object> map = (Map<String, Object>) hashMap.get("data"); |
| | | map.remove("obsTime"); |
| | | map.remove("icon"); |
| | | resultMap.put("time",now); |
| | | jsonMap.putAll(map); |
| | | resultMap.put("json",JSONObject.toJSONString(jsonMap)); |
| | | } |
| | | List<Map<String,Object>> resultList=new ArrayList<>(); |
| | | for (Map<String, Object> map : hashSet) { |
| | | Map<String,Object> resultMap=new HashMap<>(); |
| | | resultMap.put("cityCode",map.get("cityCode").toString()); |
| | | resultMap.put("time",now); |
| | | Map<String, Object> jsonMap = (Map<String, Object>) map.get("data"); |
| | | jsonMap.remove("obsTime"); |
| | | jsonMap.remove("icon"); |
| | | String text = jsonMap.get("text").toString(); |
| | | if(text.equals("晴")){ |
| | | jsonMap.put("condition","100"); |
| | | }else if(text.equals("多云")){ |
| | | jsonMap.put("condition","80"); |
| | | }else if(text.equals("阴")){ |
| | | jsonMap.put("condition","60"); |
| | | }else if(text.equals("雷阵雨")||text.equals("阵雨")){ |
| | | jsonMap.put("condition","40"); |
| | | }else if(text.equals("小雨")){ |
| | | jsonMap.put("condition","20"); |
| | | }else if(text.equals("雨")){ |
| | | jsonMap.put("condition","0"); |
| | | } |
| | | resultMap.put("json",JSONObject.toJSONString(jsonMap)); |
| | | resultList.add(resultMap); |
| | | } |
| | | |
| | | for (Map<String, Object> map : resultList) { |
| | | map.remove("name"); |
| | | } |
| | | realWeatherMapper.insertRealWeather(resultList); |
| | | return realWeatherMapper.insertRealWeather(resultList); |
| | | } |
| | | } |