hjl
2024-07-02 133ddd233e5c552fe02150b62ab1691838cbce57
ruoyi-service/ruoyi-goods/src/main/java/com/ruoyi/goods/service/impl/TGoodsServiceImpl.java
@@ -66,7 +66,8 @@
        if (null != goods.getTotal()) {
            // 库存预热,redisson分布式锁
            String key = String.format(RedisConstants.GOOD_STOCK, goods.getId());
            List<TOrder> orderList = orderService.lambdaQuery().eq(TOrder::getGoodsId, goodId)
            List<TOrder> orderList = orderService.lambdaQuery()
                    .eq(TOrder::getGoodsId, goodId)
                    .eq(TOrder::getDisabled, 0).list();
            int sum = 0;
            if (!orderList.isEmpty()) {
@@ -88,6 +89,7 @@
    @Override
    @Transactional(rollbackFor = Exception.class)
    public R<String> goodExchange(GoodExchangeDTO goodExchange, Recipient recipient) {
        // 用户本次兑换数量
        Integer number = goodExchange.getNumber();
        Integer goodId = goodExchange.getGoodId();
        TGoods good = lambdaQuery().eq(TGoods::getId, goodId).one();
@@ -122,6 +124,7 @@
            // 检查用户兑换数量是否超过单用户最大兑换数量
            List<TOrder> orderList = orderService.lambdaQuery().eq(TOrder::getUserId, user.getId())
                    .eq(TOrder::getGoodsId, goodId).list();
            // 用户已兑换数量
            int totalNumber;
            if (orderList.isEmpty()) {
                totalNumber = 0;
@@ -130,8 +133,14 @@
                totalNumber = orderList.stream().map(TOrder::getCount).collect(Collectors.toList())
                        .stream().mapToInt(Integer::intValue).sum();
            }
            boolean canExchange = orderList.isEmpty() || null == good.getUserCount() ||
                    (totalNumber + number) <= good.getUserCount();
            boolean canExchange = true;
            if (null != good.getUserCount()) {
                if (number > good.getUserCount()) {
                    canExchange = false;
                } else {
                    canExchange = (totalNumber + number) <= good.getUserCount();
                }
            }
            if (!canExchange) {
                return R.exchangeError("兑换失败,当前兑换数量已超过最大兑换数量,剩余兑换数量为: " +
                        (good.getUserCount() - totalNumber) + "!");
@@ -259,8 +268,14 @@
                totalNumber = orderList.stream().map(TOrder::getCount).collect(Collectors.toList())
                        .stream().mapToInt(Integer::intValue).sum();
            }
            boolean canExchange = orderList.isEmpty() || null == good.getUserCount() ||
                    (totalNumber + number) <= good.getUserCount();
            boolean canExchange = true;
            if (null != good.getUserCount()) {
                if (number > good.getUserCount()) {
                    canExchange = false;
                } else {
                    canExchange = (totalNumber + number) <= good.getUserCount();
                }
            }
            if (!canExchange) {
                return R.exchangeError("兑换失败,当前兑换数量已超过最大兑换数量,剩余兑换数量为: " +
                        (good.getUserCount() - totalNumber) + "!");