package com.ruoyi.task.vipUser;

import java.time.LocalDate;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.ruoyi.bst.vipUser.domain.VipUserQuery;
import com.ruoyi.bst.vipUser.domain.VipUserVO;
import com.ruoyi.bst.vipUser.service.VipUserService;
import com.ruoyi.common.utils.ServiceUtil;
import com.ruoyi.common.utils.collection.CollectionUtils;

import lombok.extern.slf4j.Slf4j;

@Component
@Slf4j
public class VipUserTask {
    
    @Autowired
    private VipUserService vipUserService;

    /**
     * 每天执行一次，刷新今天需要刷新的VIP周期计数
     */
    public void resetVipRoundCount() {

        // 查询今天需要刷新的VIP周期计数
        VipUserQuery query = new VipUserQuery();
        query.setNextResetDate(LocalDate.now());
        query.setEnableLimit(true);
        query.setIsEffective(true);
        List<VipUserVO> vipUserList = vipUserService.selectVipUserList(query);

        if (CollectionUtils.isEmpty(vipUserList)) {
            log.info("今天没有需要刷新的卡券周期计数");
            return;
        }

        // 清除今天需要刷新的VIP周期计数
        for (VipUserVO vipUser : vipUserList) {
            try {
                int result = vipUserService.resetVipRoundCount(vipUser);
                ServiceUtil.assertion(result != 1, "刷新卡券周期计数失败，卡券ID:{}", vipUser.getId());
            } catch (Exception e) {
                log.error("刷新卡券周期计数失败", e);
            }
        }
    }

}
