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

	_agent.agentDo.UseDB(db, opts...)
	_agent.agentDo.UseModel(&model.Agent{})

	tableName := _agent.agentDo.TableName()
	_agent.ALL = field.NewAsterisk(tableName)
	_agent.ID = field.NewInt64(tableName, "id")
	_agent.CreatedAt = field.NewTime(tableName, "created_at")
	_agent.UpdatedAt = field.NewTime(tableName, "updated_at")
	_agent.Status = field.NewInt64(tableName, "status")
	_agent.DeletedAt = field.NewField(tableName, "deleted_at")
	_agent.Name = field.NewString(tableName, "name")
	_agent.Role = field.NewString(tableName, "role")
	_agent.Background = field.NewString(tableName, "background")
	_agent.Examples = field.NewString(tableName, "examples")
	_agent.OrganizationID = field.NewInt64(tableName, "organization_id")
	_agent.DatasetID = field.NewString(tableName, "dataset_id")
	_agent.CollectionID = field.NewString(tableName, "collection_id")

	_agent.fillFieldMap()

	return _agent
}

type agent struct {
	agentDo

	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 | 删除日期
	Name           field.String // agent 名称
	Role           field.String // 角色设定
	Background     field.String // 背景介绍
	Examples       field.String // 对话案例
	OrganizationID field.Int64  // 机构 ID
	DatasetID      field.String // 知识库ID
	CollectionID   field.String // 集合ID

	fieldMap map[string]field.Expr
}

func (a agent) Table(newTableName string) *agent {
	a.agentDo.UseTable(newTableName)
	return a.updateTableName(newTableName)
}

func (a agent) As(alias string) *agent {
	a.agentDo.DO = *(a.agentDo.As(alias).(*gen.DO))
	return a.updateTableName(alias)
}

func (a *agent) updateTableName(table string) *agent {
	a.ALL = field.NewAsterisk(table)
	a.ID = field.NewInt64(table, "id")
	a.CreatedAt = field.NewTime(table, "created_at")
	a.UpdatedAt = field.NewTime(table, "updated_at")
	a.Status = field.NewInt64(table, "status")
	a.DeletedAt = field.NewField(table, "deleted_at")
	a.Name = field.NewString(table, "name")
	a.Role = field.NewString(table, "role")
	a.Background = field.NewString(table, "background")
	a.Examples = field.NewString(table, "examples")
	a.OrganizationID = field.NewInt64(table, "organization_id")
	a.DatasetID = field.NewString(table, "dataset_id")
	a.CollectionID = field.NewString(table, "collection_id")

	a.fillFieldMap()

	return a
}

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

func (a *agent) fillFieldMap() {
	a.fieldMap = make(map[string]field.Expr, 12)
	a.fieldMap["id"] = a.ID
	a.fieldMap["created_at"] = a.CreatedAt
	a.fieldMap["updated_at"] = a.UpdatedAt
	a.fieldMap["status"] = a.Status
	a.fieldMap["deleted_at"] = a.DeletedAt
	a.fieldMap["name"] = a.Name
	a.fieldMap["role"] = a.Role
	a.fieldMap["background"] = a.Background
	a.fieldMap["examples"] = a.Examples
	a.fieldMap["organization_id"] = a.OrganizationID
	a.fieldMap["dataset_id"] = a.DatasetID
	a.fieldMap["collection_id"] = a.CollectionID
}

func (a agent) clone(db *gorm.DB) agent {
	a.agentDo.ReplaceConnPool(db.Statement.ConnPool)
	return a
}

func (a agent) replaceDB(db *gorm.DB) agent {
	a.agentDo.ReplaceDB(db)
	return a
}

type agentDo struct{ gen.DO }

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

func (a agentDo) Debug() IAgentDo {
	return a.withDO(a.DO.Debug())
}

func (a agentDo) WithContext(ctx context.Context) IAgentDo {
	return a.withDO(a.DO.WithContext(ctx))
}

func (a agentDo) ReadDB() IAgentDo {
	return a.Clauses(dbresolver.Read)
}

func (a agentDo) WriteDB() IAgentDo {
	return a.Clauses(dbresolver.Write)
}

func (a agentDo) Session(config *gorm.Session) IAgentDo {
	return a.withDO(a.DO.Session(config))
}

func (a agentDo) Clauses(conds ...clause.Expression) IAgentDo {
	return a.withDO(a.DO.Clauses(conds...))
}

func (a agentDo) Returning(value interface{}, columns ...string) IAgentDo {
	return a.withDO(a.DO.Returning(value, columns...))
}

