src/main/java/com/moral/entity/RealWeather.java
File was renamed from src/main/java/com/moral/entity/RealTimeWeather.java @@ -7,7 +7,7 @@ import javax.persistence.Id; @Data public class RealTimeWeather { public class RealWeather { @Id private Integer monitorPointId; private Date time; src/main/java/com/moral/mapper/ForecastWeatherMapper.java
@@ -6,4 +6,6 @@ public interface ForecastWeatherMapper { void insertForecastWeather(List<Map<String,Object>> list); void deleteByTime(Map<String,Object> parameters); } src/main/java/com/moral/mapper/RealTimeWeatherMapper.java
File was deleted src/main/java/com/moral/mapper/RealWeatherMapper.java
New file @@ -0,0 +1,9 @@ package com.moral.mapper; import java.util.List; import java.util.Map; public interface RealWeatherMapper { void insertRealWeather(List<Map<String,Object>> list); } src/main/java/com/moral/service/RealTimeWeatherService.java
File was deleted src/main/java/com/moral/service/RealWeatherService.java
New file @@ -0,0 +1,9 @@ package com.moral.service; import java.text.ParseException; public interface RealWeatherService { void insertRealWeather() throws ParseException; } 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; @@ -54,15 +54,15 @@ 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) { @@ -86,6 +86,10 @@ 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); @@ -96,89 +100,84 @@ } 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<>(); } 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); 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) { dataMap = new HashMap<>(); } 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 (date.equals(nextDay)) { for (int i = 0; i < 17; i++) { Map<String, Object> resultMap = list.get(i); arrayList.add(resultMap); if (hour1.equals(nextNextDay)) { nextDayList.add(hourlyMap); } map.put("data", nextDayList); } } 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"); ArrayList<Map<String, Object>> arrayList = new ArrayList<>(); for (Map<String, Object> hashMap : hashSet) { if (resultMap.get("name").equals(hashMap.get("name1"))) { ArrayList<Map<String, Object>> list = (ArrayList<Map<String, Object>>) hashMap.get("data"); for (Map<String, Object> hourMap : list) { 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); } } map.put("monitorPointId", resultMap.get("monitorPointId").toString()); Map<String, Object> 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); } 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); } } arrayList.remove(0); Map<String, Object> parameters = new HashMap<>(); parameters.put("start", start); parameters.put("end", end); parameters.put("monitorPointId", resultMap.get("monitorPointId").toString()); forecastWeatherMapper.deleteByTime(parameters); for (Map<String, Object> map : arrayList) { Map<String, Object> jsonMap = (Map<String, Object>) JSONObject.parse(map.get("json").toString()); jsonMap.remove("fxTime"); map.put("json", JSONObject.toJSONString(jsonMap)); } forecastWeatherMapper.insertForecastWeather(list); forecastWeatherMapper.insertForecastWeather(arrayList); } } } src/main/java/com/moral/service/impl/RealWeatherServiceImpl.java
File was renamed from src/main/java/com/moral/service/impl/RealTimeWeatherServiceImpl.java @@ -4,6 +4,7 @@ import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Calendar; import java.util.Date; import java.util.HashMap; import java.util.HashSet; @@ -26,15 +27,15 @@ 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; @@ -49,10 +50,13 @@ 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) { @@ -76,6 +80,10 @@ 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); @@ -91,50 +99,53 @@ 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<>(); } realTimeWeatherMapper.insertRealTimeWeather(insertList); 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); } 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); } } src/main/java/com/moral/task/RealTimeWeatherInsertTask.java
File was deleted src/main/java/com/moral/task/RealWeatherInsertTask.java
New file @@ -0,0 +1,27 @@ package com.moral.task; import java.text.ParseException; import javax.annotation.Resource; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Component; import com.moral.service.RealWeatherService; import com.xxl.job.core.biz.model.ReturnT; import com.xxl.job.core.handler.annotation.XxlJob; @Component public class RealWeatherInsertTask { private static transient Logger logger = LoggerFactory.getLogger(RealWeatherInsertTask.class); @Resource private RealWeatherService realWeatherService; @XxlJob("realWeatherHour") public ReturnT insertRealWeather(String param) throws ParseException { realWeatherService.insertRealWeather(); return new ReturnT(200, "插入成功"); } } src/main/resources/mapper/ForecastWeatherMapper.xml
@@ -13,4 +13,12 @@ (#{item.monitorPointId},#{item.time},#{item.json}) </foreach> </insert> <delete id="deleteByTime"> delete from forecast_weather where time >= #{start} AND time <![CDATA[<]]> #{end} and monitor_point_id=#{monitorPointId} </delete> </mapper> src/main/resources/mapper/RealWeatherMapper.xml
File was renamed from src/main/resources/mapper/RealTimeWeatherMapper.xml @@ -1,12 +1,12 @@ <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.moral.mapper.RealTimeWeatherMapper"> <resultMap id="BaseResultMap" type="com.moral.entity.RealTimeWeather" > <mapper namespace="com.moral.mapper.RealWeatherMapper"> <resultMap id="BaseResultMap" type="com.moral.entity.RealWeather" > <id column="monitor_point_id" property="monitorPointId" jdbcType="INTEGER" /> </resultMap> <insert id="insertRealTimeWeather"> <insert id="insertRealWeather"> insert into realtime_weather real_weather values <foreach collection="list" item="item" separator=","> (#{item.monitorPointId},#{item.time},#{item.json})