mitao
2024-09-21 f44e4d609e7efaed9eac545137970b1e334f8106
ruoyi-modules/ruoyi-goods/src/main/java/com/ruoyi/goods/service/async/AsyncMethodService.java
@@ -3,13 +3,14 @@
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.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,50 +32,54 @@
    @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);
                    log.info(">>>>>>>>>>>>>>>>>>>>秒杀商品:{} 开始秒杀<<<<<<<<<<<<<<<<<<<<", id);
                } else {
                    Duration duration = Duration.between(LocalDateTime.now(), startTime);
                    handleStartDelayTask(id, DelayTaskEnum.SECKILL_START_TASK, startTime,
                            duration.getSeconds());
                }
                // 秒杀结束延时任务
                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);
        LocalDateTime checkTime = LocalDateTime.now().plusMinutes(61);
        return checkTime.isAfter(startTime);
    }
@@ -114,9 +119,6 @@
    private void handleStartDelayTask(Long id, DelayTaskEnum delayTaskEnum, LocalDateTime startTime,
            Long timeout) {
        String startTaskKey = delayTaskEnum.getCode() + "-" + id;
        redisService.setCacheObject(
                startTaskKey,
                startTime, timeout, TimeUnit.SECONDS);
        //查询延时任务
        DelayTask startDelayTask = sysUserClient.getDelayTask(
                startTaskKey, SecurityConstants.INNER).getData();
@@ -139,9 +141,10 @@
                startDelayTask.setExecuteTime(LocalDateTime.now().plusSeconds(timeout));
                startDelayTask.setRedisKey(
                        startTaskKey);
                sysUserClient.addDelayTask(startDelayTask, SecurityConstants.INNER);
            }
        }
        redisService.setCacheObject(startTaskKey, startTime, timeout, TimeUnit.SECONDS);
        log.info(">>>>>>>>>>>>>>>>>>>>延时任务{}执行了<<<<<<<<<<<<<<<<<<<<", startTaskKey);
    }
}