From 7c62f1dcf902202966e1630b25104819b3f42299 Mon Sep 17 00:00:00 2001 From: Hanserwei <2628273921@qq.com> Date: Sun, 2 Nov 2025 14:40:42 +0800 Subject: [PATCH] =?UTF-8?q?feat(search):=20=E5=A2=9E=E5=8A=A0=E7=AC=94?= =?UTF-8?q?=E8=AE=B0=E5=8F=91=E5=B8=83=E6=97=B6=E9=97=B4=E8=8C=83=E5=9B=B4?= =?UTF-8?q?=E7=AD=9B=E9=80=89=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 在 DateConstants 中新增 MM-dd 和 HH:mm 时间格式常量 - 在 DateUtils 中增加 localDateTime2String 和 formatRelativeTime 方法- 新增 NotePublishTimeRangeEnum 枚举类用于定义发布时间范围 - 在搜索服务中实现按发布时间范围筛选逻辑 - 修改 SearchNoteReqVO 添加 publishTimeRange 参数 - 修改 SearchNoteRspVO 将 updateTime 改为字符串类型并新增评论数和收藏数字段 - 更新搜索结果处理逻辑以支持新的时间格式化和数据展示 --- .../enums/NotePublishTimeRangeEnum.java | 37 +++++++++++ .../search/model/vo/SearchNoteReqVO.java | 5 ++ .../search/model/vo/SearchNoteRspVO.java | 14 ++++- .../search/service/impl/NoteServiceImpl.java | 39 +++++++++++- .../common/constant/DateConstants.java | 11 ++++ .../framework/common/utils/DateUtils.java | 62 +++++++++++++++++++ 6 files changed, 163 insertions(+), 5 deletions(-) create mode 100644 han-note-search/src/main/java/com/hanserwei/hannote/search/enums/NotePublishTimeRangeEnum.java diff --git a/han-note-search/src/main/java/com/hanserwei/hannote/search/enums/NotePublishTimeRangeEnum.java b/han-note-search/src/main/java/com/hanserwei/hannote/search/enums/NotePublishTimeRangeEnum.java new file mode 100644 index 0000000..dc241e6 --- /dev/null +++ b/han-note-search/src/main/java/com/hanserwei/hannote/search/enums/NotePublishTimeRangeEnum.java @@ -0,0 +1,37 @@ +package com.hanserwei.hannote.search.enums; + +import lombok.AllArgsConstructor; +import lombok.Getter; + +import java.util.Objects; + +@Getter +@AllArgsConstructor +public enum NotePublishTimeRangeEnum { + + // 一天内 + DAY(0), + // 一周内 + WEEK(1), + // 半年内 + HALF_YEAR(2), + ; + + private final Integer code; + + /** + * 根据类型 code 获取对应的枚举 + * + * @param code 类型 code + * @return 枚举 + */ + public static NotePublishTimeRangeEnum valueOf(Integer code) { + for (NotePublishTimeRangeEnum notePublishTimeRangeEnum : NotePublishTimeRangeEnum.values()) { + if (Objects.equals(code, notePublishTimeRangeEnum.getCode())) { + return notePublishTimeRangeEnum; + } + } + return null; + } + +} \ No newline at end of file diff --git a/han-note-search/src/main/java/com/hanserwei/hannote/search/model/vo/SearchNoteReqVO.java b/han-note-search/src/main/java/com/hanserwei/hannote/search/model/vo/SearchNoteReqVO.java index efd6cff..bc7d5bd 100644 --- a/han-note-search/src/main/java/com/hanserwei/hannote/search/model/vo/SearchNoteReqVO.java +++ b/han-note-search/src/main/java/com/hanserwei/hannote/search/model/vo/SearchNoteReqVO.java @@ -29,4 +29,9 @@ public class SearchNoteReqVO { */ private Integer sort; + /** + * 发布时间范围:null:不限 / 0:一天内 / 1:一周内 / 2:半年内 + */ + private Integer publishTimeRange; + } \ No newline at end of file diff --git a/han-note-search/src/main/java/com/hanserwei/hannote/search/model/vo/SearchNoteRspVO.java b/han-note-search/src/main/java/com/hanserwei/hannote/search/model/vo/SearchNoteRspVO.java index 5c50845..ba9a640 100644 --- a/han-note-search/src/main/java/com/hanserwei/hannote/search/model/vo/SearchNoteRspVO.java +++ b/han-note-search/src/main/java/com/hanserwei/hannote/search/model/vo/SearchNoteRspVO.java @@ -7,8 +7,6 @@ import lombok.Builder; import lombok.Data; import lombok.NoArgsConstructor; -import java.time.LocalDateTime; - @Data @AllArgsConstructor @NoArgsConstructor @@ -51,7 +49,7 @@ public class SearchNoteRspVO { * 最后一次编辑时间 */ @JsonAlias("update_time") - private LocalDateTime updateTime; + private String updateTime; /** * 被点赞总数 @@ -59,4 +57,14 @@ public class SearchNoteRspVO { @JsonAlias("like_total") private String likeTotal; + /** + * 被评论数 + */ + private String commentTotal; + + /** + * 被收藏数 + */ + private String collectTotal; + } \ No newline at end of file diff --git a/han-note-search/src/main/java/com/hanserwei/hannote/search/service/impl/NoteServiceImpl.java b/han-note-search/src/main/java/com/hanserwei/hannote/search/service/impl/NoteServiceImpl.java index f8836cc..62077ba 100644 --- a/han-note-search/src/main/java/com/hanserwei/hannote/search/service/impl/NoteServiceImpl.java +++ b/han-note-search/src/main/java/com/hanserwei/hannote/search/service/impl/NoteServiceImpl.java @@ -11,8 +11,11 @@ import co.elastic.clients.elasticsearch.core.search.Highlight; import co.elastic.clients.elasticsearch.core.search.HighlightField; import co.elastic.clients.elasticsearch.core.search.Hit; import co.elastic.clients.util.NamedValue; +import com.hanserwei.framework.common.constant.DateConstants; import com.hanserwei.framework.common.response.PageResponse; +import com.hanserwei.framework.common.utils.DateUtils; import com.hanserwei.framework.common.utils.NumberUtils; +import com.hanserwei.hannote.search.enums.NotePublishTimeRangeEnum; import com.hanserwei.hannote.search.enums.NoteSortTypeEnum; import com.hanserwei.hannote.search.index.NoteIndex; import com.hanserwei.hannote.search.model.vo.SearchNoteReqVO; @@ -20,6 +23,7 @@ import com.hanserwei.hannote.search.model.vo.SearchNoteRspVO; import com.hanserwei.hannote.search.service.NoteService; import jakarta.annotation.Resource; import lombok.extern.slf4j.Slf4j; +import org.apache.commons.lang3.StringUtils; import org.springframework.stereotype.Service; import java.io.IOException; @@ -46,6 +50,8 @@ public class NoteServiceImpl implements NoteService { Integer type = searchNoteReqVO.getType(); // 排序方式 Integer sort = searchNoteReqVO.getSort(); + // 发布时间范围 + Integer publishTimeRange = searchNoteReqVO.getPublishTimeRange(); // --- 2. 分页参数 --- int pageSize = 10; @@ -93,6 +99,31 @@ public class NoteServiceImpl implements NoteService { ) ); } + // 按发布时间范围过滤 + NotePublishTimeRangeEnum notePublishTimeRangeEnum = NotePublishTimeRangeEnum.valueOf(publishTimeRange); + + if (Objects.nonNull(notePublishTimeRangeEnum)) { + // 结束时间 + String endTime = LocalDateTime.now().format(DateConstants.DATE_FORMAT_Y_M_D_H_M_S); + // 开始时间 + String startTime = null; + switch (notePublishTimeRangeEnum) { + case DAY -> startTime = DateUtils.localDateTime2String(LocalDateTime.now().minusDays(1)); // 一天之前的时间 + case WEEK -> startTime = DateUtils.localDateTime2String(LocalDateTime.now().minusWeeks(1)); // 一周之前的时间 + case HALF_YEAR -> + startTime = DateUtils.localDateTime2String(LocalDateTime.now().minusMonths(6)); // 半年之前的时间 + } + // 设置时间范围 + if (StringUtils.isNoneBlank(startTime)) { + String finalStartTime = startTime; + boolQueryBuilder.filter(f -> f.range(r -> r + .term(t -> t.field(NoteIndex.FIELD_NOTE_CREATE_TIME) + .gte(finalStartTime) + .lte(endTime))) + ); + } + } + BoolQuery boolQuery = boolQueryBuilder.build(); // --- 4. 构建排序 (Sort) 和 FunctionScore --- Query finalQuery; @@ -238,8 +269,10 @@ public class NoteServiceImpl implements NoteService { String highlightTitle = source.getHighlightTitle(); String avatar = source.getAvatar(); String nickname = source.getNickname(); - LocalDateTime updateTime = source.getUpdateTime(); + String updateTime = source.getUpdateTime(); String likeTotal = source.getLikeTotal(); + String collectTotal = source.getCollectTotal(); + String commentTotal = source.getCommentTotal(); searchNoteRspVOS.add(SearchNoteRspVO.builder() .noteId(noteId) .cover(cover) @@ -247,9 +280,11 @@ public class NoteServiceImpl implements NoteService { .highlightTitle(highlightTitle) .avatar(avatar) .nickname(nickname) - .updateTime(updateTime) + .updateTime(DateUtils.formatRelativeTime(LocalDateTime.parse(updateTime, DateConstants.DATE_FORMAT_Y_M_D_H_M_S))) .highlightTitle(highlightedTitle) .likeTotal(likeTotal == null ? "0" : NumberUtils.formatNumberString(Long.parseLong(likeTotal))) + .collectTotal(collectTotal == null ? "0" : NumberUtils.formatNumberString(Long.parseLong(collectTotal))) + .collectTotal(commentTotal == null ? "0" : NumberUtils.formatNumberString(Long.parseLong(commentTotal))) .build()); } diff --git a/hanserwei-framework/hanserwei-common/src/main/java/com/hanserwei/framework/common/constant/DateConstants.java b/hanserwei-framework/hanserwei-common/src/main/java/com/hanserwei/framework/common/constant/DateConstants.java index f9ad973..bd31043 100755 --- a/hanserwei-framework/hanserwei-common/src/main/java/com/hanserwei/framework/common/constant/DateConstants.java +++ b/hanserwei-framework/hanserwei-common/src/main/java/com/hanserwei/framework/common/constant/DateConstants.java @@ -23,4 +23,15 @@ public interface DateConstants { * DateTimeFormatter:年-月 */ DateTimeFormatter DATE_FORMAT_Y_M = DateTimeFormatter.ofPattern("yyyy-MM"); + + /** + * DateTimeFormatter:月-日 + */ + DateTimeFormatter DATE_FORMAT_M_D = DateTimeFormatter.ofPattern("MM-dd"); + + + /** + * DateTimeFormatter:时:分 + */ + DateTimeFormatter DATE_FORMAT_H_M = DateTimeFormatter.ofPattern("HH:mm"); } diff --git a/hanserwei-framework/hanserwei-common/src/main/java/com/hanserwei/framework/common/utils/DateUtils.java b/hanserwei-framework/hanserwei-common/src/main/java/com/hanserwei/framework/common/utils/DateUtils.java index 369656c..a0e62c5 100644 --- a/hanserwei-framework/hanserwei-common/src/main/java/com/hanserwei/framework/common/utils/DateUtils.java +++ b/hanserwei-framework/hanserwei-common/src/main/java/com/hanserwei/framework/common/utils/DateUtils.java @@ -1,7 +1,10 @@ package com.hanserwei.framework.common.utils; +import com.hanserwei.framework.common.constant.DateConstants; + import java.time.LocalDateTime; import java.time.ZoneOffset; +import java.time.temporal.ChronoUnit; public class DateUtils { @@ -14,4 +17,63 @@ public class DateUtils { public static long localDateTime2Timestamp(LocalDateTime localDateTime) { return localDateTime.toInstant(ZoneOffset.UTC).toEpochMilli(); } + + /** + * LocalDateTime 转 String 字符串 + * + * @param time LocalDateTime + * @return String + */ + public static String localDateTime2String(LocalDateTime time) { + return time.format(DateConstants.DATE_FORMAT_Y_M_D_H_M_S); + } + + /** + * LocalDateTime 转友好的相对时间字符串 + * + * @param dateTime LocalDateTime + * @return String友好的相对时间字符串 + */ + public static String formatRelativeTime(LocalDateTime dateTime) { + // 当前时间 + LocalDateTime now = LocalDateTime.now(); + + // 计算与当前时间的差距 + long daysDiff = ChronoUnit.DAYS.between(dateTime, now); + long hoursDiff = ChronoUnit.HOURS.between(dateTime, now); + long minutesDiff = ChronoUnit.MINUTES.between(dateTime, now); + + if (daysDiff < 1) { // 如果是今天 + if (hoursDiff < 1) { // 如果是几分钟前 + return minutesDiff + "分钟前"; + } else { // 如果是几小时前 + return hoursDiff + "小时前"; + } + } else if (daysDiff == 1) { // 如果是昨天 + return "昨天 " + dateTime.format(DateConstants.DATE_FORMAT_H_M); + } else if (daysDiff < 7) { // 如果是最近一周 + return daysDiff + "天前"; + } else if (dateTime.getYear() == now.getYear()) { // 如果是今年 + return dateTime.format(DateConstants.DATE_FORMAT_M_D); + } else { // 如果是去年或更早 + return dateTime.format(DateConstants.DATE_FORMAT_Y_M_D); + } + } + + public static void main(String[] args) { + // 测试示例 + LocalDateTime dateTime1 = LocalDateTime.now().minusMinutes(10); // 10分钟前 + LocalDateTime dateTime2 = LocalDateTime.now().minusHours(3); // 3小时前 + LocalDateTime dateTime3 = LocalDateTime.now().minusDays(1).minusHours(5); // 昨天 20:12 + LocalDateTime dateTime4 = LocalDateTime.now().minusDays(2); // 2天前 + LocalDateTime dateTime5 = LocalDateTime.now().minusDays(10); // 11-06 + LocalDateTime dateTime6 = LocalDateTime.of(2023, 12, 1, 12, 30, 0); // 2023-12-01 + + System.out.println(formatRelativeTime(dateTime1)); // 输出 "10分钟前" + System.out.println(formatRelativeTime(dateTime2)); // 输出 "3小时前" + System.out.println(formatRelativeTime(dateTime3)); // 输出 "昨天 20:12" + System.out.println(formatRelativeTime(dateTime4)); // 输出 "2天前" + System.out.println(formatRelativeTime(dateTime5)); // 输出 "11-06" + System.out.println(formatRelativeTime(dateTime6)); // 输出 "2023-12-01" + } }