| | |
| | | import com.ruoyi.goods.service.IGoodsSkuService; |
| | | import com.ruoyi.system.api.domain.GoodsSeckill; |
| | | import com.ruoyi.system.api.domain.GoodsSku; |
| | | import com.ruoyi.system.api.domain.dto.GoodsStockUpdDTO; |
| | | import com.ruoyi.system.api.domain.dto.ListStatusDTO; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | import java.util.Objects; |
| | | import java.util.stream.Collectors; |
| | | import lombok.RequiredArgsConstructor; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.redisson.api.RedissonClient; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.transaction.annotation.Propagation; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | |
| | | private final IGoodsInfoTitleValueService goodsInfoTitleValueService; |
| | | private final IGoodsSeckillService goodsSeckillService; |
| | | private final IGoodsGroupPurchaseService goodsGroupPurchaseService; |
| | | private final RedissonClient redissonClient; |
| | | |
| | | private static final ObjectMapper objectMapper = new ObjectMapper(); |
| | | |
| | | /** |
| | |
| | | .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 returningStock(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) { |
| | | 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("商品库存回退失败"); |
| | | } |
| | | } |
| | | } |
| | | // 更新商品库存 |
| | | 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); |
| | | if (StringUtils.isNull(goodsSku)) { |
| | | throw new ServiceException("商品不存在"); |
| | | } |
| | | // 更新商品库存 |
| | | this.lambdaUpdate() |
| | | .set(auctionStock > 0, GoodsSku::getStock, goodsSku.getStock() + auctionStock) |
| | | .eq(GoodsSku::getId, goodsSku.getId()) |
| | | private boolean updateGoodsStock(GoodsSku skus, Integer auctionStock) { |
| | | return this.lambdaUpdate() |
| | | .set(GoodsSku::getStock, skus.getStock() + auctionStock) |
| | | .eq(GoodsSku::getId, skus.getId()) |
| | | .eq(GoodsSku::getStock, skus.getStock()) |
| | | .update(); |
| | | } |
| | | } |