Ejemplo n.º 1
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);
 }
Ejemplo n.º 2
0
 // 购物车项列表
 @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());
 }