// Code generated by gorm.io/gen. DO NOT EDIT.
// Code generated by gorm.io/gen. DO NOT EDIT.
// Code generated by gorm.io/gen. DO NOT EDIT.

package query

import (
	"context"

	"gorm.io/gorm"
	"gorm.io/gorm/clause"
	"gorm.io/gorm/schema"

	"gorm.io/gen"
	"gorm.io/gen/field"

	"gorm.io/plugin/dbresolver"

	"wechat-api/database/dao/wechat/model"
)

func newUsageDetail(db *gorm.DB, opts ...gen.DOOption) usageDetail {
	_usageDetail := usageDetail{}

	_usageDetail.usageDetailDo.UseDB(db, opts...)
	_usageDetail.usageDetailDo.UseModel(&model.UsageDetail{})

	tableName := _usageDetail.usageDetailDo.TableName()
	_usageDetail.ALL = field.NewAsterisk(tableName)
	_usageDetail.ID = field.NewInt64(tableName, "id")
	_usageDetail.CreatedAt = field.NewTime(tableName, "created_at")
	_usageDetail.UpdatedAt = field.NewTime(tableName, "updated_at")
	_usageDetail.Status = field.NewInt64(tableName, "status")
	_usageDetail.DeletedAt = field.NewField(tableName, "deleted_at")
	_usageDetail.Type = field.NewInt64(tableName, "type")
	_usageDetail.BotID = field.NewString(tableName, "bot_id")
	_usageDetail.ReceiverID = field.NewString(tableName, "receiver_id")
	_usageDetail.App = field.NewInt64(tableName, "app")
	_usageDetail.SessionID = field.NewInt64(tableName, "session_id")
	_usageDetail.Request = field.NewString(tableName, "request")
	_usageDetail.Response = field.NewString(tableName, "response")
	_usageDetail.OriginalData = field.NewString(tableName, "original_data")
	_usageDetail.TotalTokens = field.NewInt64(tableName, "total_tokens")
	_usageDetail.PromptTokens = field.NewInt64(tableName, "prompt_tokens")
	_usageDetail.CompletionTokens = field.NewInt64(tableName, "completion_tokens")
	_usageDetail.OrganizationID = field.NewInt64(tableName, "organization_id")

	_usageDetail.fillFieldMap()

	return _usageDetail
}

type usageDetail struct {
	usageDetailDo

	ALL              field.Asterisk
	ID               field.Int64
	CreatedAt        field.Time   // Create Time | 创建日期
	UpdatedAt        field.Time   // Update Time | 修改日期
	Status           field.Int64  // 状态 1 正常 2 禁用
	DeletedAt        field.Field  // Delete Time | 删除日期
	Type             field.Int64  // 1 微信 2 名片
	BotID            field.String // 微信或名片id
	ReceiverID       field.String // 微信id或open_id
	App              field.Int64  // 1 cow-basic 2 cow-agent-优化 3 cow-agent 4 cow-sop-阶段 5cow-sop-节点 6 mp-card 7 mp-employee
	SessionID        field.Int64  // 名片会话id
	Request          field.String // 请求内容
	Response         field.String // 响应内容
	OriginalData     field.String // 原始数据
	TotalTokens      field.Int64  // 使用token总数
	PromptTokens     field.Int64  // 请求token数
	CompletionTokens field.Int64  // 响应token数
	OrganizationID   field.Int64  // 机构 ID

	fieldMap map[string]field.Expr
}

func (u usageDetail) Table(newTableName string) *usageDetail {
	u.usageDetailDo.UseTable(newTableName)
	return u.updateTableName(newTableName)
}

func (u usageDetail) As(alias string) *usageDetail {
	u.usageDetailDo.DO = *(u.usageDetailDo.As(alias).(*gen.DO))
	return u.updateTableName(alias)
}

func (u *usageDetail) updateTableName(table string) *usageDetail {
	u.ALL = field.NewAsterisk(table)
	u.ID = field.NewInt64(table, "id")
	u.CreatedAt = field.NewTime(table, "created_at")
	u.UpdatedAt = field.NewTime(table, "updated_at")
	u.Status = field.NewInt64(table, "status")
	u.DeletedAt = field.NewField(table, "deleted_at")
	u.Type = field.NewInt64(table, "type")
	u.BotID = field.NewString(table, "bot_id")
	u.ReceiverID = field.NewString(table, "receiver_id")
	u.App = field.NewInt64(table, "app")
	u.SessionID = field.NewInt64(table, "session_id")
	u.Request = field.NewString(table, "request")
	u.Response = field.NewString(table, "response")
	u.OriginalData = field.NewString(table, "original_data")
	u.TotalTokens = field.NewInt64(table, "total_tokens")
	u.PromptTokens = field.NewInt64(table, "prompt_tokens")
	u.CompletionTokens = field.NewInt64(table, "completion_tokens")
	u.OrganizationID = field.NewInt64(table, "organization_id")

	u.fillFieldMap()

	return u
}

