|
@@ -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);
|
|
|
}
|
|
|
}
|