| | |
| | | |
| | | 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.enums.StartStatusEnum; |
| | | import com.ruoyi.common.core.exception.ServiceException; |
| | |
| | | import com.ruoyi.goods.service.IGoodsSeckillService; |
| | | import com.ruoyi.goods.service.IGoodsSkuService; |
| | | import com.ruoyi.goods.service.async.AsyncMethodService; |
| | | import com.ruoyi.system.api.WebSocketUsers; |
| | | 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.LocalDateTime; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | import java.util.Optional; |
| | | import java.util.concurrent.ConcurrentHashMap; |
| | | import lombok.RequiredArgsConstructor; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | import util.WebSocketUsers; |
| | | |
| | | /** |
| | | * <p> |
| | |
| | | private final RedisService redisService; |
| | | private final SysUserClient sysUserClient; |
| | | private final AsyncMethodService asyncMethodService; |
| | | // 创建一个静态共享的ObjectMapper实例以重用 |
| | | private static final ObjectMapper objectMapper = new ObjectMapper(); |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public void addGoodsSeckill(GoodsSeckillDTO dto) { |
| | |
| | | * @param seckillId 秒杀id |
| | | */ |
| | | @Override |
| | | public void startSeckill(Long seckillId) { |
| | | public void startSeckill(Long seckillId) throws JsonProcessingException { |
| | | log.info(">>>>>>>>>>>>>>>>>>>>{}秒杀开始<<<<<<<<<<<<<<<<<<<<", seckillId); |
| | | GoodsSeckill goodsSeckill = this.getById(seckillId); |
| | | //秒杀商品不能为空且状态为未开始 |
| | |
| | | goodsSeckill.getSeckillStock()); |
| | | } |
| | | //推送秒杀开始消息 |
| | | WebSocketUsers.sendMessageToUsersByText("秒杀活动已开始"); |
| | | 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); |
| | | } |
| | | |
| | | /** |
| | |
| | | * @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) |
| | |
| | | // 将秒杀商品从缓存中移除 |
| | | redisService.deleteObject(CacheConstants.SECKILL_GOODS + goodsSeckill.getId()); |
| | | } |
| | | //TODO websocket 推送秒杀结束消息 |
| | | WebSocketUsers.sendMessageToUsersByText("秒杀活动已结束"); |
| | | 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); |
| | | } |
| | | } |