// 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 newToken(db *gorm.DB, opts ...gen.DOOption) token {
	_token := token{}

	_token.tokenDo.UseDB(db, opts...)
	_token.tokenDo.UseModel(&model.Token{})

	tableName := _token.tokenDo.TableName()
	_token.ALL = field.NewAsterisk(tableName)
	_token.ID = field.NewInt64(tableName, "id")
	_token.CreatedAt = field.NewTime(tableName, "created_at")
	_token.UpdatedAt = field.NewTime(tableName, "updated_at")
	_token.DeletedAt = field.NewField(tableName, "deleted_at")
	_token.ExpireAt = field.NewTime(tableName, "expire_at")
	_token.Token = field.NewString(tableName, "token")
	_token.Mac = field.NewString(tableName, "mac")
	_token.Remark = field.NewString(tableName, "remark")
	_token.OrganizationID = field.NewInt64(tableName, "organization_id")
	_token.AgentID = field.NewInt64(tableName, "agent_id")
	_token.CustomAgentBase = field.NewString(tableName, "custom_agent_base")
	_token.CustomAgentKey = field.NewString(tableName, "custom_agent_key")
	_token.OpenaiBase = field.NewString(tableName, "openai_base")
	_token.OpenaiKey = field.NewString(tableName, "openai_key")

	_token.fillFieldMap()

	return _token
}

// token Token表
type token struct {
	tokenDo

	ALL             field.Asterisk
	ID              field.Int64
	CreatedAt       field.Time   // Create Time | 创建日期
	UpdatedAt       field.Time   // Update Time | 修改日期
	DeletedAt       field.Field  // Delete Time | 删除时间
	ExpireAt        field.Time   // 过期时间
	Token           field.String // Token
	Mac             field.String // Mac地址
	Remark          field.String // 备注
	OrganizationID  field.Int64  // 租户ID
	AgentID         field.Int64  // 0 定制
	CustomAgentBase field.String // 定制agent服务地址
	CustomAgentKey  field.String // 定制agent服务密钥
	OpenaiBase      field.String // 大模型服务地址
	OpenaiKey       field.String // 大模型服务密钥

	fieldMap map[string]field.Expr
}

func (t token) Table(newTableName string) *token {
	t.tokenDo.UseTable(newTableName)
	return t.updateTableName(newTableName)
}

func (t token) As(alias string) *token {
	t.tokenDo.DO = *(t.tokenDo.As(alias).(*gen.DO))
	return t.updateTableName(alias)
}

func (t *token) updateTableName(table string) *token {
	t.ALL = field.NewAsterisk(table)
	t.ID = field.NewInt64(table, "id")
	t.CreatedAt = field.NewTime(table, "created_at")
	t.UpdatedAt = field.NewTime(table, "updated_at")
	t.DeletedAt = field.NewField(table, "deleted_at")
	t.ExpireAt = field.NewTime(table, "expire_at")
	t.Token = field.NewString(table, "token")
	t.Mac = field.NewString(table, "mac")
	t.Remark = field.NewString(table, "remark")
	t.OrganizationID = field.NewInt64(table, "organization_id")
	t.AgentID = field.NewInt64(table, "agent_id")
	t.CustomAgentBase = field.NewString(table, "custom_agent_base")
	t.CustomAgentKey = field.NewString(table, "custom_agent_key")
	t.OpenaiBase = field.NewString(table, "openai_base")
	t.OpenaiKey = field.NewString(table, "openai_key")

	t.fillFieldMap()

	return t
}

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

func (t *token) fillFieldMap() {
	t.fieldMap = make(map[string]field.Expr, 14)
	t.fieldMap["id"] = t.ID
	t.fieldMap["created_at"] = t.CreatedAt
	t.fieldMap["updated_at"] = t.UpdatedAt
	t.fieldMap["deleted_at"] = t.DeletedAt
	t.fieldMap["expire_at"] = t.ExpireAt
	t.fieldMap["token"] = t.Token
	t.fieldMap["mac"] = t.Mac
	t.fieldMap["remark"] = t.Remark
	t.fieldMap["organization_id"] = t.OrganizationID
	t.fieldMap["agent_id"] = t.AgentID
	t.fieldMap["custom_agent_base"] = t.CustomAgentBase
	t.fieldMap["custom_agent_key"] = t.CustomAgentKey
	t.fieldMap["openai_base"] = t.OpenaiBase
	t.fieldMap["openai_key"] = t.OpenaiKey
}

func (t token) clone(db *gorm.DB) token {
	t.tokenDo.ReplaceConnPool(db.Statement.ConnPool)
	return t
}

