package com.sqx.scheduler.integral;

import com.sqx.modules.coupon.service.TbCouponUserService;
import com.sqx.modules.integral.service.UserIntegralService;
import com.sqx.scheduler.config.SchedulerLock;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/**
 * 优惠券定时任务
 *
 * @author : codingliang
 * @date : 2024-09-09 12:22
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class IntegralScheduler {

    private final UserIntegralService integralService;
    private final RedissonClient redissonClient;

    /**
     * 将所有超过失效时间的积分改为失效状态
     * 每分钟运行一次
     * 暂不设置过期
     */
    @Async
//    @Scheduled(cron = "0 0 2 * * ?", zone = "Asia/Shanghai")
    public void couponEnd(){
        RLock lock = redissonClient.getLock(SchedulerLock.INTEGRAL_OF_USER_EXPIRATION_LOCK);
        lock.lock();
        try {
            log.info("用户积分自动过期任务运行");
            integralService.updateExpiration();
            log.info("用户积分自动过期任务运行成功");
        } catch (Exception e) {
            log.error("用户积分自动过期任务运行失败，【{}】", e);
        } finally {
            lock.unlock();
        }

    }
}