func (u *usageDetail) GetFieldByName(fieldName string) (field.OrderExpr, bool) {
	_f, ok := u.fieldMap[fieldName]
	if !ok || _f == nil {
		return nil, false
	}
	_oe, ok := _f.(field.OrderExpr)
	return _oe, ok
}

func (u *usageDetail) fillFieldMap() {
	u.fieldMap = make(map[string]field.Expr, 17)
	u.fieldMap["id"] = u.ID
	u.fieldMap["created_at"] = u.CreatedAt
	u.fieldMap["updated_at"] = u.UpdatedAt
	u.fieldMap["status"] = u.Status
	u.fieldMap["deleted_at"] = u.DeletedAt
	u.fieldMap["type"] = u.Type
	u.fieldMap["bot_id"] = u.BotID
	u.fieldMap["receiver_id"] = u.ReceiverID
	u.fieldMap["app"] = u.App
	u.fieldMap["session_id"] = u.SessionID
	u.fieldMap["request"] = u.Request
	u.fieldMap["response"] = u.Response
	u.fieldMap["original_data"] = u.OriginalData
	u.fieldMap["total_tokens"] = u.TotalTokens
	u.fieldMap["prompt_tokens"] = u.PromptTokens
	u.fieldMap["completion_tokens"] = u.CompletionTokens
	u.fieldMap["organization_id"] = u.OrganizationID
}

func (u usageDetail) clone(db *gorm.DB) usageDetail {
	u.usageDetailDo.ReplaceConnPool(db.Statement.ConnPool)
	return u
}

func (u usageDetail) replaceDB(db *gorm.DB) usageDetail {
	u.usageDetailDo.ReplaceDB(db)
	return u
}

type usageDetailDo struct{ gen.DO }

type IUsageDetailDo interface {
	gen.SubQuery
	Debug() IUsageDetailDo
	WithContext(ctx context.Context) IUsageDetailDo
	WithResult(fc func(tx gen.Dao)) gen.ResultInfo
	ReplaceDB(db *gorm.DB)
	ReadDB() IUsageDetailDo
	WriteDB() IUsageDetailDo
	As(alias string) gen.Dao
	Session(config *gorm.Session) IUsageDetailDo
	Columns(cols ...field.Expr) gen.Columns
	Clauses(conds ...clause.Expression) IUsageDetailDo
	Not(conds ...gen.Condition) IUsageDetailDo
	Or(conds ...gen.Condition) IUsageDetailDo
	Select(conds ...field.Expr) IUsageDetailDo
	Where(conds ...gen.Condition) IUsageDetailDo
	Order(conds ...field.Expr) IUsageDetailDo
	Distinct(cols ...field.Expr) IUsageDetailDo
	Omit(cols ...field.Expr) IUsageDetailDo
	Join(table schema.Tabler, on ...field.Expr) IUsageDetailDo
	LeftJoin(table schema.Tabler, on ...field.Expr) IUsageDetailDo
	RightJoin(table schema.Tabler, on ...field.Expr) IUsageDetailDo
	Group(cols ...field.Expr) IUsageDetailDo
	Having(conds ...gen.Condition) IUsageDetailDo
	Limit(limit int) IUsageDetailDo
	Offset(offset int) IUsageDetailDo
	Count() (count int64, err error)
	Scopes(funcs ...func(gen.Dao) gen.Dao) IUsageDetailDo
	Unscoped() IUsageDetailDo
	Create(values ...*model.UsageDetail) error
	CreateInBatches(values []*model.UsageDetail, batchSize int) error
	Save(values ...*model.UsageDetail) error
	First() (*model.UsageDetail, error)
	Take() (*model.UsageDetail, error)
	Last() (*model.UsageDetail, error)
	Find() ([]*model.UsageDetail, error)
	FindInBatch(batchSize int, fc func(tx gen.Dao, batch int) error) (results []*model.UsageDetail, err error)
	FindInBatches(result *[]*model.UsageDetail, batchSize int, fc func(tx gen.Dao, batch int) error) error
	Pluck(column field.Expr, dest interface{}) error
	Delete(...*model.UsageDetail) (info gen.ResultInfo, err error)
	Update(column field.Expr, value interface{}) (info gen.ResultInfo, err error)
	UpdateSimple(columns ...field.AssignExpr) (info gen.ResultInfo, err error)
	Updates(value interface{}) (info gen.ResultInfo, err error)
	UpdateColumn(column field.Expr, value interface{}) (info gen.ResultInfo, err error)
	UpdateColumnSimple(columns ...field.AssignExpr) (info gen.ResultInfo, err error)
	UpdateColumns(value interface{}) (info gen.ResultInfo, err error)
	UpdateFrom(q gen.SubQuery) gen.Dao
	Attrs(attrs ...field.AssignExpr) IUsageDetailDo
	Assign(attrs ...field.AssignExpr) IUsageDetailDo
	Joins(fields ...field.RelationField) IUsageDetailDo
	Preload(fields ...field.RelationField) IUsageDetailDo
	FirstOrInit() (*model.UsageDetail, error)
	FirstOrCreate() (*model.UsageDetail, error)
	FindByPage(offset int, limit int) (result []*model.UsageDetail, count int64, err error)
	ScanByPage(result interface{}, offset int, limit int) (count int64, err error)
	Scan(result interface{}) (err error)
	Returning(value interface{}, columns ...string) IUsageDetailDo
	UnderlyingDB() *gorm.DB
	schema.Tabler
}

