spu 调整一波
This commit is contained in:
parent
2382ddfb37
commit
407cbb109f
@ -80,8 +80,7 @@ public class ProductSpuController {
|
||||
@ApiOperation("获得商品spu分页")
|
||||
@PreAuthorize("@ss.hasPermission('product:spu:query')")
|
||||
public CommonResult<PageResult<SpuRespVO>> getSpuPage(@Valid SpuPageReqVO pageVO) {
|
||||
PageResult<ProductSpuDO> pageResult = spuService.getSpuPage(pageVO);
|
||||
return success(ProductSpuConvert.INSTANCE.convertPage(pageResult));
|
||||
return success(spuService.getSpuPage(pageVO));
|
||||
}
|
||||
|
||||
@GetMapping("/export-excel")
|
||||
|
@ -12,11 +12,11 @@ import io.swagger.annotations.*;
|
||||
public class SpuRespVO extends ProductSpuBaseVO {
|
||||
|
||||
@ApiModelProperty(value = "主键", required = true)
|
||||
private Integer id;
|
||||
private Long id;
|
||||
|
||||
@ApiModelProperty(value = "创建时间")
|
||||
private Date createTime;
|
||||
|
||||
List<ProductSkuRespVO> productSkuRespVOS;
|
||||
List<ProductSkuRespVO> skus;
|
||||
|
||||
}
|
||||
|
@ -20,6 +20,6 @@ public class SpuUpdateReqVO extends ProductSpuBaseVO {
|
||||
|
||||
@ApiModelProperty(value = "sku组合")
|
||||
@Valid
|
||||
List<ProductSkuCreateReqVO> productSkuCreateReqVOS;
|
||||
List<ProductSkuCreateReqVO> skus;
|
||||
|
||||
}
|
||||
|
@ -46,9 +46,15 @@ public interface ProductSkuMapper extends BaseMapperX<ProductSkuDO> {
|
||||
.orderByDesc(ProductSkuDO::getId));
|
||||
}
|
||||
|
||||
default List<ProductSkuDO> selectBySpuId(Long spuId) {
|
||||
default List<ProductSkuDO> selectBySpuIds(List<Long> spuIds) {
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
@ -92,4 +92,24 @@ public interface ProductSkuService {
|
||||
* @return 商品sku 集合
|
||||
*/
|
||||
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);
|
||||
}
|
||||
|
@ -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.service.property.ProductPropertyService;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
@ -89,14 +90,16 @@ public class ProductSkuServiceImpl implements ProductSkuService {
|
||||
}
|
||||
|
||||
// TODO @franky:这个方法,貌似实现的还是有点问题哈。例如说,throw 异常,后面还执行逻辑~
|
||||
// TODO @艿艿 咳咳,throw 那里我是偷懒省略了{},哈哈,我加上,然后我调试下,在优化下
|
||||
@Override
|
||||
public void validateSkus(List<ProductSkuCreateReqVO> list) {
|
||||
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<ProductPropertyRespVO> propertyAndValueList = productPropertyService.selectByIds(propertyIds);
|
||||
if (propertyAndValueList.isEmpty())
|
||||
if (propertyAndValueList.isEmpty()) {
|
||||
throw ServiceExceptionUtil.exception(PROPERTY_NOT_EXISTS);
|
||||
}
|
||||
Map<Long, ProductPropertyRespVO> propertyMap = propertyAndValueList.stream().collect(Collectors.toMap(ProductPropertyRespVO::getId, p -> p));
|
||||
skuPropertyList.forEach(p -> {
|
||||
ProductPropertyRespVO productPropertyRespVO = propertyMap.get(p.getPropertyId());
|
||||
@ -126,6 +129,55 @@ public class ProductSkuServiceImpl implements ProductSkuService {
|
||||
|
||||
@Override
|
||||
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()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -57,7 +57,7 @@ public interface ProductSpuService {
|
||||
* @param pageReqVO 分页查询
|
||||
* @return 商品spu分页
|
||||
*/
|
||||
PageResult<ProductSpuDO> getSpuPage(SpuPageReqVO pageReqVO);
|
||||
PageResult<SpuRespVO> getSpuPage(SpuPageReqVO pageReqVO);
|
||||
|
||||
/**
|
||||
* 获得商品spu列表, 用于 Excel 导出
|
||||
|
@ -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.sku.ProductSkuService;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import javax.validation.Valid;
|
||||
|
||||
@ -67,20 +68,31 @@ public class ProductSpuServiceImpl implements ProductSpuService {
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional
|
||||
public void updateSpu(SpuUpdateReqVO updateReqVO) {
|
||||
// 校验存在
|
||||
// 校验 spu 是否存在
|
||||
this.validateSpuExists(updateReqVO.getId());
|
||||
// 校验分类
|
||||
categoryService.validatedCategoryById(updateReqVO.getCategoryId());
|
||||
// 校验SKU
|
||||
List<ProductSkuCreateReqVO> skuCreateReqList = updateReqVO.getSkus();
|
||||
productSkuService.validateSkus(skuCreateReqList);
|
||||
// 更新
|
||||
ProductSpuDO updateObj = ProductSpuConvert.INSTANCE.convert(updateReqVO);
|
||||
ProductSpuMapper.updateById(updateObj);
|
||||
// 更新 sku
|
||||
productSkuService.updateSkus(updateObj.getId(), updateReqVO.getSkus());
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional
|
||||
public void deleteSpu(Long id) {
|
||||
// 校验存在
|
||||
this.validateSpuExists(id);
|
||||
// 删除
|
||||
// 删除 SPU
|
||||
ProductSpuMapper.deleteById(id);
|
||||
// 删除关联的 SKU
|
||||
productSkuService.deleteSkuBySpuId(id);
|
||||
}
|
||||
|
||||
private void validateSpuExists(Long id) {
|
||||
@ -93,8 +105,10 @@ public class ProductSpuServiceImpl implements ProductSpuService {
|
||||
public SpuRespVO getSpu(Long id) {
|
||||
ProductSpuDO spu = ProductSpuMapper.selectById(id);
|
||||
SpuRespVO spuVO = ProductSpuConvert.INSTANCE.convert(spu);
|
||||
List<ProductSkuRespVO> skuReqs = ProductSkuConvert.INSTANCE.convertList( productSkuService.getSkusBySpuId(id));
|
||||
spuVO.setProductSkuRespVOS(skuReqs);
|
||||
if (null != spuVO) {
|
||||
List<ProductSkuRespVO> skuReqs = ProductSkuConvert.INSTANCE.convertList(productSkuService.getSkusBySpuId(id));
|
||||
spuVO.setSkus(skuReqs);
|
||||
}
|
||||
return spuVO;
|
||||
}
|
||||
|
||||
@ -104,8 +118,17 @@ public class ProductSpuServiceImpl implements ProductSpuService {
|
||||
}
|
||||
|
||||
@Override
|
||||
public PageResult<ProductSpuDO> getSpuPage(SpuPageReqVO pageReqVO) {
|
||||
return ProductSpuMapper.selectPage(pageReqVO);
|
||||
public PageResult<SpuRespVO> getSpuPage(SpuPageReqVO 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
|
||||
|
@ -159,7 +159,7 @@ public class ProductSpuServiceImplTest extends BaseDbUnitTest {
|
||||
reqVO.setEndCreateTime(null);
|
||||
|
||||
// 调用
|
||||
PageResult<ProductSpuDO> pageResult = spuService.getSpuPage(reqVO);
|
||||
PageResult<SpuRespVO> pageResult = spuService.getSpuPage(reqVO);
|
||||
// 断言
|
||||
assertEquals(1, pageResult.getTotal());
|
||||
assertEquals(1, pageResult.getList().size());
|
||||
|
Loading…
Reference in New Issue
Block a user