Commit f09d5aaba97967fb5d15a20ff8cf1a4c550c05be
1 parent
63ecec86
update:购物车判断
Showing
2 changed files
with
32 additions
and
1 deletions
etrade-order/src/main/java/com/diligrp/etrade/order/service/impl/ShopCartImpl.java
... | ... | @@ -16,12 +16,15 @@ import com.diligrp.etrade.order.service.IShopCartMessageService; |
16 | 16 | import com.diligrp.etrade.order.service.IShopCartService; |
17 | 17 | import com.diligrp.etrade.order.type.ShopCartMessageCheckFlag; |
18 | 18 | import com.diligrp.etrade.order.type.ShopCartMessageProductState; |
19 | +import com.diligrp.etrade.order.util.NumberTransform; | |
19 | 20 | import com.diligrp.etrade.shop.domain.response.ProductDetailVo; |
20 | 21 | import com.diligrp.etrade.shop.domain.response.ProductListVo; |
21 | 22 | import com.diligrp.etrade.shop.domain.response.ProductPresetVo; |
22 | 23 | import com.diligrp.etrade.shop.domain.response.ShopVo; |
24 | +import com.diligrp.etrade.shop.model.Stock; | |
23 | 25 | import com.diligrp.etrade.shop.service.ProductService; |
24 | 26 | import com.diligrp.etrade.shop.service.ShopService; |
27 | +import com.diligrp.etrade.shop.service.StockService; | |
25 | 28 | import com.diligrp.etrade.shop.type.ProductAuditState; |
26 | 29 | import org.apache.commons.lang3.StringUtils; |
27 | 30 | import org.springframework.beans.BeanUtils; |
... | ... | @@ -31,6 +34,7 @@ import org.springframework.transaction.annotation.Transactional; |
31 | 34 | import org.springframework.util.Assert; |
32 | 35 | import org.springframework.util.CollectionUtils; |
33 | 36 | |
37 | +import java.math.BigDecimal; | |
34 | 38 | import java.time.LocalDateTime; |
35 | 39 | import java.util.*; |
36 | 40 | import java.util.stream.Collectors; |
... | ... | @@ -56,6 +60,9 @@ public class ShopCartImpl implements IShopCartService { |
56 | 60 | @Autowired |
57 | 61 | private ProductService productService; |
58 | 62 | |
63 | + @Autowired | |
64 | + private StockService stockService; | |
65 | + | |
59 | 66 | |
60 | 67 | /** |
61 | 68 | * 购物车列表 |
... | ... | @@ -229,6 +236,8 @@ public class ShopCartImpl implements IShopCartService { |
229 | 236 | List<ShopCartMessage> shopCartMessages = shopCartMessageService.findList(shopCartMessageDtoQuery); |
230 | 237 | //商品信息不存在,则新增商品信息 |
231 | 238 | if (CollectionUtils.isEmpty(shopCartMessages)) { |
239 | + //效验最大最大数量 | |
240 | + checkMaxProductWeight(shopCartMessageDto,null); | |
232 | 241 | AssertUtils.isTrue(count < 50L, "购物车商品数量不能超过50"); |
233 | 242 | shopCartMessageDto.setShopCartId(shopCart.getId()); |
234 | 243 | shopCartMessageService.saveShopCartMessage(shopCartMessageDto); |
... | ... | @@ -236,9 +245,30 @@ public class ShopCartImpl implements IShopCartService { |
236 | 245 | } |
237 | 246 | //存在相同商品信息,对商品信息进行更新操作 |
238 | 247 | ShopCartMessage shopCartMessageOld = shopCartMessages.get(0); |
248 | + //效验最大数量 | |
249 | + checkMaxProductWeight(shopCartMessageDto,shopCartMessageOld); | |
239 | 250 | shopCartMessageService.updateByOld(shopCartMessageOld, shopCartDto.getShopCartMessageDto(),shopCartDto.getBo()); |
240 | 251 | } |
241 | 252 | |
253 | + private void checkMaxProductWeight(ShopCartMessageDto shopCartMessageDto, ShopCartMessage shopCartMessageOld) { | |
254 | + if(shopCartMessageDto == null){ | |
255 | + return ; | |
256 | + } | |
257 | + List<Stock> stocks = stockService.listByProductIds(Collections.singletonList(shopCartMessageDto.getProductId())); | |
258 | + if(CollectionUtils.isEmpty(stocks)){ | |
259 | + throw new OrderException(OrderErrorCode.PARAM_ERROR,"该商品暂无库存"); | |
260 | + } | |
261 | + if(shopCartMessageOld!=null){ | |
262 | + if(stocks.get(0).getStock().multiply(NumberTransform.ONE_HUNDERD).compareTo(new BigDecimal(shopCartMessageOld.getWeight()+shopCartMessageDto.getWeight()))<0){ | |
263 | + throw new OrderException(OrderErrorCode.PARAM_ERROR,"该宝贝不能购买更多哦~"); | |
264 | + } | |
265 | + }else{ | |
266 | + if(stocks.get(0).getStock().compareTo(new BigDecimal(shopCartMessageDto.getWeight()))<0){ | |
267 | + throw new OrderException(OrderErrorCode.PARAM_ERROR,"该宝贝不能购买更多哦~"); | |
268 | + } | |
269 | + } | |
270 | + } | |
271 | + | |
242 | 272 | |
243 | 273 | @Transactional(rollbackFor = Exception.class) |
244 | 274 | @Override | ... | ... |
etrade-order/src/main/resources/com/diligrp/etrade/dao/mapper/shopCart/ShopCartMapper.xml
... | ... | @@ -63,7 +63,7 @@ |
63 | 63 | scm.number, |
64 | 64 | scm.unit_amount, |
65 | 65 | scm.unit_amount as prePrice, |
66 | - scm.unit_weight, | |
66 | + case when scm.weight > st.stock*100 then st.stock*100 else scm.weight end as weight, | |
67 | 67 | scm.weight, |
68 | 68 | scm.amount, |
69 | 69 | scm.type, |
... | ... | @@ -75,6 +75,7 @@ |
75 | 75 | scm.created_time as createdTime |
76 | 76 | from shop_cart s |
77 | 77 | left join shop_cart_message scm |
78 | + left join stock st | |
78 | 79 | on s.id = scm.shop_cart_id |
79 | 80 | where s.customer_id = #{customerId,jdbcType=BIGINT} |
80 | 81 | and s.market_id = #{marketId,jdbcType=BIGINT} | ... | ... |