func (t token) replaceDB(db *gorm.DB) token {
	t.tokenDo.ReplaceDB(db)
	return t
}

type tokenDo struct{ gen.DO }

type ITokenDo interface {
	gen.SubQuery
	Debug() ITokenDo
	WithContext(ctx context.Context) ITokenDo
	WithResult(fc func(tx gen.Dao)) gen.ResultInfo
	ReplaceDB(db *gorm.DB)
	ReadDB() ITokenDo
	WriteDB() ITokenDo
	As(alias string) gen.Dao
	Session(config *gorm.Session) ITokenDo
	Columns(cols ...field.Expr) gen.Columns
	Clauses(conds ...clause.Expression) ITokenDo
	Not(conds ...gen.Condition) ITokenDo
	Or(conds ...gen.Condition) ITokenDo
	Select(conds ...field.Expr) ITokenDo
	Where(conds ...gen.Condition) ITokenDo
	Order(conds ...field.Expr) ITokenDo
	Distinct(cols ...field.Expr) ITokenDo
	Omit(cols ...field.Expr) ITokenDo
	Join(table schema.Tabler, on ...field.Expr) ITokenDo
	LeftJoin(table schema.Tabler, on ...field.Expr) ITokenDo
	RightJoin(table schema.Tabler, on ...field.Expr) ITokenDo
	Group(cols ...field.Expr) ITokenDo
	Having(conds ...gen.Condition) ITokenDo
	Limit(limit int) ITokenDo
	Offset(offset int) ITokenDo
	Count() (count int64, err error)
	Scopes(funcs ...func(gen.Dao) gen.Dao) ITokenDo
	Unscoped() ITokenDo
	Create(values ...*model.Token) error
	CreateInBatches(values []*model.Token, batchSize int) error
	Save(values ...*model.Token) error
	First() (*model.Token, error)
	Take() (*model.Token, error)
	Last() (*model.Token, error)
	Find() ([]*model.Token, error)
	FindInBatch(batchSize int, fc func(tx gen.Dao, batch int) error) (results []*model.Token, err error)
	FindInBatches(result *[]*model.Token, batchSize int, fc func(tx gen.Dao, batch int) error) error
	Pluck(column field.Expr, dest interface{}) error
	Delete(...*model.Token) (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) ITokenDo
	Assign(attrs ...field.AssignExpr) ITokenDo
	Joins(fields ...field.RelationField) ITokenDo
	Preload(fields ...field.RelationField) ITokenDo
	FirstOrInit() (*model.Token, error)
	FirstOrCreate() (*model.Token, error)
	FindByPage(offset int, limit int) (result []*model.Token, 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) ITokenDo
	UnderlyingDB() *gorm.DB
	schema.Tabler
}

func (t tokenDo) Debug() ITokenDo {
	return t.withDO(t.DO.Debug())
}

func (t tokenDo) WithContext(ctx context.Context) ITokenDo {
	return t.withDO(t.DO.WithContext(ctx))
}

func (t tokenDo) ReadDB() ITokenDo {
	return t.Clauses(dbresolver.Read)
}

func (t tokenDo) WriteDB() ITokenDo {
	return t.Clauses(dbresolver.Write)
}

func (t tokenDo) Session(config *gorm.Session) ITokenDo {
	return t.withDO(t.DO.Session(config))
}

func (t tokenDo) Clauses(conds ...clause.Expression) ITokenDo {
	return t.withDO(t.DO.Clauses(conds...))
}

func (t tokenDo) Returning(value interface{}, columns ...string) ITokenDo {
	return t.withDO(t.DO.Returning(value, columns...))
}

func (t tokenDo) Not(conds ...gen.Condition) ITokenDo {
	return t.withDO(t.DO.Not(conds...))
}

func (t tokenDo) Or(conds ...gen.Condition) ITokenDo {
	return t.withDO(t.DO.Or(conds...))
}

func (t tokenDo) Select(conds ...field.Expr) ITokenDo {
	return t.withDO(t.DO.Select(conds...))
}

func (t tokenDo) Where(conds ...gen.Condition) ITokenDo {
	return t.withDO(t.DO.Where(conds...))
}

func (t tokenDo) Order(conds ...field.Expr) ITokenDo {
	return t.withDO(t.DO.Order(conds...))
}

func (t tokenDo) Distinct(cols ...field.Expr) ITokenDo {
	return t.withDO(t.DO.Distinct(cols...))
}

func (t tokenDo) Omit(cols ...field.Expr) ITokenDo {
	return t.withDO(t.DO.Omit(cols...))
}

