From 0ef88815f1893b069d5e59a4161d2a9a4a2f3a10 Mon Sep 17 00:00:00 2001 From: jinpengyong <jpy123456> Date: Fri, 07 Aug 2020 17:05:46 +0800 Subject: [PATCH] update --- src/main/java/com/moral/service/impl/ForecastWeatherServiceImpl.java | 147 +++++++++++++++++++++++------------------------- 1 files changed, 71 insertions(+), 76 deletions(-) diff --git a/src/main/java/com/moral/service/impl/ForecastWeatherServiceImpl.java b/src/main/java/com/moral/service/impl/ForecastWeatherServiceImpl.java index 5f4b537..f23339e 100644 --- a/src/main/java/com/moral/service/impl/ForecastWeatherServiceImpl.java +++ b/src/main/java/com/moral/service/impl/ForecastWeatherServiceImpl.java @@ -7,7 +7,6 @@ import java.util.Date; import java.util.HashMap; import java.util.HashSet; -import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Set; @@ -19,6 +18,7 @@ import org.springframework.web.client.RestTemplate; import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.moral.entity.Area; import com.moral.entity.City; @@ -50,135 +50,130 @@ private ProvinceMapper provinceMapper; @Override - public void insertForecastWeather() throws ParseException { + public int insertForecastWeather() throws ParseException { Calendar c = Calendar.getInstance(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); - String now = sdf.format(c.getTime()); c.add(Calendar.DAY_OF_MONTH, 1); String nextDay = sdf.format(c.getTime()); + c.setTime(c.getTime()); + Date start = sdf1.parse(nextDay + " 01:00:00"); c.add(Calendar.DAY_OF_MONTH, 1); - String nextNextDay = sdf.format(c.getTime()); - + String nextNextDay = sdf.format(c.getTime()) + " 00"; + Date end = sdf1.parse(sdf.format(c.getTime()) + " 01: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); } List<Element> elements = Dom4jUtils.readDocument(); String cityID = "101190404"; - List<Map<String, Object>> insertList = new ArrayList<>(); for (Map<String, Object> map : hashSet) { - Map<String, Object> hashMap = new HashMap<>(); String name1 = map.get("name1").toString(); - hashMap.put("name", name1); for (Element element : elements) { String name2 = element.element("name").getText(); String parentName = map.get("parentName").toString(); + if (name2.equals("���������")) { + name2 = "���������"; + } if (name1.equals(name2)) { - cityID = element.element("Fid").getText(); + cityID = element.element("Fweathercn").getText(); + map.put("cityID", cityID); break; } if (name2.endsWith(name1)) { if (name2.startsWith(parentName)) { - cityID = element.element("Fid").getText(); + cityID = element.element("Fweathercn").getText(); + map.put("cityID", cityID); break; } } } - Map<String, Object> map1 = restTemplate.getForObject("https://yiketianqi.com/api?version=v9&appid=68884517&appsecret=kqUYTBk3&cityid={1}", Map.class, cityID); - if (map1 == null) { - map1=new HashMap<>(); + } + + 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/72h?key=da05c6c4852d4f7aa3364a9236ee9e26&gzip=n&location={1}", Map.class, id); + if (dataMap == null) { + return count; } + String json = JSONObject.toJSONString(dataMap); + dataMap = (Map<String, Object>) JSONObject.parse(json); + List<Map<String, Object>> nextDayList = new ArrayList<>(); + String string = dataMap.get("hourly").toString(); + List<Map<String, Object>> hourlyList = (List<Map<String, Object>>) JSONObject.parse(string); + for (Map<String, Object> hourlyMap : hourlyList) { + String hour = hourlyMap.get("fxTime").toString().split("T")[0]; + String hour1 = hourlyMap.get("fxTime").toString().split("\\+")[0].split(":")[0].replace("T", " "); + if (hour.equals(nextDay)) { + nextDayList.add(hourlyMap); + } + if (hour1.equals(nextNextDay)) { + nextDayList.add(hourlyMap); + } + map.put("data", nextDayList); + } + } - List<Map<String, Object>> dataList = (ArrayList<Map<String, Object>>) map1.get("data"); - List<Map<String, Object>> arrayList = new ArrayList<>(); - + for (Map<String, Object> map : hashSet) { + List<Map<String,Object>> resultList=new ArrayList<>(); + ArrayList<Map<String, Object>> dataList = (ArrayList<Map<String, Object>>) map.get("data"); for (Map<String, Object> dataMap : dataList) { - String date = dataMap.get("date").toString(); - ArrayList<Map<String, Object>> list = (ArrayList<Map<String, Object>>) dataMap.get("hours"); - if (date.equals(now)) { - for (int i = list.size() - 7; i < list.size(); i++) { - Map<String, Object> resultMap = list.get(i); - arrayList.add(resultMap); - } - } - if (date.equals(nextDay)) { - for (int i = 0; i < 17; i++) { - Map<String, Object> resultMap = list.get(i); - arrayList.add(resultMap); - } - } + Map<String,Object> hashMap=new HashMap<>(); + hashMap.put("cityCode",map.get("cityCode").toString()); + String time = dataMap.get("fxTime").toString().split("\\+")[0].replace("T", " ") + ":00"; + hashMap.put("time",sdf1.parse(time)); + hashMap.put("json",JSONObject.toJSONString(dataMap)); + resultList.add(hashMap); } - hashMap.put("data", arrayList); - insertList.add(hashMap); + resultList.remove(0); + for (Map<String, Object> resultMap : resultList) { + Map<String, Object> jsonMap = (Map<String, Object>) JSONObject.parse(resultMap.get("json").toString()); + jsonMap.remove("fxTime"); + jsonMap.remove("icon"); + resultMap.put("json", JSONObject.toJSONString(jsonMap)); + } + Map<String, Object> parameters = new HashMap<>(); + parameters.put("start", start); + parameters.put("end", end); + parameters.put("cityCode", map.get("cityCode").toString()); + forecastWeatherMapper.deleteByTime(parameters); + count = count+forecastWeatherMapper.insertForecastWeather(resultList); } - - for (Map<String, Object> resultMap : resultList) { - List<Map<String, Object>> list = new ArrayList<>(); - String monitorPointId = resultMap.get("monitorPointId").toString(); - for (Map<String, Object> insertMap : insertList) { - if (resultMap.get("name").equals(insertMap.get("name"))) { - ArrayList<Map<String, Object>> dataList = (ArrayList<Map<String, Object>>) insertMap.get("data"); - for (Map<String, Object> dataMap : dataList) { - dataMap.remove("wea_img"); - Map<String, Object> map = new HashMap<>(); - map.put("monitorPointId", Integer.valueOf(monitorPointId)); - String hour = dataMap.get("hours").toString().replaceAll("[\u4e00-\u9fa5]+", ""); - Date date = sdf1.parse(nextDay + " " + hour + ":00:00"); - if (hour.equals("00")) { - date = sdf1.parse(nextNextDay + " 00:00:00"); - } - map.put("time", date); - String json = JSONObject.toJSONString(dataMap); - map.put("json", json); - list.add(map); - } - } - } - - for (Map<String, Object> map : list) { - String json = map.get("json").toString(); - Map<String, Object> jsonMap = JSON.parseObject(json, Map.class); - Iterator<String> iterator = jsonMap.keySet().iterator(); - while (iterator.hasNext()) { - if (iterator.next().equals("hours")) { - iterator.remove(); - json = JSONObject.toJSONString(jsonMap); - map.put("json",json); - } - } - } - forecastWeatherMapper.insertForecastWeather(list); - } + return count; } } -- Gitblit v1.8.0