GetUserUsageStatistics 获取特定用户的用量统计数据
(userId int, startDate, endDate string, tokenId int, modelName string, page, pageSize int)
| 402 | |
| 403 | // GetUserUsageStatistics 获取特定用户的用量统计数据 |
| 404 | func GetUserUsageStatistics(userId int, startDate, endDate string, tokenId int, modelName string, page, pageSize int) ([]*UsageStatistics, int64, error) { |
| 405 | var statistics []*UsageStatistics |
| 406 | var total int64 |
| 407 | |
| 408 | // 基本查询,需要通过token表连接过滤用户 |
| 409 | query := DB.Table("usage_statistics"). |
| 410 | Joins("JOIN tokens ON usage_statistics.token_id = tokens.id"). |
| 411 | Where("tokens.user_id = ?", userId) |
| 412 | |
| 413 | // 添加查询条件 |
| 414 | if startDate != "" { |
| 415 | query = query.Where("usage_statistics.date >= ?", startDate) |
| 416 | } |
| 417 | if endDate != "" { |
| 418 | query = query.Where("usage_statistics.date <= ?", endDate) |
| 419 | } |
| 420 | if tokenId > 0 { |
| 421 | query = query.Where("usage_statistics.token_id = ?", tokenId) |
| 422 | } |
| 423 | if modelName != "" { |
| 424 | query = query.Where("usage_statistics.model_name LIKE ?", "%"+modelName+"%") |
| 425 | } |
| 426 | |
| 427 | // 获取总数 |
| 428 | err := query.Count(&total).Error |
| 429 | if err != nil { |
| 430 | return nil, 0, err |
| 431 | } |
| 432 | |
| 433 | // 分页查询 |
| 434 | offset := (page - 1) * pageSize |
| 435 | err = query.Select("usage_statistics.*"). |
| 436 | Order("usage_statistics.date DESC, usage_statistics.token_id ASC, usage_statistics.model_name ASC"). |
| 437 | Offset(offset).Limit(pageSize).Find(&statistics).Error |
| 438 | |
| 439 | return statistics, total, err |
| 440 | } |
| 441 | |
| 442 | // GetUserMonthlyUsageStatistics 获取特定用户的月度用量统计数据 |
| 443 | func GetUserMonthlyUsageStatistics(userId int, startDate, endDate string, tokenId int, modelName string, page, pageSize int) ([]*UsageStatistics, int64, error) { |
no outgoing calls
no test coverage detected