diff --git a/order-service-project/order-service-api/src/main/java/cn/iocoder/mall/orderservice/rpc/cart/CartRpc.java b/order-service-project/order-service-api/src/main/java/cn/iocoder/mall/orderservice/rpc/cart/CartRpc.java index c1a5272d1..97d8ea2f1 100644 --- a/order-service-project/order-service-api/src/main/java/cn/iocoder/mall/orderservice/rpc/cart/CartRpc.java +++ b/order-service-project/order-service-api/src/main/java/cn/iocoder/mall/orderservice/rpc/cart/CartRpc.java @@ -1,8 +1,9 @@ package cn.iocoder.mall.orderservice.rpc.cart; import cn.iocoder.common.framework.vo.CommonResult; -import cn.iocoder.mall.orderservice.rpc.cart.dto.CartItemAddReqDTO; -import cn.iocoder.mall.orderservice.rpc.cart.dto.CartItemUpdateQuantityReqDTO; +import cn.iocoder.mall.orderservice.rpc.cart.dto.*; + +import java.util.List; /** * 购物车 Rpc 接口 @@ -20,9 +21,41 @@ public interface CartRpc { /** * 更新购物车商品数量 * - * @param updateQuantityReqDTO 更新商品数量 + * @param updateQuantityReqDTO 更新商品数量 DTO * @return 成功 */ - CommonResult updateCartItemSelected(CartItemUpdateQuantityReqDTO updateQuantityReqDTO); + CommonResult updateCartItemQuantity(CartItemUpdateQuantityReqDTO updateQuantityReqDTO); + + /** + * 更新购物车商品是否选中 + * + * @param updateSelectedReqDTO 更新商品是否选中 DTO + * @return 成功 + */ + CommonResult updateCartItemSelected(CartItemUpdateSelectedReqDTO updateSelectedReqDTO); + + /** + * 删除购物车商品列表 + * + * @param deleteListReqDTO 删除商品列表 DTO + * @return 成功 + */ + CommonResult deleteCartItems(CartItemDeleteListReqDTO deleteListReqDTO); + + /** + * 查询用户在购物车中的商品数量 + * + * @param userId 用户编号 + * @return 商品数量 + */ + CommonResult sumCartItemQuantity(Integer userId); + + /** + * 查询用户在购物车种的商品列表 + * + * @param listReqDTO 查询条件 DTO + * @return 购物车中商品列表信息 + */ + CommonResult> listCartItems(CartItemListReqDTO listReqDTO); } diff --git a/order-service-project/order-service-api/src/main/java/cn/iocoder/mall/orderservice/rpc/cart/dto/CartItemDeleteListReqDTO.java b/order-service-project/order-service-api/src/main/java/cn/iocoder/mall/orderservice/rpc/cart/dto/CartItemDeleteListReqDTO.java new file mode 100644 index 000000000..0956d00a2 --- /dev/null +++ b/order-service-project/order-service-api/src/main/java/cn/iocoder/mall/orderservice/rpc/cart/dto/CartItemDeleteListReqDTO.java @@ -0,0 +1,28 @@ +package cn.iocoder.mall.orderservice.rpc.cart.dto; + +import lombok.Data; +import lombok.experimental.Accessors; + +import javax.validation.constraints.NotNull; +import java.io.Serializable; +import java.util.List; + +/** + * 购物车删除商品列表 Request DTO + */ +@Data +@Accessors(chain = true) +public class CartItemDeleteListReqDTO implements Serializable { + + /** + * 用户编号 + */ + @NotNull(message = "用户编号不能为空") + private Integer userId; + /** + * 商品 SKU 编号列表 + */ + @NotNull(message = "商品 SKU 编号列表不能为空") + private List skuIds; + +} diff --git a/order-service-project/order-service-api/src/main/java/cn/iocoder/mall/orderservice/rpc/cart/dto/CartItemListReqDTO.java b/order-service-project/order-service-api/src/main/java/cn/iocoder/mall/orderservice/rpc/cart/dto/CartItemListReqDTO.java new file mode 100644 index 000000000..040ea13a6 --- /dev/null +++ b/order-service-project/order-service-api/src/main/java/cn/iocoder/mall/orderservice/rpc/cart/dto/CartItemListReqDTO.java @@ -0,0 +1,26 @@ +package cn.iocoder.mall.orderservice.rpc.cart.dto; + +import lombok.Data; +import lombok.experimental.Accessors; + +import javax.validation.constraints.NotNull; +import java.io.Serializable; + +/** + * 购物车的商品信息查询 BO + */ +@Data +@Accessors(chain = true) +public class CartItemListReqDTO implements Serializable { + + /** + * 用户编号 + */ + @NotNull(message = "用户编号不能为空") + private Integer userId; + /** + * 是否选中 + */ + private Boolean selected; + +} diff --git a/order-service-project/order-service-api/src/main/java/cn/iocoder/mall/orderservice/rpc/cart/dto/CartItemRespDTO.java b/order-service-project/order-service-api/src/main/java/cn/iocoder/mall/orderservice/rpc/cart/dto/CartItemRespDTO.java new file mode 100644 index 000000000..068ac06c8 --- /dev/null +++ b/order-service-project/order-service-api/src/main/java/cn/iocoder/mall/orderservice/rpc/cart/dto/CartItemRespDTO.java @@ -0,0 +1,67 @@ +package cn.iocoder.mall.orderservice.rpc.cart.dto; + +import lombok.Data; +import lombok.experimental.Accessors; + +import java.io.Serializable; + +/** + * 购物车的商品信息 Response DTO + */ +@Data +@Accessors(chain = true) +public class CartItemRespDTO implements Serializable { + + // ========= 基础字段 BEGIN ========= + + /** + * 编号,唯一自增。 + */ + private Integer id; + /** + * 是否选中 + */ + private Boolean selected; + + // ========= 基础字段 END ========= + + // ========= 买家信息 BEGIN ========= + + /** + * 用户编号 + */ + private Integer userId; + + // ========= 买家信息 END ========= + + // ========= 商品信息 BEGIN ========= + + /** + * 商品 SPU 编号 + */ + private Integer spuId; + /** + * 商品 SKU 编号 + */ + private Integer skuId; + /** + * 商品购买数量 + */ + private Integer quantity; + + // ========= 商品信息 END ========= + + // ========= 优惠信息 BEGIN ========= + +// /** +// * 商品营销活动编号 +// */ +// private Integer activityId; +// /** +// * 商品营销活动类型 +// */ +// private Integer activityType; + + // ========= 优惠信息 END ========= + +} diff --git a/order-service-project/order-service-api/src/main/java/cn/iocoder/mall/orderservice/rpc/cart/dto/CartItemUpdateSelectedReqDTO.java b/order-service-project/order-service-api/src/main/java/cn/iocoder/mall/orderservice/rpc/cart/dto/CartItemUpdateSelectedReqDTO.java new file mode 100644 index 000000000..075be10e9 --- /dev/null +++ b/order-service-project/order-service-api/src/main/java/cn/iocoder/mall/orderservice/rpc/cart/dto/CartItemUpdateSelectedReqDTO.java @@ -0,0 +1,33 @@ +package cn.iocoder.mall.orderservice.rpc.cart.dto; + +import lombok.Data; +import lombok.experimental.Accessors; + +import javax.validation.constraints.NotNull; +import java.io.Serializable; +import java.util.List; + +/** + * 购物车更新是否选中 Request DTO + */ +@Data +@Accessors(chain = true) +public class CartItemUpdateSelectedReqDTO implements Serializable { + + /** + * 用户编号 + */ + @NotNull(message = "用户编号不能为空") + private Integer userId; + /** + * 商品 SKU 编号列表 + */ + @NotNull(message = "商品 SKU 编号列表不能为空") + private List skuIds; + /** + * 是否选中 + */ + @NotNull(message = "是否选中不能为空") + private Boolean selected; + +} diff --git a/order-service-project/order-service-app/src/main/java/cn/iocoder/mall/orderservice/convert/cart/CartConvert.java b/order-service-project/order-service-app/src/main/java/cn/iocoder/mall/orderservice/convert/cart/CartConvert.java index 94803ea57..913ab20bb 100644 --- a/order-service-project/order-service-app/src/main/java/cn/iocoder/mall/orderservice/convert/cart/CartConvert.java +++ b/order-service-project/order-service-app/src/main/java/cn/iocoder/mall/orderservice/convert/cart/CartConvert.java @@ -2,10 +2,16 @@ package cn.iocoder.mall.orderservice.convert.cart; import cn.iocoder.mall.orderservice.dal.mysql.dataobject.cart.CartItemDO; import cn.iocoder.mall.orderservice.rpc.cart.dto.CartItemAddReqDTO; +import cn.iocoder.mall.orderservice.rpc.cart.dto.CartItemListReqDTO; +import cn.iocoder.mall.orderservice.rpc.cart.dto.CartItemRespDTO; import cn.iocoder.mall.orderservice.service.cart.bo.CartItemAddBO; +import cn.iocoder.mall.orderservice.service.cart.bo.CartItemBO; +import cn.iocoder.mall.orderservice.service.cart.bo.CartItemListQueryBO; import org.mapstruct.Mapper; import org.mapstruct.factory.Mappers; +import java.util.List; + @Mapper public interface CartConvert { @@ -15,4 +21,10 @@ public interface CartConvert { CartItemAddBO convert(CartItemAddReqDTO bean); + List convertList(List list); + + CartItemListQueryBO convert(CartItemListReqDTO bean); + + List convertList02(List list); + } diff --git a/order-service-project/order-service-app/src/main/java/cn/iocoder/mall/orderservice/dal/mysql/mapper/cart/CartItemMapper.java b/order-service-project/order-service-app/src/main/java/cn/iocoder/mall/orderservice/dal/mysql/mapper/cart/CartItemMapper.java index f7e051244..bb87a4933 100644 --- a/order-service-project/order-service-app/src/main/java/cn/iocoder/mall/orderservice/dal/mysql/mapper/cart/CartItemMapper.java +++ b/order-service-project/order-service-app/src/main/java/cn/iocoder/mall/orderservice/dal/mysql/mapper/cart/CartItemMapper.java @@ -2,6 +2,7 @@ package cn.iocoder.mall.orderservice.dal.mysql.mapper.cart; import cn.iocoder.mall.mybatis.core.query.QueryWrapperX; import cn.iocoder.mall.orderservice.dal.mysql.dataobject.cart.CartItemDO; +import cn.iocoder.mall.orderservice.service.cart.bo.CartItemListQueryBO; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.mapper.BaseMapper; import org.apache.ibatis.annotations.Param; @@ -9,6 +10,7 @@ import org.springframework.stereotype.Repository; import java.util.Collection; import java.util.List; +import java.util.Map; import java.util.Set; @Repository @@ -29,4 +31,18 @@ public interface CartItemMapper extends BaseMapper { ids.forEach(id -> updateById(updateObject.setId(id))); } + default Integer selectSumQuantityByUserId(Integer userId) { + // SQL sum 查询 + List> result = selectMaps(new QueryWrapper() + .select("SUM(quantity) AS sumQuantity") + .eq("user_id", userId)); + // 获得数量 + return (Integer) result.get(0).get("sumQuantity"); + } + + default List selectList(CartItemListQueryBO queryBO) { + return selectList(new QueryWrapperX().eq("user_id", queryBO.getUserId()) + .eq("selected", queryBO.getSelected())); + } + } diff --git a/order-service-project/order-service-app/src/main/java/cn/iocoder/mall/orderservice/manager/cart/CartManager.java b/order-service-project/order-service-app/src/main/java/cn/iocoder/mall/orderservice/manager/cart/CartManager.java index a355a8fb1..17ca53189 100644 --- a/order-service-project/order-service-app/src/main/java/cn/iocoder/mall/orderservice/manager/cart/CartManager.java +++ b/order-service-project/order-service-app/src/main/java/cn/iocoder/mall/orderservice/manager/cart/CartManager.java @@ -4,15 +4,17 @@ import cn.iocoder.common.framework.enums.CommonStatusEnum; import cn.iocoder.common.framework.exception.util.ServiceExceptionUtil; import cn.iocoder.common.framework.vo.CommonResult; import cn.iocoder.mall.orderservice.convert.cart.CartConvert; -import cn.iocoder.mall.orderservice.rpc.cart.dto.CartItemAddReqDTO; -import cn.iocoder.mall.orderservice.rpc.cart.dto.CartItemUpdateQuantityReqDTO; +import cn.iocoder.mall.orderservice.rpc.cart.dto.*; import cn.iocoder.mall.orderservice.service.cart.CartService; +import cn.iocoder.mall.orderservice.service.cart.bo.CartItemBO; import cn.iocoder.mall.productservice.rpc.sku.ProductSkuRpc; import cn.iocoder.mall.productservice.rpc.sku.dto.ProductSkuRespDTO; import org.apache.dubbo.config.annotation.DubboReference; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; +import java.util.List; + import static cn.iocoder.mall.orderservice.enums.OrderErrorCodeConstants.CARD_ITEM_SKU_NOT_FOUND; /** @@ -52,6 +54,55 @@ public class CartManager { updateQuantityReqDTO.getQuantity(), skuDTO.getQuantity()); } + /** + * 更新购物车商品是否选中 + * + * @param updateSelectedReqDTO 更新商品是否选中 DTO + */ + public void updateCartItemSelected(CartItemUpdateSelectedReqDTO updateSelectedReqDTO) { + cartService.updateCartItemSelected(updateSelectedReqDTO.getUserId(), + updateSelectedReqDTO.getSkuIds(), updateSelectedReqDTO.getSelected()); + } + + /** + * 删除购物车商品列表 + * + * @param deleteListReqDTO 删除商品列表 DTO + */ + public void deleteCartItems(CartItemDeleteListReqDTO deleteListReqDTO) { + cartService.deleteCartItems(deleteListReqDTO.getUserId(), + deleteListReqDTO.getSkuIds()); + } + + /** + * 查询用户在购物车中的商品数量 + * + * @param userId 用户编号 + * @return 商品数量 + */ + public Integer sumCartItemQuantity(Integer userId) { + return cartService.sumCartItemQuantity(userId); + } + + /** + * 查询用户在购物车种的商品列表 + * + * @param listReqDTO 查询条件 DTO + * @return 购物车中商品列表信息 + */ + public List listCartItems(CartItemListReqDTO listReqDTO) { + List cartItemBOs = cartService.listCartItems(CartConvert.INSTANCE.convert(listReqDTO)); + return CartConvert.INSTANCE.convertList02(cartItemBOs); + } + + /** + * 校验商品 SKU 是否合法 + * 1. 是否存在 + * 2. 是否下架 + * + * @param skuId 商品 SKU 编号 + * @return 商品 SKU 信息 + */ private ProductSkuRespDTO checkProductSku(Integer skuId) { CommonResult getProductSkuResult = productSkuRpc.getProductSku(skuId); getProductSkuResult.checkError(); diff --git a/order-service-project/order-service-app/src/main/java/cn/iocoder/mall/orderservice/rpc/cart/CartRpcImpl.java b/order-service-project/order-service-app/src/main/java/cn/iocoder/mall/orderservice/rpc/cart/CartRpcImpl.java index 260ae7638..491e9211f 100644 --- a/order-service-project/order-service-app/src/main/java/cn/iocoder/mall/orderservice/rpc/cart/CartRpcImpl.java +++ b/order-service-project/order-service-app/src/main/java/cn/iocoder/mall/orderservice/rpc/cart/CartRpcImpl.java @@ -2,11 +2,14 @@ package cn.iocoder.mall.orderservice.rpc.cart; import cn.iocoder.common.framework.vo.CommonResult; import cn.iocoder.mall.orderservice.manager.cart.CartManager; -import cn.iocoder.mall.orderservice.rpc.cart.dto.CartItemAddReqDTO; -import cn.iocoder.mall.orderservice.rpc.cart.dto.CartItemUpdateQuantityReqDTO; +import cn.iocoder.mall.orderservice.rpc.cart.dto.*; import org.apache.dubbo.config.annotation.DubboService; import org.springframework.beans.factory.annotation.Autowired; +import java.util.List; + +import static cn.iocoder.common.framework.vo.CommonResult.success; + @DubboService public class CartRpcImpl implements CartRpc { @@ -16,13 +19,35 @@ public class CartRpcImpl implements CartRpc { @Override public CommonResult addCartItem(CartItemAddReqDTO addItemReqDTO) { cartManager.addCartItem(addItemReqDTO); - return CommonResult.success(true); + return success(true); } @Override - public CommonResult updateCartItemSelected(CartItemUpdateQuantityReqDTO updateQuantityReqDTO) { + public CommonResult updateCartItemQuantity(CartItemUpdateQuantityReqDTO updateQuantityReqDTO) { cartManager.updateCartItemSelected(updateQuantityReqDTO); - return CommonResult.success(true); + return success(true); + } + + @Override + public CommonResult updateCartItemSelected(CartItemUpdateSelectedReqDTO updateSelectedReqDTO) { + cartManager.updateCartItemSelected(updateSelectedReqDTO); + return success(true); + } + + @Override + public CommonResult deleteCartItems(CartItemDeleteListReqDTO deleteListReqDTO) { + cartManager.deleteCartItems(deleteListReqDTO); + return success(true); + } + + @Override + public CommonResult sumCartItemQuantity(Integer userId) { + return success(cartManager.sumCartItemQuantity(userId)); + } + + @Override + public CommonResult> listCartItems(CartItemListReqDTO listReqDTO) { + return success(cartManager.listCartItems(listReqDTO)); } } diff --git a/order-service-project/order-service-app/src/main/java/cn/iocoder/mall/orderservice/service/cart/CartService.java b/order-service-project/order-service-app/src/main/java/cn/iocoder/mall/orderservice/service/cart/CartService.java index c66e5ce6d..bf78f2ba7 100644 --- a/order-service-project/order-service-app/src/main/java/cn/iocoder/mall/orderservice/service/cart/CartService.java +++ b/order-service-project/order-service-app/src/main/java/cn/iocoder/mall/orderservice/service/cart/CartService.java @@ -6,6 +6,8 @@ import cn.iocoder.mall.orderservice.convert.cart.CartConvert; import cn.iocoder.mall.orderservice.dal.mysql.dataobject.cart.CartItemDO; import cn.iocoder.mall.orderservice.dal.mysql.mapper.cart.CartItemMapper; import cn.iocoder.mall.orderservice.service.cart.bo.CartItemAddBO; +import cn.iocoder.mall.orderservice.service.cart.bo.CartItemBO; +import cn.iocoder.mall.orderservice.service.cart.bo.CartItemListQueryBO; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.validation.annotation.Validated; @@ -99,13 +101,35 @@ public class CartService { * @param userId 用户编号 * @param skuIds 商品 SKU 编号的数组 */ - public void deleteList(Integer userId, List skuIds) { + public void deleteCartItems(Integer userId, List skuIds) { // 查询 CartItemDO 列表 List itemDOs = cartItemMapper.selectListByUserIdAndSkuIds(userId, skuIds); + if (CollectionUtils.isEmpty(itemDOs)) { + return; + } // 批量标记删除 cartItemMapper.deleteBatchIds(CollectionUtils.convertSet(itemDOs, CartItemDO::getId)); } + /** + * 查询用户在购物车中的商品数量 + * + * @param userId 用户编号 + * @return 商品数量 + */ + public Integer sumCartItemQuantity(Integer userId) { + return cartItemMapper.selectSumQuantityByUserId(userId); + } + /** + * 查询用户在购物车种的商品列表 + * + * @param queryBO 查询条件 BO + * @return 购物车中商品列表信息 + */ + public List listCartItems(CartItemListQueryBO queryBO) { + List cartItemDOs = cartItemMapper.selectList(queryBO); + return CartConvert.INSTANCE.convertList(cartItemDOs); + } } diff --git a/order-service-project/order-service-app/src/main/java/cn/iocoder/mall/orderservice/service/cart/bo/CartItemListQueryBO.java b/order-service-project/order-service-app/src/main/java/cn/iocoder/mall/orderservice/service/cart/bo/CartItemListQueryBO.java new file mode 100644 index 000000000..c94970da1 --- /dev/null +++ b/order-service-project/order-service-app/src/main/java/cn/iocoder/mall/orderservice/service/cart/bo/CartItemListQueryBO.java @@ -0,0 +1,25 @@ +package cn.iocoder.mall.orderservice.service.cart.bo; + +import lombok.Data; +import lombok.experimental.Accessors; + +import javax.validation.constraints.NotNull; + +/** + * 购物车的商品信息查询 BO + */ +@Data +@Accessors(chain = true) +public class CartItemListQueryBO { + + /** + * 用户编号 + */ + @NotNull(message = "用户编号不能为空") + private Integer userId; + /** + * 是否选中 + */ + private Boolean selected; + +} diff --git a/order/order-biz/src/main/java/cn/iocoder/mall/order/biz/service/CartService.java b/order/order-biz/src/main/java/cn/iocoder/mall/order/biz/service/CartService.java index 9982cc0d1..e2aaf0fcd 100644 --- a/order/order-biz/src/main/java/cn/iocoder/mall/order/biz/service/CartService.java +++ b/order/order-biz/src/main/java/cn/iocoder/mall/order/biz/service/CartService.java @@ -3,27 +3,6 @@ package cn.iocoder.mall.order.biz.service; public interface CartService { -// Boolean deleteList(Integer userId, List skuIds); -// - -// -// /** -// * 查询用户在购物车中的商品数量 -// * -// * @param userId 用户编号 -// * @return 商品数量 -// */ -// Integer count(Integer userId); -// -// /** -// * 显示买家购物车中的商品列表,并根据 selected 进行过滤。 -// * -// * @param userId 用户编号 -// * @param selected 是否选中。若为空,则不进行筛选 -// * @return 购物车中商品列表信息 -// */ -// List list(Integer userId, @Nullable Boolean selected); -// // // ========== 购物车与订单相关的逻辑 ========== // // /** diff --git a/order/order-service-impl/src/main/java/cn/iocoder/mall/order/biz/service/CartServiceImpl.java b/order/order-service-impl/src/main/java/cn/iocoder/mall/order/biz/service/CartServiceImpl.java index 9293ffd36..d61a87004 100644 --- a/order/order-service-impl/src/main/java/cn/iocoder/mall/order/biz/service/CartServiceImpl.java +++ b/order/order-service-impl/src/main/java/cn/iocoder/mall/order/biz/service/CartServiceImpl.java @@ -45,17 +45,6 @@ public class CartServiceImpl implements CartService { @Autowired private CartMapper cartMapper; - @Override - public Integer count(Integer userId) { - return cartMapper.selectQuantitySumByUserIdAndStatus(userId, CartItemStatusEnum.ENABLE.getValue()); - } - - @Override - public List list(Integer userId, Boolean selected) { - List items = cartMapper.selectByUserIdAndStatusAndSelected(userId, CartItemStatusEnum.ENABLE.getValue(), selected); - return CartConvert.INSTANCE.convert(items); - } - @Override public CalcOrderPriceBO calcOrderPrice(CalcOrderPriceDTO calcOrderPriceDTO) { // TODO 芋艿,补充一些表单校验。例如说,需要传入用户编号。