Browse Source

商品库存加锁

LuoDLeo 2 months ago
parent
commit
fb755f13b0

+ 6 - 0
pom.xml

@@ -322,6 +322,12 @@
             <artifactId>java-sdk</artifactId>
             <version>4.16.19</version>
         </dependency>
+
+        <dependency>
+            <groupId>org.redisson</groupId>
+            <artifactId>redisson</artifactId>
+            <version>3.20.1</version>
+        </dependency>
     </dependencies>
 
     <build>

+ 130 - 114
src/main/java/org/springblade/modules/finance/service/impl/OrderGoodsServiceImpl.java

@@ -6,7 +6,10 @@ import com.baomidou.mybatisplus.core.metadata.IPage;
 import com.baomidou.mybatisplus.core.toolkit.Wrappers;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 import lombok.AllArgsConstructor;
+import org.redisson.api.RLock;
+import org.redisson.api.RedissonClient;
 import org.springblade.common.enums.YesOrNoEnum;
+import org.springblade.core.log.exception.ServiceException;
 import org.springblade.modules.api.request.SubmitOrderRequest;
 import org.springblade.modules.api.service.SubmitOrderService;
 import org.springblade.modules.business.entity.FacilityCargoWay;
@@ -32,6 +35,7 @@ import org.springframework.transaction.annotation.Transactional;
 import java.math.BigDecimal;
 import java.util.Date;
 import java.util.List;
