From 5af19f92571eaa101b79da2fbd3cc22bf11af678 Mon Sep 17 00:00:00 2001 From: YunaiV <> Date: Tue, 5 Mar 2019 23:04:42 +0800 Subject: [PATCH] =?UTF-8?q?=E5=95=86=E5=93=81=E8=AF=A6=E6=83=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../common/framework/util/StringUtil.java | 10 +++++++ .../admins/AdminsProductSpuController.java | 5 ++-- .../users/ProductSpuController.java | 3 +- .../mall/product/api/ProductSpuService.java | 4 +-- .../service/ProductSpuServiceImpl.java | 29 +++++++++++++++---- 5 files changed, 40 insertions(+), 11 deletions(-) diff --git a/common/common-framework/src/main/java/cn/iocoder/common/framework/util/StringUtil.java b/common/common-framework/src/main/java/cn/iocoder/common/framework/util/StringUtil.java index 05292a919..8e98169ed 100644 --- a/common/common-framework/src/main/java/cn/iocoder/common/framework/util/StringUtil.java +++ b/common/common-framework/src/main/java/cn/iocoder/common/framework/util/StringUtil.java @@ -2,6 +2,7 @@ package cn.iocoder.common.framework.util; import org.springframework.util.StringUtils; +import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.List; @@ -17,4 +18,13 @@ public class StringUtil { return Arrays.asList(stringArray); } + public static List splitToInt(String toSplit, String delim) { + String[] stringArray = StringUtils.tokenizeToStringArray(toSplit, delim); + List array = new ArrayList<>(stringArray.length); + for (String string : stringArray) { + array.add(Integer.valueOf(string)); + } + return array; + } + } \ No newline at end of file diff --git a/product/product-application/src/main/java/cn/iocoder/mall/product/application/controller/admins/AdminsProductSpuController.java b/product/product-application/src/main/java/cn/iocoder/mall/product/application/controller/admins/AdminsProductSpuController.java index 0430e28b9..0c64f6dab 100644 --- a/product/product-application/src/main/java/cn/iocoder/mall/product/application/controller/admins/AdminsProductSpuController.java +++ b/product/product-application/src/main/java/cn/iocoder/mall/product/application/controller/admins/AdminsProductSpuController.java @@ -121,8 +121,9 @@ public class AdminsProductSpuController { @GetMapping("/spu/info") @ApiOperation("商品 SPU 明细") - public CommonResult info() { - return null; + @ApiImplicitParam(name = "id", value = "SPU 编号", required = true, example = "100") + public CommonResult info(@RequestParam("id") Integer id) { + return ProductSpuConvert.INSTANCE.convert(productSpuService.getProductSpu(id)); } private List parseSkus(String skuStr, Class clazz) { diff --git a/product/product-application/src/main/java/cn/iocoder/mall/product/application/controller/users/ProductSpuController.java b/product/product-application/src/main/java/cn/iocoder/mall/product/application/controller/users/ProductSpuController.java index ac88a885a..44162ced1 100644 --- a/product/product-application/src/main/java/cn/iocoder/mall/product/application/controller/users/ProductSpuController.java +++ b/product/product-application/src/main/java/cn/iocoder/mall/product/application/controller/users/ProductSpuController.java @@ -19,7 +19,8 @@ public class ProductSpuController { // TODO 详情 @GetMapping("/info") public ProductSpuBO info(@RequestParam("id") Integer id) { - return productSpuService.getProductSpu(id); +// return productSpuService.getProductSpu(id); + return null; } // TODO 分页 diff --git a/product/product-service-api/src/main/java/cn/iocoder/mall/product/api/ProductSpuService.java b/product/product-service-api/src/main/java/cn/iocoder/mall/product/api/ProductSpuService.java index 25eae89ce..845a7b1c4 100644 --- a/product/product-service-api/src/main/java/cn/iocoder/mall/product/api/ProductSpuService.java +++ b/product/product-service-api/src/main/java/cn/iocoder/mall/product/api/ProductSpuService.java @@ -1,7 +1,6 @@ package cn.iocoder.mall.product.api; import cn.iocoder.common.framework.vo.CommonResult; -import cn.iocoder.mall.product.api.bo.ProductSpuBO; import cn.iocoder.mall.product.api.bo.ProductSpuDetailBO; import cn.iocoder.mall.product.api.bo.ProductSpuPageBO; import cn.iocoder.mall.product.api.dto.ProductSpuAddDTO; @@ -10,8 +9,7 @@ import cn.iocoder.mall.product.api.dto.ProductSpuUpdateDTO; public interface ProductSpuService { - // TODO 芋艿,需要去改改 - ProductSpuBO getProductSpu(Integer id); + CommonResult getProductSpu(Integer id); CommonResult addProductSpu(Integer adminId, ProductSpuAddDTO productSpuAddDTO); diff --git a/product/product-service-impl/src/main/java/cn/iocoder/mall/product/service/ProductSpuServiceImpl.java b/product/product-service-impl/src/main/java/cn/iocoder/mall/product/service/ProductSpuServiceImpl.java index 982558898..145d32041 100644 --- a/product/product-service-impl/src/main/java/cn/iocoder/mall/product/service/ProductSpuServiceImpl.java +++ b/product/product-service-impl/src/main/java/cn/iocoder/mall/product/service/ProductSpuServiceImpl.java @@ -7,7 +7,6 @@ import cn.iocoder.common.framework.util.StringUtil; import cn.iocoder.common.framework.vo.CommonResult; import cn.iocoder.mall.product.api.ProductSpuService; import cn.iocoder.mall.product.api.bo.ProductAttrDetailBO; -import cn.iocoder.mall.product.api.bo.ProductSpuBO; import cn.iocoder.mall.product.api.bo.ProductSpuDetailBO; import cn.iocoder.mall.product.api.bo.ProductSpuPageBO; import cn.iocoder.mall.product.api.constant.ProductErrorCodeEnum; @@ -43,11 +42,31 @@ public class ProductSpuServiceImpl implements ProductSpuService { @Autowired private ProductAttrServiceImpl productAttrService; +// @Override +// public ProductSpuBO getProductSpu(Integer id) { +// ProductSpuDO productSpuDO = productSpuMapper.selectById(id); +// // 转换成 BO +// return ProductSpuConvert.INSTANCE.convert(productSpuDO); +// } + @Override - public ProductSpuBO getProductSpu(Integer id) { - ProductSpuDO productSpuDO = productSpuMapper.selectById(id); - // 转换成 BO - return ProductSpuConvert.INSTANCE.convert(productSpuDO); + public CommonResult getProductSpu(Integer id) { + // 校验商品 spu 存在 + ProductSpuDO spu = productSpuMapper.selectById(id); + if (spu == null) { + return ServiceExceptionUtil.error(ProductErrorCodeEnum.PRODUCT_SPU_NOT_EXISTS.getCode()); + } + // 获得商品 sku 数组 + List skus = productSkuMapper.selectListBySpuIdAndStatus(id, ProductSpuConstants.SKU_STATUS_ENABLE); + // 获得规格 + Set productAttrValueIds = new HashSet<>(); + skus.forEach(sku -> productAttrValueIds.addAll(StringUtil.splitToInt(sku.getAttrs(), ","))); + CommonResult> validAttrResult = productAttrService.validProductAttrAndValue(productAttrValueIds); + if (validAttrResult.isError()) { + return CommonResult.error(validAttrResult); + } + // 返回成功 + return CommonResult.success(ProductSpuConvert.INSTANCE.convert2(spu, skus, validAttrResult.getData())); } @SuppressWarnings("Duplicates")