rentaiming
2024-06-13 fe2d5b14031edbe43238770fb1fc21e8a322b51a
ruoyi-modules/ruoyi-goods/src/main/java/com/ruoyi/goods/service/async/AsyncMethodService.java
@@ -3,13 +3,15 @@
import com.ruoyi.common.core.constant.SecurityConstants;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.redis.service.RedisService;
import com.ruoyi.goods.domain.GoodsGroupPurchase;
import com.ruoyi.system.api.domain.GoodsGroupPurchase;
import com.ruoyi.system.api.constants.DelayTaskEnum;
import com.ruoyi.system.api.domain.DelayTask;
import com.ruoyi.system.api.domain.GoodsGroupPurchase;
import com.ruoyi.system.api.domain.GoodsSeckill;
import com.ruoyi.system.api.feignClient.SysUserClient;
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.List;
import java.util.concurrent.TimeUnit;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
@@ -31,48 +33,52 @@
    @Async
    @Transactional(rollbackFor = Exception.class)
    public void seckillScheduleTask(GoodsSeckill goodsSeckill) {
        LocalDateTime startTime = goodsSeckill.getStartTime();
        LocalDateTime endTime = goodsSeckill.getEndTime();
        //秒杀在一小时内开始
        if (isWithinOneHour(startTime)) {
            Long id = goodsSeckill.getId();
            //秒杀已经开始
            if (LocalDateTime.now().isAfter(startTime)) {
                handleStartDelayTask(id, DelayTaskEnum.SECKILL_START_TASK, startTime, 3L);
            } else {
                Duration duration = Duration.between(LocalDateTime.now(), startTime);
                handleStartDelayTask(id, DelayTaskEnum.SECKILL_START_TASK, startTime,
                        duration.getSeconds());
    public void seckillScheduleTask(List<GoodsSeckill> goodsSeckillList) {
        for (GoodsSeckill goodsSeckill : goodsSeckillList) {
            LocalDateTime startTime = goodsSeckill.getStartTime();
            LocalDateTime endTime = goodsSeckill.getEndTime();
            // 秒杀在一小时内开始
            if (isWithinOneHour(startTime)) {
                Long id = goodsSeckill.getId();
                // 秒杀已经开始
                if (LocalDateTime.now().isAfter(startTime)) {
                    handleStartDelayTask(id, DelayTaskEnum.SECKILL_START_TASK, startTime, 3L);
                } else {
                    Duration duration = Duration.between(LocalDateTime.now(), startTime);
                    handleStartDelayTask(id, DelayTaskEnum.SECKILL_START_TASK, startTime,
                            duration.getSeconds());
                }
                log.info(">>>>>>>>>>>>>>>>>>>>秒杀商品:{} 开始秒杀<<<<<<<<<<<<<<<<<<<<", id);
                // 秒杀结束延时任务
                handleEndDelayTask(id, DelayTaskEnum.SECKILL_END_TASK, endTime);
            }
            log.info(">>>>>>>>>>>>>>>>>>>>秒杀商品:{} 开始秒杀<<<<<<<<<<<<<<<<<<<<", id);
            //秒杀结束延时任务
            handleEndDelayTask(id, DelayTaskEnum.SECKILL_END_TASK, endTime);
        }
    }
    @Async
    @Transactional(rollbackFor = Exception.class)
    public void groupPurchaseScheduleTask(GoodsGroupPurchase groupPurchase) {
        LocalDateTime startTime = groupPurchase.getStartTime();
        LocalDateTime endTime = groupPurchase.getEndTime();
        //秒杀在一小时内开始
        if (isWithinOneHour(startTime)) {
            Long id = groupPurchase.getId();
            //秒杀已经开始,三秒后执行
            if (LocalDateTime.now().isAfter(startTime)) {
                handleStartDelayTask(id, DelayTaskEnum.GROUP_PURCHASES_START_TASK, startTime, 3L);
            } else {
                Duration duration = Duration.between(LocalDateTime.now(), startTime);
                handleStartDelayTask(id, DelayTaskEnum.GROUP_PURCHASES_START_TASK, startTime,
                        duration.getSeconds());
    public void groupPurchaseScheduleTask(List<GoodsGroupPurchase> groupPurchaseList) {
        for (GoodsGroupPurchase goodsGroupPurchase : groupPurchaseList) {
            LocalDateTime startTime = goodsGroupPurchase.getStartTime();
            LocalDateTime endTime = goodsGroupPurchase.getEndTime();
            // 团购在一小时内开始
            if (isWithinOneHour(startTime)) {
                Long id = goodsGroupPurchase.getId();
                // 团购已经开始,三秒后执行
                if (LocalDateTime.now().isAfter(startTime)) {
                    handleStartDelayTask(id, DelayTaskEnum.GROUP_PURCHASES_START_TASK, startTime,
                            3L);
                } else {
                    Duration duration = Duration.between(LocalDateTime.now(), startTime);
                    handleStartDelayTask(id, DelayTaskEnum.GROUP_PURCHASES_START_TASK, startTime,
                            duration.getSeconds());
                }
                // 团购结束延时任务
                handleEndDelayTask(id, DelayTaskEnum.GROUP_PURCHASES_END_TASK, endTime);
            }
            //秒杀结束延时任务
            handleEndDelayTask(id, DelayTaskEnum.GROUP_PURCHASES_END_TASK, endTime);
        }
    }
    private boolean isWithinOneHour(LocalDateTime startTime) {
        LocalDateTime checkTime = LocalDateTime.now().plusHours(1);
        return checkTime.isAfter(startTime);