From 6e8a55edef22faa7f7d5bbbccd727119a6fd918f Mon Sep 17 00:00:00 2001
From: rentaiming <806181062@qq.com>
Date: 星期一, 03 六月 2024 14:05:08 +0800
Subject: [PATCH] 用户端商品
---
ruoyi-modules/ruoyi-goods/src/main/java/com/ruoyi/goods/service/impl/GoodsSeckillServiceImpl.java | 219 +++++++++++++++++++++++++++++++++++++++++++++++++-----
1 files changed, 198 insertions(+), 21 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 68bfb79..7028c5a 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
@@ -1,30 +1,48 @@
package com.ruoyi.goods.service.impl;
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
+import com.baomidou.mybatisplus.core.toolkit.Wrappers;
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.google.common.collect.Lists;
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.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.domain.GoodsSeckill;
-import com.ruoyi.system.api.domain.GoodsSku;
+import com.ruoyi.system.api.constants.NotificationTypeConstant;
+import com.ruoyi.system.api.domain.*;
+import com.ruoyi.system.api.domain.dto.AuctionGoodsListPageDTO;
+import com.ruoyi.system.api.domain.dto.HomeGoodsSkuDTO;
import com.ruoyi.system.api.domain.dto.ListStatusDTO;
+import com.ruoyi.system.api.domain.vo.AuctionGoodsListVO;
+import com.ruoyi.system.api.domain.vo.HomeGoodsSeckillInfoVO;
+import com.ruoyi.system.api.domain.vo.HomeGoodsSeckillVO;
+import com.ruoyi.system.api.feignClient.GoodsSkuClient;
import com.ruoyi.system.api.feignClient.OrderClient;
-import java.util.List;
-import java.util.Optional;
+import com.ruoyi.system.api.feignClient.SysUserClient;
+import com.ruoyi.system.api.util.WebSocketUsers;
+import java.time.LocalDateTime;
+import java.time.format.DateTimeFormatter;
+import java.util.*;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.stream.Collectors;
+
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
@@ -45,10 +63,12 @@
private final IGoodsSkuService goodsSkuService;
private final OrderClient orderClient;
+ private final GoodsSkuClient goodsSkuClient;
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) {
@@ -73,7 +93,7 @@
.set(GoodsSku::getStock, goodsSku.getStock() - seckillStock)
.ge(GoodsSku::getStock, seckillStock)
.eq(GoodsSku::getId, goodsSku.getId());
- asyncMethodService.seckillScheduleTask(goodsSeckill);
+ asyncMethodService.seckillScheduleTask(Lists.newArrayList(goodsSeckill));
}
}
@@ -102,11 +122,14 @@
//查询秒杀商品
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);
+ asyncMethodService.seckillScheduleTask(Lists.newArrayList(goodsSeckill));
}
/**
@@ -116,10 +139,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());
+ }
}
/**
@@ -132,12 +166,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;
}
@@ -148,12 +183,14 @@
* @param seckillId 秒杀id
*/
@Override
- public void startSeckill(Long seckillId) {
+ public void startSeckill(Long seckillId) throws JsonProcessingException {
log.info(">>>>>>>>>>>>>>>>>>>>{}秒杀开始<<<<<<<<<<<<<<<<<<<<", seckillId);
GoodsSeckill goodsSeckill = this.getById(seckillId);
- //秒杀商品不能为空且状态为未开始
+ // 秒杀商品不能为空且上架状态为上架中 状态为未开始
if (StringUtils.isNotNull(goodsSeckill)
- && goodsSeckill.getStartStatus().equals(StartStatusEnum.NOT_STARTED)) {
+ && goodsSeckill.getStartStatus().equals(StartStatusEnum.NOT_STARTED) &&
+ goodsSeckill.getListingStatus()
+ .equals(ListingStatusEnum.ON_SHELVES)) {
//开始秒杀
this.lambdaUpdate().set(GoodsSeckill::getStartStatus, StartStatusEnum.STARTED)
.eq(GoodsSeckill::getId, seckillId).update();
@@ -162,7 +199,15 @@
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("target_id", seckillId);
+ map.put("message_type", "start");
+ String msg = objectMapper.writeValueAsString(map);
+ WebSocketUsers.sendMessageToUsersByText(msg);
+ log.info("===================>发送websocket通知,消息体{}", msg);
}
/**
@@ -171,7 +216,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,6 +227,138 @@
// 将秒杀商品从缓存中移除
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("target_id", seckillId);
+ map.put("message_type", "end");
+ String msg = objectMapper.writeValueAsString(map);
+ WebSocketUsers.sendMessageToUsersByText(msg);
+ log.info("===================>发送websocket通知,消息体{}", msg);
+ }
+
+ @Override
+ public PageDTO<HomeGoodsSeckillVO> getHomeGoodsSeckillVOList(HomeGoodsSkuDTO homeGoodsSkuDTO) {
+ LambdaQueryWrapper<GoodsSku> wrapper3= Wrappers.lambdaQuery();
+ wrapper3.eq(GoodsSku::getDelFlag,0);
+ wrapper3.eq(GoodsSku::getListingStatus,0);
+ wrapper3.like(GoodsSku::getSkuName,homeGoodsSkuDTO.getSkuName());
+ wrapper3.eq(GoodsSku::getSkuName,homeGoodsSkuDTO.getSkuName());
+ wrapper3.eq(GoodsSku::getBrandId,homeGoodsSkuDTO.getBrandId());
+ wrapper3.eq(GoodsSku::getCategoryId,homeGoodsSkuDTO.getCategoryId());
+ wrapper3.eq(GoodsSku::getSeriesId,homeGoodsSkuDTO.getSeriesId());
+ wrapper3.eq(GoodsSku::getFlavorTypeId,homeGoodsSkuDTO.getFlavorTypeId());
+ if (homeGoodsSkuDTO.getSort()==2){
+ wrapper3.orderByAsc(GoodsSku::getPrice);
+ }
+ if (homeGoodsSkuDTO.getSort()==3){
+ wrapper3.orderByDesc(GoodsSku::getPrice);
+ }
+ if (homeGoodsSkuDTO.getSort()==4){
+ wrapper3.orderByAsc(GoodsSku::getSoldQuantity);
+ }
+ if (homeGoodsSkuDTO.getSort()==5){
+ wrapper3.orderByDesc(GoodsSku::getSoldQuantity);
+ }
+ if(homeGoodsSkuDTO.getYear()==2){
+ LocalDateTime now = LocalDateTime.now();
+ DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
+ LocalDateTime newTime7 = now.minusYears(30);
+ String formattedDate7 = newTime7.format(formatter);
+ LocalDateTime parseTime7 = LocalDateTime.parse(formattedDate7, formatter);
+ wrapper3.gt(GoodsSku::getYears,parseTime7);
+ }
+ if(homeGoodsSkuDTO.getYear()==3){
+ LocalDateTime now = LocalDateTime.now();
+ DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
+ LocalDateTime newTime7 = now.minusYears(30);
+ String formattedDate7 = newTime7.format(formatter);
+ LocalDateTime parseTime7 = LocalDateTime.parse(formattedDate7, formatter);
+
+ LocalDateTime newTime15 = now.minusYears(15);
+ String formattedDate15 = newTime15.format(formatter);
+ LocalDateTime parseTime15= LocalDateTime.parse(formattedDate15, formatter);
+ wrapper3.gt(GoodsSku::getYears, parseTime15)
+ .le(GoodsSku::getYears, parseTime7);
+ }
+
+ if(homeGoodsSkuDTO.getYear()==3){
+ LocalDateTime now = LocalDateTime.now();
+ DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
+ LocalDateTime newTime7 = now.minusYears(5);
+ String formattedDate7 = newTime7.format(formatter);
+ LocalDateTime parseTime7 = LocalDateTime.parse(formattedDate7, formatter);
+
+ LocalDateTime newTime15 = now.minusYears(15);
+ String formattedDate15 = newTime15.format(formatter);
+ LocalDateTime parseTime15= LocalDateTime.parse(formattedDate15, formatter);
+ wrapper3.ge(GoodsSku::getYears, parseTime7)
+ .le(GoodsSku::getYears, parseTime15);
+ }
+ List<GoodsSku> page1 = goodsSkuService.list(wrapper3);
+
+ Set<Long> goodsSkuIdList = null;
+ goodsSkuIdList = page1.stream().map(GoodsSku::getId)
+ .collect(Collectors.toSet());
+
+ Page<GoodsSeckill> page = new Page<>();
+ page.setSize(homeGoodsSkuDTO.getPageSize());
+ page.setCurrent(homeGoodsSkuDTO.getPageCurr());
+
+ LambdaQueryWrapper<GoodsSeckill> wrapper4= Wrappers.lambdaQuery();
+ wrapper4.eq(GoodsSeckill::getDelFlag,0);
+ wrapper4.eq(GoodsSeckill::getListingStatus,0);
+ List<Integer> in=new ArrayList<>();
+ in.add(0);
+ in.add(1);
+ wrapper4.in(GoodsSeckill::getStartStatus,in);
+ wrapper4.in(GoodsSeckill::getGoodsSkuId,goodsSkuIdList);
+ wrapper4.orderByAsc(GoodsSeckill::getSortNum);
+ Page<GoodsSeckill> page2 = this.page(page, wrapper4);
+
+ PageDTO<HomeGoodsSeckillVO> HomeGoodsSeckillVOPageDTO = PageDTO.of(page2, HomeGoodsSeckillVO.class);
+ List<HomeGoodsSeckillVO> list2 = HomeGoodsSeckillVOPageDTO.getList();
+ for (HomeGoodsSeckillVO homeGoodsSeckillVO:list2){
+ GoodsSku data = goodsSkuClient.getGoodsSkuOne(homeGoodsSeckillVO.getGoodsSkuId(), SecurityConstants.INNER).getData();
+ homeGoodsSeckillVO.setPrice(data.getPrice());
+ homeGoodsSeckillVO.setYears(String.valueOf(data.getYears().getYear()));
+ homeGoodsSeckillVO.setCoverPic(data.getCoverPic());
+ homeGoodsSeckillVO.setSkuName(data.getSkuName());
+ }
+ return HomeGoodsSeckillVOPageDTO;
+ }
+
+ @Override
+ public HomeGoodsSeckillInfoVO getHomeGoodsSeckillInfo(HomeGoodsSkuDTO homeGoodsSkuDTO) {
+ GoodsSeckill byId = this.getById(homeGoodsSkuDTO.getGoodsSkuId());
+ GoodsSku data6 = goodsSkuClient.getGoodsSkuOne(byId.getGoodsSkuId(), SecurityConstants.INNER).getData();
+ HomeGoodsSeckillInfoVO homeGoodsSeckillInfoVO=new HomeGoodsSeckillInfoVO();
+ homeGoodsSeckillInfoVO.setGoodsSkuId(byId.getId());
+ homeGoodsSeckillInfoVO.setSkuName(data6.getSkuName());
+ homeGoodsSeckillInfoVO.setYears(String.valueOf(data6.getYears().getYear()));
+ GoodsBrand data = goodsSkuClient.getBrandOne(data6.getBrandId(), SecurityConstants.INNER).getData();
+ GoodsCategory data1 = goodsSkuClient.getCategoryOne(data6.getCategoryId(), SecurityConstants.INNER).getData();
+ GoodsSeries data2 = goodsSkuClient.getSeriesOne(data6.getSeriesId(), SecurityConstants.INNER).getData();
+ GoodsFlavorType data3 = goodsSkuClient.getFlavorTypeOne(data6.getFlavorTypeId(), SecurityConstants.INNER).getData();
+ homeGoodsSeckillInfoVO.setBrand(data.getBrandName());
+ homeGoodsSeckillInfoVO.setCategory(data1.getCategoryName());
+ homeGoodsSeckillInfoVO.setSeries(data2.getSeriesName());
+ homeGoodsSeckillInfoVO.setFlavorType(data3.getFlavorTypeName());
+ homeGoodsSeckillInfoVO.setPrice(data6.getPrice());
+ homeGoodsSeckillInfoVO.setSoldQuantity(byId.getSoldQuantity());
+ homeGoodsSeckillInfoVO.setUnit(data6.getUnit());
+ homeGoodsSeckillInfoVO.setSpecUnit(data6.getSpecUnit());
+ homeGoodsSeckillInfoVO.setCoverPic(data6.getCoverPic());
+ homeGoodsSeckillInfoVO.setAlbum(data6.getAlbum());
+ homeGoodsSeckillInfoVO.setShareTitle(byId.getShareTitle());
+ homeGoodsSeckillInfoVO.setSeckillPrice(byId.getSeckillPrice());
+ homeGoodsSeckillInfoVO.setDescription(data6.getDescription());
+ homeGoodsSeckillInfoVO.setDetail(data6.getDetail());
+ homeGoodsSeckillInfoVO.setLimitNumber(byId.getLimitNumber());
+ homeGoodsSeckillInfoVO.setSeckillPrice(byId.getSeckillPrice());
+ homeGoodsSeckillInfoVO.setStartTime(byId.getStartTime());
+ homeGoodsSeckillInfoVO.setEndTime(byId.getEndTime());
+ homeGoodsSeckillInfoVO.setStartStatus(byId.getStartStatus());
+ return homeGoodsSeckillInfoVO;
}
}
--
Gitblit v1.7.1