Browse Source

chore: support limitDuration

master
wangxiang 2 years ago
parent
commit
413155b637
No known key found for this signature in database
GPG Key ID: 1BA7946AB6B232E4
  1. 30
      kicc-platform/kicc-platform-biz/kicc-system-biz/src/main/java/com/cloud/kicc/system/service/impl/ImContentServiceImpl.java

30
kicc-platform/kicc-platform-biz/kicc-system-biz/src/main/java/com/cloud/kicc/system/service/impl/ImContentServiceImpl.java

@ -38,8 +38,8 @@ import org.springframework.transaction.annotation.Transactional; @@ -38,8 +38,8 @@ import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.time.Duration;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.List;
import java.util.Map;
import java.util.Objects;
@ -231,11 +231,33 @@ public class ImContentServiceImpl extends ServiceImpl<ImContentMapper, ImContent @@ -231,11 +231,33 @@ public class ImContentServiceImpl extends ServiceImpl<ImContentMapper, ImContent
.between(ImContent::getSendTime, startTime, endTime));
// 检查是否大于发送次数
if (StrUtil.equals(SecurityUtils.<KiccUser>getUser().getSubscriber(), SubscriberEnum.NON.getValue()) && maxCount > openAiConfigProperties.getNonMaxCount()) {
throw new CheckedException("普通订阅用户已超出当前最大速率限制,请过一会在试试!");
if (StrUtil.equals(SecurityUtils.<KiccUser>getUser().getSubscriber(), SubscriberEnum.NON.getValue()) && maxCount > 1) {
throw new CheckedException(String.format("普通订阅用户已超出当前最大速率限制,%s", getLimitDuration(startTime, endTime)));
} else if (StrUtil.equals(SecurityUtils.<KiccUser>getUser().getSubscriber(), SubscriberEnum.BASIC.getValue()) && maxCount > openAiConfigProperties.getBasicMaxCount()) {
throw new CheckedException("基础订阅用户已超出当前最大速率限制,请过一会在试试!");
throw new CheckedException(String.format("基础订阅用户已超出当前最大速率限制,%s", getLimitDuration(startTime, endTime)));
}
}
/** 获取限制持续时间 */
private String getLimitDuration(LocalDateTime startTime, LocalDateTime endTime) {
// 获取当前指定范围内最大的时间
LocalDateTime maxSendTime = (LocalDateTime) baseMapper.selectObjs(Wrappers.<ImContent>lambdaQuery()
.select(ImContent::getSendTime)
.eq(ImContent::getSendUserId, SecurityUtils.getCasUser().getId())
.between(ImContent::getSendTime, startTime, endTime)
.orderByDesc(ImContent::getSendTime)
.last("LIMIT 1"))
.stream()
.findFirst()
.orElse(null);
// 计算时间差
Duration duration = Duration.between(startTime, maxSendTime);
// 提取小时、分钟、秒
long hours = duration.toHours();
long minutes = duration.toMinutes() % 60;
long seconds = duration.getSeconds() % 60;
return String.format("请过 %s小时%s分钟%s秒 之后在试!",hours,minutes,seconds);
}
}

Loading…
Cancel
Save