Java电商项目面试--购物车模块

待我称王封你为后i 2021-09-27 08:36 1053阅读 0赞

购物车模块技术要点:
1、商品总价计算复用封装
2、高复用的逻辑方法封装思想
3、解决商业运算丢失精度的坑

一、购物车模块功能
1、购物车添加商品
2、更新商品数量
3、查询商品数量
4、移除购物车中商品
5、单选/全选
二、购物车添加商品
Controller层:

  1. //购物车添加商品
  2. @RequestMapping("add.do")
  3. @ResponseBody
  4. public ServerResponse<CartVo> add(HttpSession session, Integer count, Integer productId){
  5. User user = (User)session.getAttribute(Const.CURRENT_USER);
  6. if(user ==null){
  7. return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),ResponseCode.NEED_LOGIN.getDesc());
  8. }
  9. return iCartService.add(user.getId(),productId,count);
  10. }

Service层:

  1. //购物车添加商品
  2. public ServerResponse<CartVo> add(Integer userId,Integer productId,Integer count){
  3. if(productId == null || count == null){
  4. return ServerResponse.createByErrorCodeMessage(ResponseCode.ILLEGAL_ARGUMENT.getCode(),ResponseCode.ILLEGAL_ARGUMENT.getDesc());
  5. }
  6. Cart cart = cartMapper.selectCartByUserIdProductId(userId,productId);
  7. if(cart == null){
  8. //这个产品不在这个购物车里,需要新增一个这个产品的记录
  9. Cart cartItem = new Cart();
  10. cartItem.setQuantity(count);
  11. cartItem.setChecked(Const.Cart.CHECKED);
  12. cartItem.setProductId(productId);
  13. cartItem.setUserId(userId);
  14. cartMapper.insert(cartItem);
  15. }else{
  16. //这个产品已经在购物车里了.
  17. //如果产品已存在,数量相加
  18. count = cart.getQuantity() + count;
  19. cart.setQuantity(count);
  20. cartMapper.updateByPrimaryKeySelective(cart);
  21. }
  22. return this.list(userId);
  23. }
  24. //返回之前需要判断一下(这里将这个函数封装了一下)
  25. public ServerResponse<CartVo> list (Integer userId){
  26. CartVo cartVo = this.getCartVoLimit(userId);
  27. return ServerResponse.createBySuccess(cartVo);
  28. }
  29. //限制购物车中商品数量
  30. private CartVo getCartVoLimit(Integer userId){
  31. CartVo cartVo = new CartVo();
  32. //从购物车中取出该用户的所有购买商品
  33. List<Cart> cartList = cartMapper.selectCartByUserId(userId);
  34. List<CartProductVo> cartProductVoList = Lists.newArrayList();
  35. BigDecimal cartTotalPrice = new BigDecimal("0"); //初始化购物总价为0
  36. if(CollectionUtils.isNotEmpty(cartList)){
  37. for(Cart cartItem : cartList){ //购物车中的每一项
  38. //将该商品封装一下
  39. CartProductVo cartProductVo = new CartProductVo();
  40. cartProductVo.setId(cartItem.getId());
  41. cartProductVo.setUserId(userId);
  42. cartProductVo.setProductId(cartItem.getProductId());
  43. //查询该商品
  44. Product product = productMapper.selectByPrimaryKey(cartItem.getProductId());
  45. if(product != null){ //将该商品封装一下
  46. cartProductVo.setProductMainImage(product.getMainImage());
  47. cartProductVo.setProductName(product.getName());
  48. cartProductVo.setProductSubtitle(product.getSubtitle());
  49. cartProductVo.setProductStatus(product.getStatus());
  50. cartProductVo.setProductPrice(product.getPrice());
  51. cartProductVo.setProductStock(product.getStock());
  52. //判断库存
  53. int buyLimitCount = 0;
  54. //库存充足的时候,赋值为买的数量
  55. if(product.getStock() >= cartItem.getQuantity()){
  56. buyLimitCount = cartItem.getQuantity();
  57. cartProductVo.setLimitQuantity(Const.Cart.LIMIT_NUM_SUCCESS);
  58. }else{
  59. //库存不够,把全部库存给购买数量
  60. buyLimitCount = product.getStock();
  61. cartProductVo.setLimitQuantity(Const.Cart.LIMIT_NUM_FAIL);
  62. //购物车中更新有效库存
  63. Cart cartForQuantity = new Cart();
  64. cartForQuantity.setId(cartItem.getId());
  65. //这里设置成全部库存数量
  66. cartForQuantity.setQuantity(buyLimitCount);
  67. cartMapper.updateByPrimaryKeySelective(cartForQuantity);
  68. }
  69. //设置购买数量
  70. cartProductVo.setQuantity(buyLimitCount);
  71. //计算该项商品总价
  72. cartProductVo.setProductTotalPrice(BigDecimalUtil.mul(product.getPrice().doubleValue(),cartProductVo.getQuantity()));
  73. cartProductVo.setProductChecked(cartItem.getChecked());
  74. }
  75. //如果该项已经勾选,增加到整个的购物车总价中
  76. if(cartItem.getChecked() == Const.Cart.CHECKED){
  77. cartTotalPrice = BigDecimalUtil.add(cartTotalPrice.doubleValue(),cartProductVo.getProductTotalPrice().doubleValue());
  78. }
  79. cartProductVoList.add(cartProductVo);
  80. }
  81. }
  82. cartVo.setCartTotalPrice(cartTotalPrice);
  83. cartVo.setCartProductVoList(cartProductVoList);
  84. cartVo.setAllChecked(this.getAllCheckedStatus(userId));
  85. cartVo.setImageHost(PropertiesUtil.getProperty("ftp.server.http.prefix"));
  86. return cartVo;
  87. }

