This commit is contained in:
2026-01-13 18:30:10 +08:00
parent 0206710e29
commit 04df8460a4
29 changed files with 2472 additions and 111 deletions

View File

@@ -0,0 +1,135 @@
# 利润统计API成本计入说明
## 修改内容
在后台统计面板的利润统计部分将API调用成本计入成本计算中。
## 修改位置
文件:`ycc-proxy-server/app/main/api/internal/logic/admin_dashboard/admingetdashboardstatisticslogic.go`
方法:`calculateProfitStatistics`
## 修改详情
### 1. 今日利润计算
**修改前:**
```go
// 今日利润 = 营收 - 佣金 - 返利 - 税务成本 + 提现收税
stats.TodayProfit = todayRevenue - todayCommission - todayRebate - todayCompanyTax + todayTaxIncome
```
**修改后:**
```go
// 今日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. 当月利润计算
**修改前:**
```go
// 当月利润
stats.MonthProfit = monthRevenue - monthCommission - monthRebate - monthCompanyTax + monthTaxIncome
```
**修改后:**
```go
// 当月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. 总利润计算
**修改前:**
```go
// 总利润
stats.TotalProfit = totalRevenue - totalCommission - totalRebate - totalCompanyTax + totalTaxIncome
```
**修改后:**
```go
// 总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` 方法获取:
1. **今日API调用成本**:统计 `call_time >= todayStart AND call_time < todayEnd` 的成功调用成本
2. **当月API调用成本**:统计 `call_time >= monthStart AND call_time < monthEnd` 的成功调用成本
3. **总API调用成本**:统计所有成功调用的成本
**注意**:只有成功调用(`call_status = 1`)才会计入成本,失败调用不计成本。
## 错误处理
如果获取API调用成本失败会记录错误日志但不会影响利润计算API成本默认为0确保统计功能的稳定性。
## 依赖项
- `TianyuanapiCallLogService`:需要在 `servicecontext.go` 中初始化
- `tianyuanapi_call_log` 表:需要已创建并包含调用记录数据
## 验证
修改完成后,可以通过以下方式验证:
1. 查看后台统计面板的利润统计数据
2. 检查日志确认API调用成本是否正确获取
3. 对比修改前后的利润数据确认API成本已正确计入