From 7b18bde0e8cd64ce9c235eae1edf23f95a1fc220 Mon Sep 17 00:00:00 2001 From: sion123 <450702724@qq.com> Date: Sun, 15 Mar 2026 15:37:32 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/AiUsageStatsServiceImpl.java | 162 +++++++++--------- 1 file changed, 85 insertions(+), 77 deletions(-) diff --git a/yudao-module-tik/src/main/java/cn/iocoder/yudao/module/tik/muye/aiusagestats/service/AiUsageStatsServiceImpl.java b/yudao-module-tik/src/main/java/cn/iocoder/yudao/module/tik/muye/aiusagestats/service/AiUsageStatsServiceImpl.java index fbbea449ee..649edb738c 100644 --- a/yudao-module-tik/src/main/java/cn/iocoder/yudao/module/tik/muye/aiusagestats/service/AiUsageStatsServiceImpl.java +++ b/yudao-module-tik/src/main/java/cn/iocoder/yudao/module/tik/muye/aiusagestats/service/AiUsageStatsServiceImpl.java @@ -48,110 +48,118 @@ public class AiUsageStatsServiceImpl implements AiUsageStatsService { @Override public AiUsageOverviewRespVO getOverview(LocalDateTime startTime, LocalDateTime endTime, String bizType) { - // 查询业务类型统计 - List> bizTypeStatsList = pointRecordMapper.selectBizTypeStats(startTime, endTime); + return TenantUtils.executeIgnore(() -> { + // 查询业务类型统计 + List> bizTypeStatsList = pointRecordMapper.selectBizTypeStats(startTime, endTime); - // 计算汇总数据 - long totalCallCount = 0; - long totalConsumePoints = 0; - long totalTokens = 0; + // 计算汇总数据 + long totalCallCount = 0; + long totalConsumePoints = 0; + long totalTokens = 0; - List bizTypeStats = new ArrayList<>(); - for (Map stats : bizTypeStatsList) { - String bt = (String) stats.get("bizType"); - // 如果指定了业务类型,只统计该类型 - if (bizType != null && !bizType.isEmpty() && !bizType.equals(bt)) { - continue; + List bizTypeStats = new ArrayList<>(); + for (Map stats : bizTypeStatsList) { + String bt = (String) stats.get("bizType"); + // 如果指定了业务类型,只统计该类型 + if (bizType != null && !bizType.isEmpty() && !bizType.equals(bt)) { + continue; + } + + long callCount = getLongValue(stats, "callCount"); + long consumePoints = getLongValue(stats, "consumePoints"); + long tokens = getLongValue(stats, "totalTokens"); + + totalCallCount += callCount; + totalConsumePoints += consumePoints; + totalTokens += tokens; + + bizTypeStats.add(AiUsageOverviewRespVO.BizTypeStats.builder() + .bizType(bt) + .bizTypeName(BIZ_TYPE_NAMES.getOrDefault(bt, bt)) + .callCount(callCount) + .consumePoints(consumePoints) + .totalTokens(tokens) + .build()); } - long callCount = getLongValue(stats, "callCount"); - long consumePoints = getLongValue(stats, "consumePoints"); - long tokens = getLongValue(stats, "totalTokens"); + // 查询活跃用户数 + Long activeUserCount = pointRecordMapper.selectActiveUserCount(startTime, endTime, bizType); - totalCallCount += callCount; - totalConsumePoints += consumePoints; - totalTokens += tokens; - - bizTypeStats.add(AiUsageOverviewRespVO.BizTypeStats.builder() - .bizType(bt) - .bizTypeName(BIZ_TYPE_NAMES.getOrDefault(bt, bt)) - .callCount(callCount) - .consumePoints(consumePoints) - .totalTokens(tokens) - .build()); - } - - // 查询活跃用户数 - Long activeUserCount = pointRecordMapper.selectActiveUserCount(startTime, endTime, bizType); - - return AiUsageOverviewRespVO.builder() - .totalCallCount(totalCallCount) - .totalConsumePoints(totalConsumePoints) - .totalTokens(totalTokens) - .activeUserCount(activeUserCount) - .bizTypeStats(bizTypeStats) - .build(); + return AiUsageOverviewRespVO.builder() + .totalCallCount(totalCallCount) + .totalConsumePoints(totalConsumePoints) + .totalTokens(totalTokens) + .activeUserCount(activeUserCount) + .bizTypeStats(bizTypeStats) + .build(); + }); } @Override public PageResult getUserStatsPage(AiUsageStatsPageReqVO reqVO) { - // 查询用户统计数据 - List> userStatsList = pointRecordMapper.selectUserStats( - reqVO.getStartTime(), reqVO.getEndTime(), reqVO.getBizType(), reqVO.getUserId(), reqVO.getMobile()); + return TenantUtils.executeIgnore(() -> { + // 查询用户统计数据 + List> userStatsList = pointRecordMapper.selectUserStats( + reqVO.getStartTime(), reqVO.getEndTime(), reqVO.getBizType(), reqVO.getUserId(), reqVO.getMobile()); - // 计算总数 - long total = userStatsList.size(); + // 计算总数 + long total = userStatsList.size(); - // 分页处理 - int fromIndex = (reqVO.getPageNo() - 1) * reqVO.getPageSize(); - int toIndex = Math.min(fromIndex + reqVO.getPageSize(), userStatsList.size()); + // 分页处理 + int fromIndex = (reqVO.getPageNo() - 1) * reqVO.getPageSize(); + int toIndex = Math.min(fromIndex + reqVO.getPageSize(), userStatsList.size()); - if (fromIndex >= userStatsList.size()) { - return new PageResult<>(Collections.emptyList(), total); - } + if (fromIndex >= userStatsList.size()) { + return new PageResult<>(Collections.emptyList(), total); + } - List> pagedList = userStatsList.subList(fromIndex, toIndex); + List> pagedList = userStatsList.subList(fromIndex, toIndex); - // 转换为 VO - List list = pagedList.stream() - .map(this::convertToUserStatsVO) - .collect(Collectors.toList()); + // 转换为 VO + List list = pagedList.stream() + .map(this::convertToUserStatsVO) + .collect(Collectors.toList()); - return new PageResult<>(list, total); + return new PageResult<>(list, total); + }); } @Override public List getAppStats(LocalDateTime startTime, LocalDateTime endTime, String bizType) { - // 查询应用统计数据 - List> appStatsList = pointRecordMapper.selectAppStats( - startTime, endTime, bizType); + return TenantUtils.executeIgnore(() -> { + // 查询应用统计数据 + List> appStatsList = pointRecordMapper.selectAppStats( + startTime, endTime, bizType); - // 获取服务配置信息 - Map configMap = aiServiceConfigMapper.selectList( - new LambdaQueryWrapperX().eq(AiServiceConfigDO::getStatus, 1)) - .stream() - .collect(Collectors.toMap(AiServiceConfigDO::getServiceCode, c -> c, (a, b) -> a)); + // 获取服务配置信息 + Map configMap = aiServiceConfigMapper.selectList( + new LambdaQueryWrapperX().eq(AiServiceConfigDO::getStatus, 1)) + .stream() + .collect(Collectors.toMap(AiServiceConfigDO::getServiceCode, c -> c, (a, b) -> a)); - // 转换为 VO - return appStatsList.stream() - .map(stats -> convertToAppStatsVO(stats, configMap)) - .collect(Collectors.toList()); + // 转换为 VO + return appStatsList.stream() + .map(stats -> convertToAppStatsVO(stats, configMap)) + .collect(Collectors.toList()); + }); } @Override public AiUsageTrendRespVO getTrend(LocalDateTime startTime, LocalDateTime endTime, String bizType, String type) { - // 查询趋势数据 - List> trendList = pointRecordMapper.selectTrend( - startTime, endTime, bizType, "day".equals(type) ? "%Y-%m-%d" : "%Y-%m-%d %H:00"); + return TenantUtils.executeIgnore(() -> { + // 查询趋势数据 + List> trendList = pointRecordMapper.selectTrend( + startTime, endTime, bizType, "day".equals(type) ? "%Y-%m-%d" : "%Y-%m-%d %H:00"); - // 转换为 VO - List items = trendList.stream() - .map(this::convertToTrendItem) - .collect(Collectors.toList()); + // 转换为 VO + List items = trendList.stream() + .map(this::convertToTrendItem) + .collect(Collectors.toList()); - return AiUsageTrendRespVO.builder() - .trendList(items) - .build(); + return AiUsageTrendRespVO.builder() + .trendList(items) + .build(); + }); } /**