yangyb 1 неделя назад
Родитель
Сommit
0c6a183834

+ 1 - 1
mall-service/src/main/java/com/txz/mall/service/StoreCombinationService.java

@@ -39,5 +39,5 @@ public interface StoreCombinationService extends Service<StoreCombination> {
      * @param num  数量
      * @param type 类型:add—添加,sub—扣减
      */
-    void operationStock(Integer id, Integer num, String type);
+    void operationStock(Long id, Integer num, String type);
 }

+ 20 - 15
mall-service/src/main/java/com/txz/mall/service/impl/StoreCombinationServiceImpl.java

@@ -1,7 +1,6 @@
 package com.txz.mall.service.impl;
 
 import cn.hutool.core.collection.CollUtil;
-import cn.hutool.core.util.StrUtil;
 import com.alibaba.fastjson.JSONObject;
 import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
 import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
@@ -125,24 +124,30 @@ public class StoreCombinationServiceImpl extends AbstractService<StoreCombinatio
     }
 
     @Override
-    public void operationStock(Integer id, Integer num, String type) {
+    public synchronized void operationStock(Long id, Integer num, String type) {
+        StoreCombination combination = findById(id);
         UpdateWrapper<StoreCombination> updateWrapper = new UpdateWrapper<>();
         if ("add".equals(type)) {
-            updateWrapper.setSql(StrUtil.format("stock = stock + {}", num));
-            updateWrapper.setSql(StrUtil.format("sales = sales - {}", num));
-            updateWrapper.setSql(StrUtil.format("quota = quota + {}", num));
+            combination.setSales(combination.getNum() - num);
+            combination.setStock(combination.getStock() + num);
+            combination.setQuota(combination.getQuota() + num);
         }
         if ("sub".equals(type)) {
-            updateWrapper.setSql(StrUtil.format("stock = stock - {}", num));
-            updateWrapper.setSql(StrUtil.format("sales = sales + {}", num));
-            updateWrapper.setSql(StrUtil.format("quota = quota - {}", num));
-            // 扣减时加乐观锁保证库存不为负
-            updateWrapper.last(StrUtil.format(" and (quota - {} >= 0)", num));
+            combination.setStock(combination.getStock() - num);
+            combination.setSales(combination.getSales() + num);
+            combination.setQuota(combination.getQuota() - num);
+
+            Condition condition = new Condition(StoreCombination.class);
+            Example.Criteria criteria = condition.createCriteria();
+            criteria.andEqualTo("id", id);
+            criteria.andGreaterThanOrEqualTo("quota", num);
+            criteria.andEqualTo("isDelete", 0);
+            criteria.andGreaterThanOrEqualTo("stock", num);
+            List<StoreCombination> combinationList = findByCondition(condition);
+            if (CollectionUtils.isEmpty(combinationList)) {
+                throw new ServiceException("更新拼团商品库存失败,商品id = " + id + ",库存不足");
+            }
         }
-        updateWrapper.eq("id", id);
-//        boolean update = update(updateWrapper);
-//        if (!update) {
-//            throw new ServiceException("更新拼团商品库存失败,商品id = " + id);
-//        }
+        update(combination);
     }
 }

+ 6 - 2
mall-service/src/main/java/com/txz/mall/service/impl/StoreOrderServiceImpl.java

@@ -31,6 +31,7 @@ import lombok.AllArgsConstructor;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.commons.lang.time.DateUtils;
 import org.apache.commons.lang3.StringEscapeUtils;
+import org.apache.commons.lang3.StringUtils;
 import org.springframework.beans.BeanUtils;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
@@ -274,7 +275,7 @@ public class StoreOrderServiceImpl extends AbstractService<StoreOrder> implement
                             vo.getRealName(),
                             vo.getUserPhone(),
                             vo.getUserAddress(),
-                            orderAddressVO.getPostCode(),
+                            (orderAddressVO == null || StringUtils.isBlank(orderAddressVO.getPostCode())) ? "" : orderAddressVO.getPostCode(),
                             vo.getDeliveryName(),
                             vo.getDeliveryId()
                     );
@@ -1249,7 +1250,7 @@ public class StoreOrderServiceImpl extends AbstractService<StoreOrder> implement
             // 拼团扣库存
             MyRecord skuRecord = skuRecordList.get(0);
             // 拼团商品扣库存
-            storeCombinationService.operationStock(skuRecord.getInt("activityId"), skuRecord.getInt("num"), "sub");
+            storeCombinationService.operationStock(skuRecord.getLong("activityId"), skuRecord.getInt("num"), "sub");
             // 拼团商品规格扣库存
             productAttrValueService.operationStock(skuRecord.getInt("activityAttrValueId"), skuRecord.getInt("num"), "sub", Constants.PRODUCT_TYPE_PINGTUAN);
             // 普通商品口库存
@@ -1348,7 +1349,10 @@ public class StoreOrderServiceImpl extends AbstractService<StoreOrder> implement
         storeOrder.setRefundStatus(1);
         update(storeOrder);
 
+        // 归还用户余额
 //        accountDubboServiceClient.refund(id.toString(), storeOrder.getOrderId(), storeOrder.getPayPrice());
+        // 库存增加
+        storeCombinationService.operationStock(storeOrder.getCombinationId(), storeOrder.getTotalNum(), "add");
     }
 
     @Override