New file |
| | |
| | | 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.Iterator; |
| | | 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.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"); |
| | | String now = sdf.format(c.getTime()); |
| | | c.add(Calendar.DAY_OF_MONTH, 1); |
| | | String nextDay = sdf.format(c.getTime()); |
| | | c.add(Calendar.DAY_OF_MONTH, 1); |
| | | String nextNextDay = sdf.format(c.getTime()); |
| | | |
| | | 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 = ""; |
| | | 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(); |
| | | } |
| | | hashMap.put("name1", name1); |
| | | hashMap.put("parentName", parentName); |
| | | 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 (name1.equals(name2)) { |
| | | cityID = element.element("Fid").getText(); |
| | | break; |
| | | } |
| | | if (name2.endsWith(name1)) { |
| | | if (name2.startsWith(parentName)) { |
| | | cityID = element.element("Fid").getText(); |
| | | 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<>(); |
| | | } |
| | | |
| | | List<Map<String, Object>> dataList = (ArrayList<Map<String, Object>>) map1.get("data"); |
| | | List<Map<String, Object>> arrayList = new ArrayList<>(); |
| | | |
| | | 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); |
| | | } |
| | | } |
| | | } |
| | | hashMap.put("data", arrayList); |
| | | insertList.add(hashMap); |
| | | } |
| | | |
| | | 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); |
| | | } |
| | | } |
| | | } |