package cn.superdesk.uniorder.executor.scheduler.task;

import cn.hutool.core.date.DateUtil;
import cn.superdesk.uniorder.dao.entity.marketing.UnifiedStatisticsVirtDimDay;
import cn.superdesk.uniorder.dubbo.consumer.marketing.UnifiedStatisticsVirtDimDayDubboConsumerService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @Description: 每天统计上一天 TODO 已自测 mark by linjinyu at 20210906
 * @Author: linjinyu
 * @CreateDate: 2021/9/3 下午3:51
 * @UpdateUser: linjinyu
 * @UpdateDate: 2021/9/3 下午3:51
 * @UpdateRemark: 修改内容
 * @Version: 1.0
 */
@Slf4j
@Component
public class StatisticsJobVirtOrderStatisticalDimDayTask {

	@Resource
	private UnifiedStatisticsVirtDimDayDubboConsumerService unifiedStatisticsVirtDimDayDubboConsumerService;

	/**
	 * 目前按原做法 配置成 0 0 * * * ? *
	 * 实际上是可以 配置成 0 0 17,23 * * ?
	 *
	 * @param params
	 */
	public void run(String params) {

		Integer year       = DateUtil.year(DateUtil.date());
		Integer month      = DateUtil.month(DateUtil.date()) + 1;// 获得月份,从0开始计数
		Integer dayOfMonth = DateUtil.dayOfMonth(DateUtil.date());
		Integer hour       = DateUtil.hour(DateUtil.date(), true);

		if (hour == 23 || hour == 17) {
			log.info("{}:>>>>>----开始执行统计......", DateUtil.now());
			runTask(year, month, dayOfMonth);
		} else {
			log.info("{}:>>>>>----未在统计时间内----<<<<< {}", DateUtil.now(), getClass());
			return;
		}
	}

	public synchronized void runTask(Integer year, Integer month, Integer dayOfMonth) {

		/****************************************** StatisticsJobVirtOrderStatistical start ******************************************/

		Map<String, Object> queryMap = new HashMap<>();
		queryMap.put("year", year);
		queryMap.put("month", month);
		queryMap.put("dayOfMonth", dayOfMonth);

		List<UnifiedStatisticsVirtDimDay> resultList = unifiedStatisticsVirtDimDayDubboConsumerService.queryVirtOrderStatisticalDimDay(queryMap);

		if (resultList != null && resultList.size() > 0) {
			for (UnifiedStatisticsVirtDimDay item : resultList) {
				unifiedStatisticsVirtDimDayDubboConsumerService.storeVirtOrderStatisticalDimDay(item, year, month, dayOfMonth);
			}
		}
	}
}
