package com.goafanti.common.task;

import java.util.Calendar;
import java.util.Date;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.goafanti.common.utils.LoggerUtils;
import com.goafanti.report.service.SalesReportServiceImpl;

@Component
public class OrderReportTask {

	Logger							logger	= LoggerFactory.getLogger(OrderReportTask.class);

	@Resource
	private SalesReportServiceImpl	dailySalesReportServiceImpl;

	/**
	 * "0 0 17 * * ?" 每天17点触发一次
	 */
	@Scheduled(cron = "0 0 17 * * ?")
	public void runStatistics() {
		LoggerUtils.debug(logger, "统计订单报表任务开始");
		Calendar now = Calendar.getInstance();
		now.set(Calendar.MILLISECOND, 0);
		now.set(Calendar.SECOND, 0);
		now.set(Calendar.MINUTE, 0);
		Date start = new Calendar.Builder()
				.setDate(now.get(Calendar.YEAR), now.get(Calendar.MONTH), now.get(Calendar.DAY_OF_MONTH) - 1)
				.setTimeOfDay(17, 0, 0, 0).build().getTime();
		// 统计从昨天17点到今天17点的数据，算作今日数据
		int count = dailySalesReportServiceImpl.insertDailyReports(start, now.getTime());
		LoggerUtils.debug(logger, "统计今日订单【%s】条。", count);
	}
}
