package com.ylcm.sys.task;

import com.ylcm.sys.enums.AdTheatreTimeEnum;
import com.ylcm.sys.form.AdTheatreAnalysisQueryForm;
import com.ylcm.sys.service.AdTheatreTvStatService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.task.TaskExecutor;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

/**
 * 央卫剧场分析
 */
@Component
@Slf4j
public class TheatreTopTask {

	@Resource
	private AdTheatreTvStatService adTheatreTvStatService;

	@Resource
	private TaskExecutor taskExecutor;


	/**
	 * spring容器初始化事件
	 */
	@PostConstruct
	public void init() throws InterruptedException {
		taskExecutor.execute(new Runnable() {
			@Override
			public void run() {
				try {
					Thread.sleep(10000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}

				topTaskWeek();
				topTaskMonth();

				AdTheatreAnalysisQueryForm queryForm = new AdTheatreAnalysisQueryForm();
				queryForm.setPageNo(1);
				queryForm.setPageSize(10);
				queryForm.setName("");

				LocalDate now = LocalDate.now();
				now = now.minusDays(1);
				LocalDate day7before = now.minusDays(7);
				queryForm.setStartDate(day7before.format(DateTimeFormatter.ISO_DATE));
				queryForm.setEndDate(now.format(DateTimeFormatter.ISO_DATE));
				queryForm.setTheatreTime(AdTheatreTimeEnum.DAY.getCode());

				adTheatreTvStatService.getPage(queryForm);
			}
		});

	}
	/**
	 * 最近一周的每2小时运行 电视剧及卫视剧场排名
	 */
	@Scheduled(cron = "0 0 0/1 * * ?")
	public void topTaskWeek(){
		log.info("电视剧及卫视剧场排名---start");
		LocalDate now = LocalDate.now();
		now = now.minusDays(1);
		LocalDate day7before = now.minusDays(7);
		adTheatreTvStatService.getTvTop(AdTheatreTimeEnum.DAY.getCode(), now.format(DateTimeFormatter.ISO_DATE), day7before.format(DateTimeFormatter.ISO_DATE));
		adTheatreTvStatService.getMediaTop(null, AdTheatreTimeEnum.DAY.getCode(), now.format(DateTimeFormatter.ISO_DATE), day7before.format(DateTimeFormatter.ISO_DATE), 30);
		log.info("电视剧及卫视剧场排名---end");
	}
	
	/**
	 * 月、年的每天运行两次
	 */
	@Scheduled(cron = "0 0 4,12 * * ?")
	public void topTaskMonth(){
		log.info("电视剧排名---start");
		LocalDate now = LocalDate.now();
		now = now.minusDays(1);
		LocalDate day7before = now.minusDays(30);
		adTheatreTvStatService.getTvTop(AdTheatreTimeEnum.DAY.getCode(), now.format(DateTimeFormatter.ISO_DATE), day7before.format(DateTimeFormatter.ISO_DATE));
		adTheatreTvStatService.getMediaTop(null, AdTheatreTimeEnum.DAY.getCode(), now.format(DateTimeFormatter.ISO_DATE), day7before.format(DateTimeFormatter.ISO_DATE), 30);
		
		LocalDate day365before = now.minusDays(365);
		adTheatreTvStatService.getTvTop(AdTheatreTimeEnum.DAY.getCode(), now.format(DateTimeFormatter.ISO_DATE), day365before.format(DateTimeFormatter.ISO_DATE));
		adTheatreTvStatService.getMediaTop(null, AdTheatreTimeEnum.DAY.getCode(), now.format(DateTimeFormatter.ISO_DATE), day365before.format(DateTimeFormatter.ISO_DATE), 30);
		
		log.info("电视剧排名---end");
	}
	
	
	
	
}
