mirror of
https://gitee.com/hhyykk/ipms-sjy.git
synced 2025-08-08 07:11:53 +08:00
优惠券:新增/修改时,校验对应的商品、分类是否存在
This commit is contained in:
@@ -0,0 +1,27 @@
|
||||
package cn.iocoder.yudao.module.product.api.category;
|
||||
|
||||
import cn.iocoder.yudao.module.product.service.category.ProductCategoryService;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.Collection;
|
||||
|
||||
/**
|
||||
* 商品分类 API 接口实现类
|
||||
*
|
||||
* @author owen
|
||||
*/
|
||||
@Service
|
||||
@Validated
|
||||
public class ProductCategoryApiImpl implements ProductCategoryApi {
|
||||
|
||||
@Resource
|
||||
private ProductCategoryService productCategoryService;
|
||||
|
||||
@Override
|
||||
public void validateCategoryList(Collection<Long> ids) {
|
||||
productCategoryService.validateCategoryList(ids);
|
||||
}
|
||||
|
||||
}
|
@@ -3,9 +3,6 @@ package cn.iocoder.yudao.module.product.api.spu;
|
||||
import cn.hutool.core.collection.CollectionUtil;
|
||||
import cn.iocoder.yudao.module.product.api.spu.dto.ProductSpuRespDTO;
|
||||
import cn.iocoder.yudao.module.product.convert.spu.ProductSpuConvert;
|
||||
import cn.iocoder.yudao.module.product.dal.dataobject.spu.ProductSpuDO;
|
||||
import cn.iocoder.yudao.module.product.dal.mysql.spu.ProductSpuMapper;
|
||||
import cn.iocoder.yudao.module.product.service.sku.ProductSkuService;
|
||||
import cn.iocoder.yudao.module.product.service.spu.ProductSpuService;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
@@ -36,9 +33,19 @@ public class ProductSpuApiImpl implements ProductSpuApi {
|
||||
return ProductSpuConvert.INSTANCE.convertList2(spuService.getSpuList(ids));
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<ProductSpuRespDTO> getSpuListAndValidate(Collection<Long> ids) {
|
||||
return ProductSpuConvert.INSTANCE.convertList2(spuService.validateSpuList(ids));
|
||||
}
|
||||
|
||||
@Override
|
||||
public ProductSpuRespDTO getSpu(Long id) {
|
||||
return ProductSpuConvert.INSTANCE.convert02(spuService.getSpu(id));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void validateSpuList(Collection<Long> ids) {
|
||||
spuService.validateSpuList(ids);
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -6,6 +6,7 @@ import cn.iocoder.yudao.module.product.controller.admin.category.vo.ProductCateg
|
||||
import cn.iocoder.yudao.module.product.dal.dataobject.category.ProductCategoryDO;
|
||||
|
||||
import javax.validation.Valid;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
@@ -75,4 +76,12 @@ public interface ProductCategoryService {
|
||||
*/
|
||||
List<ProductCategoryDO> getEnableCategoryList();
|
||||
|
||||
/**
|
||||
* 校验商品分类是否有效。如下情况,视为无效:
|
||||
* 1. 商品分类编号不存在
|
||||
* 2. 商品分类被禁用
|
||||
*
|
||||
* @param ids 商品分类编号数组
|
||||
*/
|
||||
void validateCategoryList(Collection<Long> ids);
|
||||
}
|
||||
|
@@ -1,6 +1,8 @@
|
||||
package cn.iocoder.yudao.module.product.service.category;
|
||||
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
|
||||
import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils;
|
||||
import cn.iocoder.yudao.module.product.controller.admin.category.vo.ProductCategoryCreateReqVO;
|
||||
import cn.iocoder.yudao.module.product.controller.admin.category.vo.ProductCategoryListReqVO;
|
||||
import cn.iocoder.yudao.module.product.controller.admin.category.vo.ProductCategoryUpdateReqVO;
|
||||
@@ -13,7 +15,9 @@ import org.springframework.stereotype.Service;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
|
||||
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
|
||||
@@ -99,6 +103,26 @@ public class ProductCategoryServiceImpl implements ProductCategoryService {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void validateCategoryList(Collection<Long> ids) {
|
||||
if (CollUtil.isEmpty(ids)) {
|
||||
return;
|
||||
}
|
||||
// 获得商品分类信息
|
||||
List<ProductCategoryDO> categoryList = productCategoryMapper.selectBatchIds(ids);
|
||||
Map<Long, ProductCategoryDO> categoryMap = CollectionUtils.convertMap(categoryList, ProductCategoryDO::getId);
|
||||
// 校验
|
||||
ids.forEach(id -> {
|
||||
ProductCategoryDO category = categoryMap.get(id);
|
||||
if (category == null) {
|
||||
throw exception(CATEGORY_NOT_EXISTS);
|
||||
}
|
||||
if (!CommonStatusEnum.ENABLE.getStatus().equals(category.getStatus())) {
|
||||
throw exception(CATEGORY_DISABLED, category.getName());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public ProductCategoryDO getCategory(Long id) {
|
||||
return productCategoryMapper.selectById(id);
|
||||
|
@@ -136,4 +136,14 @@ public interface ProductSpuService {
|
||||
*/
|
||||
Long getSpuCountByCategoryId(Long categoryId);
|
||||
|
||||
|
||||
/**
|
||||
* 校验商品是否有效。如下情况,视为无效:
|
||||
* 1. 商品编号不存在
|
||||
* 2. 商品被禁用
|
||||
*
|
||||
* @param ids 商品编号数组
|
||||
* @return 商品 SPU 列表
|
||||
*/
|
||||
List<ProductSpuDO> validateSpuList(Collection<Long> ids);
|
||||
}
|
||||
|
@@ -1,5 +1,6 @@
|
||||
package cn.iocoder.yudao.module.product.service.spu;
|
||||
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
|
||||
import cn.iocoder.yudao.framework.common.pojo.PageResult;
|
||||
@@ -139,6 +140,28 @@ public class ProductSpuServiceImpl implements ProductSpuService {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<ProductSpuDO> validateSpuList(Collection<Long> ids) {
|
||||
if (CollUtil.isEmpty(ids)) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
// 获得商品信息
|
||||
List<ProductSpuDO> spuList = productSpuMapper.selectBatchIds(ids);
|
||||
Map<Long, ProductSpuDO> spuMap = CollectionUtils.convertMap(spuList, ProductSpuDO::getId);
|
||||
// 校验
|
||||
ids.forEach(id -> {
|
||||
ProductSpuDO spu = spuMap.get(id);
|
||||
if (spu == null) {
|
||||
throw exception(SPU_NOT_EXISTS);
|
||||
}
|
||||
if (!ProductSpuStatusEnum.isEnable(spu.getStatus())) {
|
||||
throw exception(SPU_NOT_ENABLE, spu.getName());
|
||||
}
|
||||
});
|
||||
|
||||
return spuList;
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public void deleteSpu(Long id) {
|
||||
|
Reference in New Issue
Block a user