func (a agentDo) Not(conds ...gen.Condition) IAgentDo {
	return a.withDO(a.DO.Not(conds...))
}

func (a agentDo) Or(conds ...gen.Condition) IAgentDo {
	return a.withDO(a.DO.Or(conds...))
}

func (a agentDo) Select(conds ...field.Expr) IAgentDo {
	return a.withDO(a.DO.Select(conds...))
}

func (a agentDo) Where(conds ...gen.Condition) IAgentDo {
	return a.withDO(a.DO.Where(conds...))
}

func (a agentDo) Order(conds ...field.Expr) IAgentDo {
	return a.withDO(a.DO.Order(conds...))
}

func (a agentDo) Distinct(cols ...field.Expr) IAgentDo {
	return a.withDO(a.DO.Distinct(cols...))
}

func (a agentDo) Omit(cols ...field.Expr) IAgentDo {
	return a.withDO(a.DO.Omit(cols...))
}

func (a agentDo) Join(table schema.Tabler, on ...field.Expr) IAgentDo {
	return a.withDO(a.DO.Join(table, on...))
}

func (a agentDo) LeftJoin(table schema.Tabler, on ...field.Expr) IAgentDo {
	return a.withDO(a.DO.LeftJoin(table, on...))
}

func (a agentDo) RightJoin(table schema.Tabler, on ...field.Expr) IAgentDo {
	return a.withDO(a.DO.RightJoin(table, on...))
}

func (a agentDo) Group(cols ...field.Expr) IAgentDo {
	return a.withDO(a.DO.Group(cols...))
}

func (a agentDo) Having(conds ...gen.Condition) IAgentDo {
	return a.withDO(a.DO.Having(conds...))
}

func (a agentDo) Limit(limit int) IAgentDo {
	return a.withDO(a.DO.Limit(limit))
}

func (a agentDo) Offset(offset int) IAgentDo {
	return a.withDO(a.DO.Offset(offset))
}

func (a agentDo) Scopes(funcs ...func(gen.Dao) gen.Dao) IAgentDo {
	return a.withDO(a.DO.Scopes(funcs...))
}

func (a agentDo) Unscoped() IAgentDo {
	return a.withDO(a.DO.Unscoped())
}

func (a agentDo) Create(values ...*model.Agent) error {
	if len(values) == 0 {
		return nil
	}
	return a.DO.Create(values)
}

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

func (a agentDo) First() (*model.Agent, error) {
	if result, err := a.DO.First(); err != nil {
		return nil, err
	} else {
		return result.(*model.Agent), nil
	}
}

func (a agentDo) Take() (*model.Agent, error) {
	if result, err := a.DO.Take(); err != nil {
		return nil, err
	} else {
		return result.(*model.Agent), nil
	}
}

func (a agentDo) Last() (*model.Agent, error) {
	if result, err := a.DO.Last(); err != nil {
		return nil, err
	} else {
		return result.(*model.Agent), nil
	}
}

func (a agentDo) Find() ([]*model.Agent, error) {
	result, err := a.DO.Find()
	return result.([]*model.Agent), err
}

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

func (a agentDo) Attrs(attrs ...field.AssignExpr) IAgentDo {
	return a.withDO(a.DO.Attrs(attrs...))
}

func (a agentDo) Assign(attrs ...field.AssignExpr) IAgentDo {
	return a.withDO(a.DO.Assign(attrs...))
}

func (a agentDo) Joins(fields ...field.RelationField) IAgentDo {
	for _, _f := range fields {
		a = *a.withDO(a.DO.Joins(_f))
	}
	return &a
}

func (a agentDo) Preload(fields ...field.RelationField) IAgentDo {
	for _, _f := range fields {
		a = *a.withDO(a.DO.Preload(_f))
	}
	return &a
}

func (a agentDo) FirstOrInit() (*model.Agent, error) {
	if result, err := a.DO.FirstOrInit(); err != nil {
		return nil, err
	} else {
		return result.(*model.Agent), nil
	}
}

func (a agentDo) FirstOrCreate() (*model.Agent, error) {
	if result, err := a.DO.FirstOrCreate(); err != nil {
		return nil, err
	} else {
		return result.(*model.Agent), nil
	}
}

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

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

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

func (a agentDo) Scan(result interface{}) (err error) {
	return a.DO.Scan(result)
}

func (a agentDo) Delete(models ...*model.Agent) (result gen.ResultInfo, err error) {
	return a.DO.Delete(models)
}

func (a *agentDo) withDO(do gen.Dao) *agentDo {
	a.DO = *do.(*gen.DO)
	return a
}
