From 11b2f67ed18310bff83997227a5016d4a8239547 Mon Sep 17 00:00:00 2001 From: YunaiV <> Date: Tue, 4 Aug 2020 22:08:37 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=95=86=E5=93=81=E8=AF=A6?= =?UTF-8?q?=E6=83=85=20RPC=20=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../convert/product/ProductAttrConvert.java | 2 +- .../service/spu/ProductSpuServiceImpl.java | 57 ---------- .../mall/product/api/ProductAttrService.java | 1 - .../mall/product/api/ProductSpuService.java | 9 -- .../product/api/bo/ProductSpuDetailBO.java | 1 + .../product/api/dto/ProductAttrPageDTO.java | 22 ---- .../product/convert/ProductSpuConvert.java | 25 +---- .../users/UsersProductSpuController.java | 20 ---- .../vo/users/UsersProductSpuVO.java | 42 ------- .../enums/spu/ProductSpuDetailFieldEnum.java | 28 +++++ .../rpc/attr/ProductAttrRpc.java | 2 +- .../attr/dto/ProductAttrKeyValueRespDTO.java | 25 +++++ ...a => ProductAttrValueListQueryReqDTO.java} | 2 +- .../productservice/rpc/spu/ProductSpuRpc.java | 8 +- .../rpc/spu/dto/ProductSpuDetailRespDTO.java | 106 ++++++++++++++++++ .../convert/attr/ProductAttrConvert.java | 2 +- .../convert/spu/ProductSpuConvert.java | 39 ++++++- .../mysql/mapper/sku/ProductSkuMapper.java | 3 +- .../manager/attr/ProductAttrManager.java | 2 +- .../manager/spu/ProductSpuManager.java | 40 ++++++- .../rpc/attr/ProductAttrRpcImpl.java | 2 +- .../rpc/spu/ProductSpuRpcImpl.java | 11 +- .../service/sku/bo/ProductSkuListQueryBO.java | 4 + 23 files changed, 255 insertions(+), 198 deletions(-) delete mode 100644 moved/product/product-biz/src/main/java/cn/iocoder/mall/product/biz/service/spu/ProductSpuServiceImpl.java delete mode 100644 moved/product/product-service-api/src/main/java/cn/iocoder/mall/product/api/dto/ProductAttrPageDTO.java delete mode 100644 moved/product/product-start/src/main/java/cn/iocoder/mall/product/application/vo/users/UsersProductSpuVO.java create mode 100644 product-service-project/product-service-api/src/main/java/cn/iocoder/mall/productservice/enums/spu/ProductSpuDetailFieldEnum.java create mode 100644 product-service-project/product-service-api/src/main/java/cn/iocoder/mall/productservice/rpc/attr/dto/ProductAttrKeyValueRespDTO.java rename product-service-project/product-service-api/src/main/java/cn/iocoder/mall/productservice/rpc/attr/dto/{ProductAttrValueListQueryRequestDTO.java => ProductAttrValueListQueryReqDTO.java} (85%) create mode 100644 product-service-project/product-service-api/src/main/java/cn/iocoder/mall/productservice/rpc/spu/dto/ProductSpuDetailRespDTO.java diff --git a/management-web-app/src/main/java/cn/iocoder/mall/managementweb/convert/product/ProductAttrConvert.java b/management-web-app/src/main/java/cn/iocoder/mall/managementweb/convert/product/ProductAttrConvert.java index 60d0cfd34..5ea731780 100644 --- a/management-web-app/src/main/java/cn/iocoder/mall/managementweb/convert/product/ProductAttrConvert.java +++ b/management-web-app/src/main/java/cn/iocoder/mall/managementweb/convert/product/ProductAttrConvert.java @@ -34,6 +34,6 @@ public interface ProductAttrConvert { List convertList02(List list); - ProductAttrValueListQueryRequestDTO convert(ProductAttrValueListQueryReqVO bean); + ProductAttrValueListQueryReqDTO convert(ProductAttrValueListQueryReqVO bean); } diff --git a/moved/product/product-biz/src/main/java/cn/iocoder/mall/product/biz/service/spu/ProductSpuServiceImpl.java b/moved/product/product-biz/src/main/java/cn/iocoder/mall/product/biz/service/spu/ProductSpuServiceImpl.java deleted file mode 100644 index 9735aa181..000000000 --- a/moved/product/product-biz/src/main/java/cn/iocoder/mall/product/biz/service/spu/ProductSpuServiceImpl.java +++ /dev/null @@ -1,57 +0,0 @@ -package cn.iocoder.mall.product.biz.service.spu; - -import cn.iocoder.common.framework.util.ServiceExceptionUtil; -import cn.iocoder.common.framework.util.StringUtil; -import cn.iocoder.mall.mybatis.core.enums.DeletedStatusEnum; -import cn.iocoder.mall.product.biz.bo.product.ProductAttrAndValuePairBO; -import cn.iocoder.mall.product.biz.bo.product.ProductSpuDetailBO; -import cn.iocoder.mall.product.biz.convert.sku.ProductSpuConvert; -import cn.iocoder.mall.product.biz.dao.category.ProductCategoryMapper; -import cn.iocoder.mall.product.biz.dao.sku.ProductSkuMapper; -import cn.iocoder.mall.product.biz.dao.sku.ProductSpuMapper; -import cn.iocoder.mall.product.biz.dataobject.category.ProductCategoryDO; -import cn.iocoder.mall.product.biz.dataobject.spu.ProductSkuDO; -import cn.iocoder.mall.product.biz.dataobject.spu.ProductSpuDO; -import cn.iocoder.mall.product.biz.dto.sku.ProductSkuAddOrUpdateDTO; -import cn.iocoder.mall.product.biz.dto.sku.ProductSpuAddDTO; -import cn.iocoder.mall.product.biz.enums.ProductErrorCodeEnum; -import cn.iocoder.mall.product.biz.enums.category.ProductCategoryNodeEnum; -import cn.iocoder.mall.product.biz.enums.spu.ProductSpuConstants; -import cn.iocoder.mall.product.biz.service.attr.ProductAttrService; -import cn.iocoder.mall.product.biz.service.category.ProductCategoryService; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Service; -import org.springframework.transaction.annotation.Transactional; -import org.springframework.util.Assert; - -import java.util.*; -import java.util.stream.Collectors; - -@Service -public class ProductSpuServiceImpl implements ProductSpuService { - - @Autowired - private ProductAttrService productAttrService; - - @Override - public ProductSpuDetailBO getProductSpuDetail(Integer spuId) { - // 校验商品 spu 存在 - ProductSpuDO spu = productSpuMapper.selectById(spuId); - if (spu == null) { - throw ServiceExceptionUtil.exception(ProductErrorCodeEnum.PRODUCT_SPU_NOT_EXISTS.getCode()); - } - // 获得商品分类分类 - ProductCategoryDO category = productCategoryMapper.selectById(spu.getCid()); - Assert.notNull(category, String.format("分类编号(%d) 对应", spu.getCid())); - // 获得商品 sku 数组 - List skus = productSkuMapper.selectListBySpuIdAndStatus(spuId, ProductSpuConstants.SKU_STATUS_ENABLE); - // 获得规格 - Set productAttrValueIds = new HashSet<>(); - skus.forEach(sku -> productAttrValueIds.addAll(StringUtil.splitToInt(sku.getAttrs(), ","))); - // 读取规格时,不考虑规格是否被禁用 - List attrAndValuePairList = productAttrService.validProductAttrAndValue(productAttrValueIds, false); - // 返回成功 - return ProductSpuConvert.INSTANCE.convert2(spu, skus, attrAndValuePairList, category); - } - -} diff --git a/moved/product/product-service-api/src/main/java/cn/iocoder/mall/product/api/ProductAttrService.java b/moved/product/product-service-api/src/main/java/cn/iocoder/mall/product/api/ProductAttrService.java index 6c749d058..86f008413 100644 --- a/moved/product/product-service-api/src/main/java/cn/iocoder/mall/product/api/ProductAttrService.java +++ b/moved/product/product-service-api/src/main/java/cn/iocoder/mall/product/api/ProductAttrService.java @@ -12,7 +12,6 @@ import java.util.List; public interface ProductAttrService { - ProductAttrPageBO getProductAttrPage(ProductAttrPageDTO productAttrPageDTO); /** * 获得规格属性数组 diff --git a/moved/product/product-service-api/src/main/java/cn/iocoder/mall/product/api/ProductSpuService.java b/moved/product/product-service-api/src/main/java/cn/iocoder/mall/product/api/ProductSpuService.java index f1b4c2d71..718a2b081 100644 --- a/moved/product/product-service-api/src/main/java/cn/iocoder/mall/product/api/ProductSpuService.java +++ b/moved/product/product-service-api/src/main/java/cn/iocoder/mall/product/api/ProductSpuService.java @@ -13,15 +13,6 @@ public interface ProductSpuService { ProductSpuDetailBO getProductSpuDetail(Integer id); - /** - * 增量获得商品列表,按照 lastId 递增获得 - * - * @param lastId 最后查询的编号 - * @param limit 大小 - * @return 商品列表 - */ - List getProductSpuDetailListForSync(Integer lastId, Integer limit); - List getProductSpuSearchList(ProductSpuSearchListDTO productSpuSearchListDTO); List getProductSpuList(Collection ids); diff --git a/moved/product/product-service-api/src/main/java/cn/iocoder/mall/product/api/bo/ProductSpuDetailBO.java b/moved/product/product-service-api/src/main/java/cn/iocoder/mall/product/api/bo/ProductSpuDetailBO.java index ad3d7000b..63e7bdc6b 100644 --- a/moved/product/product-service-api/src/main/java/cn/iocoder/mall/product/api/bo/ProductSpuDetailBO.java +++ b/moved/product/product-service-api/src/main/java/cn/iocoder/mall/product/api/bo/ProductSpuDetailBO.java @@ -11,6 +11,7 @@ import java.util.List; */ @Data @Accessors(chain = true) +@Deprecated public class ProductSpuDetailBO implements Serializable { /** diff --git a/moved/product/product-service-api/src/main/java/cn/iocoder/mall/product/api/dto/ProductAttrPageDTO.java b/moved/product/product-service-api/src/main/java/cn/iocoder/mall/product/api/dto/ProductAttrPageDTO.java deleted file mode 100644 index ef27ada19..000000000 --- a/moved/product/product-service-api/src/main/java/cn/iocoder/mall/product/api/dto/ProductAttrPageDTO.java +++ /dev/null @@ -1,22 +0,0 @@ -package cn.iocoder.mall.product.api.dto; - -import lombok.Data; -import lombok.experimental.Accessors; - -import javax.validation.constraints.NotNull; - -/** - * 商品规格分页 DTO - */ -@Data -@Accessors(chain = true) -public class ProductAttrPageDTO { - - private String name; - - @NotNull(message = "页码不能为空") - private Integer pageNo; - @NotNull(message = "每页条数不能为空") - private Integer pageSize; - -} diff --git a/moved/product/product-service-impl/src/main/java/cn/iocoder/mall/product/convert/ProductSpuConvert.java b/moved/product/product-service-impl/src/main/java/cn/iocoder/mall/product/convert/ProductSpuConvert.java index a957b51ee..6aea9a899 100644 --- a/moved/product/product-service-impl/src/main/java/cn/iocoder/mall/product/convert/ProductSpuConvert.java +++ b/moved/product/product-service-impl/src/main/java/cn/iocoder/mall/product/convert/ProductSpuConvert.java @@ -81,30 +81,7 @@ public interface ProductSpuConvert { }) ProductSkuBO convert4(ProductSkuDO sku); - @Mappings({}) // TODO 芋艿,后续细看下 mapstruct 的 API ,优化这块 - default ProductSpuDetailBO convert2(ProductSpuDO spu, List skus, List productAttrDetailBOs, - ProductCategoryDO category) { - // 创建并转换 ProductSpuDetailBO 对象 - ProductSpuDetailBO spuDetail = this.convert2(spu).setPicUrls(StringUtil.split(spu.getPicUrls(), ",")); - // 创建 ProductAttrDetailBO 的映射。其中,KEY 为 ProductAttrDetailBO.attrValueId ,即规格值的编号 - Map productAttrDetailBOMap = productAttrDetailBOs.stream().collect( - Collectors.toMap(ProductAttrAndValuePairBO::getAttrValueId, productAttrDetailBO -> productAttrDetailBO)); - // 创建并转换 ProductSpuDetailBO 数组 - spuDetail.setSkus(new ArrayList<>()); - skus.forEach(sku -> { - // 创建 ProductSpuDetailBO 对象 - ProductSpuDetailBO.Sku skuDetail = ProductSpuConvert.this.convert2(sku) - .setAttrs(new ArrayList<>()); - spuDetail.getSkus().add(skuDetail); - // 设置 ProductSpuDetailBO 的 attrs 规格属性 - List attrs = StringUtil.split(sku.getAttrs(), ","); - attrs.forEach(attr -> skuDetail.getAttrs().add(productAttrDetailBOMap.get(Integer.valueOf(attr)))); - }); - // 设置分类名 - spuDetail.setCategoryName(category.getName()); - // 返回 - return spuDetail; - } + @Mappings({}) // TODO 芋艿,后续细看下 mapstruct 的 API ,优化这块 default List convert3(List skus, List spus, List productAttrDetailBOs) { diff --git a/moved/product/product-start/src/main/java/cn/iocoder/mall/product/application/controller/users/UsersProductSpuController.java b/moved/product/product-start/src/main/java/cn/iocoder/mall/product/application/controller/users/UsersProductSpuController.java index 4f3e3c327..be4dab6b2 100644 --- a/moved/product/product-start/src/main/java/cn/iocoder/mall/product/application/controller/users/UsersProductSpuController.java +++ b/moved/product/product-start/src/main/java/cn/iocoder/mall/product/application/controller/users/UsersProductSpuController.java @@ -34,24 +34,4 @@ public class UsersProductSpuController { return success(ProductSpuConvert.INSTANCE.convert4(productSpuService.getProductSpuDetail(id))); } - @GetMapping("/page") - @ApiOperation("商品 SPU 分页列表") - @ApiImplicitParams({ - @ApiImplicitParam(name = "cid", value = "分类编号", example = "1"), - @ApiImplicitParam(name = "pageNo", value = "页码,从 1 开始", example = "1"), - @ApiImplicitParam(name = "pageSize", value = "每页条数", required = true, example = "10"), - }) - @Deprecated // 使用商品搜索接口 - public CommonResult page(@RequestParam(value = "cid", required = false) Integer cid, - @RequestParam(value = "pageNo", defaultValue = "0") Integer pageNo, - @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) { - // 创建 ProductSpuPageDTO 对象 - ProductSpuPageDTO productSpuPageDTO = new ProductSpuPageDTO().setCid(cid).setVisible(true) - .setPageNo(pageNo).setPageSize(pageSize); - // 查询分页 - ProductSpuPageBO result = productSpuService.getProductSpuPage(productSpuPageDTO); - // 返回结果 - return success(ProductSpuConvert.INSTANCE.convert3(result)); - } - } diff --git a/moved/product/product-start/src/main/java/cn/iocoder/mall/product/application/vo/users/UsersProductSpuVO.java b/moved/product/product-start/src/main/java/cn/iocoder/mall/product/application/vo/users/UsersProductSpuVO.java deleted file mode 100644 index 1236a0e12..000000000 --- a/moved/product/product-start/src/main/java/cn/iocoder/mall/product/application/vo/users/UsersProductSpuVO.java +++ /dev/null @@ -1,42 +0,0 @@ -package cn.iocoder.mall.product.application.vo.users; - -import io.swagger.annotations.ApiModel; -import io.swagger.annotations.ApiModelProperty; -import lombok.Data; -import lombok.experimental.Accessors; - -import java.util.List; - -@ApiModel(value = "商品 SPU VO", description = "不包括 SKU 信息 VO") -@Data -@Accessors(chain = true) -public class UsersProductSpuVO { - - @ApiModelProperty(value = "SPU 编号", required = true, example = "1") - private Integer id; - - // ========== 基本信息 ========= - @ApiModelProperty(value = "SPU 名字", required = true, example = "厮大牛逼") - private String name; - @ApiModelProperty(value = "卖点", required = true, example = "各种 MQ 骚操作") - private String sellPoint; - @ApiModelProperty(value = "分类编号", required = true, example = "反正我是信了") - private Integer cid; - @ApiModelProperty(value = "商品主图地址的数组", required = true, example = "http://www.iocoder.cn") - private List picUrls; - - // ========== Sku 相关字段 ========= - /** - * 价格 - * - * 目前的计算方式是,以 Sku 最小价格为准 - */ - private Integer price; - /** - * 库存数量 - * - * 目前的计算方式是,以 Sku 库存累加为准 - */ - private Integer quantity; - -} diff --git a/product-service-project/product-service-api/src/main/java/cn/iocoder/mall/productservice/enums/spu/ProductSpuDetailFieldEnum.java b/product-service-project/product-service-api/src/main/java/cn/iocoder/mall/productservice/enums/spu/ProductSpuDetailFieldEnum.java new file mode 100644 index 000000000..20b3c3796 --- /dev/null +++ b/product-service-project/product-service-api/src/main/java/cn/iocoder/mall/productservice/enums/spu/ProductSpuDetailFieldEnum.java @@ -0,0 +1,28 @@ +package cn.iocoder.mall.productservice.enums.spu; + +import cn.iocoder.mall.productservice.rpc.spu.dto.ProductSpuDetailRespDTO; + +/** + * 商品 SPU 明细的字段枚举 + * + * @see ProductSpuDetailRespDTO + */ +public enum ProductSpuDetailFieldEnum { + + SKU("sku"), + ATTR("attr"); + + /** + * 字段 + */ + private final String field; + + ProductSpuDetailFieldEnum(String field) { + this.field = field; + } + + public String getField() { + return field; + } + +} diff --git a/product-service-project/product-service-api/src/main/java/cn/iocoder/mall/productservice/rpc/attr/ProductAttrRpc.java b/product-service-project/product-service-api/src/main/java/cn/iocoder/mall/productservice/rpc/attr/ProductAttrRpc.java index 385e224b3..b36697489 100644 --- a/product-service-project/product-service-api/src/main/java/cn/iocoder/mall/productservice/rpc/attr/ProductAttrRpc.java +++ b/product-service-project/product-service-api/src/main/java/cn/iocoder/mall/productservice/rpc/attr/ProductAttrRpc.java @@ -79,6 +79,6 @@ public interface ProductAttrRpc { * @param queryDTO 商品规格值的列表查询条件 DTO * @return 商品规格值列表 */ - CommonResult> listProductAttrValues(ProductAttrValueListQueryRequestDTO queryDTO); + CommonResult> listProductAttrValues(ProductAttrValueListQueryReqDTO queryDTO); } diff --git a/product-service-project/product-service-api/src/main/java/cn/iocoder/mall/productservice/rpc/attr/dto/ProductAttrKeyValueRespDTO.java b/product-service-project/product-service-api/src/main/java/cn/iocoder/mall/productservice/rpc/attr/dto/ProductAttrKeyValueRespDTO.java new file mode 100644 index 000000000..0e5906f05 --- /dev/null +++ b/product-service-project/product-service-api/src/main/java/cn/iocoder/mall/productservice/rpc/attr/dto/ProductAttrKeyValueRespDTO.java @@ -0,0 +1,25 @@ +package cn.iocoder.mall.productservice.rpc.attr.dto; + +/** + * 商品规格 KEY + VALUE 对的 Response DTO + */ +public class ProductAttrKeyValueRespDTO { + + /** + * 规格 KEY 编号 + */ + private Integer attrKeyId; + /** + * 规格 KEY 名 + */ + private String attrKeyName; + /** + * 规格 VALUE 编号 + */ + private Integer attrValueId; + /** + * 规格 VALUE 名 + */ + private String attrValueName; + +} diff --git a/product-service-project/product-service-api/src/main/java/cn/iocoder/mall/productservice/rpc/attr/dto/ProductAttrValueListQueryRequestDTO.java b/product-service-project/product-service-api/src/main/java/cn/iocoder/mall/productservice/rpc/attr/dto/ProductAttrValueListQueryReqDTO.java similarity index 85% rename from product-service-project/product-service-api/src/main/java/cn/iocoder/mall/productservice/rpc/attr/dto/ProductAttrValueListQueryRequestDTO.java rename to product-service-project/product-service-api/src/main/java/cn/iocoder/mall/productservice/rpc/attr/dto/ProductAttrValueListQueryReqDTO.java index d11f7b31d..745502d61 100644 --- a/product-service-project/product-service-api/src/main/java/cn/iocoder/mall/productservice/rpc/attr/dto/ProductAttrValueListQueryRequestDTO.java +++ b/product-service-project/product-service-api/src/main/java/cn/iocoder/mall/productservice/rpc/attr/dto/ProductAttrValueListQueryReqDTO.java @@ -11,7 +11,7 @@ import java.util.List; */ @Data @Accessors(chain = true) -public class ProductAttrValueListQueryRequestDTO implements Serializable { +public class ProductAttrValueListQueryReqDTO implements Serializable { /** * 商品规格值编号列表 diff --git a/product-service-project/product-service-api/src/main/java/cn/iocoder/mall/productservice/rpc/spu/ProductSpuRpc.java b/product-service-project/product-service-api/src/main/java/cn/iocoder/mall/productservice/rpc/spu/ProductSpuRpc.java index df7cca42f..0076212e8 100644 --- a/product-service-project/product-service-api/src/main/java/cn/iocoder/mall/productservice/rpc/spu/ProductSpuRpc.java +++ b/product-service-project/product-service-api/src/main/java/cn/iocoder/mall/productservice/rpc/spu/ProductSpuRpc.java @@ -2,11 +2,9 @@ package cn.iocoder.mall.productservice.rpc.spu; import cn.iocoder.common.framework.vo.CommonResult; import cn.iocoder.common.framework.vo.PageResult; -import cn.iocoder.mall.productservice.rpc.spu.dto.ProductSpuAndSkuCreateReqDTO; -import cn.iocoder.mall.productservice.rpc.spu.dto.ProductSpuAndSkuUpdateReqDTO; -import cn.iocoder.mall.productservice.rpc.spu.dto.ProductSpuPageReqDTO; -import cn.iocoder.mall.productservice.rpc.spu.dto.ProductSpuRespDTO; +import cn.iocoder.mall.productservice.rpc.spu.dto.*; +import java.util.Collection; import java.util.List; /** @@ -62,4 +60,6 @@ public interface ProductSpuRpc { */ CommonResult> listProductSpuIds(Integer lastSpuId, Integer limit); + CommonResult getProductSpuDetail(Integer productSpuId, Collection fields); + } diff --git a/product-service-project/product-service-api/src/main/java/cn/iocoder/mall/productservice/rpc/spu/dto/ProductSpuDetailRespDTO.java b/product-service-project/product-service-api/src/main/java/cn/iocoder/mall/productservice/rpc/spu/dto/ProductSpuDetailRespDTO.java new file mode 100644 index 000000000..a8b3f4402 --- /dev/null +++ b/product-service-project/product-service-api/src/main/java/cn/iocoder/mall/productservice/rpc/spu/dto/ProductSpuDetailRespDTO.java @@ -0,0 +1,106 @@ +package cn.iocoder.mall.productservice.rpc.spu.dto; + +import cn.iocoder.mall.productservice.rpc.attr.dto.ProductAttrKeyValueRespDTO; +import lombok.Data; +import lombok.experimental.Accessors; + +import java.io.Serializable; +import java.util.List; + +/** + * 商品 SPU 明细 Response DTO + */ +@Data +@Accessors(chain = true) +public class ProductSpuDetailRespDTO implements Serializable { + + /** + * SPU 编号 + */ + private Integer id; + + // ========== 基本信息 ========= + /** + * SPU 名字 + */ + private String name; + /** + * 卖点 + */ + private String sellPoint; + /** + * 描述 + */ + private String description; + /** + * 分类编号 + */ + private Integer cid; + /** + * 分类名 + */ + private String categoryName; + /** + * 商品主图地址 + * + * 数组,以逗号分隔 + * + * 建议尺寸:800*800像素,你可以拖拽图片调整顺序,最多上传15张 + */ + private List picUrls; + + // ========== 其他信息 ========= + /** + * 是否上架商品(是否可见)。 + * + * true 为已上架 + * false 为已下架 + */ + private Boolean visible; + /** + * 排序字段 + */ + private Integer sort; + + // ========== SKU ========= + + /** + * SKU 数组 + */ + private List skus; + + /** + * 商品 Sku 明细 + */ + @Data + @Accessors(chain = true) + public static class Sku implements Serializable { + + /** + * sku 编号 + */ + private Integer id; + /** + * 商品编号 + */ + private Integer spuId; + /** + * 图片地址 + */ + private String picURL; + /** + * 规格值数组 + */ + private List attrs; + /** + * 价格,单位:分 + */ + private Integer price; + /** + * 库存数量 + */ + private Integer quantity; + + } + +} diff --git a/product-service-project/product-service-app/src/main/java/cn/iocoder/mall/productservice/convert/attr/ProductAttrConvert.java b/product-service-project/product-service-app/src/main/java/cn/iocoder/mall/productservice/convert/attr/ProductAttrConvert.java index 9e6e1ee7c..9489b607d 100644 --- a/product-service-project/product-service-app/src/main/java/cn/iocoder/mall/productservice/convert/attr/ProductAttrConvert.java +++ b/product-service-project/product-service-app/src/main/java/cn/iocoder/mall/productservice/convert/attr/ProductAttrConvert.java @@ -56,6 +56,6 @@ public interface ProductAttrConvert { List convertList04(List list); - ProductAttrValueListQueryBO convert(ProductAttrValueListQueryRequestDTO bean); + ProductAttrValueListQueryBO convert(ProductAttrValueListQueryReqDTO bean); } diff --git a/product-service-project/product-service-app/src/main/java/cn/iocoder/mall/productservice/convert/spu/ProductSpuConvert.java b/product-service-project/product-service-app/src/main/java/cn/iocoder/mall/productservice/convert/spu/ProductSpuConvert.java index fff78d08b..bbde45c1b 100644 --- a/product-service-project/product-service-app/src/main/java/cn/iocoder/mall/productservice/convert/spu/ProductSpuConvert.java +++ b/product-service-project/product-service-app/src/main/java/cn/iocoder/mall/productservice/convert/spu/ProductSpuConvert.java @@ -3,10 +3,11 @@ package cn.iocoder.mall.productservice.convert.spu; import cn.iocoder.common.framework.util.StringUtils; import cn.iocoder.common.framework.vo.PageResult; import cn.iocoder.mall.productservice.dal.mysql.dataobject.spu.ProductSpuDO; -import cn.iocoder.mall.productservice.rpc.spu.dto.ProductSpuAndSkuCreateReqDTO; -import cn.iocoder.mall.productservice.rpc.spu.dto.ProductSpuAndSkuUpdateReqDTO; -import cn.iocoder.mall.productservice.rpc.spu.dto.ProductSpuPageReqDTO; -import cn.iocoder.mall.productservice.rpc.spu.dto.ProductSpuRespDTO; +import cn.iocoder.mall.productservice.rpc.attr.dto.ProductAttrKeyValueRespDTO; +import cn.iocoder.mall.productservice.rpc.spu.dto.*; +import cn.iocoder.mall.productservice.service.attr.bo.ProductAttrKeyValueBO; +import cn.iocoder.mall.productservice.service.category.bo.ProductCategoryBO; +import cn.iocoder.mall.productservice.service.sku.bo.ProductSkuBO; import cn.iocoder.mall.productservice.service.sku.bo.ProductSkuCreateOrUpdateBO; import cn.iocoder.mall.productservice.service.spu.bo.ProductSpuBO; import cn.iocoder.mall.productservice.service.spu.bo.ProductSpuCreateBO; @@ -18,7 +19,10 @@ import org.mapstruct.Mapping; import org.mapstruct.Named; import org.mapstruct.factory.Mappers; +import java.util.ArrayList; import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; @Mapper public interface ProductSpuConvert { @@ -44,6 +48,7 @@ public interface ProductSpuConvert { ProductSpuUpdateBO convert(ProductSpuAndSkuUpdateReqDTO bean); ProductSpuRespDTO convert(ProductSpuBO bean); + ProductSpuDetailRespDTO convert2(ProductSpuBO bean); List convertList02(List list); @@ -54,6 +59,32 @@ public interface ProductSpuConvert { List convert(List list); List convert02(List list); + ProductSpuDetailRespDTO.Sku convert(ProductSkuBO bean); + + ProductAttrKeyValueRespDTO convert(ProductAttrKeyValueBO bean); + + default ProductSpuDetailRespDTO convert(ProductSpuBO spuBO, List skuBOs, List attrBOs, + ProductCategoryBO categoryBO) { + // 创建并转换 ProductSpuDetailBO 对象 + ProductSpuDetailRespDTO spuDetailDTO = this.convert2(spuBO); + // 创建 ProductAttrDetailBO 的映射。其中,KEY 为 ProductAttrDetailBO.attrValueId ,即规格值的编号 + Map productAttrDetailBOMap = attrBOs.stream().collect( + Collectors.toMap(ProductAttrKeyValueBO::getAttrValueId, attrBO -> attrBO)); + // 创建并转换 ProductSpuDetailBO 数组 + spuDetailDTO.setSkus(new ArrayList<>()); + skuBOs.forEach(sku -> { + // 创建 ProductSpuDetailBO 对象 + ProductSpuDetailRespDTO.Sku skuDetail = convert(sku).setAttrs(new ArrayList<>()); + spuDetailDTO.getSkus().add(skuDetail); + // 设置 ProductSpuDetailBO 的 attrs 规格属性 + sku.getAttrValueIds().forEach(attrValueId -> skuDetail.getAttrs().add(convert(productAttrDetailBOMap.get(attrValueId)))); + }); + // 设置分类名 + spuDetailDTO.setCategoryName(categoryBO.getName()); + // 返回 + return spuDetailDTO; + } + @Named("translatePicUrlsFromString") default List translatePicUrlsFromList(String picUrls) { return StringUtils.split(picUrls, ","); diff --git a/product-service-project/product-service-app/src/main/java/cn/iocoder/mall/productservice/dal/mysql/mapper/sku/ProductSkuMapper.java b/product-service-project/product-service-app/src/main/java/cn/iocoder/mall/productservice/dal/mysql/mapper/sku/ProductSkuMapper.java index 356251c09..87989a62c 100644 --- a/product-service-project/product-service-app/src/main/java/cn/iocoder/mall/productservice/dal/mysql/mapper/sku/ProductSkuMapper.java +++ b/product-service-project/product-service-app/src/main/java/cn/iocoder/mall/productservice/dal/mysql/mapper/sku/ProductSkuMapper.java @@ -21,7 +21,8 @@ public interface ProductSkuMapper extends BaseMapper { default List selectList(ProductSkuListQueryBO queryBO) { return selectList(new QueryWrapperX().eqIfPresent("id", queryBO.getProductSkuId()) - .eqIfPresent("spu_id", queryBO.getProductSpuId())); + .eqIfPresent("spu_id", queryBO.getProductSpuId()) + .eqIfPresent("status", queryBO.getProductSkuStatus())); } } diff --git a/product-service-project/product-service-app/src/main/java/cn/iocoder/mall/productservice/manager/attr/ProductAttrManager.java b/product-service-project/product-service-app/src/main/java/cn/iocoder/mall/productservice/manager/attr/ProductAttrManager.java index 12aa25f4b..0e4a4ac81 100644 --- a/product-service-project/product-service-app/src/main/java/cn/iocoder/mall/productservice/manager/attr/ProductAttrManager.java +++ b/product-service-project/product-service-app/src/main/java/cn/iocoder/mall/productservice/manager/attr/ProductAttrManager.java @@ -110,7 +110,7 @@ public class ProductAttrManager { * @param queryDTO 商品规格值的列表查询条件 DTO * @return 商品规格值列表 */ - public List listProductAttrValues(ProductAttrValueListQueryRequestDTO queryDTO) { + public List listProductAttrValues(ProductAttrValueListQueryReqDTO queryDTO) { List productAttrValueBOs = productAttrService.listProductAttrValues(ProductAttrConvert.INSTANCE.convert(queryDTO)); return ProductAttrConvert.INSTANCE.convertList04(productAttrValueBOs); } diff --git a/product-service-project/product-service-app/src/main/java/cn/iocoder/mall/productservice/manager/spu/ProductSpuManager.java b/product-service-project/product-service-app/src/main/java/cn/iocoder/mall/productservice/manager/spu/ProductSpuManager.java index 967ce2cd4..a4676f1e0 100644 --- a/product-service-project/product-service-app/src/main/java/cn/iocoder/mall/productservice/manager/spu/ProductSpuManager.java +++ b/product-service-project/product-service-app/src/main/java/cn/iocoder/mall/productservice/manager/spu/ProductSpuManager.java @@ -1,24 +1,27 @@ package cn.iocoder.mall.productservice.manager.spu; +import cn.iocoder.common.framework.enums.CommonStatusEnum; import cn.iocoder.common.framework.exception.util.ServiceExceptionUtil; +import cn.iocoder.common.framework.util.CollectionUtils; import cn.iocoder.common.framework.vo.PageResult; import cn.iocoder.mall.productservice.convert.spu.ProductSpuConvert; import cn.iocoder.mall.productservice.enums.category.ProductCategoryIdEnum; +import cn.iocoder.mall.productservice.enums.spu.ProductSpuDetailFieldEnum; import cn.iocoder.mall.productservice.mq.producer.ProductMQProducer; -import cn.iocoder.mall.productservice.rpc.spu.dto.ProductSpuAndSkuCreateReqDTO; -import cn.iocoder.mall.productservice.rpc.spu.dto.ProductSpuAndSkuUpdateReqDTO; -import cn.iocoder.mall.productservice.rpc.spu.dto.ProductSpuPageReqDTO; -import cn.iocoder.mall.productservice.rpc.spu.dto.ProductSpuRespDTO; +import cn.iocoder.mall.productservice.rpc.spu.dto.*; import cn.iocoder.mall.productservice.service.attr.ProductAttrService; import cn.iocoder.mall.productservice.service.attr.bo.ProductAttrKeyValueBO; import cn.iocoder.mall.productservice.service.category.ProductCategoryService; import cn.iocoder.mall.productservice.service.category.bo.ProductCategoryBO; import cn.iocoder.mall.productservice.service.sku.ProductSkuService; +import cn.iocoder.mall.productservice.service.sku.bo.ProductSkuBO; import cn.iocoder.mall.productservice.service.sku.bo.ProductSkuCreateOrUpdateBO; +import cn.iocoder.mall.productservice.service.sku.bo.ProductSkuListQueryBO; import cn.iocoder.mall.productservice.service.spu.ProductSpuService; import cn.iocoder.mall.productservice.service.spu.bo.ProductSpuBO; import cn.iocoder.mall.productservice.service.spu.bo.ProductSpuCreateBO; import cn.iocoder.mall.productservice.service.spu.bo.ProductSpuUpdateBO; +import lombok.extern.slf4j.Slf4j; import org.springframework.aop.framework.AopContext; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @@ -33,6 +36,7 @@ import static cn.iocoder.mall.productservice.enums.ProductErrorCodeConstants.*; * 商品 SPU Manager */ @Service +@Slf4j public class ProductSpuManager { @Autowired @@ -207,4 +211,32 @@ public class ProductSpuManager { return attrKeyValueBOs; } + public ProductSpuDetailRespDTO getProductSpuDetail(Integer productSpuId, Collection fields) { + // 获得商品 SPU 信息 + ProductSpuBO spuBO = productSpuService.getProductSpu(productSpuId); + if (spuBO == null) { + throw ServiceExceptionUtil.exception(PRODUCT_SPU_NOT_EXISTS); + } + // 获取商品分类 + ProductCategoryBO categoryBO = productCategoryService.getProductCategory(spuBO.getCid()); + if (categoryBO == null) { + throw ServiceExceptionUtil.exception(PRODUCT_CATEGORY_NOT_EXISTS); + } + // 获得商品 SKU 数组 + List skuBOs = Collections.emptyList(); + if (fields.contains(ProductSpuDetailFieldEnum.SKU.getField())) { + skuBOs = productSkuService.listProductSkus(new ProductSkuListQueryBO().setProductSpuId(productSpuId) + .setProductSkuStatus(CommonStatusEnum.ENABLE.getValue())); + } + // 获取商品 SKU 的规格数组 + List attrBOs = Collections.emptyList(); + if (fields.contains(ProductSpuDetailFieldEnum.ATTR.getField()) && !CollectionUtils.isEmpty(skuBOs)) { + Set productAttrValueIds = new HashSet<>(); + skuBOs.forEach(sku -> productAttrValueIds.addAll(sku.getAttrValueIds())); + attrBOs = productAttrService.validProductAttr(productAttrValueIds, false); // 读取规格时,不考虑规格是否被禁用 + } + // 拼接最终返回 + return ProductSpuConvert.INSTANCE.convert(spuBO, skuBOs, attrBOs, categoryBO); + } + } diff --git a/product-service-project/product-service-app/src/main/java/cn/iocoder/mall/productservice/rpc/attr/ProductAttrRpcImpl.java b/product-service-project/product-service-app/src/main/java/cn/iocoder/mall/productservice/rpc/attr/ProductAttrRpcImpl.java index 0606b06d1..889e261de 100644 --- a/product-service-project/product-service-app/src/main/java/cn/iocoder/mall/productservice/rpc/attr/ProductAttrRpcImpl.java +++ b/product-service-project/product-service-app/src/main/java/cn/iocoder/mall/productservice/rpc/attr/ProductAttrRpcImpl.java @@ -63,7 +63,7 @@ public class ProductAttrRpcImpl implements ProductAttrRpc { } @Override - public CommonResult> listProductAttrValues(ProductAttrValueListQueryRequestDTO queryDTO) { + public CommonResult> listProductAttrValues(ProductAttrValueListQueryReqDTO queryDTO) { return success(productAttrManager.listProductAttrValues(queryDTO)); } diff --git a/product-service-project/product-service-app/src/main/java/cn/iocoder/mall/productservice/rpc/spu/ProductSpuRpcImpl.java b/product-service-project/product-service-app/src/main/java/cn/iocoder/mall/productservice/rpc/spu/ProductSpuRpcImpl.java index 8ff164979..e9ae280a3 100644 --- a/product-service-project/product-service-app/src/main/java/cn/iocoder/mall/productservice/rpc/spu/ProductSpuRpcImpl.java +++ b/product-service-project/product-service-app/src/main/java/cn/iocoder/mall/productservice/rpc/spu/ProductSpuRpcImpl.java @@ -3,13 +3,11 @@ package cn.iocoder.mall.productservice.rpc.spu; import cn.iocoder.common.framework.vo.CommonResult; import cn.iocoder.common.framework.vo.PageResult; import cn.iocoder.mall.productservice.manager.spu.ProductSpuManager; -import cn.iocoder.mall.productservice.rpc.spu.dto.ProductSpuAndSkuCreateReqDTO; -import cn.iocoder.mall.productservice.rpc.spu.dto.ProductSpuAndSkuUpdateReqDTO; -import cn.iocoder.mall.productservice.rpc.spu.dto.ProductSpuPageReqDTO; -import cn.iocoder.mall.productservice.rpc.spu.dto.ProductSpuRespDTO; +import cn.iocoder.mall.productservice.rpc.spu.dto.*; import org.apache.dubbo.config.annotation.DubboService; import org.springframework.beans.factory.annotation.Autowired; +import java.util.Collection; import java.util.List; import static cn.iocoder.common.framework.vo.CommonResult.success; @@ -54,4 +52,9 @@ public class ProductSpuRpcImpl implements ProductSpuRpc { return success(productSpuManager.listProductSpuIds(lastSpuId, limit)); } + @Override + public CommonResult getProductSpuDetail(Integer productSpuId, Collection fields) { + return success(productSpuManager.getProductSpuDetail(productSpuId, fields)); + } + } diff --git a/product-service-project/product-service-app/src/main/java/cn/iocoder/mall/productservice/service/sku/bo/ProductSkuListQueryBO.java b/product-service-project/product-service-app/src/main/java/cn/iocoder/mall/productservice/service/sku/bo/ProductSkuListQueryBO.java index 6c59211d5..076a39abd 100644 --- a/product-service-project/product-service-app/src/main/java/cn/iocoder/mall/productservice/service/sku/bo/ProductSkuListQueryBO.java +++ b/product-service-project/product-service-app/src/main/java/cn/iocoder/mall/productservice/service/sku/bo/ProductSkuListQueryBO.java @@ -18,5 +18,9 @@ public class ProductSkuListQueryBO { * 商品 SPU 编号 */ private Integer productSpuId; + /** + * 商品 SKU 状态 + */ + private Integer productSkuStatus; }