jinpengyong
2020-09-09 a978b4279a12497d0b476b0df31b1c8409498676
update
5 files added
5 files modified
339 ■■■■■ changed files
src/main/java/com/moral/entity/ForecastWeather.java 1 ●●●● patch | view | raw | blame | history
src/main/java/com/moral/entity/RealWeatherDaily.java 15 ●●●●● patch | view | raw | blame | history
src/main/java/com/moral/mapper/RealWeatherDailyMapper.java 11 ●●●●● patch | view | raw | blame | history
src/main/java/com/moral/mapper/RealWeatherMapper.java 9 ●●●● patch | view | raw | blame | history
src/main/java/com/moral/service/RealWeatherDailyService.java 9 ●●●●● patch | view | raw | blame | history
src/main/java/com/moral/service/impl/ForecastWeatherServiceImpl.java 2 ●●●●● patch | view | raw | blame | history
src/main/java/com/moral/service/impl/RealWeatherDailyImpl.java 221 ●●●●● patch | view | raw | blame | history
src/main/resources/mapper/HistoryMapper.xml 2 ●●● patch | view | raw | blame | history
src/main/resources/mapper/RealWeatherAqiDailyMapper.xml 22 ●●●●● patch | view | raw | blame | history
src/main/resources/mapper/RealWeatherMapper.xml 47 ●●●●● patch | view | raw | blame | history
src/main/java/com/moral/entity/ForecastWeather.java
@@ -12,5 +12,4 @@
    private String cityCode;
    private Date time;
    private String json;
    private String result;
}
src/main/java/com/moral/entity/RealWeatherDaily.java
New file
@@ -0,0 +1,15 @@
package com.moral.entity;
import lombok.Data;
import java.util.Date;
import javax.persistence.Id;
@Data
public class RealWeatherDaily {
    @Id
    private String cityCode;
    private Date time;
    private String json;
}
src/main/java/com/moral/mapper/RealWeatherDailyMapper.java
New file
@@ -0,0 +1,11 @@
package com.moral.mapper;
import java.util.List;
import java.util.Map;
public interface RealWeatherDailyMapper {
    int insertRealWeatherDaily(List<Map<String,Object>> list);
    Map<String,Object> getTemp(Map<String,Object> params);
}
src/main/java/com/moral/mapper/RealWeatherMapper.java
@@ -5,8 +5,13 @@
public interface RealWeatherMapper {
    int insertRealWeather(List<Map<String,Object>> list);
    int insertRealWeather(List<Map<String, Object>> list);
    List<Map<String,Object>> getTempAndCloud(Map<String,Object> params);
    List<Map<String, Object>> getTempAndCloud(Map<String, Object> params);
    Map<String, Object> getDayData(Map<String, Object> params);
    Map<String, Object> getTimeMaxTemp(Map<String, Object> params);
    Map<String, Object> getTimeMinTemp(Map<String, Object> params);
}
src/main/java/com/moral/service/RealWeatherDailyService.java
New file
@@ -0,0 +1,9 @@
package com.moral.service;
import java.text.ParseException;
public interface RealWeatherDailyService {
    int insertRealWeatherDaily() throws ParseException;
}
src/main/java/com/moral/service/impl/ForecastWeatherServiceImpl.java
@@ -188,6 +188,8 @@
                    condition = "20";
                } else if ("雨".equals(text)) {
                    condition = "0";
                }else {
                    condition = "50";
                }
                nextDayMap.put("condition", condition);
            }
