package com.moral.service.impl; import java.text.ParseException; 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 java.util.List; import java.util.Map; import java.util.Set; import javax.annotation.Resource; import org.dom4j.Element; import org.springframework.stereotype.Service; 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; import com.moral.entity.MonitorPoint; import com.moral.mapper.AreaMapper; import com.moral.mapper.CityMapper; import com.moral.mapper.ForecastWeatherMapper; import com.moral.mapper.MonitorPointMapper; import com.moral.mapper.ProvinceMapper; import com.moral.service.ForecastWeatherService; import com.moral.util.Dom4jUtils; @Service public class ForecastWeatherServiceImpl implements ForecastWeatherService { @Resource private ForecastWeatherMapper forecastWeatherMapper; @Resource private MonitorPointMapper monitorPointMapper; @Resource private AreaMapper areaMapper; @Resource private CityMapper cityMapper; @Resource private ProvinceMapper provinceMapper; @Override public void insertForecastWeather() throws ParseException { Calendar c = Calendar.getInstance(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 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()) + " 00"; Date end = sdf1.parse(sdf.format(c.getTime()) + " 01:00:00"); RestTemplate restTemplate = new RestTemplate(); List monitorPointList = monitorPointMapper.getMonitorPointList(); Set> hashSet = new HashSet<>(); List> resultList = new ArrayList<>(); for (MonitorPoint monitorPoint : monitorPointList) { Map hashMap1 = new HashMap<>(); Map hashMap = new HashMap<>(); Integer areaCode = monitorPoint.getAreaCode(); Integer cityCode = monitorPoint.getCityCode(); Integer provinceCode = monitorPoint.getProvinceCode(); String parentName = ""; String name1 = ""; if (areaCode != null) { Area area = areaMapper.getAreaByAreaCode(areaCode); City city = cityMapper.getCityByCityCode(cityCode); name1 = area.getAreaName(); parentName = city.getCityName(); if ("市辖区".equals(name1)) { name1 = parentName; parentName = provinceMapper.getProvinceByProvinceCode(provinceCode).getProvinceName(); } } else { 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); hashSet.add(hashMap); hashMap1.put("monitorPointId", monitorPoint.getId()); hashMap1.put("name", name1); resultList.add(hashMap1); } List elements = Dom4jUtils.readDocument(); String cityID = "101190404"; for (Map map : hashSet) { String name1 = map.get("name1").toString(); 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("Fweathercn").getText(); map.put("cityID", cityID); break; } if (name2.endsWith(name1)) { if (name2.startsWith(parentName)) { cityID = element.element("Fweathercn").getText(); map.put("cityID", cityID); break; } } } } for (Map map : hashSet) { String id = map.get("cityID").toString(); Map dataMap = restTemplate.getForObject("https://api.heweather.net/v7/weather/72h?key=da05c6c4852d4f7aa3364a9236ee9e26&gzip=n&location={1}", Map.class, id); if (dataMap == null) { dataMap = new HashMap<>(); } String json = JSONObject.toJSONString(dataMap); dataMap = (Map) JSONObject.parse(json); List> nextDayList = new ArrayList<>(); String string = dataMap.get("hourly").toString(); List> hourlyList = (List>) JSONObject.parse(string); for (Map 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); } } for (Map resultMap : resultList) { ArrayList> arrayList = new ArrayList<>(); for (Map hashMap : hashSet) { if (resultMap.get("name").equals(hashMap.get("name1"))) { ArrayList> list = (ArrayList>) hashMap.get("data"); for (Map hourMap : list) { Map map = new HashMap<>(); map.put("monitorPointId", resultMap.get("monitorPointId").toString()); Map jsonMap = new HashMap<>(); String time = hourMap.get("fxTime").toString().split("\\+")[0].replace("T", " ") + ":00"; map.put("time", sdf1.parse(time)); hourMap.remove("icon"); jsonMap.putAll(hourMap); map.put("json", JSONObject.toJSONString(jsonMap)); arrayList.add(map); } } } arrayList.remove(0); Map parameters = new HashMap<>(); parameters.put("start", start); parameters.put("end", end); parameters.put("monitorPointId", resultMap.get("monitorPointId").toString()); forecastWeatherMapper.deleteByTime(parameters); for (Map map : arrayList) { Map jsonMap = (Map) JSONObject.parse(map.get("json").toString()); jsonMap.remove("fxTime"); map.put("json", JSONObject.toJSONString(jsonMap)); } forecastWeatherMapper.insertForecastWeather(arrayList); } } }