Exemplo 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);
 }
Exemplo n.º 2
0
 // 添加购物车项
 @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);
 }
Exemplo n.º 3
0
 // 购物车项列表
 @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";
 }