feature(管理后台): 商品列表

pull/2/head
luowenfeng 2022-09-21 19:40:07 +08:00
parent fdb2d7339f
commit 77a676ee07
3 changed files with 43 additions and 41 deletions

View File

@ -1,6 +1,5 @@
package cn.iocoder.yudao.module.product.service.category;
import cn.hutool.core.util.ObjectUtil;
import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
import cn.iocoder.yudao.module.product.controller.admin.category.vo.ProductCategoryCreateReqVO;
import cn.iocoder.yudao.module.product.controller.admin.category.vo.ProductCategoryListReqVO;
@ -93,33 +92,20 @@ public class ProductCategoryServiceImpl implements ProductCategoryService {
@Override
public void validateCategoryLevel(Long id) {
Integer level = getProductCategoryLevel(id, 1);
if (level < 3){
Long parentId = id;
for (int i = 0; i < 3; i++) {
if (Objects.equals(parentId, ProductCategoryDO.PARENT_ID_NULL)) {
throw exception(CATEGORY_LEVEL_ERROR);
}
}
// TODO @Luowenfeng建议使用 for 循环,避免递归
/**
*
*
* @param id
* @return
*/
private Integer getProductCategoryLevel(Long id, int level){
ProductCategoryDO category = productCategoryMapper.selectById(id);
ProductCategoryDO category = productCategoryMapper.selectById(parentId);
if (category == null) {
throw exception(CATEGORY_NOT_EXISTS);
}
// TODO Luowenfeng去掉是否开启它不影响级别哈
if (ObjectUtil.notEqual(category.getStatus(), CommonStatusEnum.ENABLE.getStatus())) {
throw exception(CATEGORY_DISABLED);
parentId = category.getParentId();
}
// TODO Luowenfeng不使用 0 直接比较哈,使用枚举
if (category.getParentId() == 0) {
return level;
if (!Objects.equals(parentId, ProductCategoryDO.PARENT_ID_NULL)) {
throw exception(CATEGORY_LEVEL_ERROR);
}
return getProductCategoryLevel(category.getParentId(), ++level);
}
@Override

View File

@ -144,36 +144,52 @@ public class ProductSkuServiceImpl implements ProductSkuService {
public void updateProductSkus(Long spuId, List<ProductSkuCreateOrUpdateReqVO> skus) {
// 查询 SPU 下已经存在的 SKU 的集合
List<ProductSkuDO> existsSkus = productSkuMapper.selectListBySpuId(spuId);
Map<Long, ProductSkuDO> existsSkuMap = CollectionUtils.convertMap(existsSkus, ProductSkuDO::getId);
Map<String, Long> existsSkuMap = existsSkus.stream()
.map(v -> {
String collect = v.getProperties() == null? "null": v.getProperties()
.stream()
.map(m -> String.valueOf(m.getValueId()))
.collect(Collectors.joining());
return String.join("-", collect, String.valueOf(v.getId()));
})
.collect(Collectors.toMap(v -> v.split("-")[0], v -> Long.valueOf(v.split("-")[1])));
// 拆分三个集合,新插入的、需要更新的、需要删除的
List<ProductSkuDO> insertSkus = new ArrayList<>();
List<ProductSkuDO> updateSkus = new ArrayList<>(); // TODO Luowenfeng使用 Long 即可
List<ProductSkuDO> deleteSkus = new ArrayList<>();
List<ProductSkuDO> updateSkus = new ArrayList<>();
List<Long> deleteSkus = new ArrayList<>();
// TODO @Luowenfeng是不是基于规格匹配会比较好。可以参考下 onemall 的 ProductSpuServiceImpl 的 updateProductSpu 逻辑
List<ProductSkuDO> allUpdateSkus = ProductSkuConvert.INSTANCE.convertSkuDOList(skus);
allUpdateSkus.forEach(p -> {
if (p.getId() != null) {
if (existsSkuMap.containsKey(p.getId())) {
String propertiesKey = p.getProperties() == null? "null": p.getProperties().stream().map(m -> String.valueOf(m.getValueId())).collect(Collectors.joining());
// 1、找得到的进行更新
if (existsSkuMap.containsKey(propertiesKey)) {
updateSkus.add(p);
existsSkuMap.remove(propertiesKey);
return;
}
deleteSkus.add(p);
return;
}
// 2、找不到进行插入
p.setSpuId(spuId);
insertSkus.add(p);
});
// 3、多余的删除
if(!existsSkuMap.isEmpty()){
deleteSkus = new ArrayList<>(existsSkuMap.values());
}
if (CollectionUtil.isNotEmpty(insertSkus)) {
// 4、执行修改 Sku
if (!insertSkus.isEmpty()) {
productSkuMapper.insertBatch(insertSkus);
}
if (updateSkus.size() > 0) {
if (!updateSkus.isEmpty()) {
updateSkus.forEach(p -> productSkuMapper.updateById(p));
}
if (deleteSkus.size() > 0) {
productSkuMapper.deleteBatchIds(deleteSkus.stream().map(ProductSkuDO::getId).collect(Collectors.toList()));
if (!deleteSkus.isEmpty()) {
productSkuMapper.deleteBatchIds(deleteSkus);
}
}
}

View File

@ -361,7 +361,7 @@ export default {
if (!valid) {
return;
}
let rates = this.ratesForm.rates;
let rates = JSON.parse(JSON.stringify(this.ratesForm.rates));
//
rates.forEach(r => {