rentaiming
2024-06-13 fe2d5b14031edbe43238770fb1fc21e8a322b51a
ruoyi-modules/ruoyi-goods/src/main/java/com/ruoyi/goods/service/impl/GoodsSkuServiceImpl.java
@@ -1,9 +1,12 @@
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.ruoyi.common.core.constant.SecurityConstants;
import com.ruoyi.common.core.enums.ListingStatusEnum;
import com.ruoyi.common.core.exception.ServiceException;
import com.ruoyi.common.core.utils.StringUtils;
@@ -14,21 +17,42 @@
import com.ruoyi.goods.controller.management.dto.GoodsInfoTitleValueDTO;
import com.ruoyi.goods.controller.management.dto.GoodsSkuDTO;
import com.ruoyi.goods.controller.management.dto.GoodsSkuQuery;
import com.ruoyi.goods.controller.management.vo.GoodsInfoTitleValueVO;
import com.ruoyi.goods.controller.management.vo.GoodsSkuVO;
import com.ruoyi.goods.domain.GoodsGroupPurchase;
import com.ruoyi.goods.domain.GoodsInfoTitleValue;
import com.ruoyi.goods.domain.MemberGoodsCollection;
import com.ruoyi.goods.mapper.GoodsGroupPurchaseMapper;
import com.ruoyi.goods.mapper.GoodsSeckillMapper;
import com.ruoyi.goods.mapper.GoodsSkuMapper;
import com.ruoyi.goods.service.IGoodsGroupPurchaseService;
import com.ruoyi.goods.service.IGoodsInfoTitleValueService;
import com.ruoyi.goods.service.IGoodsSeckillService;
import com.ruoyi.goods.service.IGoodsSkuService;
import com.ruoyi.goods.service.IMemberGoodsCollectionService;
import com.ruoyi.system.api.domain.GoodsBrand;
import com.ruoyi.system.api.domain.GoodsCategory;
import com.ruoyi.system.api.domain.GoodsFlavorType;
import com.ruoyi.system.api.domain.GoodsGroupPurchase;
import com.ruoyi.system.api.domain.GoodsSeckill;
import com.ruoyi.system.api.domain.GoodsSeries;
import com.ruoyi.system.api.domain.GoodsSku;
import com.ruoyi.system.api.domain.dto.GoodsStockUpdDTO;
import com.ruoyi.system.api.domain.dto.HomeGoodsSkuDTO;
import com.ruoyi.system.api.domain.dto.ListStatusDTO;
import java.util.List;
import java.util.Objects;
import com.ruoyi.system.api.domain.vo.HomeGoodsSkuInfoVO;
import com.ruoyi.system.api.domain.vo.HomeGoodsSkuListVO;
import com.ruoyi.system.api.domain.vo.getHomeGoodsSkuXxiVO;
import com.ruoyi.system.api.feignClient.GoodsSkuClient;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import javax.annotation.Resource;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
@@ -47,8 +71,17 @@
public class GoodsSkuServiceImpl extends ServiceImpl<GoodsSkuMapper, GoodsSku> implements IGoodsSkuService {
    private final IGoodsInfoTitleValueService goodsInfoTitleValueService;
    private final IGoodsSeckillService goodsSeckillService;
    private final IGoodsGroupPurchaseService goodsGroupPurchaseService;
    private final GoodsSeckillMapper goodsSeckillMapper;
    private final GoodsGroupPurchaseMapper goodsGroupPurchaseMapper;
    private final RedissonClient redissonClient;
    @Resource
    private  GoodsSkuClient goodsSkuClient;
    @Resource
    private IMemberGoodsCollectionService iMemberGoodsCollectionService;
    private static final ObjectMapper objectMapper = new ObjectMapper();
    /**
@@ -177,17 +210,26 @@
    }
    private void updateGoodsStatus(ListStatusDTO dto) {
        goodsSeckillService.lambdaUpdate()
                .set(GoodsSeckill::getListingStatus,
                        ListingStatusEnum.REMOVED_FROM_THE_SHELF)
                .eq(GoodsSeckill::getListingStatus, ListingStatusEnum.ON_SHELVES)
                .eq(GoodsSeckill::getGoodsSkuId, dto.getId()).update();
        goodsGroupPurchaseService.lambdaUpdate()
                .set(GoodsGroupPurchase::getListingStatus,
                        ListingStatusEnum.REMOVED_FROM_THE_SHELF)
                .eq(GoodsGroupPurchase::getListingStatus,
        LambdaQueryWrapper<GoodsSeckill> queryWrapper =new LambdaQueryWrapper<>();
        queryWrapper.eq(GoodsSeckill::getListingStatus, ListingStatusEnum.ON_SHELVES)
                .eq(GoodsSeckill::getGoodsSkuId, dto.getId());
        List<GoodsSeckill> goodsSeckillList = goodsSeckillMapper.selectList(queryWrapper);
        if (StringUtils.isNotNull(goodsSeckillList)) {
            for (GoodsSeckill goodsSeckill : goodsSeckillList) {
                goodsSeckill.setListingStatus(ListingStatusEnum.REMOVED_FROM_THE_SHELF);
                goodsSeckillMapper.updateById(goodsSeckill);
            }
        }
        LambdaQueryWrapper<GoodsGroupPurchase> queryWrapper1 =new LambdaQueryWrapper<>();
        queryWrapper1.eq(GoodsGroupPurchase::getListingStatus,
                        ListingStatusEnum.ON_SHELVES)
                .eq(GoodsGroupPurchase::getGoodsSkuId, dto.getId()).update();
                .eq(GoodsGroupPurchase::getGoodsSkuId, dto.getId());
        GoodsGroupPurchase goodsGroupPurchase = goodsGroupPurchaseMapper.selectOne(queryWrapper1);
        if (StringUtils.isNotNull(goodsGroupPurchase)) {
            goodsGroupPurchase.setListingStatus(ListingStatusEnum.REMOVED_FROM_THE_SHELF);
            goodsGroupPurchaseMapper.updateById(goodsGroupPurchase);
        }
    }
    /**
@@ -203,37 +245,276 @@
                .list();
    }
    /**
     * 扣减商品库存
     * 批量更新商品库存 auctionStock 负数则为减库存,正数为加库存
     *
     * @param goodsSkuId   商品SKU ID
     * @param auctionStock 拍卖库存
     * @param goodsStockUpdDTOS 商品库存修改数据传输对象
     */
    @Override
    public void deductStock(Long goodsSkuId, Integer auctionStock) {
        GoodsSku goodsSku = this.getById(goodsSkuId);
        if (StringUtils.isNull(goodsSku)) {
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public void updGoodsStock(List<GoodsStockUpdDTO> goodsStockUpdDTOS) {
        List<Long> goodsSkuIdList = goodsStockUpdDTOS.stream()
                .map(GoodsStockUpdDTO::getGoodsSkuId)
                .collect(Collectors.toList());
        List<GoodsSku> goodsSkus = this.listByIds(goodsSkuIdList);
        if (StringUtils.isEmpty(goodsSkus)) {
            throw new ServiceException("商品不存在");
        }
        if (goodsSku.getStock() < auctionStock) {
            throw new ServiceException("库存不足");
        Map<Long, Integer> stockMap = goodsStockUpdDTOS.stream()
                .collect(Collectors.toMap(GoodsStockUpdDTO::getGoodsSkuId,
                        GoodsStockUpdDTO::getAuctionStock));
        for (GoodsSku skus : goodsSkus) {
            String goodsLock = "goods_lock:" + skus.getId();
            RLock redissonLock = redissonClient.getLock(goodsLock);
            try {
                redissonLock.lock(30, TimeUnit.SECONDS);
                Integer auctionStock = stockMap.get(skus.getId());
                if (StringUtils.isNotNull(auctionStock) && auctionStock > 0) {
                    // 更新商品库存,更新五次失败抛出异常
                    boolean isUpdated = false;
                    for (int i = 0; i < 5; i++) {
                        isUpdated = updateGoodsStock(skus, auctionStock);
                        if (isUpdated) {
                            break;
                        } else {
                            skus = this.getById(skus.getId());
                        }
                    }
                    if (!isUpdated) {
                        throw new ServiceException("商品库存回退失败");
                    }
                }
            } finally {
                redissonLock.unlock();
            }
        }
        // 更新商品库存
        this.lambdaUpdate().set(GoodsSku::getStock, goodsSku.getStock() - auctionStock)
                .ge(GoodsSku::getStock, auctionStock).eq(GoodsSku::getId, goodsSku.getId())
                .update();
    }
    @Override
    public void returningStock(Long goodsSkuId, Integer auctionStock) {
        GoodsSku goodsSku = this.getById(goodsSkuId);
    public PageDTO<HomeGoodsSkuListVO> getHomeGoodsSkuVOList(HomeGoodsSkuDTO homeGoodsSkuDTO) {
        Page<GoodsSku> page = new Page<>();
        page.setSize(homeGoodsSkuDTO.getPageSize());
        page.setCurrent(homeGoodsSkuDTO.getPageCurr());
        LambdaQueryWrapper<GoodsSku> wrapper3= Wrappers.lambdaQuery();
        wrapper3.eq(GoodsSku::getDelFlag,0);
        wrapper3.eq(GoodsSku::getListingStatus,0);
        if (homeGoodsSkuDTO.getSkuName()!=null){
            wrapper3.like(GoodsSku::getSkuName,homeGoodsSkuDTO.getSkuName());
        }
        if (homeGoodsSkuDTO.getBrandId()!=null){
            wrapper3.eq(GoodsSku::getBrandId,homeGoodsSkuDTO.getBrandId());
        }
        if (homeGoodsSkuDTO.getCategoryId()!=null){
            wrapper3.eq(GoodsSku::getCategoryId,homeGoodsSkuDTO.getCategoryId());
        }
        if (homeGoodsSkuDTO.getSeriesId()!=null){
            wrapper3.eq(GoodsSku::getSeriesId,homeGoodsSkuDTO.getSeriesId());
        }
        if (homeGoodsSkuDTO.getFlavorTypeId()!=null){
            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 = LocalDate.parse(formattedDate7).atStartOfDay();
            wrapper3.le(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 = LocalDate.parse(formattedDate7).atStartOfDay();
            LocalDateTime newTime15 = now.minusYears(15);
            String formattedDate15 = newTime15.format(formatter);
            LocalDateTime parseTime15= LocalDate.parse(formattedDate15).atStartOfDay();
            wrapper3.le(GoodsSku::getYears, parseTime15)
                    .gt(GoodsSku::getYears, parseTime7);
        }
        if(homeGoodsSkuDTO.getYear()==4){
            LocalDateTime now = LocalDateTime.now();
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
            LocalDateTime newTime7 = now.minusYears(5);
            String formattedDate7 = newTime7.format(formatter);
            LocalDateTime parseTime7 = LocalDate.parse(formattedDate7).atStartOfDay();
            LocalDateTime newTime15 = now.minusYears(15);
            String formattedDate15 = newTime15.format(formatter);
            LocalDateTime parseTime15= LocalDate.parse(formattedDate15).atStartOfDay();
            wrapper3.le(GoodsSku::getYears, parseTime7)
                    .gt(GoodsSku::getYears, parseTime15);
        }
        if(homeGoodsSkuDTO.getYear()==5){
            LocalDateTime now = LocalDateTime.now();
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
            LocalDateTime newTime7 = now.minusYears(1);
            String formattedDate7 = newTime7.format(formatter);
            LocalDateTime parseTime7 = LocalDate.parse(formattedDate7).atStartOfDay();
            LocalDateTime newTime15 = now.minusYears(5);
            String formattedDate15 = newTime15.format(formatter);
            LocalDateTime parseTime15= LocalDate.parse(formattedDate15).atStartOfDay();
            wrapper3.le(GoodsSku::getYears, parseTime7)
                    .ge(GoodsSku::getYears, parseTime15);
        }
        wrapper3.orderByAsc(GoodsSku::getSortNum);
        Page<GoodsSku> page1 = this.page(page, wrapper3);
        PageDTO<HomeGoodsSkuListVO> articleCommentsVOPageDTO = PageDTO.of(page1, HomeGoodsSkuListVO.class);
        return  articleCommentsVOPageDTO;
    }
    @Override
    public HomeGoodsSkuInfoVO getHomeGoodsSkuInfo(HomeGoodsSkuDTO homeGoodsSkuDTO) {
        GoodsSku byId = this.getById(homeGoodsSkuDTO.getGoodsSkuId());
        HomeGoodsSkuInfoVO homeGoodsSkuInfoVO=new HomeGoodsSkuInfoVO();
        homeGoodsSkuInfoVO.setId(byId.getId());
        homeGoodsSkuInfoVO.setSkuName(byId.getSkuName());
        homeGoodsSkuInfoVO.setYears(String.valueOf(byId.getYears().getYear()));
        GoodsBrand data = goodsSkuClient.getBrandOne(byId.getBrandId(), SecurityConstants.INNER).getData();
        GoodsCategory data1 = goodsSkuClient.getCategoryOne(byId.getCategoryId(), SecurityConstants.INNER).getData();
        GoodsSeries data2 = goodsSkuClient.getSeriesOne(byId.getSeriesId(), SecurityConstants.INNER).getData();
        GoodsFlavorType data3 = goodsSkuClient.getFlavorTypeOne(byId.getFlavorTypeId(), SecurityConstants.INNER).getData();
        homeGoodsSkuInfoVO.setBrand(data.getBrandName());
        homeGoodsSkuInfoVO.setCategory(data1.getCategoryName());
        homeGoodsSkuInfoVO.setSeries(data2.getSeriesName());
        homeGoodsSkuInfoVO.setFlavorType(data3.getFlavorTypeName());
        homeGoodsSkuInfoVO.setPrice(byId.getPrice());
        homeGoodsSkuInfoVO.setSoldQuantity(byId.getSoldQuantity());
        homeGoodsSkuInfoVO.setUnit(byId.getUnit());
        homeGoodsSkuInfoVO.setSpec(byId.getSpec());
        homeGoodsSkuInfoVO.setSpecUnit(byId.getSpecUnit());
        homeGoodsSkuInfoVO.setCoverPic(byId.getCoverPic());
        homeGoodsSkuInfoVO.setAlbum(byId.getAlbum());
        homeGoodsSkuInfoVO.setDescription(byId.getDescription());
        homeGoodsSkuInfoVO.setDetail(byId.getDetail());
        homeGoodsSkuInfoVO.setShareTitle(byId.getShareTitle());
        homeGoodsSkuInfoVO.setSharePic(byId.getSharePic());
        LambdaQueryWrapper<GoodsGroupPurchase> queryWrapper1 =new LambdaQueryWrapper<>();
        queryWrapper1.eq(GoodsGroupPurchase::getListingStatus,
                        ListingStatusEnum.ON_SHELVES)
                .eq(GoodsGroupPurchase::getGoodsSkuId, byId.getId());
        GoodsGroupPurchase goodsGroupPurchase = goodsGroupPurchaseMapper.selectOne(queryWrapper1);
        if(goodsGroupPurchase!=null){
            homeGoodsSkuInfoVO.setGroupPurchaseGoods(2);
        }else {
            homeGoodsSkuInfoVO.setGroupPurchaseGoods(1);
        }
        LambdaQueryWrapper<MemberGoodsCollection> wrapper3= Wrappers.lambdaQuery();
        wrapper3.eq(MemberGoodsCollection::getDelFlag,0);
        wrapper3.eq(MemberGoodsCollection::getMemberId,homeGoodsSkuDTO.getMemberId());
        wrapper3.eq(MemberGoodsCollection::getTargetId,homeGoodsSkuDTO.getGoodsSkuId());
        List<MemberGoodsCollection> list = iMemberGoodsCollectionService.list(wrapper3);
        if (list.size()>0){
            homeGoodsSkuInfoVO.setIsCollection(2);
        }else{
            homeGoodsSkuInfoVO.setIsCollection(1);
        }
        LambdaQueryWrapper<GoodsGroupPurchase> queryWrapper1 =new LambdaQueryWrapper<>();
        queryWrapper1.eq(GoodsGroupPurchase::getListingStatus,
                        ListingStatusEnum.ON_SHELVES)
                .eq(GoodsGroupPurchase::getGoodsSkuId, byId.getId());
        GoodsGroupPurchase goodsGroupPurchase = goodsGroupPurchaseMapper.selectOne(queryWrapper1);
        if (goodsGroupPurchase!=null){
            homeGoodsSkuInfoVO.setIsGoodsGroupPurchase(2);
        }else{
            homeGoodsSkuInfoVO.setIsGoodsGroupPurchase(1);
        }
        return homeGoodsSkuInfoVO;
    }
    @Override
    public List<getHomeGoodsSkuXxiVO> getHomeGoodsSkuXxi(HomeGoodsSkuDTO homeGoodsSkuDTO) {
        List<getHomeGoodsSkuXxiVO> homeGoodsSkuXxiVOS=new ArrayList<>();
        GoodsSku byId = this.getById(homeGoodsSkuDTO.getGoodsSkuId());
        GoodsBrand data = goodsSkuClient.getBrandOne(byId.getBrandId(), SecurityConstants.INNER).getData();
        GoodsCategory data1 = goodsSkuClient.getCategoryOne(byId.getCategoryId(), SecurityConstants.INNER).getData();
        GoodsSeries data2 = goodsSkuClient.getSeriesOne(byId.getSeriesId(), SecurityConstants.INNER).getData();
        GoodsFlavorType data3 = goodsSkuClient.getFlavorTypeOne(byId.getFlavorTypeId(), SecurityConstants.INNER).getData();
        getHomeGoodsSkuXxiVO getHomeGoodsSkuXxiVO1=new getHomeGoodsSkuXxiVO();
        getHomeGoodsSkuXxiVO1.setContent(data1.getCategoryName());
        getHomeGoodsSkuXxiVO1.setTitleName("分类");
        homeGoodsSkuXxiVOS.add(getHomeGoodsSkuXxiVO1);
        getHomeGoodsSkuXxiVO getHomeGoodsSkuXxiVO2=new getHomeGoodsSkuXxiVO();
        getHomeGoodsSkuXxiVO2.setContent(data2.getSeriesName());
        getHomeGoodsSkuXxiVO2.setTitleName("系列");
        homeGoodsSkuXxiVOS.add(getHomeGoodsSkuXxiVO2);
        getHomeGoodsSkuXxiVO getHomeGoodsSkuXxiVO=new getHomeGoodsSkuXxiVO();
        getHomeGoodsSkuXxiVO.setContent(data.getBrandName());
        getHomeGoodsSkuXxiVO.setTitleName("品牌");
        homeGoodsSkuXxiVOS.add(getHomeGoodsSkuXxiVO);
        getHomeGoodsSkuXxiVO getHomeGoodsSkuXxiVO3=new getHomeGoodsSkuXxiVO();
        getHomeGoodsSkuXxiVO3.setContent(data3.getFlavorTypeName());
        getHomeGoodsSkuXxiVO3.setTitleName("香型");
        homeGoodsSkuXxiVOS.add(getHomeGoodsSkuXxiVO3);
        List<GoodsInfoTitleValueVO> goodsInfoTitleValueVOList = goodsInfoTitleValueService.listByGoodsId(homeGoodsSkuDTO.getGoodsSkuId());
        Collections.sort(goodsInfoTitleValueVOList, Comparator.comparingInt(GoodsInfoTitleValueVO::getSortNum));
        for (GoodsInfoTitleValueVO goodsInfoTitleValueVO:goodsInfoTitleValueVOList){
            getHomeGoodsSkuXxiVO getHomeGoodsSkuXxiVOs=new getHomeGoodsSkuXxiVO();
            getHomeGoodsSkuXxiVOs.setContent(goodsInfoTitleValueVO.getContent());
            getHomeGoodsSkuXxiVOs.setTitleName(goodsInfoTitleValueVO.getTitleName());
            homeGoodsSkuXxiVOS.add(getHomeGoodsSkuXxiVOs);
        }
        return homeGoodsSkuXxiVOS;
    }
    private boolean updateGoodsStock(GoodsSku skus, Integer auctionStock) {
        return this.lambdaUpdate()
                .set(skus.getStock() + auctionStock > 0, GoodsSku::getStock,
                        skus.getStock() + auctionStock)
                .eq(GoodsSku::getId, skus.getId())
                .eq(GoodsSku::getStock, skus.getStock())
                .update();
    }
    /**
     * 根据商品id查询信息标题列表
     *
     * @param id 商品id
     * @return List<GoodsInfoTitleValueVO>
     */
    @Override
    public GoodsSkuVO getGoodsDetail(Long id) {
        GoodsSku goodsSku = this.getById(id);
        if (StringUtils.isNull(goodsSku)) {
            throw new ServiceException("商品不存在");
        }
        // 更新商品库存
        this.lambdaUpdate()
                .set(auctionStock > 0, GoodsSku::getStock, goodsSku.getStock() + auctionStock)
                .eq(GoodsSku::getId, goodsSku.getId())
                .update();
        GoodsSkuVO goodsSkuVO = BeanUtils.copyBean(goodsSku, GoodsSkuVO.class);
        List<GoodsInfoTitleValueVO> goodsInfoTitleValueVOList = goodsInfoTitleValueService.listByGoodsId(
                id);
        goodsSkuVO.setGoodsInfoTitleValueVOList(goodsInfoTitleValueVOList);
        return goodsSkuVO;
    }
}