// 清空购物车项 @InputConfig(resultName = "error") @SuppressWarnings("unchecked") public String ajaxClear() { Member loginMember = getLoginMember(); if (loginMember == null) { Cookie cookie = new Cookie(CartItemCookie.CART_ITEM_LIST_COOKIE_NAME, null); cookie.setPath(getRequest().getContextPath() + "/"); cookie.setMaxAge(0); getResponse().addCookie(cookie); } else { Set<CartItem> cartItemSet = loginMember.getCartItemSet(); if (cartItemSet != null) { for (CartItem cartItem : cartItemSet) { cartItemService.delete(cartItem); } } } return ajaxJsonSuccessMessage("购物车清空成功!"); }
// 添加购物车项 @SuppressWarnings("unchecked") @Validations(requiredStrings = {@RequiredStringValidator(fieldName = "id", message = "ID不允许为空!")}) @InputConfig(resultName = "error") public String ajaxAdd() { Product product = productService.load(id); if (!product.getIsMarketable()) { return ajaxJsonErrorMessage("此商品已下架!"); } if (quantity == null || quantity < 1) { quantity = 1; } // [{\"i\":\"402880ea3189939a013189af0afc0002\"\"q\":1}]" Integer totalQuantity = 0; // 总计商品数量 BigDecimal totalPrice = new BigDecimal("0"); // 总计商品价格 Member loginMember = getLoginMember(); if (loginMember == null) { List<CartItemCookie> cartItemCookieList = new ArrayList<CartItemCookie>(); boolean isExist = false; Cookie[] cookies = getRequest().getCookies(); if (cookies != null && cookies.length > 0) { for (Cookie cookie : cookies) { if (StringUtils.equalsIgnoreCase( cookie.getName(), CartItemCookie.CART_ITEM_LIST_COOKIE_NAME)) { if (StringUtils.isNotEmpty(cookie.getValue())) { JsonConfig jsonConfig = new JsonConfig(); jsonConfig.setRootClass(CartItemCookie.class); JSONArray jsonArray = JSONArray.fromObject(cookie.getValue()); List<CartItemCookie> previousCartItemCookieList = (List<CartItemCookie>) JSONSerializer.toJava(jsonArray, jsonConfig); for (CartItemCookie previousCartItemCookie : previousCartItemCookieList) { Product cartItemCookieProduct = productService.load(previousCartItemCookie.getI()); if (StringUtils.equals(previousCartItemCookie.getI(), id)) { isExist = true; previousCartItemCookie.setQ(previousCartItemCookie.getQ() + quantity); if (product.getStore() != null && (product.getFreezeStore() + previousCartItemCookie.getQ()) > product.getStore()) { return ajaxJsonErrorMessage("添加购物车失败,商品库存不足!"); } } cartItemCookieList.add(previousCartItemCookie); totalQuantity += previousCartItemCookie.getQ(); totalPrice = cartItemCookieProduct .getPreferentialPrice(getLoginMember()) .multiply(new BigDecimal(previousCartItemCookie.getQ().toString())) .add(totalPrice); } } } } } if (!isExist) { CartItemCookie cartItemCookie = new CartItemCookie(); cartItemCookie.setI(id); cartItemCookie.setQ(quantity); cartItemCookieList.add(cartItemCookie); totalQuantity += quantity; totalPrice = product .getPreferentialPrice(getLoginMember()) .multiply(new BigDecimal(quantity.toString())) .add(totalPrice); if (product.getStore() != null && (product.getFreezeStore() + cartItemCookie.getQ()) > product.getStore()) { return ajaxJsonErrorMessage("添加购物车失败,商品库存不足!"); } } for (CartItemCookie cartItemCookie : cartItemCookieList) { if (StringUtils.equals(cartItemCookie.getI(), id)) { Product cartItemCookieProduct = productService.load(cartItemCookie.getI()); if (product.getStore() != null && (cartItemCookieProduct.getFreezeStore() + cartItemCookie.getQ()) > cartItemCookieProduct.getStore()) { return ajaxJsonErrorMessage("添加购物车失败,商品库存不足!"); } } } JSONArray jsonArray = JSONArray.fromObject(cartItemCookieList); String jsonString = jsonArray.toString().replaceAll("\"", "'"); Cookie cookie = new Cookie(CartItemCookie.CART_ITEM_LIST_COOKIE_NAME, jsonString); cookie.setPath(getRequest().getContextPath() + "/"); cookie.setMaxAge(CartItemCookie.CART_ITEM_LIST_COOKIE_MAX_AGE); getResponse().addCookie(cookie); } else { boolean isExist = false; Set<CartItem> previousCartItemList = loginMember.getCartItemSet(); if (previousCartItemList != null) { for (CartItem previousCartItem : previousCartItemList) { if (StringUtils.equals(previousCartItem.getProduct().getId(), id)) { isExist = true; previousCartItem.setQuantity(previousCartItem.getQuantity() + quantity); if (product.getStore() != null && (product.getFreezeStore() + previousCartItem.getQuantity()) > product.getStore()) { return ajaxJsonErrorMessage("添加购物车失败,商品库存不足!"); } cartItemService.update(previousCartItem); } totalQuantity += previousCartItem.getQuantity(); totalPrice = previousCartItem .getProduct() .getPreferentialPrice(getLoginMember()) .multiply(new BigDecimal(previousCartItem.getQuantity().toString())) .add(totalPrice); } } if (!isExist) { CartItem cartItem = new CartItem(); cartItem.setMember(loginMember); cartItem.setProduct(product); cartItem.setQuantity(quantity); if (product.getStore() != null && (product.getFreezeStore() + cartItem.getQuantity()) > product.getStore()) { return ajaxJsonErrorMessage("添加购物车失败,商品库存不足!"); } cartItemService.save(cartItem); totalQuantity += quantity; totalPrice = product .getPreferentialPrice(getLoginMember()) .multiply(new BigDecimal(quantity.toString())) .add(totalPrice); } } totalPrice = SystemConfigUtil.getOrderScaleBigDecimal(totalPrice); DecimalFormat decimalFormat = new DecimalFormat(getOrderUnitCurrencyFormat()); String totalPriceString = decimalFormat.format(totalPrice); Map<String, String> jsonMap = new HashMap<String, String>(); jsonMap.put(STATUS, SUCCESS); jsonMap.put(MESSAGE, "添加至购物车成功!"); jsonMap.put("totalQuantity", totalQuantity.toString()); jsonMap.put("totalPrice", totalPriceString); String person = loginMember == null ? "匿名用户" : loginMember.getUsername(); LogUtil.info("[" + person + "]将[" + product.getName() + "]加入了购物车!"); return ajaxJson(jsonMap); }
// 购物车项列表 @SuppressWarnings("unchecked") @InputConfig(resultName = "error") public String list() { Member loginMember = getLoginMember(); totalQuantity = 0; totalPoint = 0; totalPrice = new BigDecimal("0"); if (loginMember == null) { Cookie[] cookies = getRequest().getCookies(); if (cookies != null && cookies.length > 0) { for (Cookie cookie : cookies) { if (StringUtils.equalsIgnoreCase( cookie.getName(), CartItemCookie.CART_ITEM_LIST_COOKIE_NAME)) { if (StringUtils.isNotEmpty(cookie.getValue())) { JsonConfig jsonConfig = new JsonConfig(); jsonConfig.setRootClass(CartItemCookie.class); String jsonString = cookie.getValue().replaceAll("'", "\""); JSONArray jsonArray = JSONArray.fromObject(jsonString); List<CartItemCookie> cartItemCookieList = (List<CartItemCookie>) JSONSerializer.toJava(jsonArray, jsonConfig); for (CartItemCookie cartItemCookie : cartItemCookieList) { Product product = productService.load(cartItemCookie.getI()); if (product != null) { totalQuantity += cartItemCookie.getQ(); if (getSystemConfig().getPointType() == PointType.productSet) { totalPoint = product.getPoint() * cartItemCookie.getQ() + totalPoint; } totalPrice = product .getPreferentialPrice(getLoginMember()) .multiply(new BigDecimal(cartItemCookie.getQ().toString())) .add(totalPrice); CartItem cartItem = new CartItem(); cartItem.setProduct(product); cartItem.setQuantity(cartItemCookie.getQ()); cartItemList.add(cartItem); } } } } } } } else { Set<CartItem> cartItemSet = loginMember.getCartItemSet(); if (cartItemSet != null) { cartItemList = new ArrayList<CartItem>(cartItemSet); for (CartItem cartItem : cartItemSet) { totalQuantity += cartItem.getQuantity(); if (getSystemConfig().getPointType() == PointType.productSet) { totalPoint = cartItem.getProduct().getPoint() * cartItem.getQuantity() + totalPoint; } totalPrice = cartItem .getProduct() .getPreferentialPrice(getLoginMember()) .multiply(new BigDecimal(cartItem.getQuantity().toString())) .add(totalPrice); } } } totalPrice = SystemConfigUtil.getOrderScaleBigDecimal(totalPrice); if (getSystemConfig().getPointType() == PointType.orderAmount) { totalPoint = totalPrice .multiply(new BigDecimal(getSystemConfig().getPointScale().toString())) .setScale(0, RoundingMode.DOWN) .intValue(); } setResponseNoCache(); return "list"; }
// 删除购物车项 @InputConfig(resultName = "error") @SuppressWarnings("unchecked") public String ajaxDelete() { Member loginMember = getLoginMember(); totalQuantity = 0; totalPoint = 0; totalPrice = new BigDecimal("0"); if (loginMember == null) { Cookie[] cookies = getRequest().getCookies(); if (cookies != null && cookies.length > 0) { for (Cookie cookie : cookies) { if (StringUtils.equalsIgnoreCase( cookie.getName(), CartItemCookie.CART_ITEM_LIST_COOKIE_NAME)) { if (StringUtils.isNotEmpty(cookie.getValue())) { JsonConfig jsonConfig = new JsonConfig(); jsonConfig.setRootClass(CartItemCookie.class); JSONArray previousJsonArray = JSONArray.fromObject(cookie.getValue()); List<CartItemCookie> cartItemCookieList = (List<CartItemCookie>) JSONSerializer.toJava(previousJsonArray, jsonConfig); Iterator<CartItemCookie> iterator = cartItemCookieList.iterator(); while (iterator.hasNext()) { CartItemCookie cartItemCookie = iterator.next(); if (StringUtils.equals(cartItemCookie.getI(), id)) { iterator.remove(); } else { Product product = productService.load(cartItemCookie.getI()); totalQuantity += cartItemCookie.getQ(); if (getSystemConfig().getPointType() == PointType.productSet) { totalPoint = product.getPoint() * cartItemCookie.getQ() + totalPoint; } totalPrice = product .getPreferentialPrice(getLoginMember()) .multiply(new BigDecimal(cartItemCookie.getQ().toString())) .add(totalPrice); } } JSONArray jsonArray = JSONArray.fromObject(cartItemCookieList); Cookie newCookie = new Cookie(CartItemCookie.CART_ITEM_LIST_COOKIE_NAME, jsonArray.toString()); newCookie.setPath(getRequest().getContextPath() + "/"); newCookie.setMaxAge(CartItemCookie.CART_ITEM_LIST_COOKIE_MAX_AGE); getResponse().addCookie(newCookie); } } } } } else { Set<CartItem> cartItemSet = loginMember.getCartItemSet(); if (cartItemSet != null) { for (CartItem cartItem : cartItemSet) { if (StringUtils.equals(cartItem.getProduct().getId(), id)) { cartItemService.delete(cartItem); } else { Product product = cartItem.getProduct(); totalQuantity += cartItem.getQuantity(); if (getSystemConfig().getPointType() == PointType.productSet) { totalPoint = product.getPoint() * cartItem.getQuantity() + totalPoint; } totalPrice = product .getPreferentialPrice(getLoginMember()) .multiply(new BigDecimal(cartItem.getQuantity().toString())) .add(totalPrice); } } } } totalPrice = SystemConfigUtil.getOrderScaleBigDecimal(totalPrice); if (getSystemConfig().getPointType() == PointType.orderAmount) { totalPoint = totalPrice .multiply(new BigDecimal(getSystemConfig().getPointScale().toString())) .setScale(0, RoundingMode.DOWN) .intValue(); } DecimalFormat decimalFormat = new DecimalFormat(getOrderUnitCurrencyFormat()); String totalPriceString = decimalFormat.format(totalPrice); Map<String, String> jsonMap = new HashMap<String, String>(); jsonMap.put("totalQuantity", totalQuantity.toString()); jsonMap.put("totalPoint", totalPoint.toString()); jsonMap.put("totalPrice", totalPriceString); jsonMap.put(STATUS, SUCCESS); jsonMap.put(MESSAGE, "商品删除成功!"); return ajaxJson(jsonMap); }
// 购物车项列表 @InputConfig(resultName = "error") @SuppressWarnings("unchecked") public String ajaxList() { List<Map<String, String>> jsonList = new ArrayList<Map<String, String>>(); Member loginMember = getLoginMember(); totalQuantity = 0; totalPrice = new BigDecimal("0"); if (loginMember == null) { Cookie[] cookies = getRequest().getCookies(); if (cookies != null && cookies.length > 0) { for (Cookie cookie : cookies) { if (StringUtils.equalsIgnoreCase( cookie.getName(), CartItemCookie.CART_ITEM_LIST_COOKIE_NAME)) { if (StringUtils.isNotEmpty(cookie.getValue())) { JsonConfig jsonConfig = new JsonConfig(); jsonConfig.setRootClass(CartItemCookie.class); JSONArray jsonArray = JSONArray.fromObject(cookie.getValue()); List<CartItemCookie> cartItemCookieList = (List<CartItemCookie>) JSONSerializer.toJava(jsonArray, jsonConfig); for (CartItemCookie cartItemCookie : cartItemCookieList) { Product product = productService.load(cartItemCookie.getI()); if (product != null) { totalQuantity += cartItemCookie.getQ(); totalPrice = product .getPreferentialPrice(getLoginMember()) .multiply(new BigDecimal(cartItemCookie.getQ().toString())) .add(totalPrice); DecimalFormat decimalFormat = new DecimalFormat(getPriceCurrencyFormat()); String priceString = decimalFormat.format(product.getPreferentialPrice(getLoginMember())); Map<String, String> jsonMap = new HashMap<String, String>(); jsonMap.put("name", product.getName()); jsonMap.put("price", priceString); jsonMap.put("quantity", cartItemCookie.getQ().toString()); jsonMap.put("htmlFilePath", product.getHtmlFilePath()); jsonList.add(jsonMap); } } } } } } } else { Set<CartItem> cartItemSet = loginMember.getCartItemSet(); if (cartItemSet != null) { for (CartItem cartItem : cartItemSet) { Product product = cartItem.getProduct(); totalQuantity += cartItem.getQuantity(); totalPrice = product .getPreferentialPrice(getLoginMember()) .multiply(new BigDecimal(cartItem.getQuantity().toString())) .add(totalPrice); DecimalFormat decimalFormat = new DecimalFormat(getPriceCurrencyFormat()); String priceString = decimalFormat.format(cartItem.getProduct().getPreferentialPrice(getLoginMember())); Map<String, String> jsonMap = new HashMap<String, String>(); jsonMap.put("name", product.getName()); jsonMap.put("price", priceString); jsonMap.put("quantity", cartItem.getQuantity().toString()); jsonMap.put("htmlFilePath", cartItem.getProduct().getHtmlFilePath()); jsonList.add(jsonMap); } } } totalPrice = SystemConfigUtil.getOrderScaleBigDecimal(totalPrice); DecimalFormat decimalFormat = new DecimalFormat(getOrderUnitCurrencyFormat()); String totalPriceString = decimalFormat.format(totalPrice); Map<String, String> jsonMap = new HashMap<String, String>(); jsonMap.put("totalQuantity", totalQuantity.toString()); jsonMap.put("totalPrice", totalPriceString); jsonList.add(0, jsonMap); JSONArray jsonArray = JSONArray.fromObject(jsonList); String person = loginMember == null ? "匿名用户" : loginMember.getUsername(); LogUtil.info("[" + person + "]查看了购物车,总价值:" + totalPriceString); return ajaxJson(jsonArray.toString()); }