Search in sources :

Example 6 with OmsCartItem

use of com.xatu.gmall.entity.OmsCartItem in project GMall by 18391713434.

the class CartController method checkCart.

@LoginRequired(loginSuccess = false)
@RequestMapping("/checkCart")
public String checkCart(String isChecked, String skuId, HttpServletRequest request, HttpServletResponse response, HttpSession session, ModelMap modelMap) {
    String memberId = (String) request.getAttribute("memberId");
    String nickname = (String) request.getAttribute("nickname");
    // 调用服务,修改状态
    cartService.checkCart(skuId, memberId, isChecked);
    // 将最新的数据从缓存总查出来,渲染给内嵌页
    List<OmsCartItem> omsCartItems = cartService.carList(memberId);
    modelMap.put("cartList", omsCartItems);
    BigDecimal totalAmount = getTotalAmount(omsCartItems);
    modelMap.put("totalAmount", totalAmount.toString());
    return "cartListInner";
}
Also used : OmsCartItem(com.xatu.gmall.entity.OmsCartItem) BigDecimal(java.math.BigDecimal) LoginRequired(com.xatu.gmall.annotations.LoginRequired) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 7 with OmsCartItem

use of com.xatu.gmall.entity.OmsCartItem in project GMall by 18391713434.

the class CartController method cartList.

@LoginRequired(loginSuccess = false)
@RequestMapping("/cartList")
public String cartList(String skuId, Integer quantity, HttpServletRequest request, HttpServletResponse response, HttpSession session, ModelMap modelMap) {
    List<OmsCartItem> omsCartItems = new ArrayList<>();
    String userId = (String) request.getAttribute("memberId");
    String nickname = (String) request.getAttribute("nickname");
    if (StringUtils.isNotBlank(userId)) {
        // 已经登录  查询db
        omsCartItems = cartService.carList(userId);
    } else {
        // 没有登陆 查询cookie
        String cartListCookie = CookieUtil.getCookieValue(request, "cartListCookie", true);
        omsCartItems = JSON.parseArray(cartListCookie, OmsCartItem.class);
        for (OmsCartItem omsCartItem : omsCartItems) {
            omsCartItem.setTotalPrice(omsCartItem.getPrice().multiply(BigDecimal.valueOf(omsCartItem.getQuantity())).toString());
        }
    }
    modelMap.put("cartList", omsCartItems);
    BigDecimal totalAmount = getTotalAmount(omsCartItems);
    modelMap.put("totalAmount", totalAmount.toString());
    return "cartList";
}
Also used : OmsCartItem(com.xatu.gmall.entity.OmsCartItem) ArrayList(java.util.ArrayList) BigDecimal(java.math.BigDecimal) LoginRequired(com.xatu.gmall.annotations.LoginRequired) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 8 with OmsCartItem

use of com.xatu.gmall.entity.OmsCartItem in project GMall by 18391713434.

the class CartController method addToCart.

@LoginRequired(loginSuccess = false)
@RequestMapping("/addToCart")
public String addToCart(String skuId, int quantity, HttpServletRequest request, HttpServletResponse response) {
    // 调用商品服务查询商品信息
    PmsSkuInfo pmsSkuInfo = skuService.getSkuById(skuId);
    // 将商品信息封装成购物车信息
    OmsCartItem omsCartItem = new OmsCartItem();
    omsCartItem.setCreateDate(new Date());
    omsCartItem.setDeleteStatus(0);
    omsCartItem.setModifyDate(new Date());
    omsCartItem.setPrice(BigDecimal.valueOf(pmsSkuInfo.getPrice()));
    omsCartItem.setProductAttr("");
    omsCartItem.setProductBrand("");
    omsCartItem.setProductCategoryId(pmsSkuInfo.getCatalog3Id());
    omsCartItem.setProductId(Long.valueOf(pmsSkuInfo.getSpuId()));
    omsCartItem.setProductName(pmsSkuInfo.getSkuName());
    omsCartItem.setProductPic(pmsSkuInfo.getSkuDefaultImg());
    omsCartItem.setProductSkuCode("1111111111");
    omsCartItem.setProductSkuId(Long.valueOf(skuId));
    omsCartItem.setQuantity(quantity);
    // 判断用户是否登录
    String memberId = (String) request.getAttribute("memberId");
    String nickname = (String) request.getAttribute("nickname");
    if (StringUtils.isBlank(memberId)) {
        // 用户未登录
        List<OmsCartItem> omsCartItems = new ArrayList<>();
        // 判断用户本地是由有cookie信息
        String cartListCookie = CookieUtil.getCookieValue(request, "cartListCookie", true);
        if (cartListCookie != null) {
            // cookie不为空
            omsCartItems = JSON.parseArray(cartListCookie, OmsCartItem.class);
            boolean exist = if_cart_exist(omsCartItems, omsCartItem);
            // 判断当点添加的商品在我们的购物车是否存在
            if (exist) {
                // 之前添加过,更新操作
                for (OmsCartItem cartItem : omsCartItems) {
                    String productSkuId = cartItem.getProductSkuId().toString();
                    if (productSkuId.equals(omsCartItem.getProductSkuId())) {
                        cartItem.setQuantity(cartItem.getQuantity() + omsCartItem.getQuantity());
                        cartItem.setPrice(cartItem.getPrice().add(omsCartItem.getPrice()));
                    }
                }
            } else {
                // 之前没有添加过
                omsCartItems.add(omsCartItem);
            }
        } else {
            // cookie为空
            omsCartItems.add(omsCartItem);
        }
        CookieUtil.setCookie(request, response, "cartListCookie", JSON.toJSONString(omsCartItems), 60 * 60 * 72, true);
    } else {
        // 用户已登录
        // 在db中查询购物车数据
        OmsCartItem omsCartItemFromDB = cartService.getCartExistByUser(memberId, skuId);
        if (omsCartItemFromDB == null) {
            // 购物车中没有该商品
            omsCartItem.setMemberId(Long.valueOf(memberId));
            cartService.addCartIterm(omsCartItem);
        } else {
            // 购物车中有该商品 直接更新数据库
            omsCartItemFromDB.setQuantity(omsCartItem.getQuantity());
            cartService.updateCarItem(omsCartItemFromDB);
        }
        // 同步缓存
        cartService.flushCache(memberId);
    }
    return "redirect:/success.html";
}
Also used : OmsCartItem(com.xatu.gmall.entity.OmsCartItem) PmsSkuInfo(com.xatu.gmall.entity.PmsSkuInfo) ArrayList(java.util.ArrayList) Date(java.util.Date) LoginRequired(com.xatu.gmall.annotations.LoginRequired) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 9 with OmsCartItem

