This commit is contained in:
2025-09-12 13:20:08 +08:00
parent e05ad9e223
commit c46c1e23a1
2 changed files with 139 additions and 105 deletions

View File

@@ -28,22 +28,23 @@ import (
// StatisticsApplicationServiceImpl 统计应用服务实现
type StatisticsApplicationServiceImpl struct {
// 领域服务
aggregateService services.StatisticsAggregateService
aggregateService services.StatisticsAggregateService
calculationService services.StatisticsCalculationService
reportService services.StatisticsReportService
reportService services.StatisticsReportService
// 统计仓储
metricRepo repositories.StatisticsRepository
reportRepo repositories.StatisticsReportRepository
metricRepo repositories.StatisticsRepository
reportRepo repositories.StatisticsReportRepository
dashboardRepo repositories.StatisticsDashboardRepository
// 其他领域仓储
userRepo userRepos.UserRepository
apiCallRepo apiRepos.ApiCallRepository
userRepo userRepos.UserRepository
enterpriseInfoRepo userRepos.EnterpriseInfoRepository
apiCallRepo apiRepos.ApiCallRepository
walletTransactionRepo financeRepos.WalletTransactionRepository
rechargeRecordRepo financeRepos.RechargeRecordRepository
productRepo productRepos.ProductRepository
certificationRepo certificationRepos.CertificationQueryRepository
rechargeRecordRepo financeRepos.RechargeRecordRepository
productRepo productRepos.ProductRepository
certificationRepo certificationRepos.CertificationQueryRepository
// 日志
logger *zap.Logger
@@ -58,6 +59,7 @@ func NewStatisticsApplicationService(
reportRepo repositories.StatisticsReportRepository,
dashboardRepo repositories.StatisticsDashboardRepository,
userRepo userRepos.UserRepository,
enterpriseInfoRepo userRepos.EnterpriseInfoRepository,
apiCallRepo apiRepos.ApiCallRepository,
walletTransactionRepo financeRepos.WalletTransactionRepository,
rechargeRecordRepo financeRepos.RechargeRecordRepository,
@@ -73,6 +75,7 @@ func NewStatisticsApplicationService(
reportRepo: reportRepo,
dashboardRepo: dashboardRepo,
userRepo: userRepo,
enterpriseInfoRepo: enterpriseInfoRepo,
apiCallRepo: apiCallRepo,
walletTransactionRepo: walletTransactionRepo,
rechargeRecordRepo: rechargeRecordRepo,
@@ -540,10 +543,10 @@ func (s *StatisticsApplicationServiceImpl) GetDashboardData(ctx context.Context,
query.StartDate = now.Truncate(24 * time.Hour)
query.EndDate = query.StartDate.Add(24 * time.Hour)
case "week":
query.StartDate = now.Truncate(24 * time.Hour).AddDate(0, 0, -7)
query.StartDate = now.Truncate(24*time.Hour).AddDate(0, 0, -7)
query.EndDate = now
case "month":
query.StartDate = now.Truncate(24 * time.Hour).AddDate(0, 0, -30)
query.StartDate = now.Truncate(24*time.Hour).AddDate(0, 0, -30)
query.EndDate = now
default:
query.StartDate = now.Truncate(24 * time.Hour)
@@ -1307,7 +1310,7 @@ func (s *StatisticsApplicationServiceImpl) getUserApiCallsStats(ctx context.Cont
}
// 获取本月调用次数
monthStart := time.Now().Truncate(24 * time.Hour).AddDate(0, 0, -time.Now().Day()+1)
monthStart := time.Now().Truncate(24*time.Hour).AddDate(0, 0, -time.Now().Day()+1)
monthEnd := monthStart.AddDate(0, 1, 0)
monthCalls, err := s.getApiCallsCountByDateRange(ctx, userID, monthStart, monthEnd)
if err != nil {
@@ -1362,7 +1365,7 @@ func (s *StatisticsApplicationServiceImpl) getUserConsumptionStats(ctx context.C
}
// 获取本月消费金额
monthStart := time.Now().Truncate(24 * time.Hour).AddDate(0, 0, -time.Now().Day()+1)
monthStart := time.Now().Truncate(24*time.Hour).AddDate(0, 0, -time.Now().Day()+1)
monthEnd := monthStart.AddDate(0, 1, 0)
monthAmount, err := s.getWalletTransactionsByDateRange(ctx, userID, monthStart, monthEnd)
if err != nil {
@@ -1417,7 +1420,7 @@ func (s *StatisticsApplicationServiceImpl) getUserRechargeStats(ctx context.Cont
}
// 获取本月充值金额
monthStart := time.Now().Truncate(24 * time.Hour).AddDate(0, 0, -time.Now().Day()+1)
monthStart := time.Now().Truncate(24*time.Hour).AddDate(0, 0, -time.Now().Day()+1)
monthEnd := monthStart.AddDate(0, 1, 0)
monthAmount, err := s.getRechargeRecordsByDateRange(ctx, userID, monthStart, monthEnd)
if err != nil {
@@ -1656,10 +1659,10 @@ func (s *StatisticsApplicationServiceImpl) getUserStats(ctx context.Context, per
}
stats := map[string]interface{}{
"total_users": userStats.TotalUsers,
"new_today": userStats.TodayRegistrations,
"new_in_range": newInRange,
"daily_trend": trendData,
"total_users": userStats.TotalUsers,
"new_today": userStats.TodayRegistrations,
"new_in_range": newInRange,
"daily_trend": trendData,
}
return stats, nil
}
@@ -1703,10 +1706,10 @@ func (s *StatisticsApplicationServiceImpl) getCertificationStats(ctx context.Con
}
stats := map[string]interface{}{
"total_certified": userStats.CertifiedUsers,
"certified_today": userStats.TodayRegistrations, // 今日注册的用户
"success_rate": successRate,
"daily_trend": trendData,
"total_certified": userStats.CertifiedUsers,
"certified_today": userStats.TodayRegistrations, // 今日注册的用户
"success_rate": successRate,
"daily_trend": trendData,
}
return stats, nil
}
@@ -1753,9 +1756,9 @@ func (s *StatisticsApplicationServiceImpl) getSystemApiCallStats(ctx context.Con
}
stats := map[string]interface{}{
"total_calls": totalCalls,
"calls_today": todayCalls,
"daily_trend": trendData,
"total_calls": totalCalls,
"calls_today": todayCalls,
"daily_trend": trendData,
}
return stats, nil
}
@@ -1844,11 +1847,11 @@ func (s *StatisticsApplicationServiceImpl) getSystemFinanceStats(ctx context.Con
}
stats := map[string]interface{}{
"total_deduct": totalConsumption,
"deduct_today": todayConsumption,
"total_recharge": totalRecharge,
"recharge_today": todayRecharge,
"daily_trend": trendData,
"total_deduct": totalConsumption,
"deduct_today": todayConsumption,
"total_recharge": totalRecharge,
"recharge_today": todayRecharge,
"daily_trend": trendData,
}
return stats, nil
}
@@ -2247,11 +2250,11 @@ func (s *StatisticsApplicationServiceImpl) AdminGetUserDomainStatistics(ctx cont
}
result := map[string]interface{}{
"user_stats": userStats,
"user_stats": userStats,
"certification_stats": certificationStats,
"period": period,
"start_date": startDate,
"end_date": endDate,
"period": period,
"start_date": startDate,
"end_date": endDate,
}
return &QueryResponse{
@@ -2333,9 +2336,19 @@ func (s *StatisticsApplicationServiceImpl) AdminGetConsumptionDomainStatistics(c
return nil, err
}
todayConsumption := float64(0)
// 获取今日消费金额
today := time.Now().Truncate(24 * time.Hour)
tomorrow := today.Add(24 * time.Hour)
todayConsumption, err := s.walletTransactionRepo.GetSystemAmountByDateRange(ctx, today, tomorrow)
if err != nil {
s.logger.Error("获取今日消费金额失败", zap.Error(err))
return nil, err
}
// 获取指定时间范围内的消费金额
rangeConsumption := float64(0)
if !startTime.IsZero() && !endTime.IsZero() {
todayConsumption, err = s.walletTransactionRepo.GetSystemAmountByDateRange(ctx, startTime, endTime)
rangeConsumption, err = s.walletTransactionRepo.GetSystemAmountByDateRange(ctx, startTime, endTime)
if err != nil {
s.logger.Error("获取指定时间范围消费金额失败", zap.Error(err))
return nil, err
@@ -2355,12 +2368,13 @@ func (s *StatisticsApplicationServiceImpl) AdminGetConsumptionDomainStatistics(c
}
result := map[string]interface{}{
"total_consumption": totalConsumption,
"range_consumption": todayConsumption,
"consumption_trend": consumptionTrend,
"period": period,
"start_date": startDate,
"end_date": endDate,
"total_consumption": totalConsumption,
"today_consumption": todayConsumption,
"range_consumption": rangeConsumption,
"consumption_trend": consumptionTrend,
"period": period,
"start_date": startDate,
"end_date": endDate,
}
return &QueryResponse{
@@ -2399,9 +2413,19 @@ func (s *StatisticsApplicationServiceImpl) AdminGetRechargeDomainStatistics(ctx
return nil, err
}
todayRecharge := float64(0)
// 获取今日充值金额
today := time.Now().Truncate(24 * time.Hour)
tomorrow := today.Add(24 * time.Hour)
todayRecharge, err := s.rechargeRecordRepo.GetSystemAmountByDateRange(ctx, today, tomorrow)
if err != nil {
s.logger.Error("获取今日充值金额失败", zap.Error(err))
return nil, err
}
// 获取指定时间范围内的充值金额
rangeRecharge := float64(0)
if !startTime.IsZero() && !endTime.IsZero() {
todayRecharge, err = s.rechargeRecordRepo.GetSystemAmountByDateRange(ctx, startTime, endTime)
rangeRecharge, err = s.rechargeRecordRepo.GetSystemAmountByDateRange(ctx, startTime, endTime)
if err != nil {
s.logger.Error("获取指定时间范围充值金额失败", zap.Error(err))
return nil, err
@@ -2421,12 +2445,13 @@ func (s *StatisticsApplicationServiceImpl) AdminGetRechargeDomainStatistics(ctx
}
result := map[string]interface{}{
"total_recharge": totalRecharge,
"range_recharge": todayRecharge,
"recharge_trend": rechargeTrend,
"period": period,
"start_date": startDate,
"end_date": endDate,
"total_recharge": totalRecharge,
"today_recharge": todayRecharge,
"range_recharge": rangeRecharge,
"recharge_trend": rechargeTrend,
"period": period,
"start_date": startDate,
"end_date": endDate,
}
return &QueryResponse{
@@ -2616,11 +2641,11 @@ func (s *StatisticsApplicationServiceImpl) AdminGetTodayCertifiedEnterprises(ctx
// 查询所有已完成的认证,然后过滤今日完成的
query := &certificationQueries.ListCertificationsQuery{
Page: 1,
PageSize: 1000, // 设置较大的页面大小以获取所有数据
SortBy: "updated_at",
Page: 1,
PageSize: 1000, // 设置较大的页面大小以获取所有数据
SortBy: "updated_at",
SortOrder: "desc",
Status: certificationEnums.StatusCompleted,
Status: certificationEnums.StatusCompleted,
}
certifications, _, err := s.certificationRepo.List(ctx, query)
@@ -2633,8 +2658,8 @@ func (s *StatisticsApplicationServiceImpl) AdminGetTodayCertifiedEnterprises(ctx
var completedCertifications []*certificationEntities.Certification
for _, cert := range certifications {
if cert.CompletedAt != nil &&
cert.CompletedAt.After(todayStart) &&
cert.CompletedAt.Before(todayEnd) {
cert.CompletedAt.After(todayStart) &&
cert.CompletedAt.Before(todayEnd) {
completedCertifications = append(completedCertifications, cert)
}
}
@@ -2653,26 +2678,33 @@ func (s *StatisticsApplicationServiceImpl) AdminGetTodayCertifiedEnterprises(ctx
completedCertifications = completedCertifications[:limit]
}
// 获取用户信息
// 直接从企业信息表获取数据
var enterprises []map[string]interface{}
for _, cert := range completedCertifications {
// 获取企业信息
enterpriseInfo, err := s.enterpriseInfoRepo.GetByUserID(ctx, cert.UserID)
if err != nil {
s.logger.Warn("获取企业信息失败", zap.String("user_id", cert.UserID), zap.Error(err))
continue
}
// 获取用户基本信息(仅需要用户名)
user, err := s.userRepo.GetByID(ctx, cert.UserID)
if err != nil {
s.logger.Warn("获取用户信息失败", zap.String("user_id", cert.UserID), zap.Error(err))
continue
}
enterpriseName := ""
if user.EnterpriseInfo != nil {
enterpriseName = user.EnterpriseInfo.CompanyName
}
enterprise := map[string]interface{}{
"id": cert.ID,
"user_id": cert.UserID,
"username": user.Username,
"enterprise_name": enterpriseName,
"certified_at": cert.CompletedAt.Format(time.RFC3339),
"id": cert.ID,
"user_id": cert.UserID,
"username": user.Username,
"enterprise_name": enterpriseInfo.CompanyName,
"legal_person_name": enterpriseInfo.LegalPersonName,
"legal_person_phone": enterpriseInfo.LegalPersonPhone,
"unified_social_code": enterpriseInfo.UnifiedSocialCode,
"enterprise_address": enterpriseInfo.EnterpriseAddress,
"certified_at": cert.CompletedAt.Format(time.RFC3339),
}
enterprises = append(enterprises, enterprise)
}

View File

@@ -925,6 +925,7 @@ func NewContainer() *Container {
reportRepo domain_statistics_repo.StatisticsReportRepository,
dashboardRepo domain_statistics_repo.StatisticsDashboardRepository,
userRepo domain_user_repo.UserRepository,
enterpriseInfoRepo domain_user_repo.EnterpriseInfoRepository,
apiCallRepo domain_api_repo.ApiCallRepository,
walletTransactionRepo domain_finance_repo.WalletTransactionRepository,
rechargeRecordRepo domain_finance_repo.RechargeRecordRepository,
@@ -940,6 +941,7 @@ func NewContainer() *Container {
reportRepo,
dashboardRepo,
userRepo,
enterpriseInfoRepo,
apiCallRepo,
walletTransactionRepo,
rechargeRecordRepo,