package cn.wisenergy.web.config.auto;

import cn.wisenergy.common.utils.DateUtil;
import cn.wisenergy.mapper.WorkProjectTimeCostMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * @description: 自动统计每天统计一次，统计之前先删除近三个月的统计数据
 * @author: nh
 * @create: 2021-02-08 11:05
 **/
@Slf4j
@Configuration
@EnableScheduling
public class AutoStatistics {

    @Autowired
    private WorkProjectTimeCostMapper workProjectTimeCostMapper;

    @Transactional
    @Scheduled(cron = "0 0 0 * * ?")
    public void autoStatistics(){
        log.info("AutoStatistics[]autoStatistics[]自动统计任务");
        Date date = DateUtil.add(new Date(), Calendar.MONTH, -3);
        String firstDayOfMonth = DateUtil.convertDateToYMDStr(DateUtil.getFirstDayOfMonth(date));
        workProjectTimeCostMapper.deleteStatisticsByQuarter(firstDayOfMonth);
        workProjectTimeCostMapper.statisticsTimeOrderByMonth(firstDayOfMonth);
    }
}
