add test
This commit is contained in:
parent
25a821a3a2
commit
5c9c8f7cfb
@ -0,0 +1,93 @@
|
||||
package com.red.circle.other.test;
|
||||
|
||||
import com.mongodb.client.result.DeleteResult;
|
||||
import com.red.circle.OtherServiceApplication;
|
||||
import java.math.BigDecimal;
|
||||
import org.bson.Document;
|
||||
import org.bson.types.Decimal128;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
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.AggregationResults;
|
||||
import org.springframework.data.mongodb.core.query.Criteria;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
/**
|
||||
* 主播礼物测试类
|
||||
*/
|
||||
@SpringBootTest(classes = OtherServiceApplication.class)
|
||||
@RunWith(SpringRunner.class)
|
||||
public class AnchorGiftTest {
|
||||
|
||||
@Autowired
|
||||
private MongoTemplate mongoTemplate;
|
||||
|
||||
/**
|
||||
* 统计指定发送人和接收人之间的目标总金额
|
||||
*/
|
||||
@Test
|
||||
public void testQueryTargetAmountSum() {
|
||||
Long userId = 1996197707655929857L; // 发送礼物的人
|
||||
Long acceptUserId = 1996197707655929857L; // 接收礼物的人
|
||||
|
||||
BigDecimal totalTargetAmount = queryTargetAmountSum(userId, acceptUserId);
|
||||
|
||||
System.out.println("发送人ID: " + userId);
|
||||
System.out.println("接收人ID: " + acceptUserId);
|
||||
System.out.println("目标总金额: " + totalTargetAmount);
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询指定发送人和接收人之间的目标总金额
|
||||
*
|
||||
* @param userId 发送礼物的人ID
|
||||
* @param acceptUserId 接收礼物的人ID
|
||||
* @return 目标总金额
|
||||
*/
|
||||
private BigDecimal queryTargetAmountSum(Long userId, Long acceptUserId) {
|
||||
// 构建聚合查询
|
||||
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")
|
||||
);
|
||||
|
||||
// 执行聚合查询
|
||||
AggregationResults<Document> results = mongoTemplate.aggregate(
|
||||
aggregation,
|
||||
"gift_give_running_water",
|
||||
Document.class
|
||||
);
|
||||
|
||||
// 获取结果
|
||||
Document result = results.getUniqueMappedResult();
|
||||
if (result == null || result.get("totalTargetAmount") == null) {
|
||||
return BigDecimal.ZERO;
|
||||
}
|
||||
|
||||
// MongoDB 中的 Decimal128 需要转换为 BigDecimal
|
||||
Object totalAmount = result.get("totalTargetAmount");
|
||||
if (totalAmount instanceof Decimal128) {
|
||||
return ((Decimal128) totalAmount).bigDecimalValue();
|
||||
} else if (totalAmount instanceof BigDecimal) {
|
||||
return (BigDecimal) totalAmount;
|
||||
} else if (totalAmount instanceof Number) {
|
||||
return new BigDecimal(totalAmount.toString());
|
||||
}
|
||||
|
||||
return BigDecimal.ZERO;
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user