修复因原商品库存为0,但选中规格库存不为0时,导致商品下架

This commit is contained in:
LionCity
2020-09-11 15:12:48 +08:00
parent 46a46111e2
commit fe43c88524

View File

@ -63,9 +63,9 @@ import java.util.stream.Collectors;
/** /**
* @author hupeng * @author hupeng
* @date 2020-05-12 * @date 2020-05-12
*/ */
@Service @Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class) @Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class YxStoreCartServiceImpl extends BaseServiceImpl<StoreCartMapper, YxStoreCart> implements YxStoreCartService { public class YxStoreCartServiceImpl extends BaseServiceImpl<StoreCartMapper, YxStoreCart> implements YxStoreCartService {
@ -100,45 +100,47 @@ public class YxStoreCartServiceImpl extends BaseServiceImpl<StoreCartMapper, YxS
/** /**
* 删除购物车 * 删除购物车
*
* @param uid uid * @param uid uid
* @param ids 购物车id集合 * @param ids 购物车id集合
*/ */
@Override @Override
public void removeUserCart(Long uid, List<String> ids) { public void removeUserCart(Long uid, List<String> ids) {
List<Long> newids = ids.stream().map(Long::new).collect(Collectors.toList()); List<Long> newids = ids.stream().map(Long::new).collect(Collectors.toList());
yxStoreCartMapper.delete( Wrappers.<YxStoreCart>lambdaQuery() yxStoreCartMapper.delete(Wrappers.<YxStoreCart>lambdaQuery()
.eq(YxStoreCart::getUid,uid) .eq(YxStoreCart::getUid, uid)
.in(YxStoreCart::getId,newids)); .in(YxStoreCart::getId, newids));
} }
/** /**
* 改购物车数量 * 改购物车数量
* @param cartId 购物车id *
* @param cartId 购物车id
* @param cartNum 数量 * @param cartNum 数量
* @param uid uid * @param uid uid
*/ */
@Override @Override
public void changeUserCartNum(Long cartId, int cartNum, Long uid) { public void changeUserCartNum(Long cartId, int cartNum, Long uid) {
YxStoreCart cart = this.lambdaQuery() YxStoreCart cart = this.lambdaQuery()
.eq(YxStoreCart::getUid,uid) .eq(YxStoreCart::getUid, uid)
.eq(YxStoreCart::getId,cartId) .eq(YxStoreCart::getId, cartId)
.one(); .one();
if(cart == null){ if (cart == null) {
throw new YshopException("购物车不存在"); throw new YshopException("购物车不存在");
} }
if(cartNum <= 0){ if (cartNum <= 0) {
throw new YshopException("库存错误"); throw new YshopException("库存错误");
} }
//普通商品库存 //普通商品库存
int stock = productService.getProductStock(cart.getProductId() int stock = productService.getProductStock(cart.getProductId()
,cart.getProductAttrUnique(),""); , cart.getProductAttrUnique(), "");
if(stock < cartNum){ if (stock < cartNum) {
throw new YshopException("该产品库存不足"+cartNum); throw new YshopException("该产品库存不足" + cartNum);
} }
if(cartNum == cart.getCartNum()) { if (cartNum == cart.getCartNum()) {
return; return;
} }
@ -153,21 +155,22 @@ public class YxStoreCartServiceImpl extends BaseServiceImpl<StoreCartMapper, YxS
/** /**
* 购物车列表 * 购物车列表
* @param uid 用户id *
* @param uid 用户id
* @param cartIds 购物车id多个逗号隔开 * @param cartIds 购物车id多个逗号隔开
* @param status 0-购购物车列表 * @param status 0-购购物车列表
* @return map valid-有效购物车 invalid-失效购物车 * @return map valid-有效购物车 invalid-失效购物车
*/ */
@Override @Override
public Map<String, Object> getUserProductCartList(Long uid, String cartIds, Integer status) { public Map<String, Object> getUserProductCartList(Long uid, String cartIds, Integer status) {
QueryWrapper<YxStoreCart> wrapper = new QueryWrapper<>(); QueryWrapper<YxStoreCart> wrapper = new QueryWrapper<>();
wrapper.lambda().eq(YxStoreCart::getUid,uid) wrapper.lambda().eq(YxStoreCart::getUid, uid)
.eq(YxStoreCart::getIsPay,OrderInfoEnum.PAY_STATUS_0.getValue()) .eq(YxStoreCart::getIsPay, OrderInfoEnum.PAY_STATUS_0.getValue())
.orderByDesc(YxStoreCart::getId); .orderByDesc(YxStoreCart::getId);
if(status == null) { if (status == null) {
wrapper.lambda().eq(YxStoreCart::getIsNew, CartTypeEnum.NEW_0.getValue()); wrapper.lambda().eq(YxStoreCart::getIsNew, CartTypeEnum.NEW_0.getValue());
} }
if(StrUtil.isNotEmpty(cartIds)) { if (StrUtil.isNotEmpty(cartIds)) {
wrapper.lambda().in(YxStoreCart::getId, Arrays.asList(cartIds.split(","))); wrapper.lambda().in(YxStoreCart::getId, Arrays.asList(cartIds.split(",")));
} }
List<YxStoreCart> carts = yxStoreCartMapper.selectList(wrapper); List<YxStoreCart> carts = yxStoreCartMapper.selectList(wrapper);
@ -177,42 +180,42 @@ public class YxStoreCartServiceImpl extends BaseServiceImpl<StoreCartMapper, YxS
for (YxStoreCart storeCart : carts) { for (YxStoreCart storeCart : carts) {
YxStoreProductQueryVo storeProduct = null; YxStoreProductQueryVo storeProduct = null;
if(storeCart.getCombinationId() != null && storeCart.getCombinationId() > 0){ if (storeCart.getCombinationId() != null && storeCart.getCombinationId() > 0) {
storeProduct = ObjectUtil.clone(storeCombinationMapper.combinatiionInfo(storeCart.getCombinationId())); storeProduct = ObjectUtil.clone(storeCombinationMapper.combinatiionInfo(storeCart.getCombinationId()));
}else if(storeCart.getSeckillId() != null && storeCart.getSeckillId() > 0){ } else if (storeCart.getSeckillId() != null && storeCart.getSeckillId() > 0) {
storeProduct = ObjectUtil.clone(storeSeckillMapper.seckillInfo(storeCart.getSeckillId())); storeProduct = ObjectUtil.clone(storeSeckillMapper.seckillInfo(storeCart.getSeckillId()));
}else if(storeCart.getBargainId() != null && storeCart.getBargainId() > 0){ } else if (storeCart.getBargainId() != null && storeCart.getBargainId() > 0) {
storeProduct = ObjectUtil.clone(yxStoreBargainMapper.bargainInfo(storeCart.getBargainId())); storeProduct = ObjectUtil.clone(yxStoreBargainMapper.bargainInfo(storeCart.getBargainId()));
}else{ } else {
//必须得重新克隆创建一个新对象 //必须得重新克隆创建一个新对象
storeProduct = ObjectUtil.clone(productService storeProduct = ObjectUtil.clone(productService
.getStoreProductById(storeCart.getProductId())); .getStoreProductById(storeCart.getProductId()));
} }
YxStoreCartQueryVo storeCartQueryVo = generator.convert(storeCart,YxStoreCartQueryVo.class); YxStoreCartQueryVo storeCartQueryVo = generator.convert(storeCart, YxStoreCartQueryVo.class);
if(ObjectUtil.isNull(storeProduct)){ if (ObjectUtil.isNull(storeProduct)) {
this.removeById(storeCart.getId()); this.removeById(storeCart.getId());
}else if(ShopCommonEnum.SHOW_0.getValue().equals(storeProduct.getIsShow()) || storeProduct.getStock() == 0){ } else if ((ShopCommonEnum.SHOW_0.getValue().equals(storeProduct.getIsShow()) || storeProduct.getStock() == 0) && StrUtil.isEmpty(storeCart.getProductAttrUnique())) {
storeCartQueryVo.setProductInfo(storeProduct); storeCartQueryVo.setProductInfo(storeProduct);
invalid.add(storeCartQueryVo); invalid.add(storeCartQueryVo);
}else{ } else {
if(StrUtil.isNotEmpty(storeCart.getProductAttrUnique())){ if (StrUtil.isNotEmpty(storeCart.getProductAttrUnique())) {
YxStoreProductAttrValue productAttrValue = productAttrService YxStoreProductAttrValue productAttrValue = productAttrService
.uniqueByAttrInfo(storeCart.getProductAttrUnique()); .uniqueByAttrInfo(storeCart.getProductAttrUnique());
if(ObjectUtil.isNull(productAttrValue) || productAttrValue.getStock() == 0){ if (ObjectUtil.isNull(productAttrValue) || productAttrValue.getStock() == 0) {
storeCartQueryVo.setProductInfo(storeProduct); storeCartQueryVo.setProductInfo(storeProduct);
invalid.add(storeCartQueryVo); invalid.add(storeCartQueryVo);
}else{ } else {
storeProduct.setAttrInfo(productAttrValue); storeProduct.setAttrInfo(productAttrValue);
storeCartQueryVo.setProductInfo(storeProduct); storeCartQueryVo.setProductInfo(storeProduct);
//设置真实价格 //设置真实价格
//设置VIP价格 //设置VIP价格
double vipPrice = userService.setLevelPrice( double vipPrice = userService.setLevelPrice(
productAttrValue.getPrice().doubleValue(),uid); productAttrValue.getPrice().doubleValue(), uid);
if(storeCart.getCombinationId() > 0 || storeCart.getSeckillId() > 0 if (storeCart.getCombinationId() > 0 || storeCart.getSeckillId() > 0
|| storeCart.getBargainId() > 0){ || storeCart.getBargainId() > 0) {
vipPrice = storeProduct.getPrice().doubleValue(); vipPrice = storeProduct.getPrice().doubleValue();
} }
@ -227,12 +230,12 @@ public class YxStoreCartServiceImpl extends BaseServiceImpl<StoreCartMapper, YxS
valid.add(storeCartQueryVo); valid.add(storeCartQueryVo);
} }
}else{ } else {
//设置VIP价格,营销商品不参与 //设置VIP价格,营销商品不参与
double vipPrice = userService.setLevelPrice( double vipPrice = userService.setLevelPrice(
storeProduct.getPrice().doubleValue(),uid); storeProduct.getPrice().doubleValue(), uid);
if(storeCart.getCombinationId() > 0 || storeCart.getSeckillId() > 0 if (storeCart.getCombinationId() > 0 || storeCart.getSeckillId() > 0
|| storeCart.getBargainId() > 0){ || storeCart.getBargainId() > 0) {
vipPrice = storeProduct.getPrice().doubleValue(); vipPrice = storeProduct.getPrice().doubleValue();
} }
@ -250,42 +253,43 @@ public class YxStoreCartServiceImpl extends BaseServiceImpl<StoreCartMapper, YxS
} }
Map<String,Object> map = new LinkedHashMap<>(); Map<String, Object> map = new LinkedHashMap<>();
map.put("valid",valid); map.put("valid", valid);
map.put("invalid",invalid); map.put("invalid", invalid);
return map; return map;
} }
/** /**
* 添加购物车 * 添加购物车
* @param uid 用户id *
* @param productId 普通产品编号 * @param uid 用户id
* @param cartNum 购物车数量 * @param productId 普通产品编号
* @param cartNum 购物车数量
* @param productAttrUnique 属性唯一值 * @param productAttrUnique 属性唯一值
* @param isNew 1 加入购物车直接购买 0 加入购物车 * @param isNew 1 加入购物车直接购买 0 加入购物车
* @param combinationId 拼团id * @param combinationId 拼团id
* @param seckillId 秒杀id * @param seckillId 秒杀id
* @param bargainId 砍价id * @param bargainId 砍价id
* @return 购物车id * @return 购物车id
*/ */
@Override @Override
public long addCart(Long uid, Long productId, Integer cartNum, String productAttrUnique, public long addCart(Long uid, Long productId, Integer cartNum, String productAttrUnique,
Integer isNew, Long combinationId, Long seckillId, Long bargainId) { Integer isNew, Long combinationId, Long seckillId, Long bargainId) {
this.checkProductStock(uid,productId,cartNum,productAttrUnique,combinationId,seckillId,bargainId); this.checkProductStock(uid, productId, cartNum, productAttrUnique, combinationId, seckillId, bargainId);
QueryWrapper<YxStoreCart> wrapper = new QueryWrapper<>(); QueryWrapper<YxStoreCart> wrapper = new QueryWrapper<>();
wrapper.lambda().eq(YxStoreCart::getUid,uid) wrapper.lambda().eq(YxStoreCart::getUid, uid)
.eq(YxStoreCart::getIsPay, OrderInfoEnum.PAY_STATUS_0.getValue()) .eq(YxStoreCart::getIsPay, OrderInfoEnum.PAY_STATUS_0.getValue())
.eq(YxStoreCart::getProductId,productId) .eq(YxStoreCart::getProductId, productId)
.eq(YxStoreCart::getIsNew,isNew) .eq(YxStoreCart::getIsNew, isNew)
.eq(YxStoreCart::getProductAttrUnique,productAttrUnique) .eq(YxStoreCart::getProductAttrUnique, productAttrUnique)
.eq(YxStoreCart::getBargainId,bargainId) .eq(YxStoreCart::getBargainId, bargainId)
.eq(YxStoreCart::getCombinationId,combinationId) .eq(YxStoreCart::getCombinationId, combinationId)
.eq(YxStoreCart::getSeckillId,seckillId) .eq(YxStoreCart::getSeckillId, seckillId)
.orderByDesc(YxStoreCart::getId) .orderByDesc(YxStoreCart::getId)
.last("limit 1"); .last("limit 1");
YxStoreCart cart =yxStoreCartMapper.selectOne(wrapper); YxStoreCart cart = yxStoreCartMapper.selectOne(wrapper);
YxStoreCart storeCart = YxStoreCart.builder() YxStoreCart storeCart = YxStoreCart.builder()
.cartNum(cartNum) .cartNum(cartNum)
@ -297,13 +301,13 @@ public class YxStoreCartServiceImpl extends BaseServiceImpl<StoreCartMapper, YxS
.isNew(isNew) .isNew(isNew)
.uid(uid) .uid(uid)
.build(); .build();
if(cart != null){ if (cart != null) {
if(CartTypeEnum.NEW_0.getValue().equals(isNew)){ if (CartTypeEnum.NEW_0.getValue().equals(isNew)) {
storeCart.setCartNum(cartNum + cart.getCartNum()); storeCart.setCartNum(cartNum + cart.getCartNum());
} }
storeCart.setId(cart.getId()); storeCart.setId(cart.getId());
yxStoreCartMapper.updateById(storeCart); yxStoreCartMapper.updateById(storeCart);
}else{ } else {
yxStoreCartMapper.insert(storeCart); yxStoreCartMapper.insert(storeCart);
} }
@ -312,6 +316,7 @@ public class YxStoreCartServiceImpl extends BaseServiceImpl<StoreCartMapper, YxS
/** /**
* 返回当前用户购物车总数量 * 返回当前用户购物车总数量
*
* @param uid 用户id * @param uid 用户id
* @return int * @return int
*/ */
@ -327,81 +332,80 @@ public class YxStoreCartServiceImpl extends BaseServiceImpl<StoreCartMapper, YxS
/** /**
* 检测商品/秒杀/砍价/拼团库存 * 检测商品/秒杀/砍价/拼团库存
* @param uid 用户ID *
* @param productId 产品ID * @param uid 用户ID
* @param cartNum 购买数量 * @param productId 产品ID
* @param productAttrUnique 商品属性Unique * @param cartNum 购买数量
* @param combinationId 拼团产品ID * @param productAttrUnique 商品属性Unique
* @param seckillId 秒杀产品ID * @param combinationId 拼团产品ID
* @param bargainId 砍价产品ID * @param seckillId 秒杀产品ID
* @param bargainId 砍价产品ID
*/ */
@Override @Override
public void checkProductStock(Long uid, Long productId, Integer cartNum, String productAttrUnique, public void checkProductStock(Long uid, Long productId, Integer cartNum, String productAttrUnique,
Long combinationId, Long seckillId, Long bargainId) { Long combinationId, Long seckillId, Long bargainId) {
Date now = new Date(); Date now = new Date();
//拼团 //拼团
if(combinationId != null && combinationId > 0){ if (combinationId != null && combinationId > 0) {
YxStoreProduct product = productService YxStoreProduct product = productService
.lambdaQuery().eq(YxStoreProduct::getId,productId) .lambdaQuery().eq(YxStoreProduct::getId, productId)
.eq(YxStoreProduct::getIsShow,ShopCommonEnum.SHOW_1.getValue()) .eq(YxStoreProduct::getIsShow, ShopCommonEnum.SHOW_1.getValue())
.one(); .one();
if(product == null){ if (product == null) {
throw new YshopException("该产品已下架或删除"); throw new YshopException("该产品已下架或删除");
} }
int stock = productService.getProductStock(productId,productAttrUnique,"pink"); int stock = productService.getProductStock(productId, productAttrUnique, "pink");
if(stock < cartNum){ if (stock < cartNum) {
throw new YshopException(product.getStoreName()+"库存不足"+cartNum); throw new YshopException(product.getStoreName() + "库存不足" + cartNum);
} }
//秒杀 //秒杀
}else if(seckillId != null && seckillId > 0){ } else if (seckillId != null && seckillId > 0) {
YxStoreProduct product = productService YxStoreProduct product = productService
.lambdaQuery().eq(YxStoreProduct::getId,productId) .lambdaQuery().eq(YxStoreProduct::getId, productId)
.eq(YxStoreProduct::getIsShow,ShopCommonEnum.SHOW_1.getValue()) .eq(YxStoreProduct::getIsShow, ShopCommonEnum.SHOW_1.getValue())
.one(); .one();
if(product == null){ if (product == null) {
throw new YshopException("该产品已下架或删除"); throw new YshopException("该产品已下架或删除");
} }
int stock = productService.getProductStock(productId,productAttrUnique,"seckill"); int stock = productService.getProductStock(productId, productAttrUnique, "seckill");
if(stock < cartNum){ if (stock < cartNum) {
throw new YshopException(product.getStoreName()+"库存不足"+cartNum); throw new YshopException(product.getStoreName() + "库存不足" + cartNum);
} }
//砍价 //砍价
}else if(bargainId != null && bargainId > 0){ } else if (bargainId != null && bargainId > 0) {
YxStoreBargain yxStoreBargain = storeBargainService YxStoreBargain yxStoreBargain = storeBargainService
.lambdaQuery().eq(YxStoreBargain::getId,bargainId) .lambdaQuery().eq(YxStoreBargain::getId, bargainId)
.eq(YxStoreBargain::getStatus, ShopCommonEnum.IS_STATUS_1.getValue()) .eq(YxStoreBargain::getStatus, ShopCommonEnum.IS_STATUS_1.getValue())
.le(YxStoreBargain::getStartTime,now) .le(YxStoreBargain::getStartTime, now)
.ge(YxStoreBargain::getStopTime,now) .ge(YxStoreBargain::getStopTime, now)
.one(); .one();
if(yxStoreBargain == null){ if (yxStoreBargain == null) {
throw new YshopException("该产品已下架或删除"); throw new YshopException("该产品已下架或删除");
} }
if(yxStoreBargain.getStock() < cartNum){ if (yxStoreBargain.getStock() < cartNum) {
throw new YshopException("该产品库存不足"); throw new YshopException("该产品库存不足");
} }
}else{ } else {
YxStoreProduct product = productService YxStoreProduct product = productService
.lambdaQuery().eq(YxStoreProduct::getId,productId) .lambdaQuery().eq(YxStoreProduct::getId, productId)
.eq(YxStoreProduct::getIsShow,ShopCommonEnum.SHOW_1.getValue()) .eq(YxStoreProduct::getIsShow, ShopCommonEnum.SHOW_1.getValue())
.one(); .one();
if(product == null){ if (product == null) {
throw new YshopException("该产品已下架或删除"); throw new YshopException("该产品已下架或删除");
} }
int stock = productService.getProductStock(productId,productAttrUnique,""); int stock = productService.getProductStock(productId, productAttrUnique, "");
if(stock < cartNum){ if (stock < cartNum) {
throw new YshopException(product.getStoreName()+"库存不足"+cartNum); throw new YshopException(product.getStoreName() + "库存不足" + cartNum);
} }
} }
} }
//====================================================================// //====================================================================//
@ -419,7 +423,7 @@ public class YxStoreCartServiceImpl extends BaseServiceImpl<StoreCartMapper, YxS
@Override @Override
//@Cacheable //@Cacheable
public List<YxStoreCart> queryAll(YxStoreCartQueryCriteria criteria){ public List<YxStoreCart> queryAll(YxStoreCartQueryCriteria criteria) {
return baseMapper.selectList(QueryHelpPlus.getPredicate(YxStoreCart.class, criteria)); return baseMapper.selectList(QueryHelpPlus.getPredicate(YxStoreCart.class, criteria));
} }
@ -428,7 +432,7 @@ public class YxStoreCartServiceImpl extends BaseServiceImpl<StoreCartMapper, YxS
public void download(List<YxStoreCartDto> all, HttpServletResponse response) throws IOException { public void download(List<YxStoreCartDto> all, HttpServletResponse response) throws IOException {
List<Map<String, Object>> list = new ArrayList<>(); List<Map<String, Object>> list = new ArrayList<>();
for (YxStoreCartDto yxStoreCart : all) { for (YxStoreCartDto yxStoreCart : all) {
Map<String,Object> map = new LinkedHashMap<>(); Map<String, Object> map = new LinkedHashMap<>();
map.put("用户ID", yxStoreCart.getUid()); map.put("用户ID", yxStoreCart.getUid());
map.put("类型", yxStoreCart.getType()); map.put("类型", yxStoreCart.getType());
map.put("商品ID", yxStoreCart.getProductId()); map.put("商品ID", yxStoreCart.getProductId());