diff --git a/src/main/java/com/guo/learningprogresstracker/dto/IdRange.java b/src/main/java/com/guo/learningprogresstracker/dto/IdRange.java new file mode 100644 index 0000000..8a8efda --- /dev/null +++ b/src/main/java/com/guo/learningprogresstracker/dto/IdRange.java @@ -0,0 +1,26 @@ +package com.guo.learningprogresstracker.dto; + +import lombok.Data; + +/** + * 主键区间的聚合结果(复习-03)。 + *
随机取样需要知道 id 的跨度,才能用「随机起点 + 顺序取窗口」替代 {@code ORDER BY RAND()}。 + * 单列聚合可用 {@code selectObjs},这里需要 MIN 与 MAX 两列,因此用显式结果类 + 自定义 SQL。
+ */ +@Data +public class IdRange { + + private Integer minId; + + private Integer maxId; + + /** 区间内是否存在记录 */ + public boolean isEmpty() { + return minId == null || maxId == null; + } + + /** 区间宽度(含两端);空区间返回 0 */ + public long span() { + return isEmpty() ? 0L : (long) maxId - minId + 1L; + } +} diff --git a/src/main/java/com/guo/learningprogresstracker/mapper/StudyReportFragmentsMapper.java b/src/main/java/com/guo/learningprogresstracker/mapper/StudyReportFragmentsMapper.java index 65d326f..fcceeff 100644 --- a/src/main/java/com/guo/learningprogresstracker/mapper/StudyReportFragmentsMapper.java +++ b/src/main/java/com/guo/learningprogresstracker/mapper/StudyReportFragmentsMapper.java @@ -1,5 +1,6 @@ package com.guo.learningprogresstracker.mapper; +import com.guo.learningprogresstracker.dto.IdRange; import com.guo.learningprogresstracker.dto.TaskContentCount; import com.guo.learningprogresstracker.entity.StudyReportFragmentsEntity; import com.baomidou.mybatisplus.core.mapper.BaseMapper; @@ -25,6 +26,12 @@ public interface StudyReportFragmentsMapper extends BaseMapper语句见 {@code resources/mapper/StudyReportFragmentsMapper.xml};无数据时两列为 null。
+ */ + IdRange selectIdRange(); } diff --git a/src/main/java/com/guo/learningprogresstracker/service/impl/AiServiceClient.java b/src/main/java/com/guo/learningprogresstracker/service/impl/AiServiceClient.java index 433fb90..9d12988 100644 --- a/src/main/java/com/guo/learningprogresstracker/service/impl/AiServiceClient.java +++ b/src/main/java/com/guo/learningprogresstracker/service/impl/AiServiceClient.java @@ -36,6 +36,11 @@ public class AiServiceClient { private String url; /** 超时秒数(等待 AI 任务完成的最长时间) */ private int timeoutSeconds = 600; + /** + * 入站令牌(安全-03):lpt-ai 配置了 LPT_AI_TOKEN 时,本客户端必须携带同一令牌。 + * 为空表示不携带,兼容未启用令牌的部署。 + */ + private String token; private HttpClient httpClient; private final ObjectMapper objectMapper = new ObjectMapper(); @@ -56,6 +61,11 @@ public class AiServiceClient { return url != null && !url.isBlank(); } + /** 头里必须带值,未配置令牌时给空串(lpt-ai 未启用令牌时会忽略它) */ + private String authToken() { + return token == null ? "" : token.trim(); + } + // ============ 公开 API(签名不变,内部改为 submit + poll)============ /** @@ -144,6 +154,7 @@ public class AiServiceClient { .uri(URI.create(url + "/ai/tasks")) .timeout(Duration.ofSeconds(30)) // 提交本身不应超时 .header("Content-Type", "application/json") + .header("x-api-token", authToken()) .POST(HttpRequest.BodyPublishers.ofString(objectMapper.writeValueAsString(body))) .build(); @@ -182,6 +193,7 @@ public class AiServiceClient { HttpRequest request = HttpRequest.newBuilder() .uri(URI.create(url + "/ai/tasks/" + taskId)) .timeout(Duration.ofSeconds(10)) + .header("x-api-token", authToken()) .GET() .build(); diff --git a/src/main/java/com/guo/learningprogresstracker/service/impl/ReviewServiceImpl.java b/src/main/java/com/guo/learningprogresstracker/service/impl/ReviewServiceImpl.java index 717dab7..97ba4de 100644 --- a/src/main/java/com/guo/learningprogresstracker/service/impl/ReviewServiceImpl.java +++ b/src/main/java/com/guo/learningprogresstracker/service/impl/ReviewServiceImpl.java @@ -1,6 +1,7 @@ package com.guo.learningprogresstracker.service.impl; import com.baomidou.mybatisplus.core.toolkit.Wrappers; +import com.guo.learningprogresstracker.dto.IdRange; import com.guo.learningprogresstracker.dto.ReviewFeedItem; import com.guo.learningprogresstracker.dto.ReviewTaskStats; import com.guo.learningprogresstracker.dto.TaskContentCount; @@ -27,6 +28,7 @@ import java.time.DayOfWeek; import java.time.LocalDate; import java.time.LocalDateTime; import java.util.*; +import java.util.concurrent.ThreadLocalRandom; import java.util.stream.Collectors; import java.util.stream.Stream; @@ -59,8 +61,7 @@ public class ReviewServiceImpl implements ReviewService { boolean random = RANDOM_MODE.equalsIgnoreCase(mode); List不使用 {@code ORDER BY RAND()}:那在 MySQL 上是全表随机排序,一次 Feed 请求会触发两次。 + * 改为「随机起点 + 顺序取窗口」:先取主键区间,随机选一个起点,再按 id 顺序取够数量; + * 不足时从头回绕补齐(id 与创建时间同向,回绕等价于取更早的内容)。删除造成的 id 空洞会让 + * 靠后的内容被略微偏重,对「偶遇式」的 Feed 来说可以接受。
+ */ + private List