Esempio n. 1
0
 // 修改购物车项
 @InputConfig(resultName = "error")
 @SuppressWarnings("unchecked")
 public String ajaxEdit() {
   if (quantity == null || quantity < 1) {
     quantity = 1;
   }
   Member loginMember = getLoginMember();
   totalQuantity = 0;
   totalPoint = 0;
   totalPrice = new BigDecimal("0");
   BigDecimal subtotalPrice = 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();
               Product product = productService.load(cartItemCookie.getI());
               if (StringUtils.equals(id, cartItemCookie.getI())) {
                 cartItemCookie.setQ(quantity);
                 subtotalPrice =
                     product
                         .getPreferentialPrice(getLoginMember())
                         .multiply(new BigDecimal(quantity.toString()));
                 if (product.getStore() != null
                     && (product.getFreezeStore() + cartItemCookie.getQ()) > product.getStore()) {
                   return ajaxJsonErrorMessage("商品库存不足!");
                 }
               }
               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) {
         Product product = cartItem.getProduct();
         if (StringUtils.equals(id, cartItem.getProduct().getId())) {
           cartItem.setQuantity(quantity);
           if (product.getStore() != null
               && (product.getFreezeStore() + cartItem.getQuantity()) > product.getStore()) {
             return ajaxJsonErrorMessage("商品库存不足!");
           }
           cartItemService.update(cartItem);
           subtotalPrice = cartItem.getSubtotalPrice();
         }
         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);
       }
     }
   }
   DecimalFormat orderUnitCurrencyFormat = new DecimalFormat(getOrderUnitCurrencyFormat());
   DecimalFormat orderCurrencyFormat = new DecimalFormat(getOrderCurrencyFormat());
   totalPrice = SystemConfigUtil.getOrderScaleBigDecimal(totalPrice);
   if (getSystemConfig().getPointType() == PointType.orderAmount) {
     totalPoint =
         totalPrice
             .multiply(new BigDecimal(getSystemConfig().getPointScale().toString()))
             .setScale(0, RoundingMode.DOWN)
             .intValue();
   }
   String subtotalPriceString = orderCurrencyFormat.format(subtotalPrice);
   String totalPriceString = orderUnitCurrencyFormat.format(totalPrice);
   Map<String, String> jsonMap = new HashMap<String, String>();
   jsonMap.put("subtotalPrice", subtotalPriceString);
   jsonMap.put("totalQuantity", totalQuantity.toString());
   jsonMap.put("totalPoint", totalPoint.toString());
   jsonMap.put("totalPrice", totalPriceString);
   jsonMap.put(STATUS, SUCCESS);
   return ajaxJson(jsonMap);
 }