Commit 6b05835cb39be947a257cb2d916362c15c199d4a
1 parent
9980b125
购物车:增加获得商品数量 Map 接口
Showing
5 changed files
with
43 additions
and
1 deletions
yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/controller/app/cart/TradeCartController.http
| @@ -31,6 +31,11 @@ GET {{appApi}}/trade/cart/get-count | @@ -31,6 +31,11 @@ GET {{appApi}}/trade/cart/get-count | ||
| 31 | tenant-id: {{appTenentId}} | 31 | tenant-id: {{appTenentId}} |
| 32 | Authorization: Bearer {{appToken}} | 32 | Authorization: Bearer {{appToken}} |
| 33 | 33 | ||
| 34 | +### 请求 /trade/cart/get-count-map 接口 => 成功 | ||
| 35 | +GET {{appApi}}/trade/cart/get-count-map | ||
| 36 | +tenant-id: {{appTenentId}} | ||
| 37 | +Authorization: Bearer {{appToken}} | ||
| 38 | + | ||
| 34 | ### 请求 /trade/cart/list 接口 => 成功 | 39 | ### 请求 /trade/cart/list 接口 => 成功 |
| 35 | GET {{appApi}}/trade/cart/list | 40 | GET {{appApi}}/trade/cart/list |
| 36 | tenant-id: {{appTenentId}} | 41 | tenant-id: {{appTenentId}} |
yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/controller/app/cart/TradeCartController.java
| @@ -2,8 +2,8 @@ package cn.iocoder.yudao.module.trade.controller.app.cart; | @@ -2,8 +2,8 @@ package cn.iocoder.yudao.module.trade.controller.app.cart; | ||
| 2 | 2 | ||
| 3 | import cn.iocoder.yudao.framework.common.pojo.CommonResult; | 3 | import cn.iocoder.yudao.framework.common.pojo.CommonResult; |
| 4 | import cn.iocoder.yudao.framework.security.core.annotations.PreAuthenticated; | 4 | import cn.iocoder.yudao.framework.security.core.annotations.PreAuthenticated; |
| 5 | -import cn.iocoder.yudao.module.trade.controller.app.cart.vo.AppTradeCartListRespVO; | ||
| 6 | import cn.iocoder.yudao.module.trade.controller.app.cart.vo.AppTradeCartAddReqVO; | 5 | import cn.iocoder.yudao.module.trade.controller.app.cart.vo.AppTradeCartAddReqVO; |
| 6 | +import cn.iocoder.yudao.module.trade.controller.app.cart.vo.AppTradeCartListRespVO; | ||
| 7 | import cn.iocoder.yudao.module.trade.controller.app.cart.vo.AppTradeCartResetReqVO; | 7 | import cn.iocoder.yudao.module.trade.controller.app.cart.vo.AppTradeCartResetReqVO; |
| 8 | import cn.iocoder.yudao.module.trade.controller.app.cart.vo.AppTradeCartUpdateReqVO; | 8 | import cn.iocoder.yudao.module.trade.controller.app.cart.vo.AppTradeCartUpdateReqVO; |
| 9 | import cn.iocoder.yudao.module.trade.service.cart.TradeCartService; | 9 | import cn.iocoder.yudao.module.trade.service.cart.TradeCartService; |
| @@ -18,6 +18,7 @@ import org.springframework.web.bind.annotation.*; | @@ -18,6 +18,7 @@ import org.springframework.web.bind.annotation.*; | ||
| 18 | import javax.annotation.Resource; | 18 | import javax.annotation.Resource; |
| 19 | import javax.validation.Valid; | 19 | import javax.validation.Valid; |
| 20 | import java.util.List; | 20 | import java.util.List; |
| 21 | +import java.util.Map; | ||
| 21 | 22 | ||
| 22 | import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success; | 23 | import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success; |
| 23 | import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId; | 24 | import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId; |
| @@ -72,6 +73,13 @@ public class TradeCartController { | @@ -72,6 +73,13 @@ public class TradeCartController { | ||
| 72 | return success(cartService.getCartCount(getLoginUserId())); | 73 | return success(cartService.getCartCount(getLoginUserId())); |
| 73 | } | 74 | } |
| 74 | 75 | ||
| 76 | + @GetMapping("get-count-map") | ||
| 77 | + @Operation(summary = "查询用户在购物车中的商品 SPU 数量 Map") | ||
| 78 | + @PreAuthenticated | ||
| 79 | + public CommonResult<Map<Long, Integer>> getCartCountMap() { | ||
| 80 | + return success(cartService.getCartCountMap(getLoginUserId())); | ||
| 81 | + } | ||
| 82 | + | ||
| 75 | @GetMapping("/list") | 83 | @GetMapping("/list") |
| 76 | @Operation(summary = "查询用户的购物车列表") | 84 | @Operation(summary = "查询用户的购物车列表") |
| 77 | @PreAuthenticated | 85 | @PreAuthenticated |
yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/dal/mysql/cart/TradeCartMapper.java
| @@ -2,6 +2,7 @@ package cn.iocoder.yudao.module.trade.dal.mysql.cart; | @@ -2,6 +2,7 @@ package cn.iocoder.yudao.module.trade.dal.mysql.cart; | ||
| 2 | 2 | ||
| 3 | import cn.hutool.core.collection.CollUtil; | 3 | import cn.hutool.core.collection.CollUtil; |
| 4 | import cn.hutool.core.map.MapUtil; | 4 | import cn.hutool.core.map.MapUtil; |
| 5 | +import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils; | ||
| 5 | import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX; | 6 | import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX; |
| 6 | import cn.iocoder.yudao.module.trade.dal.dataobject.cart.TradeCartDO; | 7 | import cn.iocoder.yudao.module.trade.dal.dataobject.cart.TradeCartDO; |
| 7 | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; | 8 | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
| @@ -34,6 +35,19 @@ public interface TradeCartMapper extends BaseMapperX<TradeCartDO> { | @@ -34,6 +35,19 @@ public interface TradeCartMapper extends BaseMapperX<TradeCartDO> { | ||
| 34 | return CollUtil.getFirst(result) != null ? MapUtil.getInt(result.get(0), "sumCount") : 0; | 35 | return CollUtil.getFirst(result) != null ? MapUtil.getInt(result.get(0), "sumCount") : 0; |
| 35 | } | 36 | } |
| 36 | 37 | ||
| 38 | + default Map<Long, Integer> selectSumMapByUserId(Long userId) { | ||
| 39 | + // SQL sum 查询 | ||
| 40 | + List<Map<String, Object>> result = selectMaps(new QueryWrapper<TradeCartDO>() | ||
| 41 | + .select("spu_id, SUM(count) AS sumCount") | ||
| 42 | + .eq("user_id", userId) | ||
| 43 | + .eq("add_status", true) // 只计算添加到购物车中的 | ||
| 44 | + .eq("order_status", false) // 必须未下单 | ||
| 45 | + .groupBy("spu_id")); | ||
| 46 | + // 获得数量 | ||
| 47 | + return CollectionUtils.convertMap(result, item -> MapUtil.getLong(item, "spu_id"), | ||
| 48 | + item -> MapUtil.getInt(item, "sumCount")); | ||
| 49 | + } | ||
| 50 | + | ||
| 37 | default TradeCartDO selectById(Long id, Long userId) { | 51 | default TradeCartDO selectById(Long id, Long userId) { |
| 38 | return selectOne(TradeCartDO::getId, id, | 52 | return selectOne(TradeCartDO::getId, id, |
| 39 | TradeCartDO::getUserId, userId); | 53 | TradeCartDO::getUserId, userId); |
yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/service/cart/TradeCartService.java
| @@ -7,6 +7,7 @@ import cn.iocoder.yudao.module.trade.controller.app.cart.vo.AppTradeCartUpdateRe | @@ -7,6 +7,7 @@ import cn.iocoder.yudao.module.trade.controller.app.cart.vo.AppTradeCartUpdateRe | ||
| 7 | 7 | ||
| 8 | import javax.validation.Valid; | 8 | import javax.validation.Valid; |
| 9 | import java.util.Collection; | 9 | import java.util.Collection; |
| 10 | +import java.util.Map; | ||
| 10 | 11 | ||
| 11 | /** | 12 | /** |
| 12 | * 购物车 Service 接口 | 13 | * 购物车 Service 接口 |
| @@ -66,4 +67,12 @@ public interface TradeCartService { | @@ -66,4 +67,12 @@ public interface TradeCartService { | ||
| 66 | */ | 67 | */ |
| 67 | AppTradeCartListRespVO getCartList(Long userId); | 68 | AppTradeCartListRespVO getCartList(Long userId); |
| 68 | 69 | ||
| 70 | + /** | ||
| 71 | + * 获得用户的购物车商品 SPU 数量的 Map | ||
| 72 | + * | ||
| 73 | + * @param userId 用户编号 | ||
| 74 | + * @return 购物车商品 SPU 数量的 Map | ||
| 75 | + */ | ||
| 76 | + Map<Long, Integer> getCartCountMap(Long userId); | ||
| 77 | + | ||
| 69 | } | 78 | } |
yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/service/cart/TradeCartServiceImpl.java
| @@ -20,6 +20,7 @@ import javax.annotation.Resource; | @@ -20,6 +20,7 @@ import javax.annotation.Resource; | ||
| 20 | import java.util.Collection; | 20 | import java.util.Collection; |
| 21 | import java.util.Comparator; | 21 | import java.util.Comparator; |
| 22 | import java.util.List; | 22 | import java.util.List; |
| 23 | +import java.util.Map; | ||
| 23 | 24 | ||
| 24 | import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; | 25 | import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; |
| 25 | import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertSet; | 26 | import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertSet; |
| @@ -139,6 +140,11 @@ public class TradeCartServiceImpl implements TradeCartService { | @@ -139,6 +140,11 @@ public class TradeCartServiceImpl implements TradeCartService { | ||
| 139 | } | 140 | } |
| 140 | 141 | ||
| 141 | @Override | 142 | @Override |
| 143 | + public Map<Long, Integer> getCartCountMap(Long userId) { | ||
| 144 | + return cartMapper.selectSumMapByUserId(userId); | ||
| 145 | + } | ||
| 146 | + | ||
| 147 | + @Override | ||
| 142 | public AppTradeCartListRespVO getCartList(Long userId) { | 148 | public AppTradeCartListRespVO getCartList(Long userId) { |
| 143 | // 获得购物车的商品,只查询未下单的 | 149 | // 获得购物车的商品,只查询未下单的 |
| 144 | List<TradeCartDO> carts = cartMapper.selectListByUserId(userId, true, false); | 150 | List<TradeCartDO> carts = cartMapper.selectListByUserId(userId, true, false); |