spu 调整一波

This commit is contained in:
franky 2022-06-08 18:38:14 +08:00
parent 2382ddfb37
commit 407cbb109f
9 changed files with 117 additions and 17 deletions

View File

@ -80,8 +80,7 @@ public class ProductSpuController {
@ApiOperation("获得商品spu分页") @ApiOperation("获得商品spu分页")
@PreAuthorize("@ss.hasPermission('product:spu:query')") @PreAuthorize("@ss.hasPermission('product:spu:query')")
public CommonResult<PageResult<SpuRespVO>> getSpuPage(@Valid SpuPageReqVO pageVO) { public CommonResult<PageResult<SpuRespVO>> getSpuPage(@Valid SpuPageReqVO pageVO) {
PageResult<ProductSpuDO> pageResult = spuService.getSpuPage(pageVO); return success(spuService.getSpuPage(pageVO));
return success(ProductSpuConvert.INSTANCE.convertPage(pageResult));
} }
@GetMapping("/export-excel") @GetMapping("/export-excel")

View File

@ -12,11 +12,11 @@ import io.swagger.annotations.*;
public class SpuRespVO extends ProductSpuBaseVO { public class SpuRespVO extends ProductSpuBaseVO {
@ApiModelProperty(value = "主键", required = true) @ApiModelProperty(value = "主键", required = true)
private Integer id; private Long id;
@ApiModelProperty(value = "创建时间") @ApiModelProperty(value = "创建时间")
private Date createTime; private Date createTime;
List<ProductSkuRespVO> productSkuRespVOS; List<ProductSkuRespVO> skus;
} }

View File

@ -20,6 +20,6 @@ public class SpuUpdateReqVO extends ProductSpuBaseVO {
@ApiModelProperty(value = "sku组合") @ApiModelProperty(value = "sku组合")
@Valid @Valid
List<ProductSkuCreateReqVO> productSkuCreateReqVOS; List<ProductSkuCreateReqVO> skus;
} }

View File

@ -46,9 +46,15 @@ public interface ProductSkuMapper extends BaseMapperX<ProductSkuDO> {
.orderByDesc(ProductSkuDO::getId)); .orderByDesc(ProductSkuDO::getId));
} }
default List<ProductSkuDO> selectBySpuId(Long spuId) { default List<ProductSkuDO> selectBySpuIds(List<Long> spuIds) {
return selectList(new LambdaQueryWrapperX<ProductSkuDO>() return selectList(new LambdaQueryWrapperX<ProductSkuDO>()
.eqIfPresent(ProductSkuDO::getSpuId, spuId) .inIfPresent(ProductSkuDO::getSpuId, spuIds)
); );
} }
default void deleteBySpuId(Long spuId) {
LambdaQueryWrapperX<ProductSkuDO> lambdaQueryWrapperX = new LambdaQueryWrapperX<ProductSkuDO>()
.eqIfPresent(ProductSkuDO::getSpuId, spuId);
delete(lambdaQueryWrapperX);
}
} }

View File

@ -92,4 +92,24 @@ public interface ProductSkuService {
* @return 商品sku 集合 * @return 商品sku 集合
*/ */
List<ProductSkuDO> getSkusBySpuId(Long spuId); List<ProductSkuDO> getSkusBySpuId(Long spuId);
/**
*
* @param spuIds spu 编码集合
* @return 商品 sku 集合
*/
List<ProductSkuDO> getSkusBySpuIds(List<Long> spuIds);
/**
* 通过 spuId 删除 sku 信息
* @param spuId spu 编码
*/
void deleteSkuBySpuId(Long spuId);
/**
* 根据 spuId 更新 spu 下的 sku 信息
* @param spuId spu 编码
* @param skus sku 的集合
*/
void updateSkus(Long spuId, List<ProductSkuCreateReqVO> skus);
} }

View File