func (t tokenDo) Join(table schema.Tabler, on ...field.Expr) ITokenDo {
	return t.withDO(t.DO.Join(table, on...))
}

func (t tokenDo) LeftJoin(table schema.Tabler, on ...field.Expr) ITokenDo {
	return t.withDO(t.DO.LeftJoin(table, on...))
}

func (t tokenDo) RightJoin(table schema.Tabler, on ...field.Expr) ITokenDo {
	return t.withDO(t.DO.RightJoin(table, on...))
}

func (t tokenDo) Group(cols ...field.Expr) ITokenDo {
	return t.withDO(t.DO.Group(cols...))
}

func (t tokenDo) Having(conds ...gen.Condition) ITokenDo {
	return t.withDO(t.DO.Having(conds...))
}

func (t tokenDo) Limit(limit int) ITokenDo {
	return t.withDO(t.DO.Limit(limit))
}

func (t tokenDo) Offset(offset int) ITokenDo {
	return t.withDO(t.DO.Offset(offset))
}

func (t tokenDo) Scopes(funcs ...func(gen.Dao) gen.Dao) ITokenDo {
	return t.withDO(t.DO.Scopes(funcs...))
}

func (t tokenDo) Unscoped() ITokenDo {
	return t.withDO(t.DO.Unscoped())
}

func (t tokenDo) Create(values ...*model.Token) error {
	if len(values) == 0 {
		return nil
	}
	return t.DO.Create(values)
}

func (t tokenDo) CreateInBatches(values []*model.Token, batchSize int) error {
	return t.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 (t tokenDo) Save(values ...*model.Token) error {
	if len(values) == 0 {
		return nil
	}
	return t.DO.Save(values)
}

func (t tokenDo) First() (*model.Token, error) {
	if result, err := t.DO.First(); err != nil {
		return nil, err
	} else {
		return result.(*model.Token), nil
	}
}

func (t tokenDo) Take() (*model.Token, error) {
	if result, err := t.DO.Take(); err != nil {
		return nil, err
	} else {
		return result.(*model.Token), nil
	}
}

func (t tokenDo) Last() (*model.Token, error) {
	if result, err := t.DO.Last(); err != nil {
		return nil, err
	} else {
		return result.(*model.Token), nil
	}
}

func (t tokenDo) Find() ([]*model.Token, error) {
	result, err := t.DO.Find()
	return result.([]*model.Token), err
}

func (t tokenDo) FindInBatch(batchSize int, fc func(tx gen.Dao, batch int) error) (results []*model.Token, err error) {
	buf := make([]*model.Token, 0, batchSize)
	err = t.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 (t tokenDo) FindInBatches(result *[]*model.Token, batchSize int, fc func(tx gen.Dao, batch int) error) error {
	return t.DO.FindInBatches(result, batchSize, fc)
}

func (t tokenDo) Attrs(attrs ...field.AssignExpr) ITokenDo {
	return t.withDO(t.DO.Attrs(attrs...))
}

func (t tokenDo) Assign(attrs ...field.AssignExpr) ITokenDo {
	return t.withDO(t.DO.Assign(attrs...))
}

func (t tokenDo) Joins(fields ...field.RelationField) ITokenDo {
	for _, _f := range fields {
		t = *t.withDO(t.DO.Joins(_f))
	}
	return &t
}

func (t tokenDo) Preload(fields ...field.RelationField) ITokenDo {
	for _, _f := range fields {
		t = *t.withDO(t.DO.Preload(_f))
	}
	return &t
}

func (t tokenDo) FirstOrInit() (*model.Token, error) {
	if result, err := t.DO.FirstOrInit(); err != nil {
		return nil, err
	} else {
		return result.(*model.Token), nil
	}
}

func (t tokenDo) FirstOrCreate() (*model.Token, error) {
	if result, err := t.DO.FirstOrCreate(); err != nil {
		return nil, err
	} else {
		return result.(*model.Token), nil
	}
}

func (t tokenDo) FindByPage(offset int, limit int) (result []*model.Token, count int64, err error) {
	result, err = t.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 = t.Offset(-1).Limit(-1).Count()
	return
}

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

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

func (t tokenDo) Scan(result interface{}) (err error) {
	return t.DO.Scan(result)
}

func (t tokenDo) Delete(models ...*model.Token) (result gen.ResultInfo, err error) {
	return t.DO.Delete(models)
}

func (t *tokenDo) withDO(do gen.Dao) *tokenDo {
	t.DO = *do.(*gen.DO)
	return t
}