func (u usageDetailDo) Debug() IUsageDetailDo {
	return u.withDO(u.DO.Debug())
}

func (u usageDetailDo) WithContext(ctx context.Context) IUsageDetailDo {
	return u.withDO(u.DO.WithContext(ctx))
}

func (u usageDetailDo) ReadDB() IUsageDetailDo {
	return u.Clauses(dbresolver.Read)
}

func (u usageDetailDo) WriteDB() IUsageDetailDo {
	return u.Clauses(dbresolver.Write)
}

func (u usageDetailDo) Session(config *gorm.Session) IUsageDetailDo {
	return u.withDO(u.DO.Session(config))
}

func (u usageDetailDo) Clauses(conds ...clause.Expression) IUsageDetailDo {
	return u.withDO(u.DO.Clauses(conds...))
}

func (u usageDetailDo) Returning(value interface{}, columns ...string) IUsageDetailDo {
	return u.withDO(u.DO.Returning(value, columns...))
}

func (u usageDetailDo) Not(conds ...gen.Condition) IUsageDetailDo {
	return u.withDO(u.DO.Not(conds...))
}

func (u usageDetailDo) Or(conds ...gen.Condition) IUsageDetailDo {
	return u.withDO(u.DO.Or(conds...))
}

func (u usageDetailDo) Select(conds ...field.Expr) IUsageDetailDo {
	return u.withDO(u.DO.Select(conds...))
}

func (u usageDetailDo) Where(conds ...gen.Condition) IUsageDetailDo {
	return u.withDO(u.DO.Where(conds...))
}

func (u usageDetailDo) Order(conds ...field.Expr) IUsageDetailDo {
	return u.withDO(u.DO.Order(conds...))
}

func (u usageDetailDo) Distinct(cols ...field.Expr) IUsageDetailDo {
	return u.withDO(u.DO.Distinct(cols...))
}

func (u usageDetailDo) Omit(cols ...field.Expr) IUsageDetailDo {
	return u.withDO(u.DO.Omit(cols...))
}

func (u usageDetailDo) Join(table schema.Tabler, on ...field.Expr) IUsageDetailDo {
	return u.withDO(u.DO.Join(table, on...))
}

func (u usageDetailDo) LeftJoin(table schema.Tabler, on ...field.Expr) IUsageDetailDo {
	return u.withDO(u.DO.LeftJoin(table, on...))
}

func (u usageDetailDo) RightJoin(table schema.Tabler, on ...field.Expr) IUsageDetailDo {
	return u.withDO(u.DO.RightJoin(table, on...))
}

func (u usageDetailDo) Group(cols ...field.Expr) IUsageDetailDo {
	return u.withDO(u.DO.Group(cols...))
}

func (u usageDetailDo) Having(conds ...gen.Condition) IUsageDetailDo {
	return u.withDO(u.DO.Having(conds...))
}

func (u usageDetailDo) Limit(limit int) IUsageDetailDo {
	return u.withDO(u.DO.Limit(limit))
}

func (u usageDetailDo) Offset(offset int) IUsageDetailDo {
	return u.withDO(u.DO.Offset(offset))
}

func (u usageDetailDo) Scopes(funcs ...func(gen.Dao) gen.Dao) IUsageDetailDo {
	return u.withDO(u.DO.Scopes(funcs...))
}