src/main/java/com/moral/service/impl/RealWeatherDailyImpl.java
New file
@@ -0,0 +1,221 @@
package com.moral.service.impl;
import java.math.BigDecimal;
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.JSONObject;
import com.moral.entity.Area;
import com.moral.entity.City;
import com.moral.entity.MonitorPoint;
import com.moral.entity.Province;
import com.moral.mapper.AreaMapper;
import com.moral.mapper.CityMapper;
import com.moral.mapper.MonitorPointMapper;
import com.moral.mapper.ProvinceMapper;
import com.moral.mapper.RealWeatherDailyMapper;
import com.moral.mapper.RealWeatherMapper;
import com.moral.service.RealWeatherDailyService;
import com.moral.util.Dom4jUtils;
@Service
public class RealWeatherDailyImpl implements RealWeatherDailyService {
    @Resource
    private MonitorPointMapper monitorPointMapper;
    @Resource
    private AreaMapper areaMapper;
    @Resource
    private CityMapper cityMapper;
    @Resource
    private ProvinceMapper provinceMapper;
    @Resource
    private RealWeatherMapper realWeatherMapper;
    @Resource
    private RealWeatherDailyMapper realWeatherDailyMapper;
    @Override
    public int insertRealWeatherDaily() throws ParseException {
        Calendar c = Calendar.getInstance();
        Date today = c.getTime();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        today = sdf.parse(sdf.format(today));
        c.add(Calendar.DAY_OF_MONTH, -1);
        Date yesterday = c.getTime();
        c.add(Calendar.DAY_OF_MONTH, -1);
        Date beforeDay = c.getTime();
        //明天日期
        String yesterdayString = sdf.format(yesterday);
        yesterday = sdf.parse(yesterdayString);
        RestTemplate restTemplate = new RestTemplate();
        List<MonitorPoint> monitorPointList = monitorPointMapper.getMonitorPointList();
        Set<Map<String, Object>> hashSet = new HashSet<>();
        for (MonitorPoint monitorPoint : monitorPointList) {
            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;
            Integer parentCode = 0;
            if (areaCode != null) {
                Area area = areaMapper.getAreaByAreaCode(areaCode);
                City city = cityMapper.getCityByCityCode(cityCode);
                name1 = area.getAreaName();
                parentName = city.getCityName();
                parentCode = city.getCityCode();
                code = areaCode;
                if ("市辖区".equals(name1)) {
                    name1 = parentName;
                    Province province = provinceMapper.getProvinceByProvinceCode(provinceCode);
                    parentName = province.getProvinceName();
                    parentCode = province.getProvinceCode();
                    code = cityCode;
                }
            } else {
                City city = cityMapper.getCityByCityCode(cityCode);
                name1 = city.getCityName();
                Province province = provinceMapper.getProvinceByProvinceCode(provinceCode);
                parentName = province.getProvinceName();
                code = cityCode;
                parentCode = province.getProvinceCode();
                if ("市辖区".equals(name1)) {
                    name1 = parentName;
                    parentName = "";
                    code = provinceCode;
                }
            }
            hashMap.put("name1", name1);
            hashMap.put("parentName", parentName);
            hashMap.put("cityCode", code);
            hashMap.put("parentCode", parentCode);
            hashSet.add(hashMap);
        }
        List<Element> elements = Dom4jUtils.readDocument();
        String cityID = "101190404";
        for (Map<String, Object> 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;
                    }
                }
            }
        }
        int count = 0;
        List<Map<String, Object>> resultList = new ArrayList<>();
        String date = yesterdayString.replace("-", "");
        for (Map<String, Object> map : hashSet) {
            System.out.println(map);
            map.put("start", yesterday);
            map.put("end", today);
            map.put("typeFormat", "%Y-%m-%d %H:%i:%s");
            Map<String, Object> resultMap = new HashMap<>();
            resultMap.put("cityCode", map.get("cityCode").toString());
            resultMap.put("time", yesterday);
            String id = map.get("cityID").toString();
            Map<String, Object> dataMap = restTemplate.getForObject("https://datasetapi.heweather.net/v7/historical/weather?key=da05c6c4852d4f7aa3364a9236ee9e26&gzip=n&location={1}&date={2}", Map.class, id, date);
            if (!dataMap.get("code").equals("200")) {
                return count;
            }
            String json = JSONObject.toJSONString(dataMap);
            dataMap = (Map<String, Object>) JSONObject.parse(json);
            String string = dataMap.get("weatherDaily").toString();
            Map<String, Object> dailyMap = (Map<String, Object>) JSONObject.parse(string);
            //温度平均值
            dailyMap.remove("date");
            Map<String, Object> dayData = realWeatherMapper.getDayData(map);
            for (String key : dayData.keySet()) {
                if (!key.equals("precip")) {
                    dayData.put(key, String.valueOf(Math.round(Double.valueOf(dayData.get(key).toString()))));
                }
            }
            System.out.println(dayData);
            Double windSpeed = Double.valueOf(dayData.get("windSpeed").toString());
            windSpeed = new BigDecimal(windSpeed).setScale(1, BigDecimal.ROUND_HALF_UP).doubleValue();
            //风速km/h转m/s,进行风级判断
            windSpeed = windSpeed * 1000 / 3600;
            int windScale = 0;
            if (windSpeed >= 0.0 && windSpeed <= 0.2) {
                windScale = 0;
            } else if (windSpeed >= 0.3 && windSpeed <= 1.5) {
                windScale = 1;
            } else if (windSpeed >= 1.6 && windSpeed <= 3.3) {
                windScale = 2;
            } else if (windSpeed >= 3.4 && windSpeed <= 5.4) {
                windScale = 3;
            } else if (windSpeed >= 5.5 && windSpeed <= 7.9) {
                windScale = 4;
            } else if (windSpeed >= 8.0 && windSpeed <= 10.7) {
                windScale = 5;
            } else if (windSpeed >= 10.8 && windSpeed <= 13.8) {
                windScale = 6;
            } else if (windSpeed >= 13.9 && windSpeed <= 17.1) {
                windScale = 7;
            } else if (windSpeed >= 17.2 && windSpeed <= 20.7) {
                windScale = 8;
            }
            dayData.put("windScale", windScale);
            //相比昨日气温
            map.put("time", beforeDay);
            Integer yesterdayTemp = Integer.valueOf(dayData.get("tempAvg").toString());
            System.out.println(map);
            Map<String, Object> temp = realWeatherDailyMapper.getTemp(map);
            System.out.println(temp);
            if (temp != null) {
                Integer beforeYesterdayTemp = Integer.valueOf(temp.get("beforeYesterdayTemp").toString());
                dayData.put("tempChange", String.valueOf(yesterdayTemp - beforeYesterdayTemp));
            }
            //最低温,最高温时间点
            Map<String, Object> timeMaxTemp = realWeatherMapper.getTimeMaxTemp(map);
            String hour1 = timeMaxTemp.get("time").toString().substring(11, 13);
            System.out.println(hour1);
            dayData.put("maxTempTime", hour1);
            Map<String, Object> timeMinTemp = realWeatherMapper.getTimeMinTemp(map);
            String hour2 = timeMinTemp.get("time").toString().substring(11, 13);
            dayData.put("minTempTime", hour2);
            System.out.println(hour2);
            dailyMap.putAll(dayData);
            resultMap.put("json", JSONObject.toJSONString(dailyMap));
            resultList.add(resultMap);
        }
        return realWeatherDailyMapper.insertRealWeatherDaily(null);
    }
}
src/main/resources/mapper/HistoryMapper.xml
@@ -179,8 +179,8 @@
    <delete id="deleteHistoryData" parameterType="java.lang.String">
        delete from history where  time &lt; #{oldTime};
    </delete>
    <update id="deletePartition">
        ALTER table history drop PARTITION ${p};
    </update>