+import java.util.concurrent.TimeUnit;
 
 /**
  * 商品订单表 服务实现类
@@ -43,120 +47,132 @@ import java.util.List;
 @Service("orderGoodsServiceImpl")
 public class OrderGoodsServiceImpl extends ServiceImpl<OrderGoodsMapper, OrderGoods> implements IOrderGoodsService, SubmitOrderService {
 
-	private final IOrderService iOrderService;
-	private final IGoodsInfoService iGoodsInfoService;
-	private final IFacilityCargoWayService iFacilityCargoWayService;
-	private final IShopService iShopService;
-	private final IUserService iUserService;
-	private final IAfterSalesService afterSalesService;
-
-	@Override
-	public IPage<OrderGoodsVO> selectOrderGoodsPage(IPage<OrderGoodsVO> page, OrderGoodsVO orderGoods) {
-		return page.setRecords(baseMapper.selectOrderGoodsPage(page, orderGoods));
-	}
-
-	@Override
-	public IPage<OrderGoodsVO> selectUserOrder(IPage<OrderGoodsVO> page, OrderGoodsVO orderGoods) {
-		List<OrderGoodsVO> orderGoodsVOS = baseMapper.selectUserOrder(page, orderGoods);
-		for (OrderGoodsVO order : orderGoodsVOS) {
-			if (YesOrNoEnum.YES.getValue().equals(order.getIsAfterSales())){
-				LambdaQueryWrapper<AfterSales> q = new LambdaQueryWrapper<>();
-				q.eq(AfterSales::getOrderId,order.getOrderId());
-				AfterSales afterSales = afterSalesService.getOne(q);
-				order.setAfterSales(afterSales);
-			}
-		}
-		return page.setRecords(orderGoodsVOS);
-	}
-
-	@Override
-	public IPage<MerchantPageOrderVO> getMerchantOrder(IPage page, MerchantPageOrderVO merchantPageOrderVO){
-		return page.setRecords(baseMapper.getMerchantOrder(page, merchantPageOrderVO));
-	}
-
-	@Override
-	public String getMerchantOrderNum(Long merchantId){
-		return baseMapper.getMerchantOrderNum(merchantId);
-	}
-
-	@Transactional
-	@Override
-	public String submitOrder(SubmitOrderRequest request) {
-		GoodsInfo goods = iGoodsInfoService.getById(request.getGoodsInfoId());
-		Assert.notNull(goods, "商品信息不存在");
-		FacilityCargoWay cargoWay = iFacilityCargoWayService.getById(request.getCargoWayId());
-		Assert.notNull(cargoWay, "货道信息不存在");
-		Shop shop = iShopService.getById(request.getShopId());
-		Assert.notNull(shop, "门店信息不存在");
-		User user = iUserService.getById(request.getUserId());
-
-		// 订单初始化
-		Order order = iOrderService.init();
-		order.setOrderType(request.getOrderType());
-		order.setOrderType(request.getOrderType());
-		order.setSource(request.getOrderSource());
-		order.setUserId(request.getUserId());
-		// 订单总金额
-		order.setTotalAmount(cargoWay.getSalePrice().multiply(BigDecimal.valueOf(request.getGoodsNum())));
-		order.setDiscountAmount(BigDecimal.valueOf(cargoWay.getDiscount()).multiply(BigDecimal.valueOf(request.getGoodsNum())));
-		// 实付金额
-		order.setPaymentAmount(order.getTotalAmount().subtract(order.getDiscountAmount()));
-		order.setConsignee(user.getName());
-		order.setConsigneePhoneNumber(user.getPhone());
-		order.setImage(goods.getGoodsCover());
-		iOrderService.save(order);
-
-		OrderGoods orderGoods = new OrderGoods();
-		orderGoods.setOrderId(order.getId());
-		orderGoods.setGoodsNum(request.getGoodsNum());
-		orderGoods.setShipmentNum(0);
-		orderGoods.setDeviceId(request.getDeviceId());
-		orderGoods.setGoodsName(goods.getGoodsName());
-		orderGoods.setCargoWayId(request.getCargoWayId());
-		orderGoods.setCargoWayNo(request.getCargoWayNo());
-		//orderGoods.setShippingTime(LocalDateTime.now());
-		orderGoods.setGoodsNo(goods.getGoodsNo());
-		orderGoods.setGoodsImage(goods.getGoodsCover());
-		orderGoods.setGoodsId(request.getGoodsInfoId());
-		orderGoods.setGoodsPrice(cargoWay.getSalePrice().subtract(BigDecimal.valueOf(cargoWay.getDiscount())));
-		orderGoods.setMerchantId(shop.getMerchantId());
-		orderGoods.setShopId(request.getShopId());
-		orderGoods.setUserId(request.getUserId());
-		orderGoods.setStoreName(shop.getStoreName());
-		baseMapper.insert(orderGoods);
-
-		// 扣减货道库存
-		cargoWay.setGoodsStore(cargoWay.getGoodsStore() - request.getGoodsNum());
-		iFacilityCargoWayService.updateById(cargoWay);
-		return order.getOrderNo();
-	}
-
-
-	@Override
-	public void releaseGoodsStock(Order order) {
-		OrderGoods orderGoods = baseMapper.selectOne(Wrappers.lambdaQuery(OrderGoods.class).eq(OrderGoods::getOrderId, order.getId()));
-		// 更新货道库存
-		FacilityCargoWay cargoWay = iFacilityCargoWayService.getById(orderGoods.getCargoWayId());
-		cargoWay.setGoodsStore(cargoWay.getGoodsStore() + orderGoods.getGoodsNum());
-		iFacilityCargoWayService.updateById(cargoWay);
-	}
-
-	@Override
-	public void paySuccessAfter(Order order) {
-		OrderGoods orderGoods = baseMapper.selectOne(Wrappers.lambdaQuery(OrderGoods.class).eq(OrderGoods::getOrderId, order.getId()));
-		// 更新货道销售信息
-		FacilityCargoWay cargoWay = iFacilityCargoWayService.getById(orderGoods.getCargoWayId());
-		cargoWay.setTotalTrade(cargoWay.getTotalTrade() + orderGoods.getGoodsNum());
-		cargoWay.setTotalAmount(cargoWay.getTotalAmount().add(order.getPaymentAmount()));
-		iFacilityCargoWayService.updateById(cargoWay);
-	}
-
-    public List<OrderGoods> getMerchantOrder(Long userId, Date startDate, Date endDate){
-       return this.lambdaQuery()
-               .eq(OrderGoods::getMerchantId,userId)
-               .eq(OrderGoods::getIsDelete,0)
-               .between(OrderGoods::getCreateTime,startDate,endDate)
-               .list();
+    private final IOrderService iOrderService;
+    private final IGoodsInfoService iGoodsInfoService;
+    private final IFacilityCargoWayService iFacilityCargoWayService;
+    private final IShopService iShopService;
+    private final IUserService iUserService;
+    private final IAfterSalesService afterSalesService;
+    private final RedissonClient redissonClient;
+
+    @Override
+    public IPage<OrderGoodsVO> selectOrderGoodsPage(IPage<OrderGoodsVO> page, OrderGoodsVO orderGoods) {
+        return page.setRecords(baseMapper.selectOrderGoodsPage(page, orderGoods));
+    }
+
+    @Override
+    public IPage<OrderGoodsVO> selectUserOrder(IPage<OrderGoodsVO> page, OrderGoodsVO orderGoods) {
+        List<OrderGoodsVO> orderGoodsVOS = baseMapper.selectUserOrder(page, orderGoods);
+        for (OrderGoodsVO order : orderGoodsVOS) {
+            if (YesOrNoEnum.YES.getValue().equals(order.getIsAfterSales())) {
+                LambdaQueryWrapper<AfterSales> q = new LambdaQueryWrapper<>();
+                q.eq(AfterSales::getOrderId, order.getOrderId());
+                AfterSales afterSales = afterSalesService.getOne(q);
+                order.setAfterSales(afterSales);
+            }
+        }
+        return page.setRecords(orderGoodsVOS);
+    }
+
+    @Override
+    public IPage<MerchantPageOrderVO> getMerchantOrder(IPage page, MerchantPageOrderVO merchantPageOrderVO) {
+        return page.setRecords(baseMapper.getMerchantOrder(page, merchantPageOrderVO));
+    }
+
+    @Override
+    public String getMerchantOrderNum(Long merchantId) {
+        return baseMapper.getMerchantOrderNum(merchantId);
+    }
+
+    @Transactional
+    @Override
+    public String submitOrder(SubmitOrderRequest request) {
+        RLock lock = redissonClient.getLock("order:" + request.getGoodsInfoId());
+        try {
+            lock.lock(10, TimeUnit.SECONDS);
+            GoodsInfo goods = iGoodsInfoService.getById(request.getGoodsInfoId());
+            Assert.notNull(goods, "商品信息不存在");
+            FacilityCargoWay cargoWay = iFacilityCargoWayService.getById(request.getCargoWayId());
+            //库存校验
+            Assert.isTrue(cargoWay.getGoodsStore() < 1 || cargoWay.getGoodsStore() < request.getGoodsNum(), "商品库存不足");
+            Assert.notNull(cargoWay, "货道信息不存在");
+            Shop shop = iShopService.getById(request.getShopId());
+            Assert.notNull(shop, "门店信息不存在");
+            User user = iUserService.getById(request.getUserId());
+
+            // 订单初始化
+            Order order = iOrderService.init();
+            order.setOrderType(request.getOrderType());
+            order.setOrderType(request.getOrderType());
+            order.setSource(request.getOrderSource());
+            order.setUserId(request.getUserId());
+            // 订单总金额
+            order.setTotalAmount(cargoWay.getSalePrice().multiply(BigDecimal.valueOf(request.getGoodsNum())));
+            order.setDiscountAmount(BigDecimal.valueOf(cargoWay.getDiscount()).multiply(BigDecimal.valueOf(request.getGoodsNum())));
+            // 实付金额
+            order.setPaymentAmount(order.getTotalAmount().subtract(order.getDiscountAmount()));
+            order.setConsignee(user.getName());
+            order.setConsigneePhoneNumber(user.getPhone());
+            order.setImage(goods.getGoodsCover());
+            iOrderService.save(order);
+
+            OrderGoods orderGoods = new OrderGoods();
+            orderGoods.setOrderId(order.getId());
+            orderGoods.setGoodsNum(request.getGoodsNum());
+            orderGoods.setShipmentNum(0);
+            orderGoods.setDeviceId(request.getDeviceId());
+            orderGoods.setGoodsName(goods.getGoodsName());
+            orderGoods.setCargoWayId(request.getCargoWayId());
+            orderGoods.setCargoWayNo(request.getCargoWayNo());
+            //orderGoods.setShippingTime(LocalDateTime.now());
+            orderGoods.setGoodsNo(goods.getGoodsNo());
+            orderGoods.setGoodsImage(goods.getGoodsCover());
+            orderGoods.setGoodsId(request.getGoodsInfoId());
+            orderGoods.setGoodsPrice(cargoWay.getSalePrice().subtract(BigDecimal.valueOf(cargoWay.getDiscount())));
+            orderGoods.setMerchantId(shop.getMerchantId());
+            orderGoods.setShopId(request.getShopId());
+            orderGoods.setUserId(request.getUserId());
+            orderGoods.setStoreName(shop.getStoreName());
+            baseMapper.insert(orderGoods);
+
+            // 扣减货道库存
+            cargoWay.setGoodsStore(cargoWay.getGoodsStore() - request.getGoodsNum());
+            iFacilityCargoWayService.updateById(cargoWay);
+            return order.getOrderNo();
+        } catch (Exception e) {
+            log.error("下单失败", e);
+            throw new ServiceException("下单失败");
+        } finally {
+            lock.unlock();
+        }
+    }
+
+
+    @Override
+    public void releaseGoodsStock(Order order) {
+        OrderGoods orderGoods = baseMapper.selectOne(Wrappers.lambdaQuery(OrderGoods.class).eq(OrderGoods::getOrderId, order.getId()));
+        // 更新货道库存
+        FacilityCargoWay cargoWay = iFacilityCargoWayService.getById(orderGoods.getCargoWayId());
+        cargoWay.setGoodsStore(cargoWay.getGoodsStore() + orderGoods.getGoodsNum());
+        iFacilityCargoWayService.updateById(cargoWay);
+    }
+
+    @Override
+    public void paySuccessAfter(Order order) {
+        OrderGoods orderGoods = baseMapper.selectOne(Wrappers.lambdaQuery(OrderGoods.class).eq(OrderGoods::getOrderId, order.getId()));
+        // 更新货道销售信息
+        FacilityCargoWay cargoWay = iFacilityCargoWayService.getById(orderGoods.getCargoWayId());
+        cargoWay.setTotalTrade(cargoWay.getTotalTrade() + orderGoods.getGoodsNum());
+        cargoWay.setTotalAmount(cargoWay.getTotalAmount().add(order.getPaymentAmount()));
+        iFacilityCargoWayService.updateById(cargoWay);
+    }
+
+    public List<OrderGoods> getMerchantOrder(Long userId, Date startDate, Date endDate) {
+        return this.lambdaQuery()
+                .eq(OrderGoods::getMerchantId, userId)
+                .eq(OrderGoods::getIsDelete, 0)
+                .between(OrderGoods::getCreateTime, startDate, endDate)
+                .list();
     }
 
 }