This commit is contained in:
tianfeng 2025-12-13 15:38:31 +08:00
parent 5c9c8f7cfb
commit f1d5e994fd

View File

@ -3,6 +3,8 @@ package com.red.circle.other.test;
import com.mongodb.client.result.DeleteResult;
import com.red.circle.OtherServiceApplication;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import org.bson.Document;
import org.bson.types.Decimal128;
import org.junit.Test;
@ -11,6 +13,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.AggregationOperation;
import org.springframework.data.mongodb.core.aggregation.AggregationResults;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.test.context.junit4.SpringRunner;
@ -30,12 +33,12 @@ public class AnchorGiftTest {
*/
@Test
public void testQueryTargetAmountSum() {
Long userId = 1996197707655929857L; // 发送礼物的人
Long userId = null; // 发送礼物的人null表示查询所有发送人
Long acceptUserId = 1996197707655929857L; // 接收礼物的人
BigDecimal totalTargetAmount = queryTargetAmountSum(userId, acceptUserId);
System.out.println("发送人ID: " + userId);
System.out.println("发送人ID: " + (userId == null ? "全部" : userId));
System.out.println("接收人ID: " + acceptUserId);
System.out.println("目标总金额: " + totalTargetAmount);
}
@ -43,26 +46,29 @@ public class AnchorGiftTest {
/**
* 查询指定发送人和接收人之间的目标总金额
*
* @param userId 发送礼物的人ID
* @param userId 发送礼物的人IDnull表示查询所有发送人
* @param acceptUserId 接收礼物的人ID
* @return 目标总金额
*/
private BigDecimal queryTargetAmountSum(Long userId, Long acceptUserId) {
List<AggregationOperation> operations = new ArrayList<>();
// 1. 匹配发送人如果userId不为null
if (userId != null) {
operations.add(Aggregation.match(Criteria.where("userId").is(userId)));
}
// 2. 展开 acceptUsers 数组
operations.add(Aggregation.unwind("acceptUsers"));
// 3. 匹配接收人
operations.add(Aggregation.match(Criteria.where("acceptUsers.acceptUserId").is(acceptUserId)));
// 4. 聚合求和
operations.add(Aggregation.group().sum("acceptUsers.targetAmount").as("totalTargetAmount"));
// 构建聚合查询
Aggregation aggregation = Aggregation.newAggregation(
// 1. 匹配发送人
Aggregation.match(Criteria.where("userId").is(userId)),
// 2. 展开 acceptUsers 数组
Aggregation.unwind("acceptUsers"),
// 3. 匹配接收人
Aggregation.match(Criteria.where("acceptUsers.acceptUserId").is(acceptUserId)),
// 4. 聚合求和
Aggregation.group()
.sum("acceptUsers.targetAmount").as("totalTargetAmount")
);
Aggregation aggregation = Aggregation.newAggregation(operations);
// 执行聚合查询
AggregationResults<Document> results = mongoTemplate.aggregate(
@ -90,4 +96,179 @@ public class AnchorGiftTest {
return BigDecimal.ZERO;
}
/**
* 按礼物ID分组统计详细信息
*/
@Test
public void testQueryGiftDetailGroupByGiftId() {
Long userId = null; // 发送礼物的人null表示查询所有发送人
Long acceptUserId = 1996197707655929857L; // 接收礼物的人
List<GiftStatisticResult> results = queryGiftDetailGroupByGiftId(userId, acceptUserId);
System.out.println("发送人ID: " + (userId == null ? "全部" : userId));
System.out.println("接收人ID: " + acceptUserId);
System.out.println("礼物统计结果:");
System.out.println("========================================");
for (GiftStatisticResult result : results) {
System.out.println("礼物ID: " + result.getGiftId());
System.out.println("礼物类型: " + result.getGiftType());
System.out.println("是否幸运礼物: " + (result.getLuckyGift() ? "" : ""));
System.out.println("赠送次数: " + result.getTotalCount());
System.out.println("总数量: " + result.getTotalQuantity());
System.out.println("总价值: " + result.getTotalGiftValue());
System.out.println("总目标金额: " + result.getTotalTargetAmount());
System.out.println("----------------------------------------");
}
}
/**
* 按礼物ID分组统计详细信息
*
* @param userId 发送礼物的人IDnull表示查询所有发送人
* @param acceptUserId 接收礼物的人ID
* @return 礼物统计结果列表
*/
private List<GiftStatisticResult> queryGiftDetailGroupByGiftId(Long userId, Long acceptUserId) {
List<AggregationOperation> operations = new ArrayList<>();
// 1. 匹配发送人如果userId不为null
if (userId != null) {
operations.add(Aggregation.match(Criteria.where("userId").is(userId)));
}
// 2. 展开 acceptUsers 数组
operations.add(Aggregation.unwind("acceptUsers"));
// 3. 匹配接收人
operations.add(Aggregation.match(Criteria.where("acceptUsers.acceptUserId").is(acceptUserId)));
// 4. 按礼物ID分组并统计
operations.add(Aggregation.group("giftId")
.first("giftId").as("giftId")
.first("giftType").as("giftType")
.first("luckyGift").as("luckyGift")
.count().as("totalCount") // 赠送次数
.sum("giftValue.quantity").as("totalQuantity") // 总数量
.sum("giftValue.giftValue").as("totalGiftValue") // 总价值
.sum("acceptUsers.targetAmount").as("totalTargetAmount") // 总目标金额
);
// 5. 按总目标金额降序排序
operations.add(Aggregation.sort(org.springframework.data.domain.Sort.Direction.DESC, "totalTargetAmount"));
// 构建聚合查询
Aggregation aggregation = Aggregation.newAggregation(operations);
// 执行聚合查询
AggregationResults<Document> results = mongoTemplate.aggregate(
aggregation,
"gift_give_running_water",
Document.class
);
// 转换结果
List<GiftStatisticResult> resultList = new ArrayList<>();
for (Document doc : results.getMappedResults()) {
GiftStatisticResult result = new GiftStatisticResult();
result.setGiftId(doc.getLong("giftId"));
result.setGiftType(doc.getString("giftType"));
result.setLuckyGift(doc.getBoolean("luckyGift", false));
result.setTotalCount(doc.getInteger("totalCount", 0));
result.setTotalQuantity(doc.getInteger("totalQuantity", 0));
result.setTotalGiftValue(convertToBigDecimal(doc.get("totalGiftValue")));
result.setTotalTargetAmount(convertToBigDecimal(doc.get("totalTargetAmount")));
resultList.add(result);
}
return resultList;
}
/**
* 将MongoDB的数字类型转换为BigDecimal
*/
private BigDecimal convertToBigDecimal(Object value) {
if (value == null) {
return BigDecimal.ZERO;
}
if (value instanceof Decimal128) {
return ((Decimal128) value).bigDecimalValue();
} else if (value instanceof BigDecimal) {
return (BigDecimal) value;
} else if (value instanceof Number) {
return new BigDecimal(value.toString());
}
return BigDecimal.ZERO;
}
/**
* 礼物统计结果
*/
public static class GiftStatisticResult {
private Long giftId;
private String giftType;
private Boolean luckyGift;
private Integer totalCount;
private Integer totalQuantity;
private BigDecimal totalGiftValue;
private BigDecimal totalTargetAmount;
public Long getGiftId() {
return giftId;
}
public void setGiftId(Long giftId) {
this.giftId = giftId;
}
public String getGiftType() {
return giftType;
}
public void setGiftType(String giftType) {
this.giftType = giftType;
}
public Boolean getLuckyGift() {
return luckyGift;
}
public void setLuckyGift(Boolean luckyGift) {
this.luckyGift = luckyGift;
}
public Integer getTotalCount() {
return totalCount;
}
public void setTotalCount(Integer totalCount) {
this.totalCount = totalCount;
}
public Integer getTotalQuantity() {
return totalQuantity;
}
public void setTotalQuantity(Integer totalQuantity) {
this.totalQuantity = totalQuantity;
}
public BigDecimal getTotalGiftValue() {
return totalGiftValue;
}
public void setTotalGiftValue(BigDecimal totalGiftValue) {
this.totalGiftValue = totalGiftValue;
}
public BigDecimal getTotalTargetAmount() {
return totalTargetAmount;
}
public void setTotalTargetAmount(BigDecimal totalTargetAmount) {
this.totalTargetAmount = totalTargetAmount;
}
}
}