From c26092c422c4b72fa5d51a38f6de1b48ab1ccd87 Mon Sep 17 00:00:00 2001
From: mitao <2763622819@qq.com>
Date: 星期一, 27 五月 2024 15:53:36 +0800
Subject: [PATCH] 提交普通拍品、资讯文章、商城商品收藏基础代码
---
ruoyi-modules/ruoyi-goods/src/main/java/com/ruoyi/goods/service/impl/GoodsSeckillServiceImpl.java | 51 +++++++++++++++++++++++++++++++++++++++++++--------
1 files changed, 43 insertions(+), 8 deletions(-)
diff --git a/ruoyi-modules/ruoyi-goods/src/main/java/com/ruoyi/goods/service/impl/GoodsSeckillServiceImpl.java b/ruoyi-modules/ruoyi-goods/src/main/java/com/ruoyi/goods/service/impl/GoodsSeckillServiceImpl.java
index 8cf04e0..d44a70c 100644
--- a/ruoyi-modules/ruoyi-goods/src/main/java/com/ruoyi/goods/service/impl/GoodsSeckillServiceImpl.java
+++ b/ruoyi-modules/ruoyi-goods/src/main/java/com/ruoyi/goods/service/impl/GoodsSeckillServiceImpl.java
@@ -2,7 +2,10 @@
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.ListingStatusEnum;
import com.ruoyi.common.core.enums.StartStatusEnum;
import com.ruoyi.common.core.exception.ServiceException;
import com.ruoyi.common.core.utils.StringUtils;
@@ -17,18 +20,23 @@
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.DelayTaskEnum;
+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>
@@ -48,6 +56,8 @@
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) {
@@ -101,7 +111,10 @@
//查询秒杀商品
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);
@@ -115,10 +128,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());
+ }
}
/**
@@ -131,7 +155,7 @@
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());
@@ -147,7 +171,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);
//秒杀商品不能为空且状态为未开始
@@ -162,7 +186,13 @@
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);
}
/**
@@ -171,7 +201,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)
@@ -182,7 +212,12 @@
// 将秒杀商品从缓存中移除
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);
}
}
--
Gitblit v1.7.1