File was renamed from src/main/java/com/moral/service/impl/RealTimeWeatherServiceImpl.java |
| | |
| | | import java.text.SimpleDateFormat; |
| | | |
| | | import java.util.ArrayList; |
| | | import java.util.Calendar; |
| | | import java.util.Date; |
| | | import java.util.HashMap; |
| | | import java.util.HashSet; |
| | |
| | | import com.moral.mapper.CityMapper; |
| | | import com.moral.mapper.MonitorPointMapper; |
| | | import com.moral.mapper.ProvinceMapper; |
| | | import com.moral.mapper.RealTimeWeatherMapper; |
| | | import com.moral.service.RealTimeWeatherService; |
| | | import com.moral.mapper.RealWeatherMapper; |
| | | import com.moral.service.RealWeatherService; |
| | | import com.moral.util.Dom4jUtils; |
| | | |
| | | @Service |
| | | public class RealTimeWeatherServiceImpl implements RealTimeWeatherService { |
| | | public class RealWeatherServiceImpl implements RealWeatherService { |
| | | |
| | | @Resource |
| | | private RealTimeWeatherMapper realTimeWeatherMapper; |
| | | private RealWeatherMapper realWeatherMapper; |
| | | |
| | | @Resource |
| | | private MonitorPointMapper monitorPointMapper; |
| | |
| | | private ProvinceMapper provinceMapper; |
| | | |
| | | @Override |
| | | public void insertRealTimeWeather() throws ParseException { |
| | | public void 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"); |
| | | RestTemplate restTemplate = new RestTemplate(); |
| | | List<MonitorPoint> monitorPointList = monitorPointMapper.getMonitorPointList(); |
| | | List<Map<String, Object>> list = new ArrayList<>(); |
| | | Set<Map<String, Object>> hashSet = new HashSet<>(); |
| | | List<Map<String, Object>> resultList = new ArrayList<>(); |
| | | for (MonitorPoint monitorPoint : monitorPointList) { |
| | |
| | | City city = cityMapper.getCityByCityCode(cityCode); |
| | | name1 = city.getCityName(); |
| | | parentName = provinceMapper.getProvinceByProvinceCode(provinceCode).getProvinceName(); |
| | | if ("市辖区".equals(name1)) { |
| | | name1 = parentName; |
| | | parentName = ""; |
| | | } |
| | | } |
| | | hashMap.put("name1", name1); |
| | | hashMap.put("parentName", parentName); |
| | |
| | | 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("http://sapi.7drlb.com/api/mj?cityID={1}&apiKey=condition", Map.class, cityID); |
| | | Map<String, Object> dateMap = (Map<String, Object>) map1.get("data"); |
| | | Map<String, Object> conditionMap = (Map<String, Object>) dateMap.get("condition"); |
| | | conditionMap.put("name", name1); |
| | | list.add(conditionMap); |
| | | } |
| | | List<Map<String, Object>> insertList = new ArrayList<>(); |
| | | for (Map<String, Object> resultMap : resultList) { |
| | | Map<String, Object> map = new HashMap<>(); |
| | | String monitorPointId = resultMap.get("monitorPointId").toString(); |
| | | map.put("monitorPointId", Integer.valueOf(monitorPointId)); |
| | | for (Map<String, Object> listMap : list) { |
| | | if (resultMap.get("name").equals(listMap.get("name"))) { |
| | | String json = JSONObject.toJSONString(listMap); |
| | | String[] strings = listMap.get("updatetime").toString().split(" "); |
| | | String time = strings[0] + " " + strings[1].split(":")[0] + ":00:00"; |
| | | Date date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(time); |
| | | map.put("json", json); |
| | | map.put("time", date); |
| | | } |
| | | } |
| | | insertList.add(map); |
| | | } |
| | | |
| | | for (Map<String, Object> map : insertList) { |
| | | String json = map.get("json").toString(); |
| | | Map<String, Object> parse = (Map<String, Object>) JSONObject.parse(json); |
| | | parse.remove("name"); |
| | | parse.remove("icon"); |
| | | parse.remove("conditionId"); |
| | | parse.remove("tips"); |
| | | map.put("json", JSONObject.toJSONString(parse)); |
| | | 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<>(); |
| | | } |
| | | 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); |
| | | } |
| | | realTimeWeatherMapper.insertRealTimeWeather(insertList); |
| | | |
| | | 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)); |
| | | } |
| | | } |
| | | } |
| | | |
| | | for (Map<String, Object> map : resultList) { |
| | | map.remove("name"); |
| | | } |
| | | realWeatherMapper.insertRealWeather(resultList); |
| | | } |
| | | } |