f
This commit is contained in:
@@ -257,6 +257,7 @@ func (l *AdminGetDashboardStatisticsLogic) calculateProfitStatistics(todayStart,
|
||||
// 今日API调用成本
|
||||
todayApiCost := 0.0
|
||||
if l.svcCtx.TianyuanapiCallLogService != nil {
|
||||
logx.Infof("开始获取今日API调用成本,时间范围: %v 到 %v", todayStart, todayEnd)
|
||||
todayApiStats, err := l.svcCtx.TianyuanapiCallLogService.GetStatistics(l.ctx, service.StatisticsFilter{
|
||||
StartDate: todayStart,
|
||||
EndDate: todayEnd,
|
||||
@@ -265,7 +266,10 @@ func (l *AdminGetDashboardStatisticsLogic) calculateProfitStatistics(todayStart,
|
||||
logx.Errorf("获取今日API调用成本失败: %v", err)
|
||||
} else {
|
||||
todayApiCost = todayApiStats.TotalCost
|
||||
logx.Infof("今日API调用成本统计完成,总成本: %f", todayApiCost)
|
||||
}
|
||||
} else {
|
||||
logx.Errorf("TianyuanapiCallLogService 未初始化,无法获取API调用成本")
|
||||
}
|
||||
// 今日利润 = 营收 - 佣金 - 返利 - 税务成本 - API调用成本 + 提现收税
|
||||
stats.TodayProfit = todayRevenue - todayCommission - todayRebate - todayCompanyTax - todayApiCost + todayTaxIncome
|
||||
@@ -313,6 +317,7 @@ func (l *AdminGetDashboardStatisticsLogic) calculateProfitStatistics(todayStart,
|
||||
// 当月API调用成本
|
||||
monthApiCost := 0.0
|
||||
if l.svcCtx.TianyuanapiCallLogService != nil {
|
||||
logx.Infof("开始获取当月API调用成本,时间范围: %v 到 %v", monthStart, monthEnd)
|
||||
monthApiStats, err := l.svcCtx.TianyuanapiCallLogService.GetStatistics(l.ctx, service.StatisticsFilter{
|
||||
StartDate: monthStart,
|
||||
EndDate: monthEnd,
|
||||
@@ -321,7 +326,10 @@ func (l *AdminGetDashboardStatisticsLogic) calculateProfitStatistics(todayStart,
|
||||
logx.Errorf("获取当月API调用成本失败: %v", err)
|
||||
} else {
|
||||
monthApiCost = monthApiStats.TotalCost
|
||||
logx.Infof("当月API调用成本统计完成,总成本: %f", monthApiCost)
|
||||
}
|
||||
} else {
|
||||
logx.Errorf("TianyuanapiCallLogService 未初始化,无法获取API调用成本")
|
||||
}
|
||||
// 当月利润
|
||||
stats.MonthProfit = monthRevenue - monthCommission - monthRebate - monthCompanyTax - monthApiCost + monthTaxIncome
|
||||
@@ -369,12 +377,16 @@ func (l *AdminGetDashboardStatisticsLogic) calculateProfitStatistics(todayStart,
|
||||
// 总API调用成本
|
||||
totalApiCost := 0.0
|
||||
if l.svcCtx.TianyuanapiCallLogService != nil {
|
||||
logx.Infof("开始获取总API调用成本(无时间限制)")
|
||||
totalApiStats, err := l.svcCtx.TianyuanapiCallLogService.GetStatistics(l.ctx, service.StatisticsFilter{})
|
||||
if err != nil {
|
||||
logx.Errorf("获取总API调用成本失败: %v", err)
|
||||
} else {
|
||||
totalApiCost = totalApiStats.TotalCost
|
||||
logx.Infof("总API调用成本统计完成,总成本: %f", totalApiCost)
|
||||
}
|
||||
} else {
|
||||
logx.Errorf("TianyuanapiCallLogService 未初始化,无法获取API调用成本")
|
||||
}
|
||||
// 总利润
|
||||
stats.TotalProfit = totalRevenue - totalCommission - totalRebate - totalCompanyTax - totalApiCost + totalTaxIncome
|
||||
|
||||
@@ -49,11 +49,16 @@ func (s *TianyuanapiCallLogService) RecordCall(ctx context.Context, opts CallLog
|
||||
// 1. 获取feature的成本价
|
||||
costPrice := 0.00
|
||||
if opts.CallStatus == 1 { // 只有成功才计算成本
|
||||
feature, err := s.featureModel.FindOne(ctx, opts.FeatureID)
|
||||
if err == nil {
|
||||
costPrice = feature.CostPrice
|
||||
if opts.FeatureID == "" {
|
||||
logx.Infof("记录API调用时feature_id为空,api_id=%s,无法获取成本价", opts.ApiID)
|
||||
} else {
|
||||
logx.Errorf("查询feature成本价失败,feature_id=%s, err=%v", opts.FeatureID, err)
|
||||
feature, err := s.featureModel.FindOne(ctx, opts.FeatureID)
|
||||
if err == nil {
|
||||
costPrice = feature.CostPrice
|
||||
logx.Infof("记录API调用 - feature_id=%s, api_id=%s, cost_price=%f", opts.FeatureID, opts.ApiID, costPrice)
|
||||
} else {
|
||||
logx.Errorf("查询feature成本价失败,feature_id=%s, api_id=%s, err=%v", opts.FeatureID, opts.ApiID, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -165,9 +170,11 @@ func (s *TianyuanapiCallLogService) GetStatistics(ctx context.Context, filter St
|
||||
}
|
||||
if !filter.StartDate.IsZero() {
|
||||
builder = builder.Where(squirrel.GtOrEq{"call_time": filter.StartDate})
|
||||
logx.Infof("API成本统计 - 开始时间: %v", filter.StartDate)
|
||||
}
|
||||
if !filter.EndDate.IsZero() {
|
||||
builder = builder.Where(squirrel.Lt{"call_time": filter.EndDate})
|
||||
logx.Infof("API成本统计 - 结束时间: %v", filter.EndDate)
|
||||
}
|
||||
|
||||
// 统计总调用次数
|
||||
@@ -175,6 +182,7 @@ func (s *TianyuanapiCallLogService) GetStatistics(ctx context.Context, filter St
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("统计总调用次数失败: %w", err)
|
||||
}
|
||||
logx.Infof("API成本统计 - 总调用次数: %d", totalCalls)
|
||||
|
||||
// 统计成功次数
|
||||
successBuilder := builder.Where(squirrel.Eq{"call_status": 1})
|
||||
@@ -182,15 +190,22 @@ func (s *TianyuanapiCallLogService) GetStatistics(ctx context.Context, filter St
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("统计成功次数失败: %w", err)
|
||||
}
|
||||
logx.Infof("API成本统计 - 成功调用次数: %d", successCalls)
|
||||
|
||||
// 统计失败次数
|
||||
failedCalls := totalCalls - successCalls
|
||||
|
||||
// 统计总成本(仅成功调用)
|
||||
// 先打印SQL以便调试(复制builder避免影响后续查询)
|
||||
debugBuilder := successBuilder
|
||||
query, values, _ := debugBuilder.Columns("IFNULL(SUM(cost_price),0)").Where("del_state = ?", 0).ToSql()
|
||||
logx.Infof("API成本统计 - SQL: %s, 参数: %v", query, values)
|
||||
|
||||
totalCost, err := s.tianyuanapiCallLogModel.FindSum(ctx, successBuilder, "cost_price")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("统计总成本失败: %w", err)
|
||||
}
|
||||
logx.Infof("API成本统计 - 总成本: %f", totalCost)
|
||||
|
||||
return &Statistics{
|
||||
TotalCalls: totalCalls,
|
||||
|
||||
@@ -249,6 +249,10 @@ func NewServiceContext(c config.Config) *ServiceContext {
|
||||
FeatureModel: featureModel,
|
||||
ProductFeatureModel: productFeatureModel,
|
||||
|
||||
// 天元API调用记录模型
|
||||
TianyuanapiCallLogModel: tianyuanapiCallLogModel,
|
||||
TianyuanapiCallLogService: tianyuanapiCallLogService,
|
||||
|
||||
// 白名单相关模型
|
||||
UserFeatureWhitelistModel: userFeatureWhitelistModel,
|
||||
WhitelistOrderModel: whitelistOrderModel,
|
||||
|
||||
Reference in New Issue
Block a user