CartProductVo
单个商品的信息,限制商品数量以及计算某项商品的总价描述

  1. public class CartProductVo {
  2. //结合了产品和购物车的一个抽象对象
  3. private Integer id;
  4. private Integer userId;
  5. private Integer productId;
  6. private Integer quantity; //购物车中此商品的数量
  7. private String productName;
  8. private String productSubtitle;
  9. private String productMainImage;
  10. private BigDecimal productPrice;
  11. private Integer productStatus;
  12. private BigDecimal productTotalPrice;
  13. private Integer productStock;
  14. private Integer productChecked;//此商品是否勾选
  15. private String limitQuantity; //限制数量的一个返回结果
  16. }

CartProductVo
用来描述购物车中的情况,封装所有商品总价,全选描述

  1. public class CartVo {
  2. private List<CartProductVo> cartProductVoList;
  3. private BigDecimal cartTotalPrice;
  4. private Boolean allChecked; //是否已经都勾选
  5. private String imageHost;
  6. }

解决商业运算丢失精度的坑
在计算总价的时候会出现浮点数精度丢失的问题可以参考我的这篇博客:Java电商项目面试–浮点型商业运算中丢失精度问题
三、更新商品数量
Controller层:

  1. //更新购物车
  2. @RequestMapping("update.do")
  3. @ResponseBody
  4. public ServerResponse<CartVo> update(HttpSession session, Integer count, Integer productId){
  5. User user = (User)session.getAttribute(Const.CURRENT_USER);
  6. if(user ==null)
  7. return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),ResponseCode.NEED_LOGIN.getDesc());
  8. return iCartService.update(user.getId(),productId,count);
  9. }

Service层:

  1. //更新购物车
  2. public ServerResponse<CartVo> update(Integer userId,Integer productId,Integer count){
  3. if(productId == null || count == null)
  4. return ServerResponse.createByErrorCodeMessage(ResponseCode.ILLEGAL_ARGUMENT.getCode(),ResponseCode.ILLEGAL_ARGUMENT.getDesc());
  5. Cart cart = cartMapper.selectCartByUserIdProductId(userId,productId); //查询出购物车中的该项
  6. if(cart != null)
  7. cart.setQuantity(count);
  8. cartMapper.updateByPrimaryKey(cart);
  9. return this.list(userId);
  10. }

四、移除购物车中商品
Controller层:

  1. //购物车中删除商品
  2. @RequestMapping("delete_product.do")
  3. @ResponseBody
  4. //和前端约定,删除多项,则用逗号分割
  5. public ServerResponse<CartVo> deleteProduct(HttpSession session,String productIds){
  6. User user = (User)session.getAttribute(Const.CURRENT_USER);
  7. if(user ==null)
  8. return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),ResponseCode.NEED_LOGIN.getDesc());
  9. return iCartService.deleteProduct(user.getId(),productIds);
  10. }

