SchedulerTask.java 3.73 KB
package com.viontech.job;

import java.io.IOException;
import java.sql.*;
import java.util.Date;
import java.time.LocalDate;
import java.time.ZoneId;
import java.util.List;

import com.viontech.model.Mall;
import com.viontech.service.impl.CityService;
import com.viontech.service.impl.WeatherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;


import com.viontech.model.City;
import com.viontech.model.SimpleWeather;
import com.viontech.service.impl.JsonService;

/**
 * @author zhaibolin
 */
@Component
public class SchedulerTask implements CommandLineRunner{

    @Autowired
	private  JdbcTemplate jdbcTemplate;

	@Autowired
	private JsonService jsonService;

	@Autowired
	private CityService cityService;

	@Autowired
	private WeatherService weatherService;
    
    @Scheduled(cron="0 0 8 * * ?")
    private void process() throws IOException{
    	weatherService.getWeatherInfo();

//    	Date now = new Date();
//    	LocalDate localDate=now.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
//    	Date date=java.sql.Date.valueOf(localDate);
//		long s=System.currentTimeMillis();
//		System.out.println(new Timestamp(s));
//    	System.out.println(date+"天气服务调用开始");
////    	RowMapper<City> rowMapper = new BeanPropertyRowMapper<>(City.class);
////      List<City> Citys = jdbcTemplate.query("select city_id,province_id,weather_code,city_name,province_name from b_city where weather_code is not null and weather_code!='';", rowMapper);
//
////      RowMapper<SimpleWeather> rowMappers = new BeanPropertyRowMapper<>(SimpleWeather.class);
////      List<SimpleWeather> Weathers=jdbcTemplate.query("select province_id,city_id,type,data_date,ltemp,htemp,aqi,wind_intensity from d_weather where data_date='"+date+"';", rowMappers);
////      List<Integer> cityIds = Weathers.stream().map(SimpleWeather::getCity_id).collect(Collectors.toList());
//
//        if(cityList.isEmpty()){
//        	System.out.println("请先给您所在城市添加城市编码");
//		}else{
//			for (City bcity : cityList) {
//				if(cityIds.contains(bcity.getCityId())){
//					System.out.println(""+bcity.getProvinceName()+bcity.getCityName()+"天气已经存在");
//					continue;
//				}
//				Integer  weatherCode= null;
//				try {
//					weatherCode = new Integer(bcity.getWeatherCode().trim());
//				} catch (NumberFormatException e) {
//					continue;
//				}
//				SimpleWeather  simpleWeather= jsonService.getWeatherInfo(weatherCode.intValue());
//				if(simpleWeather == null){
//                    System.out.println(weatherCode.intValue());
//                }
//
//				jdbcTemplate.update(
//				        "insert into d_weather(province_id,city_id,type,data_date,ltemp,htemp,aqi,modify_time,create_time,wind_intensity) values(?,?,?,?,?,?,?,NOW(),NOW(),?);",
//                        bcity.getProvinceId(),
//                        bcity.getCityId(),
//                        String.valueOf(simpleWeather.getType()),
//                        date,
//                        Integer.parseInt(simpleWeather.getLtemp()),
//                        Integer.parseInt(simpleWeather.getHtemp()),
//                        Integer.parseInt(simpleWeather.getAqi()),
//                        Integer.parseInt(simpleWeather.getFengli().substring(0,1))
//                );
//				System.out.println(""+bcity.getProvinceName()+bcity.getCityName()+"天气插入成功");
//			}
//		}
//        System.out.println("天气服务调用结束");
    }

	//@Override
	public void run(String... args) throws Exception {
		// TODO Auto-generated method stub
		process();
	}

}