diff --git a/rc-service/rc-service-other/other-start/src/test/java/statistics/GiftStatisticsTest.java b/rc-service/rc-service-other/other-start/src/test/java/statistics/GiftStatisticsTest.java new file mode 100644 index 00000000..5add183c --- /dev/null +++ b/rc-service/rc-service-other/other-start/src/test/java/statistics/GiftStatisticsTest.java @@ -0,0 +1,238 @@ +package statistics; + +import com.red.circle.OtherServiceApplication; +import com.red.circle.other.infra.database.mongo.entity.gift.GiftAcceptUser; +import com.red.circle.other.infra.database.mongo.entity.gift.GiftGiveRunningWater; +import lombok.extern.slf4j.Slf4j; +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.query.Criteria; +import org.springframework.data.mongodb.core.query.Query; +import org.springframework.test.context.junit4.SpringRunner; + +import java.math.BigDecimal; +import java.util.List; +import java.util.Objects; + +/** + * 礼物流水统计测试. + * + * @author Claude + */ +@Slf4j +@SpringBootTest(classes = OtherServiceApplication.class) +@RunWith(SpringRunner.class) +public class GiftStatisticsTest { + + @Autowired + private MongoTemplate mongoTemplate; + + /** + * 统计指定用户的累计礼物收入流水. + */ + @Test + public void testUserGiftStatistics() { + // TODO: 修改为要统计的用户ID + Long targetUserId = 1987264165204762626L; + + BigDecimal totalAmount = calculateUserGiftIncome(targetUserId); + + log.info("========================================"); + log.info("用户ID: {}", targetUserId); + log.info("累计礼物收入: {}", totalAmount); + log.info("========================================"); + } + + /** + * 批量统计多个用户的礼物收入. + */ + @Test + public void testBatchUserGiftStatistics() { + // TODO: 修改为要统计的用户ID列表 + Long[] userIds = {1234567890L, 9876543210L, 1111111111L}; + + log.info("========================================"); + log.info("开始批量统计礼物流水"); + log.info("========================================"); + + for (Long userId : userIds) { + BigDecimal totalAmount = calculateUserGiftIncome(userId); + log.info("用户ID: {} | 累计礼物收入: {}", userId, totalAmount); + } + + log.info("========================================"); + log.info("批量统计完成"); + log.info("========================================"); + } + + /** + * 统计指定时间段内用户的礼物收入. + */ + @Test + public void testUserGiftStatisticsWithTimeRange() { + // TODO: 修改参数 + Long targetUserId = 1234567890L; + Integer startDateNum = 20241201; // 开始日期 格式:yyyyMMdd + Integer endDateNum = 20241231; // 结束日期 格式:yyyyMMdd + + BigDecimal totalAmount = calculateUserGiftIncomeWithTimeRange( + targetUserId, startDateNum, endDateNum); + + log.info("========================================"); + log.info("用户ID: {}", targetUserId); + log.info("统计时间段: {} - {}", startDateNum, endDateNum); + log.info("累计礼物收入: {}", totalAmount); + log.info("========================================"); + } + + /** + * 详细统计:打印每笔礼物记录. + */ + @Test + public void testUserGiftStatisticsDetail() { + // TODO: 修改为要统计的用户ID + Long targetUserId = 1234567890L; + + Query query = new Query(); + query.addCriteria(Criteria.where("acceptUsers.acceptUserId").is(targetUserId)); + + List waterList = mongoTemplate.find( + query, GiftGiveRunningWater.class, "gift_give_running_water"); + + log.info("========================================"); + log.info("用户ID: {} 的礼物收入明细", targetUserId); + log.info("总记录数: {}", waterList.size()); + log.info("========================================"); + + BigDecimal totalAmount = BigDecimal.ZERO; + int recordIndex = 1; + + for (GiftGiveRunningWater water : waterList) { + if (water.getAcceptUsers() != null) { + for (GiftAcceptUser acceptUser : water.getAcceptUsers()) { + if (Objects.equals(acceptUser.getAcceptUserId(), targetUserId)) { + BigDecimal targetAmount = acceptUser.getTargetAmount() != null + ? acceptUser.getTargetAmount() : BigDecimal.ZERO; + totalAmount = totalAmount.add(targetAmount); + + log.info("第 {} 笔 | 流水ID: {} | 发送人: {} | 礼物ID: {} | 金额: {} | 时间: {}", + recordIndex++, + water.getId(), + water.getUserId(), + water.getGiftId(), + targetAmount, + water.getCreateTime()); + } + } + } + } + + log.info("========================================"); + log.info("累计礼物收入: {}", totalAmount); + log.info("========================================"); + } + + /** + * 按来源系统统计用户礼物收入. + */ + @Test + public void testUserGiftStatisticsBySysOrigin() { + // TODO: 修改参数 + Long targetUserId = 1234567890L; + String sysOrigin = "LIKEI"; // 系统来源 + + Query query = new Query(); + query.addCriteria(Criteria.where("acceptUsers.acceptUserId").is(targetUserId)); + query.addCriteria(Criteria.where("sysOrigin").is(sysOrigin)); + + List waterList = mongoTemplate.find( + query, GiftGiveRunningWater.class, "gift_give_running_water"); + + BigDecimal totalAmount = BigDecimal.ZERO; + for (GiftGiveRunningWater water : waterList) { + if (water.getAcceptUsers() != null) { + for (GiftAcceptUser acceptUser : water.getAcceptUsers()) { + if (Objects.equals(acceptUser.getAcceptUserId(), targetUserId)) { + BigDecimal targetAmount = acceptUser.getTargetAmount() != null + ? acceptUser.getTargetAmount() : BigDecimal.ZERO; + totalAmount = totalAmount.add(targetAmount); + } + } + } + } + + log.info("========================================"); + log.info("用户ID: {}", targetUserId); + log.info("系统来源: {}", sysOrigin); + log.info("累计礼物收入: {}", totalAmount); + log.info("========================================"); + } + + /** + * 计算指定用户的累计礼物收入. + * + * @param userId 用户ID + * @return 累计金额 + */ + private BigDecimal calculateUserGiftIncome(Long userId) { + Query query = new Query(); + query.addCriteria(Criteria.where("userId").is(userId)); + + List waterList = mongoTemplate.find( + query, GiftGiveRunningWater.class, "gift_give_running_water"); + + BigDecimal totalAmount = BigDecimal.ZERO; + + for (GiftGiveRunningWater water : waterList) { + if (water.getAcceptUsers() != null) { + for (GiftAcceptUser acceptUser : water.getAcceptUsers()) { + if (Objects.equals(acceptUser.getAcceptUserId(), userId)) { + BigDecimal targetAmount = acceptUser.getTargetAmount() != null + ? acceptUser.getTargetAmount() : BigDecimal.ZERO; + totalAmount = totalAmount.add(targetAmount); + } + } + } + } + + return totalAmount; + } + + /** + * 计算指定用户在指定时间段内的累计礼物收入. + * + * @param userId 用户ID + * @param startDateNum 开始日期(沙特时间,格式:yyyyMMdd) + * @param endDateNum 结束日期(沙特时间,格式:yyyyMMdd) + * @return 累计金额 + */ + private BigDecimal calculateUserGiftIncomeWithTimeRange( + Long userId, Integer startDateNum, Integer endDateNum) { + + Query query = new Query(); + query.addCriteria(Criteria.where("userId").is(userId)); + query.addCriteria(Criteria.where("createTime").gte(startDateNum).lte(endDateNum)); + + List waterList = mongoTemplate.find( + query, GiftGiveRunningWater.class, "gift_give_running_water"); + + BigDecimal totalAmount = BigDecimal.ZERO; + + for (GiftGiveRunningWater water : waterList) { + if (water.getAcceptUsers() != null) { + for (GiftAcceptUser acceptUser : water.getAcceptUsers()) { + if (Objects.equals(acceptUser.getAcceptUserId(), userId)) { + BigDecimal targetAmount = acceptUser.getTargetAmount() != null + ? acceptUser.getTargetAmount() : BigDecimal.ZERO; + totalAmount = totalAmount.add(targetAmount); + } + } + } + } + + return totalAmount; + } +} \ No newline at end of file