Commit 1352613ebe86f7a5971954735e01e7b7ec4e9eda

Authored by puhui999
1 parent d9856ff7

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

yudao-module-mall/yudao-module-promotion-api/src/main/java/cn/iocoder/yudao/module/promotion/enums/ErrorCodeConstants.java
@@ -40,13 +40,12 @@ public interface ErrorCodeConstants { @@ -40,13 +40,12 @@ public interface ErrorCodeConstants {
40 40
41 // ========== 满减送活动 1-013-006-000 ========== 41 // ========== 满减送活动 1-013-006-000 ==========
42 ErrorCode REWARD_ACTIVITY_NOT_EXISTS = new ErrorCode(1_013_006_000, "满减送活动不存在"); 42 ErrorCode REWARD_ACTIVITY_NOT_EXISTS = new ErrorCode(1_013_006_000, "满减送活动不存在");
43 - ErrorCode REWARD_ACTIVITY_SPU_CONFLICTS = new ErrorCode(1_013_006_001, "存在商品参加了其它满减送活动"); 43 + ErrorCode REWARD_ACTIVITY_SPU_CONFLICTS = new ErrorCode(1_013_006_001, "该时间段存在商品参加了其它满减送活动");
44 ErrorCode REWARD_ACTIVITY_UPDATE_FAIL_STATUS_CLOSED = new ErrorCode(1_013_006_002, "满减送活动已关闭,不能修改"); 44 ErrorCode REWARD_ACTIVITY_UPDATE_FAIL_STATUS_CLOSED = new ErrorCode(1_013_006_002, "满减送活动已关闭,不能修改");
45 ErrorCode REWARD_ACTIVITY_DELETE_FAIL_STATUS_NOT_CLOSED = new ErrorCode(1_013_006_003, "满减送活动未关闭,不能删除"); 45 ErrorCode REWARD_ACTIVITY_DELETE_FAIL_STATUS_NOT_CLOSED = new ErrorCode(1_013_006_003, "满减送活动未关闭,不能删除");
46 ErrorCode REWARD_ACTIVITY_CLOSE_FAIL_STATUS_CLOSED = new ErrorCode(1_013_006_004, "满减送活动已关闭,不能重复关闭"); 46 ErrorCode REWARD_ACTIVITY_CLOSE_FAIL_STATUS_CLOSED = new ErrorCode(1_013_006_004, "满减送活动已关闭,不能重复关闭");
47 - ErrorCode REWARD_ACTIVITY_SCOPE_ALL_EXISTS = new ErrorCode(1_013_006_005, "已存在商品范围为全场的满减送活动");  
48 - ErrorCode REWARD_ACTIVITY_SCOPE_CATEGORY_EXISTS = new ErrorCode(1_013_006_006, "存在商品类型参加了其它满减送活动");  
49 - ErrorCode REWARD_ACTIVITY_TIME_CONFLICTS = new ErrorCode(1_013_006_007, "满减送活动时段冲突"); 47 + ErrorCode REWARD_ACTIVITY_SCOPE_ALL_EXISTS = new ErrorCode(1_013_006_005, "该时间段已存在商品范围为全场的满减送活动");
  48 + ErrorCode REWARD_ACTIVITY_SCOPE_CATEGORY_EXISTS = new ErrorCode(1_013_006_006, "该时间段存在商品类型参加了其它满减送活动");
50 49
51 // ========== TODO 空着 1-013-007-000 ============ 50 // ========== TODO 空着 1-013-007-000 ============
52 51
yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/service/reward/RewardActivityServiceImpl.java
@@ -24,7 +24,6 @@ import java.util.Objects; @@ -24,7 +24,6 @@ import java.util.Objects;
24 24
25 import static cn.hutool.core.collection.CollUtil.intersectionDistinct; 25 import static cn.hutool.core.collection.CollUtil.intersectionDistinct;
26 import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; 26 import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
27 -import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.anyMatch;  
28 import static cn.iocoder.yudao.module.promotion.enums.ErrorCodeConstants.*; 27 import static cn.iocoder.yudao.module.promotion.enums.ErrorCodeConstants.*;
29 28
30 /** 29 /**
@@ -116,29 +115,29 @@ public class RewardActivityServiceImpl implements RewardActivityService { @@ -116,29 +115,29 @@ public class RewardActivityServiceImpl implements RewardActivityService {
116 * @param rewardActivity 请求 115 * @param rewardActivity 请求
117 */ 116 */
118 private void validateRewardActivitySpuConflicts(Long id, RewardActivityBaseVO rewardActivity) { 117 private void validateRewardActivitySpuConflicts(Long id, RewardActivityBaseVO rewardActivity) {
119 - // 0. 获得所有的活动包括关闭的  
120 - List<RewardActivityDO> list = rewardActivityMapper.selectList(); 118 + // 0. 获得开启的所有的活动
  119 + List<RewardActivityDO> list = rewardActivityMapper.selectList(RewardActivityDO::getStatus, CommonStatusEnum.ENABLE.getStatus());
121 if (id != null) { // 排除自己这个活动 120 if (id != null) { // 排除自己这个活动
122 list.removeIf(activity -> id.equals(activity.getId())); 121 list.removeIf(activity -> id.equals(activity.getId()));
123 } 122 }
124 123
125 - // 1.1 校验满减送活动时间是否冲突  
126 - boolean hasConflict = list.stream().anyMatch(item -> LocalDateTimeUtil.isOverlap(item.getStartTime(), item.getEndTime(),  
127 - rewardActivity.getStartTime(), rewardActivity.getEndTime()));  
128 - if (hasConflict) {  
129 - throw exception(REWARD_ACTIVITY_TIME_CONFLICTS);  
130 - }  
131 - // 1.2 校验商品范围是否重叠  
132 - if (PromotionProductScopeEnum.isAll(rewardActivity.getProductScope()) && // 情况一:全部商品参加  
133 - anyMatch(list, item -> PromotionProductScopeEnum.isAll(item.getProductScope()))) {  
134 - throw exception(REWARD_ACTIVITY_SCOPE_ALL_EXISTS);  
135 - }  
136 - if (PromotionProductScopeEnum.isSpu(rewardActivity.getProductScope()) || // 情况二:指定商品参加  
137 - PromotionProductScopeEnum.isCategory(rewardActivity.getProductScope())) { // 情况三:指定商品类型参加  
138 - if (anyMatch(list, item -> !intersectionDistinct(item.getProductScopeValues(),  
139 - rewardActivity.getProductScopeValues()).isEmpty())) {  
140 - throw exception(PromotionProductScopeEnum.isSpu(rewardActivity.getProductScope()) ?  
141 - REWARD_ACTIVITY_SPU_CONFLICTS : REWARD_ACTIVITY_SCOPE_CATEGORY_EXISTS); 124 + for (RewardActivityDO item : list) {
  125 + // 1.1 校验满减送活动时间是否冲突,如果时段不冲突那么不同的时间段内则可以存在相同的商品范围
  126 + if (!LocalDateTimeUtil.isOverlap(item.getStartTime(), item.getEndTime(),
  127 + rewardActivity.getStartTime(), rewardActivity.getEndTime())) {
  128 + continue;
  129 + }
  130 + // 1.2 校验商品范围是否重叠
  131 + if (PromotionProductScopeEnum.isAll(rewardActivity.getProductScope()) &&
  132 + PromotionProductScopeEnum.isAll(item.getProductScope())) { // 情况一:全部商品参加
  133 + throw exception(REWARD_ACTIVITY_SCOPE_ALL_EXISTS);
  134 + }
  135 + if (PromotionProductScopeEnum.isSpu(rewardActivity.getProductScope()) || // 情况二:指定商品参加
  136 + PromotionProductScopeEnum.isCategory(rewardActivity.getProductScope())) { // 情况三:指定商品类型参加
  137 + if (!intersectionDistinct(item.getProductScopeValues(), rewardActivity.getProductScopeValues()).isEmpty()) {
  138 + throw exception(PromotionProductScopeEnum.isSpu(rewardActivity.getProductScope()) ?
  139 + REWARD_ACTIVITY_SPU_CONFLICTS : REWARD_ACTIVITY_SCOPE_CATEGORY_EXISTS);
  140 + }
142 } 141 }
143 } 142 }
144 } 143 }