/*
 * 文 件 名:  TaskService
 * 版    权:
 * 描    述:  <描述>
 * 修 改 人:  learshaw
 * 修改时间:  2021/9/22
 * 跟踪单号:  <跟踪单号>
 * 修改单号:  <修改单号>
 * 修改内容:  <修改内容>
 */
package com.huashe.common.data.service;

import com.huashe.common.exception.BusinessException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import javax.annotation.Resource;
import java.util.Calendar;

/**
 * 任务驱动
 * <功能详细描述>
 *
 * @author learshaw
 * @version [版本号, 2021/9/22]
 * @see [相关类/方法]
 * @since [产品/模块版本]
 */
@Configuration
@EnableScheduling
public class TaskService {
    private static final Logger log = LoggerFactory.getLogger(TaskService.class);

    @Autowired
    @Qualifier(value = "weatherColJuheService")
    private WeatherColService weatherColService;

    @Resource
    private DateService dateService;

    /**
     * 天气实况采集
     *
     * @see [类、类#方法、类#成员]
     */
    @Async
    @Scheduled(cron = "${weather.cron}")
    public void collectWeather() {
        log.info("开始天气采集任务...");
        weatherColService.collect();
        log.info("天气实况采集完成.");
    }

    /**
     * 采集节假日信息
     *
     * @see [类、类#方法、类#成员]
     */
    @Async
    @Scheduled(cron = "${holiday.cron}")
    public void collectHolidays() {
        log.info("开始节假日任务...");

        try {
            Calendar calendar = Calendar.getInstance();
            String year = String.valueOf(calendar.get(Calendar.YEAR));
            dateService.collectHoliday(year);
        }
        catch (BusinessException e) {
            log.error("collectHolidays fail!", e);
        }

        log.info("节假日采集完成.");
    }
}