1. 自营商家增加营业时间限制,每日 0 点到上午 9 点无法交易
This commit is contained in:
2026-05-23 12:35:09 +08:00
parent 0cff78f029
commit 08b76b9584

View File

@@ -28,8 +28,10 @@ package org.springblade.modules.market.service.impl;
import com.alibaba.fastjson.JSON;
import lombok.AllArgsConstructor;
import org.springblade.common.cache.MarketInfoCache;
import org.springblade.common.cache.ParamCache;
import org.springblade.core.log.exception.ServiceException;
import org.springblade.core.redis.ratelimiter.RateLimiter;
import org.springblade.core.tool.utils.StringPool;
import org.springblade.core.tool.utils.StringUtil;
import org.springblade.modules.api.vo.MerchantOrderApiVO;
import org.springblade.modules.api.vo.MerchantOrderDetailApiVO;
@@ -55,6 +57,10 @@ import org.springblade.core.mp.base.BaseServiceImpl;
import org.springframework.transaction.annotation.Transactional;
import java.math.BigDecimal;
import java.time.LocalTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.TimeUnit;
@@ -79,6 +85,12 @@ public class MerchantOrderServiceImpl extends BaseServiceImpl<MerchantOrderMappe
private final ApplicationEventPublisher publisher;
private static final String MERCHANT_BUSINESS_HOURS = "merchant.business.hours";
private static final DateTimeFormatter TIME_FORMATTER = DateTimeFormatter.ofPattern("HH:mm");
private static final ZoneId DEFAULT_ZONE = ZoneId.of("Asia/Shanghai");
@Override
public IPage<MerchantOrderVO> selectMerchantOrderPage(IPage<MerchantOrderVO> page, MerchantOrderVO merchantOrder) {
return page.setRecords(baseMapper.selectMerchantOrderPage(page, merchantOrder));
@@ -101,6 +113,9 @@ public class MerchantOrderServiceImpl extends BaseServiceImpl<MerchantOrderMappe
if (Objects.isNull(userId) || Objects.isNull(id) || Objects.isNull(payoutAccountType) || StringUtil.isBlank(address) || StringUtil.isBlank(consignee) || StringUtil.isBlank(consigneePhone) || Objects.isNull(quantity) || quantity < 1) {
throw new ServiceException("无效参数");
}
if (!isWithinBusinessHours()) {
throw new ServiceException("非营业时间");
}
if (!MarketInfoCache.hasVerifyRealName(userId)) {
throw new ServiceException("请先实名");
}
@@ -295,4 +310,55 @@ public class MerchantOrderServiceImpl extends BaseServiceImpl<MerchantOrderMappe
publisher.publishEvent(new CancelOrderEvent(id, order.getSellerId(), inventory.getProductName(), order.getQuantity(), reason, MarketBusinessModule.MERCHANT_SELLER_ORDER));
return true;
}
/**
* 判断当前时间是否在给定的营业时间范围内
*
* @return true 表示在营业时间内,false 表示不在
* @throws IllegalArgumentException 如果格式无效
*/
public static boolean isWithinBusinessHours() {
String timeRange = ParamCache.getValue(MERCHANT_BUSINESS_HOURS);
if (timeRange == null || timeRange.trim().isEmpty()) {
throw new IllegalArgumentException("营业时间不能为空");
}
String[] parts = timeRange.trim().split(StringPool.DASH, -1);
if (parts.length != 2) {
throw new IllegalArgumentException("营业时间格式必须为 'HH:mm-HH:mm'");
}
String startStr = parts[0].trim();
String endStr = parts[1].trim();
LocalTime startTime;
LocalTime endTime;
try {
startTime = LocalTime.parse(startStr, TIME_FORMATTER);
endTime = LocalTime.parse(endStr, TIME_FORMATTER);
} catch (DateTimeParseException e) {
throw new IllegalArgumentException("时间格式错误,必须为 HH:mm", e);
}
LocalTime now = LocalTime.now(DEFAULT_ZONE);
// 情况1:结束时间是 00:00 → 视为 24:00(即跨天到次日0点)
if (endTime.equals(LocalTime.MIDNIGHT)) {
// 营业时间从 startTime 到 24:00
return !now.isBefore(startTime); // now >= startTime
}
// 情况2:跨天营业(结束时间 <= 开始时间,且不为 00:00)
// 例如 22:00-06:00
if (!endTime.isAfter(startTime)) {
// 跨天:当前时间 >= startTime OR <= endTime
return !now.isBefore(startTime) || !now.isAfter(endTime);
}
// 情况3:同一天内营业(如 09:00-18:00)
return !now.isBefore(startTime) && !now.isAfter(endTime);
}
}