func (u usageDetailDo) Unscoped() IUsageDetailDo {
	return u.withDO(u.DO.Unscoped())
}

func (u usageDetailDo) Create(values ...*model.UsageDetail) error {
	if len(values) == 0 {
		return nil
	}
	return u.DO.Create(values)
}

func (u usageDetailDo) CreateInBatches(values []*model.UsageDetail, batchSize int) error {
	return u.DO.CreateInBatches(values, batchSize)
}

// Save : !!! underlying implementation is different with GORM
// The method is equivalent to executing the statement: db.Clauses(clause.OnConflict{UpdateAll: true}).Create(values)
func (u usageDetailDo) Save(values ...*model.UsageDetail) error {
	if len(values) == 0 {
		return nil
	}
	return u.DO.Save(values)
}

func (u usageDetailDo) First() (*model.UsageDetail, error) {
	if result, err := u.DO.First(); err != nil {
		return nil, err
	} else {
		return result.(*model.UsageDetail), nil
	}
}

func (u usageDetailDo) Take() (*model.UsageDetail, error) {
	if result, err := u.DO.Take(); err != nil {
		return nil, err
	} else {
		return result.(*model.UsageDetail), nil
	}
}

func (u usageDetailDo) Last() (*model.UsageDetail, error) {
	if result, err := u.DO.Last(); err != nil {
		return nil, err
	} else {
		return result.(*model.UsageDetail), nil
	}
}

func (u usageDetailDo) Find() ([]*model.UsageDetail, error) {
	result, err := u.DO.Find()
	return result.([]*model.UsageDetail), err
}

func (u usageDetailDo) FindInBatch(batchSize int, fc func(tx gen.Dao, batch int) error) (results []*model.UsageDetail, err error) {
	buf := make([]*model.UsageDetail, 0, batchSize)
	err = u.DO.FindInBatches(&buf, batchSize, func(tx gen.Dao, batch int) error {
		defer func() { results = append(results, buf...) }()
		return fc(tx, batch)
	})
	return results, err
}

func (u usageDetailDo) FindInBatches(result *[]*model.UsageDetail, batchSize int, fc func(tx gen.Dao, batch int) error) error {
	return u.DO.FindInBatches(result, batchSize, fc)
}

func (u usageDetailDo) Attrs(attrs ...field.AssignExpr) IUsageDetailDo {
	return u.withDO(u.DO.Attrs(attrs...))
}

func (u usageDetailDo) Assign(attrs ...field.AssignExpr) IUsageDetailDo {
	return u.withDO(u.DO.Assign(attrs...))
}

func (u usageDetailDo) Joins(fields ...field.RelationField) IUsageDetailDo {
	for _, _f := range fields {
		u = *u.withDO(u.DO.Joins(_f))
	}
	return &u
}

func (u usageDetailDo) Preload(fields ...field.RelationField) IUsageDetailDo {
	for _, _f := range fields {
		u = *u.withDO(u.DO.Preload(_f))
	}
	return &u
}

func (u usageDetailDo) FirstOrInit() (*model.UsageDetail, error) {
	if result, err := u.DO.FirstOrInit(); err != nil {
		return nil, err
	} else {
		return result.(*model.UsageDetail), nil
	}
}

func (u usageDetailDo) FirstOrCreate() (*model.UsageDetail, error) {
	if result, err := u.DO.FirstOrCreate(); err != nil {
		return nil, err
	} else {
		return result.(*model.UsageDetail), nil
	}
}

func (u usageDetailDo) FindByPage(offset int, limit int) (result []*model.UsageDetail, count int64, err error) {
	result, err = u.Offset(offset).Limit(limit).Find()
	if err != nil {
		return
	}

	if size := len(result); 0 < limit && 0 < size && size < limit {
		count = int64(size + offset)
		return
	}

	count, err = u.Offset(-1).Limit(-1).Count()
	return
}

func (u usageDetailDo) ScanByPage(result interface{}, offset int, limit int) (count int64, err error) {
	count, err = u.Count()
	if err != nil {
		return
	}

	err = u.Offset(offset).Limit(limit).Scan(result)
	return
}

func (u usageDetailDo) Scan(result interface{}) (err error) {
	return u.DO.Scan(result)
}

func (u usageDetailDo) Delete(models ...*model.UsageDetail) (result gen.ResultInfo, err error) {
	return u.DO.Delete(models)
}

func (u *usageDetailDo) withDO(do gen.Dao) *usageDetailDo {
	u.DO = *do.(*gen.DO)
	return u
}
