cjl
2024-04-01 247e9203e92423930bee311af18c355400a9b57b
fix:和风数据修复
3 files modified
68 ■■■■ changed files
screen-job/src/main/java/com/moral/api/controller/PubController.java 12 ●●●● patch | view | raw | blame | history
screen-job/src/main/java/com/moral/api/service/impl/CityWeatherForecastServiceImpl.java 44 ●●●● patch | view | raw | blame | history
screen-job/src/main/java/com/moral/api/service/impl/CityWeatherServiceImpl.java 12 ●●●● patch | view | raw | blame | history
screen-job/src/main/java/com/moral/api/controller/PubController.java
@@ -21,7 +21,6 @@
import javax.mail.MessagingException;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.util.*;
/**
@@ -56,9 +55,11 @@
    private DeviceService deviceService;
    private final EmailSpringUtil emailSpringUtil;
    private final CityWeatherForecastService cityWeatherForecastService;
    public PubController(EmailSpringUtil emailSpringUtil) {
    public PubController(EmailSpringUtil emailSpringUtil, CityWeatherForecastService cityWeatherForecastService) {
        this.emailSpringUtil = emailSpringUtil;
        this.cityWeatherForecastService = cityWeatherForecastService;
    }
    @GetMapping("insertHistoryDaily")
@@ -145,6 +146,13 @@
        return new ResultMessage();
    }
    @GetMapping("insertCityWeatherForecast")
    @ApiOperation(value = "城市每日天气预测", notes = "城市每日天气预测")
    public ResultMessage insertCityWeatherForecast() {
        cityWeatherForecastService.insertCityWeatherForecast();
        return new ResultMessage();
    }
    @GetMapping("insertHistoryMonthly")
    @ApiOperation(value = "月数据", notes = "月数据")
    public ResultMessage insertHistoryMonthly() {
screen-job/src/main/java/com/moral/api/service/impl/CityWeatherForecastServiceImpl.java
@@ -1,5 +1,6 @@
package com.moral.api.service.impl;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.moral.api.entity.CityConfigWeatherForecast;
@@ -12,9 +13,14 @@
import com.moral.util.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.*;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
@@ -35,16 +41,17 @@
    private CityConfigWeatherForecastService cityConfigWeatherForecastService;
    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private CityWeatherForecastMapper cityWeatherForecastMapper;
    //城市预测气象数据来源于,和风天气,逐小时天气预报(未来72小时)商业版
    @Override
    public void insertCityWeatherForecast() {
        Date nextDay = DateUtils.addDays(new Date(), 1);
        RestTemplate restTemplate = new RestTemplate(
                new HttpComponentsClientHttpRequestFactory()); // 使用HttpClient,支持GZIP
        restTemplate.getMessageConverters().set(1,
                new StringHttpMessageConverter(StandardCharsets.UTF_8));
        Date nextDay = DateUtils.addDays(new Date(), 0);
        String nextTime = DateUtils.dateToDateString(nextDay, DateUtils.yyyy_MM_dd_EN);
        //获取城市配置
        QueryWrapper<CityConfigWeatherForecast> wrapper = new QueryWrapper<>();
@@ -56,33 +63,44 @@
        for (CityConfigWeatherForecast cityConfigWeatherForecast : list) {
            Integer cityCode = cityConfigWeatherForecast.getCityCode();
            Integer locationId = cityConfigWeatherForecast.getLocationId();
            Map<String, Object> data = restTemplate.getForObject("https://api.qweather.com/v7/weather/72h?key=da05c6c4852d4f7aa3364a9236ee9e26&gzip=n&location={1}", Map.class, locationId);
            //Map<String, Object> data = restTemplate.getForObject("https://api.qweather.com/v7/weather/72h?key=da05c6c4852d4f7aa3364a9236ee9e26&gzip=n&location={1}", Map.class, locationId);
            Map<String, Object> data = restTemplate.getForObject("https://devapi.qweather.com/v7/weather/24h?location="+locationId+"&key=2430ab9e636c4950a686fbd84e3ccb3a", Map.class);
            //Map<String, Object> data = restTemplate.getForObject(url,Map.class);
            List<Map<String, Object>> hourly = (List<Map<String, Object>>) data.get("hourly");
            for (Map<String, Object> hourlyMap : hourly) {
                String fxTime = hourlyMap.get("fxTime").toString();
                String dayTime = fxTime.split("T")[0];
                if (dayTime.equals(nextTime)) {
                //if (dayTime.equals(nextTime)) {
                    String hourTime = fxTime.substring(0, 17).replaceAll("T", " ");
                    CityWeatherForecast cityWeatherForecast = new CityWeatherForecast();
                    cityWeatherForecast.setCityCode(cityCode);
                    cityWeatherForecast.setTime(DateUtils.getDate(hourTime, DateUtils.yyyy_MM_dd_HH_EN));
                    cityWeatherForecast.setValue(JSONObject.toJSONString(hourlyMap));
                    cityWeatherForecasts.add(cityWeatherForecast);
                }
                //}
            }
        }
        cityWeatherForecastMapper.insertCityWeatherForecast(cityWeatherForecasts);
    }
    public static void main(String[] args) {
        String time = "20221231";
        long currentMils = DateUtils.getDate(time, DateUtils.yyyyMMdd_EN).getTime();
        String url = "https://devapi.qweather.com/v7/weather/24h?location=101010100&key=2430ab9e636c4950a686fbd84e3ccb3a"; //此处换为自己的地址
        RestTemplate restTemplate = new RestTemplate(
                new HttpComponentsClientHttpRequestFactory()); // 使用HttpClient,支持GZIP
        restTemplate.getMessageConverters().set(1,
                new StringHttpMessageConverter(StandardCharsets.UTF_8));
        Map<String, Object> data = restTemplate.getForObject(url,Map.class);
        List<Map<String, Object>> hourly = (List<Map<String, Object>>) data.get("hourly");
        int i = 0;
        long startMils = DateUtils.getDate("2022", DateUtils.yyyy).getTime();
        long mss = currentMils - startMils;
        long days = mss / (1000 * 60 * 60 * 24);
        System.out.println(days + 1);
        //String url = "https://devapi.qweather.com/v7/weather/24h?location=101010100&key=2430ab9e636c4950a686fbd84e3ccb3a"; //此处换为自己的地址
      //  String response = restTemplate.getForObject(url,String.class);
    }
}
screen-job/src/main/java/com/moral/api/service/impl/CityWeatherServiceImpl.java
@@ -14,9 +14,12 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
@@ -39,8 +42,6 @@
    @Autowired
    private CityConfigWeatherService cityConfigWeatherService;
    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private RedisTemplate redisTemplate;
@@ -48,6 +49,10 @@
    //城市气象数据来源于,和风天气,实时天气商业版
    @Override
    public void insertCityWeather() {
        RestTemplate restTemplate = new RestTemplate(
                new HttpComponentsClientHttpRequestFactory()); // 使用HttpClient,支持GZIP
        restTemplate.getMessageConverters().set(1,
                new StringHttpMessageConverter(StandardCharsets.UTF_8));
        //获取城市配置
        QueryWrapper<CityConfigWeather> wrapper = new QueryWrapper<>();
        wrapper.select("city_code", "location_id").eq("is_delete", Constants.NOT_DELETE);
@@ -59,7 +64,8 @@
            CityWeather cityWeather = new CityWeather();
            Integer cityCode = cityConfigWeather.getCityCode();
            Integer locationId = cityConfigWeather.getLocationId();
            Map<String, Object> data = restTemplate.getForObject("https://api.qweather.com/v7/weather/now?key=da05c6c4852d4f7aa3364a9236ee9e26&gzip=n&location={1}", Map.class, locationId);
            //Map<String, Object> data = restTemplate.getForObject("https://api.qweather.com/v7/weather/now?key=da05c6c4852d4f7aa3364a9236ee9e26&gzip=n&location={1}", Map.class, locationId);
            Map<String, Object> data = restTemplate.getForObject("https://devapi.qweather.com/v7/weather/now?location="+locationId+"&key=2430ab9e636c4950a686fbd84e3ccb3a", Map.class);
            Map<String, Object> now = (Map<String, Object>) data.get("now");
            cityWeather.setCityCode(cityCode);
            //风速km/h->m/s