4.1 KiB
4.1 KiB
利润统计API成本计入说明
修改内容
在后台统计面板的利润统计部分,将API调用成本计入成本计算中。
修改位置
文件:ycc-proxy-server/app/main/api/internal/logic/admin_dashboard/admingetdashboardstatisticslogic.go
方法:calculateProfitStatistics
修改详情
1. 今日利润计算
修改前:
// 今日利润 = 营收 - 佣金 - 返利 - 税务成本 + 提现收税
stats.TodayProfit = todayRevenue - todayCommission - todayRebate - todayCompanyTax + todayTaxIncome
修改后:
// 今日API调用成本
todayApiCost := 0.0
if l.svcCtx.TianyuanapiCallLogService != nil {
todayApiStats, err := l.svcCtx.TianyuanapiCallLogService.GetStatistics(l.ctx, service.StatisticsFilter{
StartDate: todayStart,
EndDate: todayEnd,
})
if err != nil {
logx.Errorf("获取今日API调用成本失败: %v", err)
} else {
todayApiCost = todayApiStats.TotalCost
}
}
// 今日利润 = 营收 - 佣金 - 返利 - 税务成本 - API调用成本 + 提现收税
stats.TodayProfit = todayRevenue - todayCommission - todayRebate - todayCompanyTax - todayApiCost + todayTaxIncome
2. 当月利润计算
修改前:
// 当月利润
stats.MonthProfit = monthRevenue - monthCommission - monthRebate - monthCompanyTax + monthTaxIncome
修改后:
// 当月API调用成本
monthApiCost := 0.0
if l.svcCtx.TianyuanapiCallLogService != nil {
monthApiStats, err := l.svcCtx.TianyuanapiCallLogService.GetStatistics(l.ctx, service.StatisticsFilter{
StartDate: monthStart,
EndDate: monthEnd,
})
if err != nil {
logx.Errorf("获取当月API调用成本失败: %v", err)
} else {
monthApiCost = monthApiStats.TotalCost
}
}
// 当月利润
stats.MonthProfit = monthRevenue - monthCommission - monthRebate - monthCompanyTax - monthApiCost + monthTaxIncome
3. 总利润计算
修改前:
// 总利润
stats.TotalProfit = totalRevenue - totalCommission - totalRebate - totalCompanyTax + totalTaxIncome
修改后:
// 总API调用成本
totalApiCost := 0.0
if l.svcCtx.TianyuanapiCallLogService != nil {
totalApiStats, err := l.svcCtx.TianyuanapiCallLogService.GetStatistics(l.ctx, service.StatisticsFilter{})
if err != nil {
logx.Errorf("获取总API调用成本失败: %v", err)
} else {
totalApiCost = totalApiStats.TotalCost
}
}
// 总利润
stats.TotalProfit = totalRevenue - totalCommission - totalRebate - totalCompanyTax - totalApiCost + totalTaxIncome
利润计算公式
修改后的利润计算公式为:
利润 = 营收 - 佣金 - 返利 - 税务成本 - API调用成本 + 提现收税
其中:
- 营收:订单金额总和(status = 'paid')
- 佣金:代理佣金总和(status != 3)
- 返利:代理返利总和(status != 3)
- 税务成本:订单金额的 6%
- API调用成本:天元API成功调用的成本总和(从
tianyuanapi_call_log表统计) - 提现收税:代理提现税总和(tax_status = 2)
API调用成本统计说明
API调用成本通过 TianyuanapiCallLogService.GetStatistics 方法获取:
- 今日API调用成本:统计
call_time >= todayStart AND call_time < todayEnd的成功调用成本 - 当月API调用成本:统计
call_time >= monthStart AND call_time < monthEnd的成功调用成本 - 总API调用成本:统计所有成功调用的成本
注意:只有成功调用(call_status = 1)才会计入成本,失败调用不计成本。
错误处理
如果获取API调用成本失败,会记录错误日志,但不会影响利润计算(API成本默认为0),确保统计功能的稳定性。
依赖项
TianyuanapiCallLogService:需要在servicecontext.go中初始化tianyuanapi_call_log表:需要已创建并包含调用记录数据
验证
修改完成后,可以通过以下方式验证:
- 查看后台统计面板的利润统计数据
- 检查日志,确认API调用成本是否正确获取
- 对比修改前后的利润数据,确认API成本已正确计入