feat(usage): 使用记录增加计费模式字段 — 记录/展示/筛选 token/按次/图片

- DB: usage_logs 表新增 billing_mode VARCHAR(20) 列
- 后端: RecordUsage 写入时根据 image_count 判定计费模式
- 前端: 使用记录表格新增计费模式 badge 列 + 筛选下拉
This commit is contained in:
erio
2026-03-30 22:13:16 +08:00
parent 726730bb0e
commit a51e0047b7
25 changed files with 1495 additions and 47 deletions

View File

@@ -19725,6 +19725,11 @@ type UsageLogMutation struct {
model *string
requested_model *string
upstream_model *string
channel_id *int64
addchannel_id *int64
model_mapping_chain *string
billing_tier *string
billing_mode *string
input_tokens *int
addinput_tokens *int
output_tokens *int
@@ -20160,6 +20165,223 @@ func (m *UsageLogMutation) ResetUpstreamModel() {
delete(m.clearedFields, usagelog.FieldUpstreamModel)
}
// SetChannelID sets the "channel_id" field.
func (m *UsageLogMutation) SetChannelID(i int64) {
m.channel_id = &i
m.addchannel_id = nil
}
// ChannelID returns the value of the "channel_id" field in the mutation.
func (m *UsageLogMutation) ChannelID() (r int64, exists bool) {
v := m.channel_id
if v == nil {
return
}
return *v, true
}
// OldChannelID returns the old "channel_id" field's value of the UsageLog entity.
// If the UsageLog object wasn't provided to the builder, the object is fetched from the database.
// An error is returned if the mutation operation is not UpdateOne, or the database query fails.
func (m *UsageLogMutation) OldChannelID(ctx context.Context) (v *int64, err error) {
if !m.op.Is(OpUpdateOne) {
return v, errors.New("OldChannelID is only allowed on UpdateOne operations")
}
if m.id == nil || m.oldValue == nil {
return v, errors.New("OldChannelID requires an ID field in the mutation")
}
oldValue, err := m.oldValue(ctx)
if err != nil {
return v, fmt.Errorf("querying old value for OldChannelID: %w", err)
}
return oldValue.ChannelID, nil
}
// AddChannelID adds i to the "channel_id" field.
func (m *UsageLogMutation) AddChannelID(i int64) {
if m.addchannel_id != nil {
*m.addchannel_id += i
} else {
m.addchannel_id = &i
}
}
// AddedChannelID returns the value that was added to the "channel_id" field in this mutation.
func (m *UsageLogMutation) AddedChannelID() (r int64, exists bool) {
v := m.addchannel_id
if v == nil {
return
}
return *v, true
}
// ClearChannelID clears the value of the "channel_id" field.
func (m *UsageLogMutation) ClearChannelID() {
m.channel_id = nil
m.addchannel_id = nil
m.clearedFields[usagelog.FieldChannelID] = struct{}{}
}
// ChannelIDCleared returns if the "channel_id" field was cleared in this mutation.
func (m *UsageLogMutation) ChannelIDCleared() bool {
_, ok := m.clearedFields[usagelog.FieldChannelID]
return ok
}
// ResetChannelID resets all changes to the "channel_id" field.
func (m *UsageLogMutation) ResetChannelID() {
m.channel_id = nil
m.addchannel_id = nil
delete(m.clearedFields, usagelog.FieldChannelID)
}
// SetModelMappingChain sets the "model_mapping_chain" field.
func (m *UsageLogMutation) SetModelMappingChain(s string) {
m.model_mapping_chain = &s
}
// ModelMappingChain returns the value of the "model_mapping_chain" field in the mutation.
func (m *UsageLogMutation) ModelMappingChain() (r string, exists bool) {
v := m.model_mapping_chain
if v == nil {
return
}
return *v, true
}
// OldModelMappingChain returns the old "model_mapping_chain" field's value of the UsageLog entity.
// If the UsageLog object wasn't provided to the builder, the object is fetched from the database.
// An error is returned if the mutation operation is not UpdateOne, or the database query fails.
func (m *UsageLogMutation) OldModelMappingChain(ctx context.Context) (v *string, err error) {
if !m.op.Is(OpUpdateOne) {
return v, errors.New("OldModelMappingChain is only allowed on UpdateOne operations")
}
if m.id == nil || m.oldValue == nil {
return v, errors.New("OldModelMappingChain requires an ID field in the mutation")
}
oldValue, err := m.oldValue(ctx)
if err != nil {
return v, fmt.Errorf("querying old value for OldModelMappingChain: %w", err)
}
return oldValue.ModelMappingChain, nil
}
// ClearModelMappingChain clears the value of the "model_mapping_chain" field.
func (m *UsageLogMutation) ClearModelMappingChain() {
m.model_mapping_chain = nil
m.clearedFields[usagelog.FieldModelMappingChain] = struct{}{}
}
// ModelMappingChainCleared returns if the "model_mapping_chain" field was cleared in this mutation.
func (m *UsageLogMutation) ModelMappingChainCleared() bool {
_, ok := m.clearedFields[usagelog.FieldModelMappingChain]
return ok
}
// ResetModelMappingChain resets all changes to the "model_mapping_chain" field.
func (m *UsageLogMutation) ResetModelMappingChain() {
m.model_mapping_chain = nil
delete(m.clearedFields, usagelog.FieldModelMappingChain)
}
// SetBillingTier sets the "billing_tier" field.
func (m *UsageLogMutation) SetBillingTier(s string) {
m.billing_tier = &s
}
// BillingTier returns the value of the "billing_tier" field in the mutation.
func (m *UsageLogMutation) BillingTier() (r string, exists bool) {
v := m.billing_tier
if v == nil {
return
}
return *v, true
}
// OldBillingTier returns the old "billing_tier" field's value of the UsageLog entity.
// If the UsageLog object wasn't provided to the builder, the object is fetched from the database.
// An error is returned if the mutation operation is not UpdateOne, or the database query fails.
func (m *UsageLogMutation) OldBillingTier(ctx context.Context) (v *string, err error) {
if !m.op.Is(OpUpdateOne) {
return v, errors.New("OldBillingTier is only allowed on UpdateOne operations")
}
if m.id == nil || m.oldValue == nil {
return v, errors.New("OldBillingTier requires an ID field in the mutation")
}
oldValue, err := m.oldValue(ctx)
if err != nil {
return v, fmt.Errorf("querying old value for OldBillingTier: %w", err)
}
return oldValue.BillingTier, nil
}
// ClearBillingTier clears the value of the "billing_tier" field.
func (m *UsageLogMutation) ClearBillingTier() {
m.billing_tier = nil
m.clearedFields[usagelog.FieldBillingTier] = struct{}{}
}
// BillingTierCleared returns if the "billing_tier" field was cleared in this mutation.
func (m *UsageLogMutation) BillingTierCleared() bool {
_, ok := m.clearedFields[usagelog.FieldBillingTier]
return ok
}
// ResetBillingTier resets all changes to the "billing_tier" field.
func (m *UsageLogMutation) ResetBillingTier() {
m.billing_tier = nil
delete(m.clearedFields, usagelog.FieldBillingTier)
}
// SetBillingMode sets the "billing_mode" field.
func (m *UsageLogMutation) SetBillingMode(s string) {
m.billing_mode = &s
}
// BillingMode returns the value of the "billing_mode" field in the mutation.
func (m *UsageLogMutation) BillingMode() (r string, exists bool) {
v := m.billing_mode
if v == nil {
return
}
return *v, true
}
// OldBillingMode returns the old "billing_mode" field's value of the UsageLog entity.
// If the UsageLog object wasn't provided to the builder, the object is fetched from the database.
// An error is returned if the mutation operation is not UpdateOne, or the database query fails.
func (m *UsageLogMutation) OldBillingMode(ctx context.Context) (v *string, err error) {
if !m.op.Is(OpUpdateOne) {
return v, errors.New("OldBillingMode is only allowed on UpdateOne operations")
}
if m.id == nil || m.oldValue == nil {
return v, errors.New("OldBillingMode requires an ID field in the mutation")
}
oldValue, err := m.oldValue(ctx)
if err != nil {
return v, fmt.Errorf("querying old value for OldBillingMode: %w", err)
}
return oldValue.BillingMode, nil
}
// ClearBillingMode clears the value of the "billing_mode" field.
func (m *UsageLogMutation) ClearBillingMode() {
m.billing_mode = nil
m.clearedFields[usagelog.FieldBillingMode] = struct{}{}
}
// BillingModeCleared returns if the "billing_mode" field was cleared in this mutation.
func (m *UsageLogMutation) BillingModeCleared() bool {
_, ok := m.clearedFields[usagelog.FieldBillingMode]
return ok
}
// ResetBillingMode resets all changes to the "billing_mode" field.
func (m *UsageLogMutation) ResetBillingMode() {
m.billing_mode = nil
delete(m.clearedFields, usagelog.FieldBillingMode)
}
// SetGroupID sets the "group_id" field.
func (m *UsageLogMutation) SetGroupID(i int64) {
m.group = &i
@@ -21781,7 +22003,7 @@ func (m *UsageLogMutation) Type() string {
// order to get all numeric fields that were incremented/decremented, call
// AddedFields().
func (m *UsageLogMutation) Fields() []string {
fields := make([]string, 0, 34)
fields := make([]string, 0, 38)
if m.user != nil {
fields = append(fields, usagelog.FieldUserID)
}
@@ -21803,6 +22025,18 @@ func (m *UsageLogMutation) Fields() []string {
if m.upstream_model != nil {
fields = append(fields, usagelog.FieldUpstreamModel)
}
if m.channel_id != nil {
fields = append(fields, usagelog.FieldChannelID)
}
if m.model_mapping_chain != nil {
fields = append(fields, usagelog.FieldModelMappingChain)
}
if m.billing_tier != nil {
fields = append(fields, usagelog.FieldBillingTier)
}
if m.billing_mode != nil {
fields = append(fields, usagelog.FieldBillingMode)
}
if m.group != nil {
fields = append(fields, usagelog.FieldGroupID)
}
@@ -21906,6 +22140,14 @@ func (m *UsageLogMutation) Field(name string) (ent.Value, bool) {
return m.RequestedModel()
case usagelog.FieldUpstreamModel:
return m.UpstreamModel()
case usagelog.FieldChannelID:
return m.ChannelID()
case usagelog.FieldModelMappingChain:
return m.ModelMappingChain()
case usagelog.FieldBillingTier:
return m.BillingTier()
case usagelog.FieldBillingMode:
return m.BillingMode()
case usagelog.FieldGroupID:
return m.GroupID()
case usagelog.FieldSubscriptionID:
@@ -21983,6 +22225,14 @@ func (m *UsageLogMutation) OldField(ctx context.Context, name string) (ent.Value
return m.OldRequestedModel(ctx)
case usagelog.FieldUpstreamModel:
return m.OldUpstreamModel(ctx)
case usagelog.FieldChannelID:
return m.OldChannelID(ctx)
case usagelog.FieldModelMappingChain:
return m.OldModelMappingChain(ctx)
case usagelog.FieldBillingTier:
return m.OldBillingTier(ctx)
case usagelog.FieldBillingMode:
return m.OldBillingMode(ctx)
case usagelog.FieldGroupID:
return m.OldGroupID(ctx)
case usagelog.FieldSubscriptionID:
@@ -22095,6 +22345,34 @@ func (m *UsageLogMutation) SetField(name string, value ent.Value) error {
}
m.SetUpstreamModel(v)
return nil
case usagelog.FieldChannelID:
v, ok := value.(int64)
if !ok {
return fmt.Errorf("unexpected type %T for field %s", value, name)
}
m.SetChannelID(v)
return nil
case usagelog.FieldModelMappingChain:
v, ok := value.(string)
if !ok {
return fmt.Errorf("unexpected type %T for field %s", value, name)
}
m.SetModelMappingChain(v)
return nil
case usagelog.FieldBillingTier:
v, ok := value.(string)
if !ok {
return fmt.Errorf("unexpected type %T for field %s", value, name)
}
m.SetBillingTier(v)
return nil
case usagelog.FieldBillingMode:
v, ok := value.(string)
if !ok {
return fmt.Errorf("unexpected type %T for field %s", value, name)
}
m.SetBillingMode(v)
return nil
case usagelog.FieldGroupID:
v, ok := value.(int64)
if !ok {
@@ -22292,6 +22570,9 @@ func (m *UsageLogMutation) SetField(name string, value ent.Value) error {
// this mutation.
func (m *UsageLogMutation) AddedFields() []string {
var fields []string
if m.addchannel_id != nil {
fields = append(fields, usagelog.FieldChannelID)
}
if m.addinput_tokens != nil {
fields = append(fields, usagelog.FieldInputTokens)
}
@@ -22354,6 +22635,8 @@ func (m *UsageLogMutation) AddedFields() []string {
// was not set, or was not defined in the schema.
func (m *UsageLogMutation) AddedField(name string) (ent.Value, bool) {
switch name {
case usagelog.FieldChannelID:
return m.AddedChannelID()
case usagelog.FieldInputTokens:
return m.AddedInputTokens()
case usagelog.FieldOutputTokens:
@@ -22399,6 +22682,13 @@ func (m *UsageLogMutation) AddedField(name string) (ent.Value, bool) {
// type.
func (m *UsageLogMutation) AddField(name string, value ent.Value) error {
switch name {
case usagelog.FieldChannelID:
v, ok := value.(int64)
if !ok {
return fmt.Errorf("unexpected type %T for field %s", value, name)
}
m.AddChannelID(v)
return nil
case usagelog.FieldInputTokens:
v, ok := value.(int)
if !ok {
@@ -22539,6 +22829,18 @@ func (m *UsageLogMutation) ClearedFields() []string {
if m.FieldCleared(usagelog.FieldUpstreamModel) {
fields = append(fields, usagelog.FieldUpstreamModel)
}
if m.FieldCleared(usagelog.FieldChannelID) {
fields = append(fields, usagelog.FieldChannelID)
}
if m.FieldCleared(usagelog.FieldModelMappingChain) {
fields = append(fields, usagelog.FieldModelMappingChain)
}
if m.FieldCleared(usagelog.FieldBillingTier) {
fields = append(fields, usagelog.FieldBillingTier)
}
if m.FieldCleared(usagelog.FieldBillingMode) {
fields = append(fields, usagelog.FieldBillingMode)
}
if m.FieldCleared(usagelog.FieldGroupID) {
fields = append(fields, usagelog.FieldGroupID)
}
@@ -22586,6 +22888,18 @@ func (m *UsageLogMutation) ClearField(name string) error {
case usagelog.FieldUpstreamModel:
m.ClearUpstreamModel()
return nil
case usagelog.FieldChannelID:
m.ClearChannelID()
return nil
case usagelog.FieldModelMappingChain:
m.ClearModelMappingChain()
return nil
case usagelog.FieldBillingTier:
m.ClearBillingTier()
return nil
case usagelog.FieldBillingMode:
m.ClearBillingMode()
return nil
case usagelog.FieldGroupID:
m.ClearGroupID()
return nil
@@ -22642,6 +22956,18 @@ func (m *UsageLogMutation) ResetField(name string) error {
case usagelog.FieldUpstreamModel:
m.ResetUpstreamModel()
return nil
case usagelog.FieldChannelID:
m.ResetChannelID()
return nil
case usagelog.FieldModelMappingChain:
m.ResetModelMappingChain()
return nil
case usagelog.FieldBillingTier:
m.ResetBillingTier()
return nil
case usagelog.FieldBillingMode:
m.ResetBillingMode()
return nil
case usagelog.FieldGroupID:
m.ResetGroupID()
return nil