ruoyi-modules/ruoyi-goods/src/main/java/com/ruoyi/goods/service/impl/GoodsSeckillServiceImpl.java
@@ -2,36 +2,42 @@
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ruoyi.common.core.constant.CacheConstants;
import com.ruoyi.common.core.constant.SecurityConstants;
import com.ruoyi.common.core.enums.ListingStatusEnum;
import com.ruoyi.common.core.enums.StartStatusEnum;
import com.ruoyi.common.core.exception.ServiceException;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.core.utils.page.BeanUtils;
import com.ruoyi.common.core.utils.page.PageDTO;
import com.ruoyi.common.redis.service.RedisService;
import com.ruoyi.goods.controller.management.DTO.GoodsSeckillDTO;
import com.ruoyi.goods.controller.management.DTO.GoodsSeckillQuery;
import com.ruoyi.goods.controller.management.DTO.GoodsSeckillUpd;
import com.ruoyi.goods.controller.management.VO.GoodsSeckillVO;
import com.ruoyi.goods.domain.GoodsSeckill;
import com.ruoyi.goods.domain.GoodsSku;
import com.ruoyi.goods.controller.management.dto.GoodsSeckillDTO;
import com.ruoyi.goods.controller.management.dto.GoodsSeckillQuery;
import com.ruoyi.goods.controller.management.dto.GoodsSeckillUpd;
import com.ruoyi.goods.controller.management.vo.GoodsSeckillVO;
import com.ruoyi.goods.mapper.GoodsSeckillMapper;
import com.ruoyi.goods.service.IGoodsSeckillService;
import com.ruoyi.goods.service.IGoodsSkuService;
import com.ruoyi.goods.service.async.AsyncMethodService;
import com.ruoyi.system.api.constants.DelayTaskEnum;
import com.ruoyi.system.api.domain.DelayTask;
import com.ruoyi.system.api.constants.NotificationTypeConstant;
import com.ruoyi.system.api.domain.GoodsSeckill;
import com.ruoyi.system.api.domain.GoodsSku;
import com.ruoyi.system.api.domain.dto.ListStatusDTO;
import com.ruoyi.system.api.feignClient.OrderClient;
import com.ruoyi.system.api.feignClient.SysUserClient;
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.ConcurrentHashMap;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RedissonClient;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import util.WebSocketUsers;
/**
 * <p>
@@ -50,8 +56,9 @@
    private final OrderClient orderClient;
    private final RedisService redisService;
    private final SysUserClient sysUserClient;
    private final RedissonClient redissonClient;
    private final AsyncMethodService asyncMethodService;
    // 创建一个静态共享的ObjectMapper实例以重用
    private static final ObjectMapper objectMapper = new ObjectMapper();
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addGoodsSeckill(GoodsSeckillDTO dto) {
@@ -67,117 +74,19 @@
        this.saveBatch(goodsSeckills);
        for (GoodsSeckill goodsSeckill : goodsSeckills) {
            LocalDateTime startTime = goodsSeckill.getStartTime();
            LocalDateTime endTime = goodsSeckill.getEndTime();
            LocalDateTime checkTime = LocalDateTime.now().plusHours(1);
            //秒杀在一小时内开始
            if (checkTime.isAfter(startTime)) {
                Long id = goodsSeckill.getId();
                LocalDateTime now = LocalDateTime.now();
                String seckillStartTaskKey = DelayTaskEnum.SECKILL_START_TASK.getCode() + "-" + id;
                //秒杀已经开始
                if (now.isAfter(startTime)) {
                    //查询延时任务
                    DelayTask startDelayTask = sysUserClient.getDelayTask(
                            seckillStartTaskKey).getData();
                    if (StringUtils.isNull(startDelayTask)) {
                        redisService.setCacheObject(
                                seckillStartTaskKey,
                                startTime, 3L, TimeUnit.SECONDS);
                        startDelayTask = new DelayTask();
                        startDelayTask.setDelFlag(0);
                        startDelayTask.setCreateTime(LocalDateTime.now());
                        startDelayTask.setEndTime(LocalDateTime.now().plusSeconds(3));
                        startDelayTask.setRedisKey(
                                seckillStartTaskKey);
                        sysUserClient.addDelayTask(startDelayTask);
                    } else {
                        if (!startDelayTask.getEndTime().isEqual(startTime)) {
                            sysUserClient.deleteDelayTask(
                                    seckillStartTaskKey);
                            redisService.deleteObject(
                                    seckillStartTaskKey);
                            redisService.setCacheObject(
                                    seckillStartTaskKey,
                                    startTime, 3L, TimeUnit.SECONDS);
                            startDelayTask = new DelayTask();
                            startDelayTask.setDelFlag(0);
                            startDelayTask.setCreateTime(LocalDateTime.now());
                            startDelayTask.setEndTime(LocalDateTime.now().plusSeconds(3));
                            startDelayTask.setRedisKey(
                                    seckillStartTaskKey);
                            sysUserClient.addDelayTask(startDelayTask);
                        }
                    }
                } else {
                    DelayTask startDelayTask = sysUserClient.getDelayTask(
                            seckillStartTaskKey).getData();
                    Duration duration = Duration.between(LocalDateTime.now(), startTime);
                    if (StringUtils.isNull(startDelayTask)) {
                        redisService.setCacheObject(
                                seckillStartTaskKey,
                                startTime, duration.toMillis(), TimeUnit.MILLISECONDS);
                        startDelayTask = new DelayTask();
                        startDelayTask.setDelFlag(0);
                        startDelayTask.setCreateTime(LocalDateTime.now());
                        startDelayTask.setEndTime(startTime);
                        startDelayTask.setRedisKey(
                                seckillStartTaskKey);
                        sysUserClient.addDelayTask(startDelayTask);
                    } else {
                        if (!startDelayTask.getEndTime().isEqual(startTime)) {
                            sysUserClient.deleteDelayTask(
                                    seckillStartTaskKey);
                            redisService.deleteObject(
                                    seckillStartTaskKey);
                            redisService.setCacheObject(
                                    seckillStartTaskKey,
                                    startTime, duration.toMillis(), TimeUnit.MILLISECONDS);
                            startDelayTask = new DelayTask();
                            startDelayTask.setDelFlag(0);
                            startDelayTask.setCreateTime(LocalDateTime.now());
                            startDelayTask.setEndTime(startTime);
                            startDelayTask.setRedisKey(
                                    seckillStartTaskKey);
                            sysUserClient.addDelayTask(startDelayTask);
                        }
                    }
                }
                String seckillEndTaskKey = DelayTaskEnum.SECKILL_END_TASK.getCode() + "-" + id;
                DelayTask endDelayTask = sysUserClient.getDelayTask(
                        seckillEndTaskKey).getData();
                // 如果延时任务为空,创建延时任务控制活动定时开始和结束
                if (StringUtils.isNull(endDelayTask)) {
                    Duration duration = Duration.between(LocalDateTime.now(), endTime);
                    redisService.setCacheObject(
                            seckillEndTaskKey,
                            endTime, duration.toMillis(), TimeUnit.MILLISECONDS);
                    endDelayTask = new DelayTask();
                    endDelayTask.setDelFlag(0);
                    endDelayTask.setCreateTime(LocalDateTime.now());
                    endDelayTask.setEndTime(endTime);
                    endDelayTask.setRedisKey(seckillEndTaskKey);
                    sysUserClient.addDelayTask(endDelayTask);
                } else {
                    Duration duration = Duration.between(LocalDateTime.now(), endTime);
                    if (!endDelayTask.getEndTime().isEqual(endTime)) {
                        sysUserClient.deleteDelayTask(seckillEndTaskKey);
                        redisService.deleteObject(seckillEndTaskKey);
                        redisService.setCacheObject(
                                seckillEndTaskKey,
                                endTime, duration.toMillis(), TimeUnit.MILLISECONDS);
                        endDelayTask = new DelayTask();
                        endDelayTask.setDelFlag(0);
                        endDelayTask.setCreateTime(LocalDateTime.now());
                        endDelayTask.setEndTime(endTime);
                        endDelayTask.setRedisKey(seckillEndTaskKey);
                        sysUserClient.addDelayTask(endDelayTask);
                    }
                }
            GoodsSku goodsSku = goodsSkuService.getById(goodsSeckill.getGoodsSkuId());
            if (StringUtils.isNull(goodsSku)) {
                throw new ServiceException("商品不存在");
            }
            Integer seckillStock = goodsSeckill.getSeckillStock();
            goodsSkuService.lambdaUpdate()
                    .set(GoodsSku::getStock, goodsSku.getStock() - seckillStock)
                    .ge(GoodsSku::getStock, seckillStock)
                    .eq(GoodsSku::getId, goodsSku.getId());
            asyncMethodService.seckillScheduleTask(goodsSeckill);
        }
    }
    /**
     * 获取秒杀商品列表的分页数据
@@ -198,14 +107,19 @@
     * @param upd 商品秒杀数据传输对象
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updGoodsSeckill(GoodsSeckillUpd upd) {
        //查询秒杀商品
        GoodsSeckill goodsSeckill = this.getById(upd.getId());
        if (StringUtils.isNull(goodsSeckill)) {
            throw new RuntimeException("秒杀商品不存在");
            throw new ServiceException("秒杀商品不存在");
        }
        if (goodsSeckill.getStartStatus().equals(StartStatusEnum.STARTED)) {
            throw new ServiceException("秒杀商品已开始秒杀,不能修改");
        }
        GoodsSeckill goodsSeckillUpd = BeanUtils.copyBean(upd, GoodsSeckill.class);
        this.updateById(goodsSeckillUpd);
        asyncMethodService.seckillScheduleTask(goodsSeckill);
    }
    /**
@@ -215,10 +129,21 @@
     */
    @Override
    public void updStatus(ListStatusDTO dto) {
        GoodsSeckill goodsSeckill = this.getById(dto.getId());
        if (StringUtils.isNull(goodsSeckill)) {
            throw new ServiceException("秒杀商品不存在");
        }
        this.lambdaUpdate()
                .eq(GoodsSeckill::getId, dto.getId())
                .set(GoodsSeckill::getListingStatus, dto.getListingStatus())
                .update();
        if (dto.getListingStatus().equals(ListingStatusEnum.REMOVED_FROM_THE_SHELF)) {
            //移除该秒杀商品的延时任务
            redisService.deleteObject(
                    DelayTaskEnum.SECKILL_START_TASK.getCode() + "-" + goodsSeckill.getId());
            redisService.deleteObject(
                    DelayTaskEnum.SECKILL_END_TASK.getCode() + "-" + goodsSeckill.getId());
        }
    }
    /**
@@ -231,12 +156,13 @@
    public GoodsSeckillVO getDetail(Long id) {
        GoodsSeckill goodsSeckill = this.getById(id);
        if (StringUtils.isNull(goodsSeckill)) {
            throw new RuntimeException("秒杀商品不存在");
            throw new ServiceException("秒杀商品不存在");
        }
        GoodsSeckillVO vo = BeanUtils.copyBean(goodsSeckill, GoodsSeckillVO.class);
        GoodsSku goods = goodsSkuService.getById(goodsSeckill.getGoodsSkuId());
        Optional.of(goods).ifPresent(goodsSku -> vo.setGoodsSkuName(goodsSku.getSkuName()));
        Integer num = orderClient.getSeckillMembers(goodsSeckill.getGoodsSkuId()).getData();
        Integer num = orderClient.getSeckillMembers(goodsSeckill.getGoodsSkuId(),
                SecurityConstants.INNER).getData();
        vo.setNumberOfPurchasedMembers(num);
        return vo;
    }
@@ -247,7 +173,7 @@
     * @param seckillId 秒杀id
     */
    @Override
    public void startSeckill(Long seckillId) {
    public void startSeckill(Long seckillId) throws JsonProcessingException {
        log.info(">>>>>>>>>>>>>>>>>>>>{}秒杀开始<<<<<<<<<<<<<<<<<<<<", seckillId);
        GoodsSeckill goodsSeckill = this.getById(seckillId);
        //秒杀商品不能为空且状态为未开始
@@ -261,7 +187,14 @@
                    CacheConstants.SECKILL_GOODS + goodsSeckill.getId(),
                    goodsSeckill.getSeckillStock());
        }
        //TODO websocket 推送秒杀开始消息
        //推送秒杀开始消息
        Map<String, Object> map = new ConcurrentHashMap<>();
        map.put("notification_type", NotificationTypeConstant.SECKILL);
        map.put("notification_time", LocalDateTime.now());
        map.put("message_type", "start");
        String msg = objectMapper.writeValueAsString(map);
        WebSocketUsers.sendMessageToUsersByText(msg);
        log.info("===================>发送websocket通知,消息体{}", msg);
    }
    /**
@@ -270,7 +203,7 @@
     * @param seckillId 秒杀id
     */
    @Override
    public void endSeckill(Long seckillId) {
    public void endSeckill(Long seckillId) throws JsonProcessingException {
        log.info(">>>>>>>>>>>>>>>>>>>>{}秒杀结束<<<<<<<<<<<<<<<<<<<<", seckillId);
        GoodsSeckill goodsSeckill = this.getById(seckillId);
        if (StringUtils.isNotNull(goodsSeckill)
@@ -281,6 +214,12 @@
//            将秒杀商品从缓存中移除
            redisService.deleteObject(CacheConstants.SECKILL_GOODS + goodsSeckill.getId());
        }
        //TODO websocket 推送秒杀结束消息
        Map<String, Object> map = new ConcurrentHashMap<>();
        map.put("notification_type", NotificationTypeConstant.SECKILL);
        map.put("notification_time", LocalDateTime.now());
        map.put("message_type", "end");
        String msg = objectMapper.writeValueAsString(map);
        WebSocketUsers.sendMessageToUsersByText(msg);
        log.info("===================>发送websocket通知,消息体{}", msg);
    }
}