Service层:

  1. public ServerResponse<CartVo> deleteProduct(Integer userId,String productIds){
  2. //使用guava的Splitter方法( 用逗号分割,转成list集合 )
  3. //和前端约定,删除多项,则用逗号分割
  4. List<String> productList = Splitter.on(",").splitToList(productIds);
  5. if(CollectionUtils.isEmpty(productList))
  6. return ServerResponse.createByErrorCodeMessage(ResponseCode.ILLEGAL_ARGUMENT.getCode(),ResponseCode.ILLEGAL_ARGUMENT.getDesc());
  7. cartMapper.deleteByUserIdProductIds(userId,productList);
  8. return this.list(userId);
  9. }

Mapper.xml:

  1. <delete id="deleteByUserIdProductIds" parameterType="map">
  2. delete from mmall_cart
  3. where user_id = #{userId}
  4. <if test="productIdList != null">
  5. and product_id in
  6. <foreach collection="productIdList" item="item" index="index" open="(" separator="," close=")">
  7. #{item}
  8. </foreach>
  9. </if>
  10. </delete>

五、单选全选
Controller层:

  1. //全反选
  2. @RequestMapping("un_select_all.do")
  3. @ResponseBody
  4. public ServerResponse<CartVo> unSelectAll(HttpSession session){
  5. User user = (User)session.getAttribute(Const.CURRENT_USER);
  6. if(user ==null)
  7. return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),ResponseCode.NEED_LOGIN.getDesc());
  8. return iCartService.selectOrUnSelect(user.getId(),null,Const.Cart.UN_CHECKED);
  9. }
  10. //单独选
  11. @RequestMapping("select.do")
  12. @ResponseBody
  13. public ServerResponse<CartVo> select(HttpSession session,Integer productId){
  14. User user = (User)session.getAttribute(Const.CURRENT_USER);
  15. if(user ==null)
  16. return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),ResponseCode.NEED_LOGIN.getDesc());
  17. return iCartService.selectOrUnSelect(user.getId(),productId,Const.Cart.CHECKED);
  18. }
  19. //单独反选
  20. @RequestMapping("un_select.do")
  21. @ResponseBody
  22. public ServerResponse<CartVo> unSelect(HttpSession session,Integer productId){
  23. User user = (User)session.getAttribute(Const.CURRENT_USER);
  24. if(user ==null)
  25. return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),ResponseCode.NEED_LOGIN.getDesc());
  26. return iCartService.selectOrUnSelect(user.getId(),productId,Const.Cart.UN_CHECKED);
  27. }

Service层:

  1. //选择或者反选
  2. public ServerResponse<CartVo> selectOrUnSelect (Integer userId,Integer productId,Integer checked){
  3. cartMapper.checkedOrUncheckedProduct(userId,productId,checked);
  4. return this.list(userId);
  5. }
  6. //是否全选,即全选是否购物车中的所有项都被选择了
  7. private boolean getAllCheckedStatus(Integer userId){
  8. if(userId == null)
  9. return false;
  10. return cartMapper.selectCartProductCheckedStatusByUserId(userId) == 0;
  11. }

Mapper.xml:

  1. <!-- 选择或者不选 -->
  2. <update id="checkedOrUncheckedProduct" parameterType="map">
  3. UPDATE mmall_cart
  4. set checked = #{checked},
  5. update_time = now()
  6. where user_id = #{userId}
  7. <if test="productId != null">
  8. and product_id = #{productId}
  9. </if>
  10. </update>
  11. <!-- 是否被全选 -->
  12. <select id="selectCartProductCheckedStatusByUserId" resultType="int" parameterType="int">
  13. SELECT count(1) from mmall_cart where checked = 0 and user_id = #{userId}
  14. </select>

Java面试的完整博客目录如下:Java笔试面试目录

转载请标明出处,原文地址:https://blog.csdn.net/weixin_41835916 如果觉得本文对您有帮助,请点击顶支持一下,您的支持是我写作最大的动力,谢谢。
这里写图片描述

发表评论

表情:
评论列表 (有 0 条评论,1053人围观)

还没有评论,来说两句吧...

相关阅读