!1081 【代码优化】商城: 满减送活动校验规则

Merge pull request !1081 from puhui999/develop
This commit is contained in:
芋道源码 2024-09-10 13:12:51 +00:00 committed by Gitee
commit 996d1ca18e
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
2 changed files with 41 additions and 12 deletions

View File

@ -44,8 +44,7 @@ public interface ErrorCodeConstants {
ErrorCode REWARD_ACTIVITY_UPDATE_FAIL_STATUS_CLOSED = new ErrorCode(1_013_006_002, "满减送活动已关闭,不能修改");
ErrorCode REWARD_ACTIVITY_DELETE_FAIL_STATUS_NOT_CLOSED = new ErrorCode(1_013_006_003, "满减送活动未关闭,不能删除");
ErrorCode REWARD_ACTIVITY_CLOSE_FAIL_STATUS_CLOSED = new ErrorCode(1_013_006_004, "满减送活动已关闭,不能重复关闭");
ErrorCode REWARD_ACTIVITY_SCOPE_ALL_EXISTS = new ErrorCode(1_013_006_005, "该时间段已存在商品范围为全场的满减送活动");
ErrorCode REWARD_ACTIVITY_SCOPE_CATEGORY_EXISTS = new ErrorCode(1_013_006_006, "该时间段存在商品类型参加了其它满减送活动");
ErrorCode REWARD_ACTIVITY_SCOPE_EXISTS = new ErrorCode(1_013_006_005, "与该时间段已存在的满减送活动商品范围冲突。注意商品范围 全部 > 分类 > 商品");
// ========== TODO 空着 1-013-007-000 ============

View File

@ -6,6 +6,7 @@ import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.product.api.category.ProductCategoryApi;
import cn.iocoder.yudao.module.product.api.spu.ProductSpuApi;
import cn.iocoder.yudao.module.product.api.spu.dto.ProductSpuRespDTO;
import cn.iocoder.yudao.module.promotion.controller.admin.reward.vo.RewardActivityBaseVO;
import cn.iocoder.yudao.module.promotion.controller.admin.reward.vo.RewardActivityCreateReqVO;
import cn.iocoder.yudao.module.promotion.controller.admin.reward.vo.RewardActivityPageReqVO;
@ -24,6 +25,7 @@ import java.util.Objects;
import static cn.hutool.core.collection.CollUtil.intersectionDistinct;
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertSet;
import static cn.iocoder.yudao.module.promotion.enums.ErrorCodeConstants.*;
/**
@ -121,8 +123,7 @@ public class RewardActivityServiceImpl implements RewardActivityService {
list.removeIf(activity -> id.equals(activity.getId()));
}
// TODO @puhui999这个可能要完整对标有赞的校验完全不允许重叠
// 例如说rewardActivity 是全部活动结果有个 db 里的 activity 是某个分类它也是冲突的也就是说当前时间段内有且仅有只能有一个活动
// 完全不允许重叠
for (RewardActivityDO item : list) {
// 1.1 校验满减送活动时间是否冲突如果时段不冲突那么不同的时间段内则可以存在相同的商品范围
if (!LocalDateTimeUtil.isOverlap(item.getStartTime(), item.getEndTime(),
@ -130,15 +131,44 @@ public class RewardActivityServiceImpl implements RewardActivityService {
continue;
}
// 1.2 校验商品范围是否重叠
if (PromotionProductScopeEnum.isAll(rewardActivity.getProductScope()) &&
PromotionProductScopeEnum.isAll(item.getProductScope())) { // 情况一全部商品参加
throw exception(REWARD_ACTIVITY_SCOPE_ALL_EXISTS);
// 情况一如果与该时间段内商品范围为全部的活动冲突 rewardActivity 商品范围为全部那么则直接校验不通过
// 例如说rewardActivity 是全部活动结果有个 db 里的 activity 是某个分类它也是冲突的也就是说当前时间段内有且仅有只能有一个活动
if (PromotionProductScopeEnum.isAll(item.getProductScope()) ||
PromotionProductScopeEnum.isAll(rewardActivity.getProductScope())) {
throw exception(REWARD_ACTIVITY_SCOPE_EXISTS);
}
if (PromotionProductScopeEnum.isSpu(rewardActivity.getProductScope()) || // 情况二指定商品参加
PromotionProductScopeEnum.isCategory(rewardActivity.getProductScope())) { // 情况三指定商品类型参加
if (!intersectionDistinct(item.getProductScopeValues(), rewardActivity.getProductScopeValues()).isEmpty()) {
throw exception(PromotionProductScopeEnum.isSpu(rewardActivity.getProductScope()) ?
REWARD_ACTIVITY_SPU_CONFLICTS : REWARD_ACTIVITY_SCOPE_CATEGORY_EXISTS);
// 情况二如果与该时间段内商品范围为类别的活动冲突
if (PromotionProductScopeEnum.isCategory(item.getProductScope())) {
// 校验分类是否冲突
if (PromotionProductScopeEnum.isCategory(rewardActivity.getProductScope())) {
if (!intersectionDistinct(item.getProductScopeValues(), rewardActivity.getProductScopeValues()).isEmpty()) {
throw exception(REWARD_ACTIVITY_SCOPE_EXISTS);
}
}
// 校验商品分类是否冲突
if (PromotionProductScopeEnum.isSpu(rewardActivity.getProductScope())) {
List<ProductSpuRespDTO> spuList = productSpuApi.getSpuList(rewardActivity.getProductScopeValues());
if (!intersectionDistinct(item.getProductScopeValues(),
convertSet(spuList, ProductSpuRespDTO::getCategoryId)).isEmpty()) {
throw exception(REWARD_ACTIVITY_SCOPE_EXISTS);
}
}
}
// 情况三如果与该时间段内商品范围为商品的活动冲突
if (PromotionProductScopeEnum.isSpu(item.getProductScope())) {
// 校验商品是否冲突
if (PromotionProductScopeEnum.isSpu(rewardActivity.getProductScope())) {
if (!intersectionDistinct(item.getProductScopeValues(), rewardActivity.getProductScopeValues()).isEmpty()) {
throw exception(REWARD_ACTIVITY_SCOPE_EXISTS);
}
}
// 校验商品分类是否冲突
if (PromotionProductScopeEnum.isCategory(rewardActivity.getProductScope())) {
List<ProductSpuRespDTO> spuList = productSpuApi.getSpuList(item.getProductScopeValues());
if (!intersectionDistinct(rewardActivity.getProductScopeValues(),
convertSet(spuList, ProductSpuRespDTO::getCategoryId)).isEmpty()) {
throw exception(REWARD_ACTIVITY_SCOPE_EXISTS);
}
}
}
}