package cn.iocoder.yudao.module.member.job;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;

import cn.hutool.core.util.ObjUtil;
import cn.iocoder.yudao.framework.quartz.core.handler.JobHandler;
import cn.iocoder.yudao.framework.tenant.core.aop.TenantIgnore;

import cn.iocoder.yudao.module.member.dal.dataobject.statisticsspacecapsuleorder.StatisticsSpaceCapsuleOrderDO;

import cn.iocoder.yudao.module.member.dal.mysql.statisticsspacecapsuleorder.StatisticsSpaceCapsuleOrderMapper;
import cn.iocoder.yudao.module.member.service.statisticsspacecapsuleorder.StatisticsSpaceCapsuleOrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

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

@Slf4j
@Component
public class StatisticsSpaceCapsuleOrderJob implements JobHandler {
    @Resource
    private StatisticsSpaceCapsuleOrderMapper statisticsSpaceCapsuleOrderMapper;
    @Resource
    private StatisticsSpaceCapsuleOrderService statisticsSpaceCapsuleOrderService;
    @Override
    @TenantIgnore
    public String execute(String param) throws Exception {
        log.info("开始进行定时统计================》");
        DateTime date = DateUtil.date();
        DateTime dateTime = DateUtil.offsetDay(date, -1);
        //获取开始时间
        DateTime startTime = DateUtil.beginOfDay(dateTime);
        //结束时间
        DateTime endTime = DateUtil.endOfDay(dateTime);
        List<StatisticsSpaceCapsuleOrderDO> statistics = statisticsSpaceCapsuleOrderService.statistics(startTime, endTime);
        if(ObjUtil.isNotEmpty(statistics)){
            log.info("定时统计数量{}",statistics.size());
            statisticsSpaceCapsuleOrderMapper.insertBatch(statistics);
            return "定时统计数量"+ statistics.size();
        }else{
            return "没有定时统计数量";
        }
    }
}
