【代码优化】商城: 满减送活动

This commit is contained in:
puhui999 2024-09-10 12:24:53 +08:00
parent d9856ff79b
commit 1352613ebe
2 changed files with 22 additions and 24 deletions

View File

@ -40,13 +40,12 @@ public interface ErrorCodeConstants {
// ========== 满减送活动 1-013-006-000 ==========
ErrorCode REWARD_ACTIVITY_NOT_EXISTS = new ErrorCode(1_013_006_000, "满减送活动不存在");
ErrorCode REWARD_ACTIVITY_SPU_CONFLICTS = new ErrorCode(1_013_006_001, "存在商品参加了其它满减送活动");
ErrorCode REWARD_ACTIVITY_SPU_CONFLICTS = new ErrorCode(1_013_006_001, "该时间段存在商品参加了其它满减送活动");
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_TIME_CONFLICTS = new ErrorCode(1_013_006_007, "满减送活动时段冲突");
ErrorCode REWARD_ACTIVITY_SCOPE_ALL_EXISTS = new ErrorCode(1_013_006_005, "该时间段已存在商品范围为全场的满减送活动");
ErrorCode REWARD_ACTIVITY_SCOPE_CATEGORY_EXISTS = new ErrorCode(1_013_006_006, "该时间段存在商品类型参加了其它满减送活动");
// ========== TODO 空着 1-013-007-000 ============

View File

@ -24,7 +24,6 @@ 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.anyMatch;
import static cn.iocoder.yudao.module.promotion.enums.ErrorCodeConstants.*;
/**
@ -116,29 +115,29 @@ public class RewardActivityServiceImpl implements RewardActivityService {
* @param rewardActivity 请求
*/
private void validateRewardActivitySpuConflicts(Long id, RewardActivityBaseVO rewardActivity) {
// 0. 获得所有的活动包括关闭的
List<RewardActivityDO> list = rewardActivityMapper.selectList();
// 0. 获得开启的所有的活动
List<RewardActivityDO> list = rewardActivityMapper.selectList(RewardActivityDO::getStatus, CommonStatusEnum.ENABLE.getStatus());
if (id != null) { // 排除自己这个活动
list.removeIf(activity -> id.equals(activity.getId()));
}
// 1.1 校验满减送活动时间是否冲突
boolean hasConflict = list.stream().anyMatch(item -> LocalDateTimeUtil.isOverlap(item.getStartTime(), item.getEndTime(),
rewardActivity.getStartTime(), rewardActivity.getEndTime()));
if (hasConflict) {
throw exception(REWARD_ACTIVITY_TIME_CONFLICTS);
}
// 1.2 校验商品范围是否重叠
if (PromotionProductScopeEnum.isAll(rewardActivity.getProductScope()) && // 情况一全部商品参加
anyMatch(list, item -> PromotionProductScopeEnum.isAll(item.getProductScope()))) {
throw exception(REWARD_ACTIVITY_SCOPE_ALL_EXISTS);
}
if (PromotionProductScopeEnum.isSpu(rewardActivity.getProductScope()) || // 情况二指定商品参加
PromotionProductScopeEnum.isCategory(rewardActivity.getProductScope())) { // 情况三指定商品类型参加
if (anyMatch(list, item -> !intersectionDistinct(item.getProductScopeValues(),
rewardActivity.getProductScopeValues()).isEmpty())) {
throw exception(PromotionProductScopeEnum.isSpu(rewardActivity.getProductScope()) ?
REWARD_ACTIVITY_SPU_CONFLICTS : REWARD_ACTIVITY_SCOPE_CATEGORY_EXISTS);
for (RewardActivityDO item : list) {
// 1.1 校验满减送活动时间是否冲突如果时段不冲突那么不同的时间段内则可以存在相同的商品范围
if (!LocalDateTimeUtil.isOverlap(item.getStartTime(), item.getEndTime(),
rewardActivity.getStartTime(), rewardActivity.getEndTime())) {
continue;
}
// 1.2 校验商品范围是否重叠
if (PromotionProductScopeEnum.isAll(rewardActivity.getProductScope()) &&
PromotionProductScopeEnum.isAll(item.getProductScope())) { // 情况一全部商品参加
throw exception(REWARD_ACTIVITY_SCOPE_ALL_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);
}
}
}
}