|
|
@@ -21,6 +21,7 @@ import shop.alien.store.service.StoreProductDiscountService;
|
|
|
import java.text.ParseException;
|
|
|
import java.text.SimpleDateFormat;
|
|
|
import java.time.LocalDate;
|
|
|
+import java.time.LocalDateTime;
|
|
|
import java.time.LocalTime;
|
|
|
import java.time.format.DateTimeFormatter;
|
|
|
import java.util.*;
|
|
|
@@ -158,6 +159,47 @@ public class StoreProductDiscountServiceImpl implements StoreProductDiscountServ
|
|
|
return R.data(true);
|
|
|
}
|
|
|
|
|
|
+ @Override
|
|
|
+ public int autoCloseExpiredCustomRules() {
|
|
|
+ LocalDate today = LocalDate.now();
|
|
|
+ LocalDateTime now = LocalDateTime.now();
|
|
|
+ List<StoreProductDiscountRule> candidates = ruleMapper.selectList(
|
|
|
+ new LambdaQueryWrapper<StoreProductDiscountRule>()
|
|
|
+ .eq(StoreProductDiscountRule::getStatus, 1)
|
|
|
+ .eq(StoreProductDiscountRule::getEffectiveMode, MODE_CUSTOM)
|
|
|
+ .isNotNull(StoreProductDiscountRule::getEndDate)
|
|
|
+ .le(StoreProductDiscountRule::getEndDate, java.sql.Date.valueOf(today)));
|
|
|
+ if (candidates == null || candidates.isEmpty()) {
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+ List<Integer> ruleIds = candidates.stream().map(StoreProductDiscountRule::getId).collect(Collectors.toList());
|
|
|
+ Map<Integer, List<StoreProductDiscountRuleSlot>> slotMap = ruleSlotMapper.selectList(
|
|
|
+ new LambdaQueryWrapper<StoreProductDiscountRuleSlot>().in(StoreProductDiscountRuleSlot::getRuleId, ruleIds))
|
|
|
+ .stream().collect(Collectors.groupingBy(StoreProductDiscountRuleSlot::getRuleId));
|
|
|
+ int closed = 0;
|
|
|
+ for (StoreProductDiscountRule rule : candidates) {
|
|
|
+ LocalDate endDate = new java.sql.Date(rule.getEndDate().getTime()).toLocalDate();
|
|
|
+ LocalTime maxEnd = slotMap.getOrDefault(rule.getId(), Collections.emptyList()).stream()
|
|
|
+ .map(StoreProductDiscountRuleSlot::getEndTime)
|
|
|
+ .filter(Objects::nonNull)
|
|
|
+ .max(LocalTime::compareTo)
|
|
|
+ .orElse(LocalTime.of(23, 59, 59));
|
|
|
+ LocalDateTime expireAt = LocalDateTime.of(endDate, maxEnd);
|
|
|
+ if (!now.isBefore(expireAt)) {
|
|
|
+ StoreProductDiscountRule update = new StoreProductDiscountRule();
|
|
|
+ update.setId(rule.getId());
|
|
|
+ update.setStatus(0);
|
|
|
+ if (ruleMapper.updateById(update) > 0) {
|
|
|
+ closed++;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (closed > 0) {
|
|
|
+ log.info("自动关闭菜品优惠过期规则完成,本次关闭数量={}", closed);
|
|
|
+ }
|
|
|
+ return closed;
|
|
|
+ }
|
|
|
+
|
|
|
/* ---------------- private helpers ---------------- */
|
|
|
|
|
|
private Map<Integer, String> buildProductNameMap(Integer storeId) {
|