You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
32 lines
891 B
32 lines
891 B
package billing
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/youruser/base/model"
|
|
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
// UsageService handles usage recording
|
|
type UsageService struct{}
|
|
|
|
func NewUsageService() *UsageService {
|
|
return &UsageService{}
|
|
}
|
|
|
|
// Record inserts a usage record
|
|
func (s *UsageService) Record(ctx context.Context, db *gorm.DB, record *model.AIUsageRecord) error {
|
|
_, err := model.AIUsageRecordInsert(ctx, db, record)
|
|
return err
|
|
}
|
|
|
|
// UpdateConversationStats updates conversation token count and cost
|
|
func (s *UsageService) UpdateConversationStats(ctx context.Context, db *gorm.DB, conversationId int64, tokens int64, cost float64) error {
|
|
return db.WithContext(ctx).Model(&model.AIConversation{}).
|
|
Where("id = ?", conversationId).
|
|
Updates(map[string]interface{}{
|
|
"total_tokens": gorm.Expr("total_tokens + ?", tokens),
|
|
"total_cost": gorm.Expr("total_cost + ?", cost),
|
|
}).Error
|
|
}
|
|
|