From 6ffebc79f8085928f5b087e692d6559b412e2bf1 Mon Sep 17 00:00:00 2001 From: tianfeng <769204422@qq.com> Date: Fri, 26 Dec 2025 16:18:07 +0800 Subject: [PATCH] test update --- .../src/test/java/AnchorGiftTest.java | 39 ++++++++++++++----- 1 file changed, 30 insertions(+), 9 deletions(-) diff --git a/rc-service/rc-service-other/other-start/src/test/java/AnchorGiftTest.java b/rc-service/rc-service-other/other-start/src/test/java/AnchorGiftTest.java index eb996274..f2414950 100644 --- a/rc-service/rc-service-other/other-start/src/test/java/AnchorGiftTest.java +++ b/rc-service/rc-service-other/other-start/src/test/java/AnchorGiftTest.java @@ -3,6 +3,10 @@ package com.red.circle.other.test; import com.mongodb.client.result.DeleteResult; import com.red.circle.OtherServiceApplication; import java.math.BigDecimal; +import java.time.LocalDate; +import java.time.LocalDateTime; +import java.time.LocalTime; +import java.time.ZoneId; import java.util.ArrayList; import java.util.List; import org.bson.Document; @@ -102,9 +106,13 @@ public class AnchorGiftTest { @Test public void testQueryGiftDetailGroupByGiftId() { Long userId = null; // 发送礼物的人,null表示查询所有发送人 - Long acceptUserId = 1996197707655929857L; // 接收礼物的人 - - List results = queryGiftDetailGroupByGiftId(userId, acceptUserId); + Long acceptUserId = 1986355914717921281L; // 接收礼物的人 + + // 使用 UTC 时区 + LocalDateTime startOfDay = LocalDateTime.of(LocalDate.now(ZoneId.of("UTC")), LocalTime.MIN).minusDays(1); + LocalDateTime endOfDay = LocalDateTime.of(LocalDate.now(ZoneId.of("UTC")), LocalTime.MAX).minusDays(1); + + List results = queryGiftDetailGroupByGiftId(userId, acceptUserId, startOfDay, endOfDay); System.out.println("发送人ID: " + (userId == null ? "全部" : userId)); System.out.println("接收人ID: " + acceptUserId); @@ -130,17 +138,30 @@ public class AnchorGiftTest { * @param acceptUserId 接收礼物的人ID * @return 礼物统计结果列表 */ - private List queryGiftDetailGroupByGiftId(Long userId, Long acceptUserId) { + private List queryGiftDetailGroupByGiftId(Long userId, Long acceptUserId, LocalDateTime startTime, LocalDateTime endTime) { List operations = new ArrayList<>(); - - // 1. 匹配发送人(如果userId不为null) + + // 1. 构建时间范围条件 + Criteria criteria = new Criteria(); if (userId != null) { - operations.add(Aggregation.match(Criteria.where("userId").is(userId))); + criteria.and("userId").is(userId); } - + if (startTime != null && endTime != null) { + criteria.and("createTime").gte(startTime).lte(endTime); + } else if (startTime != null) { + criteria.and("createTime").gte(startTime); + } else if (endTime != null) { + criteria.and("createTime").lte(endTime); + } + + if (!criteria.getCriteriaObject().isEmpty()) { + operations.add(Aggregation.match(criteria)); + } + // 2. 展开 acceptUsers 数组 operations.add(Aggregation.unwind("acceptUsers")); - + + // 3. 匹配接收人 operations.add(Aggregation.match(Criteria.where("acceptUsers.acceptUserId").is(acceptUserId)));