@ -11,6 +11,7 @@ import cn.iocoder.yudao.module.product.dal.mysql.sku.ProductSkuMapper;
import cn.iocoder.yudao.module.product.enums.ErrorCodeConstants; import cn.iocoder.yudao.module.product.enums.ErrorCodeConstants;
import cn.iocoder.yudao.module.product.service.property.ProductPropertyService; import cn.iocoder.yudao.module.product.service.property.ProductPropertyService;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated; import org.springframework.validation.annotation.Validated;
import javax.annotation.Resource; import javax.annotation.Resource;
@ -89,14 +90,16 @@ public class ProductSkuServiceImpl implements ProductSkuService {
} }
// TODO @franky这个方法貌似实现的还是有点问题哈例如说throw 异常后面还执行逻辑~ // TODO @franky这个方法貌似实现的还是有点问题哈例如说throw 异常后面还执行逻辑~
// TODO @艿艿 咳咳throw 那里我是偷懒省略了{}哈哈我加上然后我调试下在优化下
@Override @Override
public void validateSkus(List<ProductSkuCreateReqVO> list) { public void validateSkus(List<ProductSkuCreateReqVO> list) {
List<ProductSkuBaseVO.Property> skuPropertyList = list.stream().flatMap(p -> p.getProperties().stream()).collect(Collectors.toList()); List<ProductSkuBaseVO.Property> skuPropertyList = list.stream().flatMap(p -> p.getProperties().stream()).collect(Collectors.toList());
// 校验规格属性以及规格值是否存在 // 校验规格属性以及规格值是否存在
List<Long> propertyIds = skuPropertyList.stream().map(ProductSkuBaseVO.Property::getPropertyId).collect(Collectors.toList()); List<Long> propertyIds = skuPropertyList.stream().map(ProductSkuBaseVO.Property::getPropertyId).collect(Collectors.toList());
List<ProductPropertyRespVO> propertyAndValueList = productPropertyService.selectByIds(propertyIds); List<ProductPropertyRespVO> propertyAndValueList = productPropertyService.selectByIds(propertyIds);
if (propertyAndValueList.isEmpty()) if (propertyAndValueList.isEmpty()) {
throw ServiceExceptionUtil.exception(PROPERTY_NOT_EXISTS); throw ServiceExceptionUtil.exception(PROPERTY_NOT_EXISTS);
}
Map<Long, ProductPropertyRespVO> propertyMap = propertyAndValueList.stream().collect(Collectors.toMap(ProductPropertyRespVO::getId, p -> p)); Map<Long, ProductPropertyRespVO> propertyMap = propertyAndValueList.stream().collect(Collectors.toMap(ProductPropertyRespVO::getId, p -> p));
skuPropertyList.forEach(p -> { skuPropertyList.forEach(p -> {
ProductPropertyRespVO productPropertyRespVO = propertyMap.get(p.getPropertyId()); ProductPropertyRespVO productPropertyRespVO = propertyMap.get(p.getPropertyId());
@ -126,6 +129,55 @@ public class ProductSkuServiceImpl implements ProductSkuService {
@Override @Override
public List<ProductSkuDO> getSkusBySpuId(Long spuId) { public List<ProductSkuDO> getSkusBySpuId(Long spuId) {
return productSkuMapper.selectBySpuId(spuId); return productSkuMapper.selectBySpuIds(Collections.singletonList(spuId));
}
@Override
public List<ProductSkuDO> getSkusBySpuIds(List<Long> spuIds) {
return productSkuMapper.selectBySpuIds(spuIds);
}
@Override
public void deleteSkuBySpuId(Long spuId) {
productSkuMapper.deleteBySpuId(spuId);
}
@Override
@Transactional
public void updateSkus(Long spuId, List<ProductSkuCreateReqVO> skus) {
List<ProductSkuDO> allUpdateSkus = ProductSkuConvert.INSTANCE.convertSkuDOList(skus);
// 查询 spu 下已经存在的 sku 的集合
List<ProductSkuDO> existsSkus = productSkuMapper.selectBySpuIds(Collections.singletonList(spuId));
Map<Long, ProductSkuDO> existsSkuMap = existsSkus.stream().collect(Collectors.toMap(ProductSkuDO::getId, p -> p));
// 拆分三个集合 新插入的 需要更新的需要删除的
List<ProductSkuDO> insertSkus = new ArrayList<>();
List<ProductSkuDO> updateSkus = new ArrayList<>();
List<ProductSkuDO> deleteSkus = new ArrayList<>();
allUpdateSkus.forEach(p -> {
if (null != p.getId()) {
if (existsSkuMap.get(p.getId()) != null) {
updateSkus.add(p);
return;
}
deleteSkus.add(p);
return;
}
p.setSpuId(spuId);
insertSkus.add(p);
});
if (insertSkus.size() > 0) {
productSkuMapper.insertBatch(insertSkus);
}
if (updateSkus.size() > 0) {
updateSkus.forEach(p -> productSkuMapper.updateById(p));
}
if (deleteSkus.size() > 0) {
productSkuMapper.deleteBatchIds(deleteSkus.stream().map(ProductSkuDO::getId).collect(Collectors.toList()));
}
} }
} }

View File

@ -57,7 +57,7 @@ public interface ProductSpuService {
* @param pageReqVO 分页查询 * @param pageReqVO 分页查询
* @return 商品spu分页 * @return 商品spu分页
*/ */
PageResult<ProductSpuDO> getSpuPage(SpuPageReqVO pageReqVO); PageResult<SpuRespVO> getSpuPage(SpuPageReqVO pageReqVO);
/** /**
* 获得商品spu列表, 用于 Excel 导出 * 获得商品spu列表, 用于 Excel 导出

View File

@ -7,6 +7,7 @@ import cn.iocoder.yudao.module.product.dal.dataobject.sku.ProductSkuDO;
import cn.iocoder.yudao.module.product.service.category.CategoryService; import cn.iocoder.yudao.module.product.service.category.CategoryService;
import cn.iocoder.yudao.module.product.service.sku.ProductSkuService; import cn.iocoder.yudao.module.product.service.sku.ProductSkuService;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import javax.annotation.Resource; import javax.annotation.Resource;
import javax.validation.Valid; import javax.validation.Valid;
@ -67,20 +68,31 @@ public class ProductSpuServiceImpl implements ProductSpuService {
} }
@Override @Override
@Transactional
public void updateSpu(SpuUpdateReqVO updateReqVO) { public void updateSpu(SpuUpdateReqVO updateReqVO) {
// 校验存在 // 校验 spu 是否存在
this.validateSpuExists(updateReqVO.getId()); this.validateSpuExists(updateReqVO.getId());
// 校验分类
categoryService.validatedCategoryById(updateReqVO.getCategoryId());
// 校验SKU
List<ProductSkuCreateReqVO> skuCreateReqList = updateReqVO.getSkus();
productSkuService.validateSkus(skuCreateReqList);
// 更新 // 更新
ProductSpuDO updateObj = ProductSpuConvert.INSTANCE.convert(updateReqVO); ProductSpuDO updateObj = ProductSpuConvert.INSTANCE.convert(updateReqVO);
ProductSpuMapper.updateById(updateObj); ProductSpuMapper.updateById(updateObj);
// 更新 sku
productSkuService.updateSkus(updateObj.getId(), updateReqVO.getSkus());
} }
@Override @Override
@Transactional
public void deleteSpu(Long id) { public void deleteSpu(Long id) {
// 校验存在 // 校验存在
this.validateSpuExists(id); this.validateSpuExists(id);
// 删除 // 删除 SPU
ProductSpuMapper.deleteById(id); ProductSpuMapper.deleteById(id);
// 删除关联的 SKU
productSkuService.deleteSkuBySpuId(id);
} }
private void validateSpuExists(Long id) { private void validateSpuExists(Long id) {
@ -93,8 +105,10 @@ public class ProductSpuServiceImpl implements ProductSpuService {
public SpuRespVO getSpu(Long id) { public SpuRespVO getSpu(Long id) {
ProductSpuDO spu = ProductSpuMapper.selectById(id); ProductSpuDO spu = ProductSpuMapper.selectById(id);
SpuRespVO spuVO = ProductSpuConvert.INSTANCE.convert(spu); SpuRespVO spuVO = ProductSpuConvert.INSTANCE.convert(spu);
List<ProductSkuRespVO> skuReqs = ProductSkuConvert.INSTANCE.convertList( productSkuService.getSkusBySpuId(id)); if (null != spuVO) {
spuVO.setProductSkuRespVOS(skuReqs); List<ProductSkuRespVO> skuReqs = ProductSkuConvert.INSTANCE.convertList(productSkuService.getSkusBySpuId(id));
spuVO.setSkus(skuReqs);
}
return spuVO; return spuVO;
} }
@ -104,8 +118,17 @@ public class ProductSpuServiceImpl implements ProductSpuService {
} }
@Override @Override
public PageResult<ProductSpuDO> getSpuPage(SpuPageReqVO pageReqVO) { public PageResult<SpuRespVO> getSpuPage(SpuPageReqVO pageReqVO) {
return ProductSpuMapper.selectPage(pageReqVO); PageResult<SpuRespVO> spuVOs = ProductSpuConvert.INSTANCE.convertPage(ProductSpuMapper.selectPage(pageReqVO));
// 查询 sku 的信息
List<Long> spuIds = spuVOs.getList().stream().map(SpuRespVO::getId).collect(Collectors.toList());
List<ProductSkuRespVO> skus = ProductSkuConvert.INSTANCE.convertList(productSkuService.getSkusBySpuIds(spuIds));
Map<Long, List<ProductSkuRespVO>> skuMap = skus.stream().collect(Collectors.groupingBy(ProductSkuRespVO::getSpuId));
// spu sku 进行组装
spuVOs.getList().forEach(p -> {
p.setSkus(skuMap.get(p.getId()));
});
return spuVOs;
} }
@Override @Override

View File

@ -159,7 +159,7 @@ public class ProductSpuServiceImplTest extends BaseDbUnitTest {
reqVO.setEndCreateTime(null); reqVO.setEndCreateTime(null);
// 调用 // 调用
PageResult<ProductSpuDO> pageResult = spuService.getSpuPage(reqVO); PageResult<SpuRespVO> pageResult = spuService.getSpuPage(reqVO);
// 断言 // 断言
assertEquals(1, pageResult.getTotal()); assertEquals(1, pageResult.getTotal());
assertEquals(1, pageResult.getList().size()); assertEquals(1, pageResult.getList().size());