</mapper>
src/main/resources/mapper/RealWeatherAqiDailyMapper.xml
New file
@@ -0,0 +1,22 @@
<?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.RealWeatherDailyMapper">
    <resultMap id="BaseResultMap" type="com.moral.entity.RealWeatherDaily" >
        <id column="cityCode" property="cityCode" jdbcType="INTEGER" />
    </resultMap>
    <insert id="insertRealWeatherDaily">
        insert into
        real_weather_daily
        values
        <foreach collection="list" item="item" separator=",">
            (#{item.cityCode},#{item.time},#{item.json})
        </foreach>
    </insert>
    <select id="getTemp" resultType="java.util.Map">
        select json->'$.tempAvg' 'beforeYesterdayTemp'
        from real_weather_daily
        where time=#{time}
        and city_code=#{cityCode}
    </select>
</mapper>
src/main/resources/mapper/RealWeatherMapper.xml
@@ -24,4 +24,51 @@
        and DATE_FORMAT(time,'%H:%i:%s')=#{time}
        and cast(json->'$.condition' as UNSIGNED INTEGER)>=#{condition}
    </select>
    <select id="getDayData" resultType="java.util.Map">
        select
        round(avg(json->'$.temp')) 'tempAvg',
        round(avg(json->'$.vis')) 'vis',
        round(avg(json->'$.cloud')) 'cloud',
        round(avg(json->'$.windSpeed')) 'windSpeed',
        round(sum(json->'$.precip'),1) 'precip'
        from real_weather
        where city_code=#{cityCode}
        and time >= #{start}
        AND time <![CDATA[<]]> #{end}
    </select>
    <select id="getTimeMaxTemp" resultType="java.util.Map">
        select
        DATE_FORMAT(time, #{typeFormat}) time,
        from real_weather
        where city_code=#{cityCode}
        and time >= #{start}
        AND time <![CDATA[<]]> #{end}
        and json->'$.temp'=
        (
        SELECT REPLACE(max(json->'$.temp'),"\"","")
        FROM real_weather
        WHERE time>=#{start}
        and time <![CDATA[<]]> #{end}
        and city_code=130900
        ) limit 0,1;
    </select>
    <select id="getTimeMinTemp" resultType="java.util.Map">
        select
        DATE_FORMAT(time, #{typeFormat}) time,
        from real_weather
        where city_code=#{cityCode}
        and time >= #{start}
        AND time <![CDATA[<]]> #{end}
        and json->'$.temp'=
        (
        SELECT REPLACE(max(json->'$.temp'),"\"","")
        FROM real_weather
        WHERE time>=#{start}
        and time <![CDATA[<]]> #{end}
        and city_code=130900
        ) limit 0,1;
    </select>
</mapper>