package com.rehome.mqttclienttemperature.task;


import com.google.gson.Gson;
import com.rehome.mqttclienttemperature.controller.WeatherController;
import com.rehome.mqttclienttemperature.entity.WebServiceCity;
import com.rehome.mqttclienttemperature.entity.WebServiceWeatherInfo;
import com.rehome.mqttclienttemperature.service.WebServiceWeatherService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;

/***
 * 调用本地布署的Webservice
 */
//@Component
public class WebServiceWeatherTask {
    private static final Logger log = LoggerFactory.getLogger(AgencyTask.class);

    @Resource
    private WeatherController weatherController;
    @Resource
    private WebServiceWeatherService webServiceWeatherService;

    //每1小时执行一次,同步省份和城市
    //@Scheduled(cron = "0 51 23 * * ?")
    protected void addRfqs() {
        log.info("每1小时执行一次,同步省份和城市，时间：{}", new Date());
        try {
            Gson gson = new Gson();
            weatherController.getSupportProvince();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //每5分钟执行一次,获取全球所有城市天气
    //@Scheduled(cron = "0 0 0/3 * * ?")
    protected void scheduledGetAllCityWeather() {
        log.info("每5分钟执行一次,获取全球所有城市天气，时间：{}", new Date());

        try {
            Gson gson = new Gson();
            List<WebServiceCity> cityList = webServiceWeatherService.findAllCity();
            if(cityList.size()>0){
                for (WebServiceCity cityItem:cityList) {
                    Thread.sleep(6000);
                    String[] weatherArray = weatherController.getWeatherByCityCode(cityItem.getCode());
                    if(weatherArray.length>0&&weatherArray.length==23){
                        System.out.println("----------weatherArray-----------");
                        System.out.println(gson.toJson(weatherArray));
                        System.out.println(weatherArray.length);

                        Date now = new Date();
                        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                        SimpleDateFormat sdfHour = new SimpleDateFormat("yyyy-MM-dd HH");
                        SimpleDateFormat sdfMinute = new SimpleDateFormat("yyyy-MM-dd HH:mm");
                        SimpleDateFormat sdfSecond = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

                        String nowDate = sdf.format(now);
                        String nowHour = sdfHour.format(now);
                        String dataMinute = sdfMinute.format(now);
                        String dataSecond = sdfSecond.format(now);

                        WebServiceWeatherInfo WebServiceWeatherInfo = new WebServiceWeatherInfo();
                        WebServiceWeatherInfo.setCreateDate(now);
                        WebServiceWeatherInfo.setDataHour(nowHour);
                        WebServiceWeatherInfo.setDataMinute(dataMinute);
                        WebServiceWeatherInfo.setDataSecond(dataSecond);

                        WebServiceWeatherInfo.setProvinceName(weatherArray[0]);
                        WebServiceWeatherInfo.setCityName(weatherArray[1]);
                        WebServiceWeatherInfo.setCityCode(weatherArray[2]);
                        WebServiceWeatherInfo.setCityPhoto(weatherArray[3]);
                        WebServiceWeatherInfo.setWeatherLastUpdate(weatherArray[4]);
                        WebServiceWeatherInfo.setWeatherNow(weatherArray[5]);
                        WebServiceWeatherInfo.setWeatherShortDesc(weatherArray[6]);
                        WebServiceWeatherInfo.setWindDesc(weatherArray[7]);
                        WebServiceWeatherInfo.setWeatherTrendStart(weatherArray[8]);
                        WebServiceWeatherInfo.setWeatherTrendEnd(weatherArray[9]);
                        WebServiceWeatherInfo.setWeatherRealStatus(weatherArray[10]);
                        WebServiceWeatherInfo.setWeatherLiveIndex(weatherArray[11]);
                        WebServiceWeatherInfo.setNextDateTemperature(weatherArray[12]);
                        WebServiceWeatherInfo.setNextDateWeatherShortDesc(weatherArray[13]);
                        WebServiceWeatherInfo.setNextDateWindDesc(weatherArray[14]);
                        WebServiceWeatherInfo.setNextDateWeatherTrendStart(weatherArray[15]);
                        WebServiceWeatherInfo.setNextDateWeatherTrendEnd(weatherArray[16]);
                        WebServiceWeatherInfo.setThreeDayTemperature(weatherArray[17]);
                        WebServiceWeatherInfo.setThreeWeatherShortDesc(weatherArray[18]);
                        WebServiceWeatherInfo.setThreeDayWindDesc(weatherArray[19]);
                        WebServiceWeatherInfo.setThreeDayWeatherTrendStart(weatherArray[20]);
                        WebServiceWeatherInfo.setThreeDayWeatherTrendEnd(weatherArray[21]);
                        WebServiceWeatherInfo.setCityIntroduce(weatherArray[22]);
                        webServiceWeatherService.saveWebServiceWeatherInfo(WebServiceWeatherInfo);
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