use of com.xatu.gmall.entity.OmsCartItem in project GMall by 18391713434.

the class CartController method if_cart_exist.

private boolean if_cart_exist(List<OmsCartItem> omsCartItems, OmsCartItem omsCartItem) {
    boolean exist = false;
    for (OmsCartItem cartItem : omsCartItems) {
        String cartItemSkuId = cartItem.getProductSkuId().toString();
        String productSkuId = omsCartItem.getProductSkuId().toString();
        if (cartItemSkuId.equals(productSkuId)) {
            exist = true;
        }
    }
    return exist;
}
Also used : OmsCartItem(com.xatu.gmall.entity.OmsCartItem)

Example 10 with OmsCartItem

use of com.xatu.gmall.entity.OmsCartItem in project GMall by 18391713434.

the class OrderController method toTrade.

@LoginRequired(loginSuccess = true)
@RequestMapping("/toTrade")
public String toTrade(HttpServletRequest request, HttpServletResponse response, HttpSession session, ModelMap modelMap) {
    String memberId = (String) request.getAttribute("memberId");
    String nickname = (String) request.getAttribute("nickname");
    // 收件人地址列表
    List<MemberReceiveAddress> receiveAddressesByMemberId = userService.getReceiveAddressByMemberId(memberId);
    modelMap.put("userAddressList", receiveAddressesByMemberId);
    // 将购物车集合转换为页面计算 清单集合
    List<OmsCartItem> omsCartItems = cartService.carList(memberId);
    List<OmsOrderItem> omsOrderItems = new ArrayList<>();
    for (OmsCartItem omsCartItem : omsCartItems) {
        if (omsCartItem.getIsChecked().equals("1")) {
            // 每循环一个购物车对象,封装一个商品的详情到OmsOrderItem
            OmsOrderItem omsOrderItem = new OmsOrderItem();
            omsOrderItem.setProductName(omsCartItem.getProductName());
            omsOrderItem.setProductPrice(omsCartItem.getPrice());
            omsOrderItem.setProductQuantity(omsCartItem.getQuantity());
            omsOrderItem.setProductPic(omsCartItem.getProductPic());
            omsOrderItems.add(omsOrderItem);
        }
    }
    modelMap.put("omsOrderItems", omsOrderItems);
    BigDecimal totalAmount = getTotalAmount(omsOrderItems);
    modelMap.put("totalAmount", totalAmount.toString());
    // 生成交易码,为了在提交订单时做交易码的校验
    String tradeCode = orderService.genTradeCode(memberId);
    modelMap.put("tradeCode", tradeCode);
    return "trade";
}
Also used : OmsCartItem(com.xatu.gmall.entity.OmsCartItem) ArrayList(java.util.ArrayList) OmsOrderItem(com.xatu.gmall.entity.OmsOrderItem) MemberReceiveAddress(com.xatu.gmall.entity.MemberReceiveAddress) BigDecimal(java.math.BigDecimal) LoginRequired(com.xatu.gmall.annotations.LoginRequired) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

OmsCartItem (com.xatu.gmall.entity.OmsCartItem)10 LoginRequired (com.xatu.gmall.annotations.LoginRequired)5 BigDecimal (java.math.BigDecimal)5 ArrayList (java.util.ArrayList)5 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)5 MemberReceiveAddress (com.xatu.gmall.entity.MemberReceiveAddress)2 OmsOrderItem (com.xatu.gmall.entity.OmsOrderItem)2 Date (java.util.Date)2 Jedis (redis.clients.jedis.Jedis)2 QueryWrapper (com.baomidou.mybatisplus.core.conditions.query.QueryWrapper)1 OmsOrder (com.xatu.gmall.entity.OmsOrder)1 PmsSkuInfo (com.xatu.gmall.entity.PmsSkuInfo)1 SimpleDateFormat (java.text.SimpleDateFormat)1 Calendar (java.util.Calendar)1 HashMap (java.util.HashMap)1 ModelAndView (org.springframework.web.servlet.ModelAndView)1