test update

This commit is contained in:
tianfeng 2025-12-26 16:18:07 +08:00
parent fed32f20cd
commit 6ffebc79f8

View File

@ -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<GiftStatisticResult> 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<GiftStatisticResult> 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<GiftStatisticResult> queryGiftDetailGroupByGiftId(Long userId, Long acceptUserId) {
private List<GiftStatisticResult> queryGiftDetailGroupByGiftId(Long userId, Long acceptUserId, LocalDateTime startTime, LocalDateTime endTime) {
List<AggregationOperation> 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)));