商品 SPU + SKU 添加
This commit is contained in:
@@ -1,11 +1,26 @@
|
||||
package cn.iocoder.mall.product.convert;
|
||||
|
||||
import cn.iocoder.common.framework.util.StringUtil;
|
||||
import cn.iocoder.mall.product.api.bo.ProductAttrDetailBO;
|
||||
import cn.iocoder.mall.product.api.bo.ProductSkuDetailBO;
|
||||
import cn.iocoder.mall.product.api.bo.ProductSpuBO;
|
||||
import cn.iocoder.mall.product.api.bo.ProductSpuDetailBO;
|
||||
import cn.iocoder.mall.product.api.dto.ProductSkuAddDTO;
|
||||
import cn.iocoder.mall.product.api.dto.ProductSkuUpdateDTO;
|
||||
import cn.iocoder.mall.product.api.dto.ProductSpuAddDTO;
|
||||
import cn.iocoder.mall.product.api.dto.ProductSpuUpdateDTO;
|
||||
import cn.iocoder.mall.product.dataobject.ProductSkuDO;
|
||||
import cn.iocoder.mall.product.dataobject.ProductSpuDO;
|
||||
import org.mapstruct.Mapper;
|
||||
import org.mapstruct.Mapping;
|
||||
import org.mapstruct.Mappings;
|
||||
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 {
|
||||
|
||||
@@ -14,4 +29,60 @@ public interface ProductSpuConvert {
|
||||
@Mappings({})
|
||||
ProductSpuBO convert(ProductSpuDO spu);
|
||||
|
||||
@Mappings({
|
||||
@Mapping(source = "picUrls", target = "picUrls", ignore = true)
|
||||
})
|
||||
ProductSpuDO convert(ProductSpuAddDTO productSpuAddDTO);
|
||||
|
||||
@Mappings({
|
||||
@Mapping(source = "attrs", target = "attrs", ignore = true)
|
||||
})
|
||||
ProductSkuDO convert(ProductSkuAddDTO productSkuAddDTO);
|
||||
|
||||
|
||||
@Mappings({
|
||||
@Mapping(source = "picUrls", target = "picUrls", ignore = true)
|
||||
})
|
||||
ProductSpuDO convert(ProductSpuUpdateDTO productSpuUpdateDTO);
|
||||
|
||||
@Mappings({
|
||||
@Mapping(source = "attrs", target = "attrs", ignore = true)
|
||||
})
|
||||
ProductSkuDO convert(ProductSkuUpdateDTO productSkuUpdateDTO);
|
||||
|
||||
@Mappings({})
|
||||
ProductSpuDetailBO convert(ProductSpuBO spu);
|
||||
|
||||
@Mappings({
|
||||
@Mapping(source = "picUrls", target = "picUrls", ignore = true)
|
||||
})
|
||||
ProductSpuDetailBO convert2(ProductSpuDO spu);
|
||||
|
||||
@Mappings({
|
||||
@Mapping(source = "attrs", target = "attrs", ignore = true)
|
||||
})
|
||||
ProductSkuDetailBO convert2(ProductSkuDO sku);
|
||||
|
||||
@Mappings({})
|
||||
default ProductSpuDetailBO convert2(ProductSpuDO spu, List<ProductSkuDO> skus, List<ProductAttrDetailBO> productAttrDetailBOs) {
|
||||
// 创建并转换 ProductSpuDetailBO 对象
|
||||
ProductSpuDetailBO spuDetail = this.convert2(spu).setPicUrls(StringUtil.split(spu.getPicUrls(), ","));
|
||||
// 创建 ProductAttrDetailBO 的映射。其中,KEY 为 ProductAttrDetailBO.attrValueId ,即规格值的编号
|
||||
Map<Integer, ProductAttrDetailBO> productAttrDetailBOMap = productAttrDetailBOs.stream().collect(
|
||||
Collectors.toMap(ProductAttrDetailBO::getAttrValueId, productAttrDetailBO -> productAttrDetailBO));
|
||||
// 创建并转换 ProductSpuDetailBO 数组
|
||||
spuDetail.setSkus(new ArrayList<>());
|
||||
skus.forEach(sku -> {
|
||||
// 创建 ProductSpuDetailBO 对象
|
||||
ProductSkuDetailBO skuDetail = ProductSpuConvert.this.convert2(sku)
|
||||
.setAttrs(new ArrayList<>());
|
||||
spuDetail.getSkus().add(skuDetail);
|
||||
// 设置 ProductSpuDetailBO 的 attrs 规格属性
|
||||
List<String> attrs = StringUtil.split(sku.getAttrs(), ",");
|
||||
attrs.forEach(attr -> skuDetail.getAttrs().add(productAttrDetailBOMap.get(Integer.valueOf(attr))));
|
||||
});
|
||||
// 返回
|
||||
return spuDetail;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package cn.iocoder.mall.product.dao;
|
||||
|
||||
import cn.iocoder.mall.product.dataobject.ProductAttrDO;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
@Repository
|
||||
public interface ProductAttrMapper {
|
||||
|
||||
ProductAttrDO selectById(@Param("id") Integer id);
|
||||
|
||||
List<ProductAttrDO> selectListByIds(@Param("ids") Collection<Integer> ids);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package cn.iocoder.mall.product.dao;
|
||||
|
||||
import cn.iocoder.mall.product.dataobject.ProductAttrValueDO;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
@Repository
|
||||
public interface ProductAttrValueMapper {
|
||||
|
||||
ProductAttrValueDO selectById(@Param("id") Integer id);
|
||||
|
||||
List<ProductAttrValueDO> selectListByIds(@Param("ids") Collection<Integer> ids);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package cn.iocoder.mall.product.dao;
|
||||
|
||||
import cn.iocoder.mall.product.dataobject.ProductSkuDO;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Repository
|
||||
public interface ProductSkuMapper {
|
||||
|
||||
ProductSkuDO selectById(Integer id);
|
||||
|
||||
void insertList(@Param("productSkuDOs") List<ProductSkuDO> productSkuDOs);
|
||||
|
||||
}
|
||||
@@ -8,4 +8,8 @@ public interface ProductSpuMapper {
|
||||
|
||||
ProductSpuDO selectById(Integer id);
|
||||
|
||||
void insert(ProductSpuDO productSpuDO);
|
||||
|
||||
void update(ProductSpuDO productSpuDO);
|
||||
|
||||
}
|
||||
@@ -1,11 +1,11 @@
|
||||
package cn.iocoder.mall.product.dataobject;
|
||||
|
||||
import java.util.Date;
|
||||
import cn.iocoder.common.framework.dataobject.BaseDO;
|
||||
|
||||
/**
|
||||
* Product 规格
|
||||
*/
|
||||
public class ProductAttrDO {
|
||||
public class ProductAttrDO extends BaseDO {
|
||||
|
||||
/**
|
||||
* 规格编号
|
||||
@@ -15,20 +15,39 @@ public class ProductAttrDO {
|
||||
* 名称
|
||||
*/
|
||||
private String name;
|
||||
/**
|
||||
* 创建时间
|
||||
*/
|
||||
private Date createTime;
|
||||
/**
|
||||
* 最后更新时间
|
||||
*/
|
||||
private Date updateTime;
|
||||
/**
|
||||
* 状态
|
||||
*
|
||||
* 1-正常
|
||||
* 2-删除
|
||||
* 1-开启
|
||||
* 2-禁用
|
||||
*/
|
||||
private Integer status;
|
||||
|
||||
public Integer getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public ProductAttrDO setId(Integer id) {
|
||||
this.id = id;
|
||||
return this;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public ProductAttrDO setName(String name) {
|
||||
this.name = name;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Integer getStatus() {
|
||||
return status;
|
||||
}
|
||||
|
||||
public ProductAttrDO setStatus(Integer status) {
|
||||
this.status = status;
|
||||
return this;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,11 +1,11 @@
|
||||
package cn.iocoder.mall.product.dataobject;
|
||||
|
||||
import java.util.Date;
|
||||
import cn.iocoder.common.framework.dataobject.BaseDO;
|
||||
|
||||
/**
|
||||
* Product 规格值
|
||||
*/
|
||||
public class ProductAttrValueDO {
|
||||
public class ProductAttrValueDO extends BaseDO {
|
||||
|
||||
/**
|
||||
* 规格值编号
|
||||
@@ -19,20 +19,48 @@ public class ProductAttrValueDO {
|
||||
* 规格值
|
||||
*/
|
||||
private String name;
|
||||
/**
|
||||
* 创建时间
|
||||
*/
|
||||
private Date createTime;
|
||||
/**
|
||||
* 最后更新时间
|
||||
*/
|
||||
private Date updateTime;
|
||||
/**
|
||||
* 状态
|
||||
*
|
||||
* 1-正常
|
||||
* 2-删除
|
||||
* 2-禁用
|
||||
*/
|
||||
private Integer status;
|
||||
|
||||
public Integer getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public ProductAttrValueDO setId(Integer id) {
|
||||
this.id = id;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Integer getAttrId() {
|
||||
return attrId;
|
||||
}
|
||||
|
||||
public ProductAttrValueDO setAttrId(Integer attrId) {
|
||||
this.attrId = attrId;
|
||||
return this;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public ProductAttrValueDO setName(String name) {
|
||||
this.name = name;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Integer getStatus() {
|
||||
return status;
|
||||
}
|
||||
|
||||
public ProductAttrValueDO setStatus(Integer status) {
|
||||
this.status = status;
|
||||
return this;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -7,9 +7,6 @@ import cn.iocoder.common.framework.dataobject.BaseDO;
|
||||
*/
|
||||
public class ProductCategoryDO extends BaseDO {
|
||||
|
||||
@Deprecated
|
||||
public static final Integer STATUS_ENABLE = 1;
|
||||
|
||||
/**
|
||||
* 分类编号
|
||||
*/
|
||||
|
||||
@@ -14,7 +14,7 @@ public class ProductSkuDO extends BaseDO {
|
||||
/**
|
||||
* 商品编号
|
||||
*/
|
||||
private Integer itemId;
|
||||
private Integer spuId;
|
||||
|
||||
// TODO 店铺编号
|
||||
|
||||
@@ -28,7 +28,7 @@ public class ProductSkuDO extends BaseDO {
|
||||
/**
|
||||
* 图片地址
|
||||
*/
|
||||
private String picURL;
|
||||
private String picUrl;
|
||||
/**
|
||||
* 规格值({@link ProductAttrDO})数组
|
||||
*
|
||||
@@ -52,4 +52,68 @@ public class ProductSkuDO extends BaseDO {
|
||||
// */
|
||||
// private Integer soldNum;
|
||||
|
||||
|
||||
public Integer getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public ProductSkuDO setId(Integer id) {
|
||||
this.id = id;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Integer getSpuId() {
|
||||
return spuId;
|
||||
}
|
||||
|
||||
public ProductSkuDO setSpuId(Integer spuId) {
|
||||
this.spuId = spuId;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Integer getStatus() {
|
||||
return status;
|
||||
}
|
||||
|
||||
public ProductSkuDO setStatus(Integer status) {
|
||||
this.status = status;
|
||||
return this;
|
||||
}
|
||||
|
||||
public String getPicUrl() {
|
||||
return picUrl;
|
||||
}
|
||||
|
||||
public ProductSkuDO setPicUrl(String picUrl) {
|
||||
this.picUrl = picUrl;
|
||||
return this;
|
||||
}
|
||||
|
||||
public String getAttrs() {
|
||||
return attrs;
|
||||
}
|
||||
|
||||
public ProductSkuDO setAttrs(String attrs) {
|
||||
this.attrs = attrs;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Integer getPrice() {
|
||||
return price;
|
||||
}
|
||||
|
||||
public ProductSkuDO setPrice(Integer price) {
|
||||
this.price = price;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Integer getQuantity() {
|
||||
return quantity;
|
||||
}
|
||||
|
||||
public ProductSkuDO setQuantity(Integer quantity) {
|
||||
this.quantity = quantity;
|
||||
return this;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -55,7 +55,7 @@ public class ProductSpuDO extends BaseDO {
|
||||
/**
|
||||
* 排序字段
|
||||
*/
|
||||
private Integer order;
|
||||
private Integer sort;
|
||||
|
||||
public Integer getId() {
|
||||
return id;
|
||||
@@ -119,12 +119,15 @@ public class ProductSpuDO extends BaseDO {
|
||||
return this;
|
||||
}
|
||||
|
||||
public Integer getOrder() {
|
||||
return order;
|
||||
public Integer getSort() {
|
||||
return sort;
|
||||
}
|
||||
|
||||
public ProductSpuDO setOrder(Integer order) {
|
||||
this.order = order;
|
||||
public ProductSpuDO setSort(Integer sort) {
|
||||
this.sort = sort;
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
package cn.iocoder.mall.product.service;
|
||||
|
||||
import cn.iocoder.common.framework.util.ServiceExceptionUtil;
|
||||
import cn.iocoder.common.framework.vo.CommonResult;
|
||||
import cn.iocoder.mall.product.api.bo.ProductAttrDetailBO;
|
||||
import cn.iocoder.mall.product.api.constant.ProductAttrConstants;
|
||||
import cn.iocoder.mall.product.api.constant.ProductErrorCodeEnum;
|
||||
import cn.iocoder.mall.product.dao.ProductAttrMapper;
|
||||
import cn.iocoder.mall.product.dao.ProductAttrValueMapper;
|
||||
import cn.iocoder.mall.product.dataobject.ProductAttrDO;
|
||||
import cn.iocoder.mall.product.dataobject.ProductAttrValueDO;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* 商品规格 Service 实现类
|
||||
*
|
||||
* @see cn.iocoder.mall.product.dataobject.ProductAttrDO
|
||||
* @see cn.iocoder.mall.product.dataobject.ProductAttrValueDO
|
||||
*/
|
||||
@Service
|
||||
public class ProductAttrServiceImpl {
|
||||
|
||||
@Autowired
|
||||
private ProductAttrMapper productAttrMapper;
|
||||
@Autowired
|
||||
private ProductAttrValueMapper productValueMapper;
|
||||
|
||||
public CommonResult<List<ProductAttrDetailBO>> validProductAttrAndValue(Set<Integer> productAttrValueIds) {
|
||||
// 首先,校验规格值
|
||||
List<ProductAttrValueDO> attrValues = productValueMapper.selectListByIds(productAttrValueIds);
|
||||
if (attrValues.size() != productAttrValueIds.size()) {
|
||||
return ServiceExceptionUtil.error(ProductErrorCodeEnum.PRODUCT_ATTR_VALUE_NOT_EXIST.getCode());
|
||||
}
|
||||
for (ProductAttrValueDO attrValue : attrValues) { // 同时,校验下状态
|
||||
if (ProductAttrConstants.ATTR_STATUS_DISABLE.equals(attrValue.getStatus())) {
|
||||
return ServiceExceptionUtil.error(ProductErrorCodeEnum.PRODUCT_ATTR_VALUE_NOT_EXIST.getCode());
|
||||
}
|
||||
}
|
||||
// 然后,校验规格
|
||||
Set<Integer> attrIds = attrValues.stream().map(ProductAttrValueDO::getAttrId).collect(Collectors.toSet());
|
||||
List<ProductAttrDO> attrs = productAttrMapper.selectListByIds(attrIds);
|
||||
if (attrs.size() != attrIds.size()) {
|
||||
return ServiceExceptionUtil.error(ProductErrorCodeEnum.PRODUCT_ATTR_NOT_EXIST.getCode());
|
||||
}
|
||||
for (ProductAttrDO attr : attrs) { // 同时,校验下状态
|
||||
if (ProductAttrConstants.ATTR_VALUE_STATUS_DISABLE.equals(attr.getStatus())) {
|
||||
return ServiceExceptionUtil.error(ProductErrorCodeEnum.PRODUCT_ATTR_NOT_EXIST.getCode());
|
||||
}
|
||||
}
|
||||
// 返回成功
|
||||
Map<Integer, ProductAttrDO> attrMap = attrs.stream().collect(Collectors.toMap(ProductAttrDO::getId, productAttrDO -> productAttrDO)); // ProductAttrDO 的映射,方便查找。
|
||||
List<ProductAttrDetailBO> result = attrValues.stream().map(productAttrValueDO -> new ProductAttrDetailBO()
|
||||
.setAttrId(productAttrValueDO.getAttrId()).setAttrName(attrMap.get(productAttrValueDO.getAttrId()).getName())
|
||||
.setAttrValueId(productAttrValueDO.getId()).setAttrValueName(productAttrValueDO.getName())).collect(Collectors.toList());
|
||||
return CommonResult.success(result);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -28,7 +28,7 @@ public class ProductCategoryServiceImpl implements ProductCategoryService {
|
||||
|
||||
@Override
|
||||
public List<ProductCategoryBO> getListByPid(Integer pid) {
|
||||
List<ProductCategoryDO> categoryList = productCategoryMapper.selectListByPidAndStatusOrderBySort(pid, ProductCategoryDO.STATUS_ENABLE);
|
||||
List<ProductCategoryDO> categoryList = productCategoryMapper.selectListByPidAndStatusOrderBySort(pid, ProductCategoryConstants.STATUS_ENABLE);
|
||||
return ProductCategoryConvert.INSTANCE.convertToBO(categoryList);
|
||||
}
|
||||
|
||||
@@ -111,6 +111,7 @@ public class ProductCategoryServiceImpl implements ProductCategoryService {
|
||||
return ServiceExceptionUtil.error(ProductErrorCodeEnum.PRODUCT_CATEGORY_DELETE_ONLY_DISABLE.getCode());
|
||||
}
|
||||
// TODO 芋艿:考虑下,是否需要判断下该分类下是否有商品
|
||||
// TODO 芋艿,需要补充下,还有子分类
|
||||
// 标记删除商品分类
|
||||
ProductCategoryDO updateProductCategory = new ProductCategoryDO().setId(productCategoryId);
|
||||
updateProductCategory.setDeleted(BaseDO.DELETED_YES);
|
||||
@@ -119,6 +120,20 @@ public class ProductCategoryServiceImpl implements ProductCategoryService {
|
||||
return CommonResult.success(true);
|
||||
}
|
||||
|
||||
public CommonResult<ProductCategoryDO> validProductCategory(Integer productCategoryId) {
|
||||
// 校验分类是否存在
|
||||
ProductCategoryDO productCategory = productCategoryMapper.selectById(productCategoryId);
|
||||
if (productCategory == null) {
|
||||
return ServiceExceptionUtil.error(ProductErrorCodeEnum.PRODUCT_CATEGORY_NOT_EXISTS.getCode());
|
||||
}
|
||||
// 只有禁用的商品分类才可以删除
|
||||
if (ProductCategoryConstants.STATUS_DISABLE.equals(productCategory.getStatus())) {
|
||||
return ServiceExceptionUtil.error(ProductErrorCodeEnum.PRODUCT_CATEGORY_MUST_ENABLE.getCode());
|
||||
}
|
||||
// 返回结果
|
||||
return CommonResult.success(productCategory);
|
||||
}
|
||||
|
||||
private boolean isValidStatus(Integer status) {
|
||||
return ProductCategoryConstants.STATUS_ENABLE.equals(status)
|
||||
|| ProductCategoryConstants.STATUS_DISABLE.equals(status);
|
||||
|
||||
@@ -1,39 +1,143 @@
|
||||
package cn.iocoder.mall.product.service;
|
||||
|
||||
import cn.iocoder.common.framework.dataobject.BaseDO;
|
||||
import cn.iocoder.common.framework.util.ServiceExceptionUtil;
|
||||
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.constant.ProductErrorCodeEnum;
|
||||
import cn.iocoder.mall.product.api.constant.ProductSpuConstants;
|
||||
import cn.iocoder.mall.product.api.dto.ProductSkuAddDTO;
|
||||
import cn.iocoder.mall.product.api.dto.ProductSpuAddDTO;
|
||||
import cn.iocoder.mall.product.api.dto.ProductSpuUpdateDTO;
|
||||
import cn.iocoder.mall.product.convert.ProductSpuConvert;
|
||||
import cn.iocoder.mall.product.dao.ProductSkuMapper;
|
||||
import cn.iocoder.mall.product.dao.ProductSpuMapper;
|
||||
import cn.iocoder.mall.product.dataobject.ProductCategoryDO;
|
||||
import cn.iocoder.mall.product.dataobject.ProductSkuDO;
|
||||
import cn.iocoder.mall.product.dataobject.ProductSpuDO;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Service // 实际上不用添加。添加的原因是,必须 Spring 报错提示
|
||||
@com.alibaba.dubbo.config.annotation.Service
|
||||
@com.alibaba.dubbo.config.annotation.Service(validation = "true")
|
||||
public class ProductSpuServiceImpl implements ProductSpuService {
|
||||
|
||||
@Autowired
|
||||
private ProductSpuMapper productSpuDAO;
|
||||
private ProductSpuMapper productSpuMapper;
|
||||
@Autowired
|
||||
private ProductSkuMapper productSkuMapper;
|
||||
|
||||
@Autowired
|
||||
private ProductCategoryServiceImpl productCategoryService;
|
||||
@Autowired
|
||||
private ProductAttrServiceImpl productAttrService;
|
||||
|
||||
@Override
|
||||
public ProductSpuBO getProductSpu(Integer id) {
|
||||
ProductSpuDO productSpuDO = productSpuDAO.selectById(id);
|
||||
ProductSpuDO productSpuDO = productSpuMapper.selectById(id);
|
||||
// 转换成 BO
|
||||
return ProductSpuConvert.INSTANCE.convert(productSpuDO);
|
||||
}
|
||||
|
||||
@SuppressWarnings("Duplicates")
|
||||
@Override
|
||||
@Transactional
|
||||
public CommonResult<ProductSpuDetailBO> addProductSpu(Integer adminId, ProductSpuAddDTO productSpuAddDTO) {
|
||||
return null;
|
||||
// 校验商品分类分类存在
|
||||
CommonResult<ProductCategoryDO> validCategoryResult = productCategoryService.validProductCategory(productSpuAddDTO.getCid());
|
||||
if (validCategoryResult.isError()) {
|
||||
return CommonResult.error(validCategoryResult);
|
||||
}
|
||||
// 校验规格是否存在
|
||||
Set<Integer> productAttrValueIds = new HashSet<>();
|
||||
productSpuAddDTO.getSkus().forEach(productSkuAddDTO -> productAttrValueIds.addAll(productSkuAddDTO.getAttrs()));
|
||||
CommonResult<List<ProductAttrDetailBO>> validAttrResult = productAttrService.validProductAttrAndValue(productAttrValueIds);
|
||||
if (validAttrResult.isError()) {
|
||||
return CommonResult.error(validAttrResult);
|
||||
}
|
||||
// 保存 Spu
|
||||
ProductSpuDO spu = ProductSpuConvert.INSTANCE.convert(productSpuAddDTO)
|
||||
.setPicUrls(StringUtil.join(productSpuAddDTO.getPicUrls(), ","))
|
||||
.setSort(0); // 排序为 0
|
||||
spu.setCreateTime(new Date()).setDeleted(BaseDO.DELETED_NO);
|
||||
productSpuMapper.insert(spu);
|
||||
// 保存 Sku
|
||||
List<ProductSkuDO> skus = productSpuAddDTO.getSkus().stream().map(productSkuAddDTO -> {
|
||||
ProductSkuDO sku = ProductSpuConvert.INSTANCE.convert(productSkuAddDTO)
|
||||
.setSpuId(spu.getId())
|
||||
.setStatus(ProductSpuConstants.SKU_STATUS_ENABLE)
|
||||
.setAttrs(StringUtil.join(productSkuAddDTO.getAttrs(), ","));
|
||||
sku.setCreateTime(new Date()).setDeleted(BaseDO.DELETED_NO);
|
||||
return sku;
|
||||
}).collect(Collectors.toList());
|
||||
// 校验 Sku 规格
|
||||
CommonResult<Boolean> validProductSkuResult = validProductSku(productSpuAddDTO.getSkus(), validAttrResult.getData());
|
||||
if (validProductSkuResult.isError()) {
|
||||
return CommonResult.error(validProductSkuResult);
|
||||
}
|
||||
productSkuMapper.insertList(skus);
|
||||
// 返回成功
|
||||
return CommonResult.success(ProductSpuConvert.INSTANCE.convert2(spu, skus, validAttrResult.getData()));
|
||||
}
|
||||
|
||||
@SuppressWarnings("Duplicates")
|
||||
@Override
|
||||
public CommonResult<Boolean> updateProductSpu(Integer adminId, ProductSpuUpdateDTO productSpuUpdateDTO) {
|
||||
return null;
|
||||
// 校验商品分类分类存在
|
||||
CommonResult<ProductCategoryDO> validCategoryResult = productCategoryService.validProductCategory(productSpuUpdateDTO.getCid());
|
||||
if (validCategoryResult.isError()) {
|
||||
return CommonResult.error(validCategoryResult);
|
||||
}
|
||||
// 校验规格是否存在
|
||||
Set<Integer> productAttrValueIds = new HashSet<>();
|
||||
productSpuUpdateDTO.getSkus().forEach(productSkuAddDTO -> productAttrValueIds.addAll(productSkuAddDTO.getAttrs()));
|
||||
CommonResult<List<ProductAttrDetailBO>> validAttrResult = productAttrService.validProductAttrAndValue(productAttrValueIds);
|
||||
if (validAttrResult.isError()) {
|
||||
return CommonResult.error(validAttrResult);
|
||||
}
|
||||
// 更新 Spu
|
||||
ProductSpuDO updateSpu = ProductSpuConvert.INSTANCE.convert(productSpuUpdateDTO)
|
||||
.setPicUrls(StringUtil.join(productSpuUpdateDTO.getPicUrls(), ","));
|
||||
productSpuMapper.update(updateSpu);
|
||||
// 校验 Sku 规格
|
||||
return CommonResult.success(true);
|
||||
}
|
||||
|
||||
private CommonResult<Boolean> validProductSku(List<ProductSkuAddDTO> productSkuAddDTOs, List<ProductAttrDetailBO> productAttrDetailBOs) {
|
||||
// 创建 ProductAttrDetailBO 的映射。其中,KEY 为 ProductAttrDetailBO.attrValueId ,即规格值的编号
|
||||
Map<Integer, ProductAttrDetailBO> productAttrDetailBOMap = productAttrDetailBOs.stream().collect(
|
||||
Collectors.toMap(ProductAttrDetailBO::getAttrValueId, productAttrDetailBO -> productAttrDetailBO));
|
||||
// 1. 先校验,一个 Sku 下,没有重复的规格。校验方式是,遍历每个 Sku ,看看是否有重复的规格 attrId
|
||||
for (ProductSkuAddDTO sku : productSkuAddDTOs) {
|
||||
Set<Integer> attrIds = sku.getAttrs().stream().map(attrValueId -> productAttrDetailBOMap.get(attrValueId).getAttrId()).collect(Collectors.toSet());
|
||||
if (attrIds.size() != sku.getAttrs().size()) {
|
||||
return ServiceExceptionUtil.error(ProductErrorCodeEnum.PRODUCT_SKU_ATTR_CANT_NOT_DUPLICATE.getCode());
|
||||
}
|
||||
}
|
||||
// 2. 再校验,每个 Sku 的规格值的数量,是一致的。
|
||||
int attrSize = productSkuAddDTOs.get(0).getAttrs().size();
|
||||
for (int i = 1; i < productSkuAddDTOs.size(); i++) {
|
||||
if (attrSize != productSkuAddDTOs.get(i).getAttrs().size()) {
|
||||
return ServiceExceptionUtil.error(ProductErrorCodeEnum.PRODUCT_SPU_ATTR_NUMBERS_MUST_BE_EQUALS.getCode());
|
||||
}
|
||||
}
|
||||
// 3. 最后校验,每个 Sku 之间不是重复的
|
||||
Set<Set<Integer>> skuAttrValues = new HashSet<>(); // 每个元素,都是一个 Sku 的 attrValueId 集合。这样,通过最外层的 Set ,判断是否有重复的.
|
||||
for (ProductSkuAddDTO sku : productSkuAddDTOs) {
|
||||
if (!skuAttrValues.add(new HashSet<>(sku.getAttrs()))) { // 添加失败,说明重复
|
||||
return ServiceExceptionUtil.error(ProductErrorCodeEnum.PRODUCT_SPU_SKU__NOT_DUPLICATE.getCode());
|
||||
}
|
||||
}
|
||||
// 校验通过
|
||||
return CommonResult.success(true);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
<?xml version="1.0" encoding="UTF-8" ?>
|
||||
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
|
||||
<mapper namespace="cn.iocoder.mall.product.dao.ProductAttrMapper">
|
||||
|
||||
<sql id="FIELDS">
|
||||
id, name, status, create_time
|
||||
</sql>
|
||||
|
||||
<!--<select id="selectList" resultType="ProductCategoryDO">-->
|
||||
<!--SELECT-->
|
||||
<!--<include refid="FIELDS" />-->
|
||||
<!--FROM product_category-->
|
||||
<!--WHERE deleted = 0-->
|
||||
<!--</select>-->
|
||||
|
||||
<select id="selectById" parameterType="Integer" resultType="ProductAttrDO">
|
||||
SELECT
|
||||
<include refid="FIELDS" />
|
||||
FROM product_attr
|
||||
WHERE id = #{id}
|
||||
AND deleted = 0
|
||||
</select>
|
||||
|
||||
<!--<insert id="insert" parameterType="ProductCategoryDO" useGeneratedKeys="true" keyColumn="id" keyProperty="id">-->
|
||||
<!--INSERT INTO product_category (-->
|
||||
<!--pid, name, description, pic_url, sort,-->
|
||||
<!--status, create_time, deleted-->
|
||||
<!--) VALUES (-->
|
||||
<!--#{pid}, #{name}, #{description}, #{picUrl}, #{sort},-->
|
||||
<!--#{status}, #{createTime}, #{deleted}-->
|
||||
<!--)-->
|
||||
<!--</insert>-->
|
||||
|
||||
<!--<update id="update" parameterType="ProductCategoryDO">-->
|
||||
<!--UPDATE product_category-->
|
||||
<!--<set>-->
|
||||
<!--<if test="pid != null">-->
|
||||
<!--pid = #{pid},-->
|
||||
<!--</if>-->
|
||||
<!--<if test="name != null">-->
|
||||
<!--name = #{name},-->
|
||||
<!--</if>-->
|
||||
<!--<if test="description != null">-->
|
||||
<!--description = #{description},-->
|
||||
<!--</if>-->
|
||||
<!--<if test="picUrl != null">-->
|
||||
<!--pic_url = #{picUrl},-->
|
||||
<!--</if>-->
|
||||
<!--<if test="sort != null">-->
|
||||
<!--sort = #{sort},-->
|
||||
<!--</if>-->
|
||||
<!--<if test="status != null">-->
|
||||
<!--status = #{status},-->
|
||||
<!--</if>-->
|
||||
<!--<if test="deleted != null">-->
|
||||
<!--deleted = #{deleted}-->
|
||||
<!--</if>-->
|
||||
<!--</set>-->
|
||||
<!--WHERE id = #{id}-->
|
||||
<!--</update>-->
|
||||
|
||||
<select id="selectListByIds" resultType="ProductAttrDO">
|
||||
SELECT
|
||||
<include refid="FIELDS" />
|
||||
FROM product_attr
|
||||
WHERE id IN
|
||||
<foreach item="id" collection="ids" separator="," open="(" close=")" index="">
|
||||
#{id}
|
||||
</foreach>
|
||||
AND deleted = 0
|
||||
</select>
|
||||
|
||||
</mapper>
|
||||
@@ -0,0 +1,73 @@
|
||||
<?xml version="1.0" encoding="UTF-8" ?>
|
||||
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
|
||||
<mapper namespace="cn.iocoder.mall.product.dao.ProductAttrValueMapper">
|
||||
|
||||
<sql id="FIELDS">
|
||||
id, attr_id, name, status, create_time
|
||||
</sql>
|
||||
|
||||
<!--<select id="selectList" resultType="ProductCategoryDO">-->
|
||||
<!--SELECT-->
|
||||
<!--<include refid="FIELDS" />-->
|
||||
<!--FROM product_category-->
|
||||
<!--WHERE deleted = 0-->
|
||||
<!--</select>-->
|
||||
|
||||
<select id="selectById" parameterType="Integer" resultType="ProductAttrValueDO">
|
||||
SELECT
|
||||
<include refid="FIELDS" />
|
||||
FROM product_attr_value
|
||||
WHERE id = #{id}
|
||||
AND deleted = 0
|
||||
</select>
|
||||
|
||||
<!--<insert id="insert" parameterType="ProductCategoryDO" useGeneratedKeys="true" keyColumn="id" keyProperty="id">-->
|
||||
<!--INSERT INTO product_category (-->
|
||||
<!--pid, name, description, pic_url, sort,-->
|
||||
<!--status, create_time, deleted-->
|
||||
<!--) VALUES (-->
|
||||
<!--#{pid}, #{name}, #{description}, #{picUrl}, #{sort},-->
|
||||
<!--#{status}, #{createTime}, #{deleted}-->
|
||||
<!--)-->
|
||||
<!--</insert>-->
|
||||
|
||||
<!--<update id="update" parameterType="ProductCategoryDO">-->
|
||||
<!--UPDATE product_category-->
|
||||
<!--<set>-->
|
||||
<!--<if test="pid != null">-->
|
||||
<!--pid = #{pid},-->
|
||||
<!--</if>-->
|
||||
<!--<if test="name != null">-->
|
||||
<!--name = #{name},-->
|
||||
<!--</if>-->
|
||||
<!--<if test="description != null">-->
|
||||
<!--description = #{description},-->
|
||||
<!--</if>-->
|
||||
<!--<if test="picUrl != null">-->
|
||||
<!--pic_url = #{picUrl},-->
|
||||
<!--</if>-->
|
||||
<!--<if test="sort != null">-->
|
||||
<!--sort = #{sort},-->
|
||||
<!--</if>-->
|
||||
<!--<if test="status != null">-->
|
||||
<!--status = #{status},-->
|
||||
<!--</if>-->
|
||||
<!--<if test="deleted != null">-->
|
||||
<!--deleted = #{deleted}-->
|
||||
<!--</if>-->
|
||||
<!--</set>-->
|
||||
<!--WHERE id = #{id}-->
|
||||
<!--</update>-->
|
||||
|
||||
<select id="selectListByIds" resultType="ProductAttrValueDO">
|
||||
SELECT
|
||||
<include refid="FIELDS" />
|
||||
FROM product_attr_value
|
||||
WHERE id IN
|
||||
<foreach item="id" collection="ids" separator="," open="(" close=")" index="">
|
||||
#{id}
|
||||
</foreach>
|
||||
AND deleted = 0
|
||||
</select>
|
||||
|
||||
</mapper>
|
||||
@@ -1,12 +1,24 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||
<mapper namespace="cn.iocoder.mall.product.dao.ProductSpuMapper">
|
||||
<mapper namespace="cn.iocoder.mall.product.dao.ProductSkuMapper">
|
||||
|
||||
<select id="selectById" parameterType="Integer" resultType="ProductSpuDO">
|
||||
<select id="selectById" parameterType="Integer" resultType="ProductSkuDO">
|
||||
SELECT
|
||||
id
|
||||
FROM product_spu
|
||||
FROM product_sku
|
||||
WHERE id = #{id}
|
||||
</select>
|
||||
|
||||
<insert id="insertList" useGeneratedKeys="true" keyColumn="id" keyProperty="id">
|
||||
INSERT INTO product_sku (
|
||||
spu_id, status, pic_url, attrs, price,
|
||||
quantity, deleted, create_time
|
||||
) VALUES
|
||||
<foreach collection="productSkuDOs" item="productSkuDO" separator=",">
|
||||
(#{productSkuDO.spuId}, #{productSkuDO.status}, #{productSkuDO.picUrl}, #{productSkuDO.attrs}, #{productSkuDO.price},
|
||||
#{productSkuDO.quantity}, #{productSkuDO.deleted}, #{productSkuDO.createTime}
|
||||
)
|
||||
</foreach>
|
||||
</insert>
|
||||
|
||||
</mapper>
|
||||
@@ -0,0 +1,26 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||
<mapper namespace="cn.iocoder.mall.product.dao.ProductSpuMapper">
|
||||
|
||||
<select id="selectById" parameterType="Integer" resultType="ProductSpuDO">
|
||||
SELECT
|
||||
id
|
||||
FROM product_spu
|
||||
WHERE id = #{id}
|
||||
</select>
|
||||
|
||||
<insert id="insert" parameterType="ProductSpuDO" useGeneratedKeys="true" keyColumn="id" keyProperty="id">
|
||||
INSERT INTO product_spu (
|
||||
name, sell_point, description, cid, pic_urls,
|
||||
visible, sort, deleted, create_time
|
||||
) VALUES (
|
||||
#{name}, #{sellPoint}, #{description}, #{cid}, #{picUrls},
|
||||
#{visible}, #{sort}, #{deleted}, #{createTime}
|
||||
)
|
||||
</insert>
|
||||
|
||||
<update id="update">
|
||||
|
||||
</update>
|
||||
|
||||
</mapper>
|
||||
Reference in New Issue
Block a user