@Override public String modifyShoppingCartNum(String accountId, Integer cartId, Integer modifyNum) { ShoppingCart shoppingCart = shoppingCartDAO.find(ShoppingCart.class, cartId); if (shoppingCart == null || ObjectUtils.notEqual(accountId, shoppingCart.getAccountId())) { return "记录不存在"; } if (shoppingCart.getProductNum() + modifyNum <= 0) { return TextConstant.PARAM_ERROR; } shoppingCart.setProductNum(shoppingCart.getProductNum() + modifyNum); shoppingCartDAO.update(ShoppingCart.class, shoppingCart); return null; }
@Override public String addShoppingCart(String accountId, Integer productId, Integer productNum) { Account account = accountDAO.find(Account.class, accountId); if (account == null) { return "账号不存在"; } ProductInfo productInfo = productInfoDAO.find(ProductInfo.class, productId); if (productInfo == null) { return "商品不存在"; } ShoppingCart shoppingCart = shoppingCartDAO.getShoppingCartByProduct(accountId, productId); if (shoppingCart == null) { shoppingCart = new ShoppingCart(); shoppingCart.setProductInfo(productInfo); shoppingCart.setCreateTime(new Date()); shoppingCart.setSellerInfo(productInfo.getSellerInfo()); shoppingCart.setAccountId(accountId); shoppingCart.setProductNum(productNum); shoppingCartDAO.add(ShoppingCart.class, shoppingCart); } else { shoppingCart.setProductNum(shoppingCart.getProductNum() + productNum); shoppingCartDAO.update(ShoppingCart.class, shoppingCart); } return null; }
private List<ShoppingCartVo> formatShoppingCartVo(List<ShoppingCart> shoppingCartList) { Map<Integer, List<ProductInfoVo>> cartVoMap = new HashMap<>(); Map<Integer, SellerInfo> sellerInfoMap = new HashMap<>(); for (ShoppingCart shoppingCart : shoppingCartList) { List<ProductInfoVo> productInfoVoList = new ArrayList<>(); Integer sellerId = shoppingCart.getSellerInfo().getSellerId(); if (cartVoMap.containsKey(sellerId)) { productInfoVoList = cartVoMap.get(sellerId); } productInfoVoList.add(new ProductInfoVo(shoppingCart)); cartVoMap.put(sellerId, productInfoVoList); sellerInfoMap.put(sellerId, shoppingCart.getSellerInfo()); } List<ShoppingCartVo> shoppingCartVoList = new ArrayList<>(); for (Integer sellerId : cartVoMap.keySet()) { SellerInfo sellerInfo = sellerInfoMap.get(sellerId); shoppingCartVoList.add(new ShoppingCartVo(sellerInfo, cartVoMap.get(sellerId))); } return shoppingCartVoList; }