// 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 dao

import (
	"context"

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

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

	"gorm.io/plugin/dbresolver"

	"chat/service/chat/model"
)

func newBot(db *gorm.DB, opts ...gen.DOOption) bot {
	_bot := bot{}

	_bot.botDo.UseDB(db, opts...)
	_bot.botDo.UseModel(&model.Bot{})

	tableName := _bot.botDo.TableName()
	_bot.ALL = field.NewAsterisk(tableName)
	_bot.ID = field.NewInt64(tableName, "id")
	_bot.Name = field.NewString(tableName, "name")
	_bot.Avatar = field.NewString(tableName, "avatar")
	_bot.Desc = field.NewString(tableName, "desc")
	_bot.UserID = field.NewInt64(tableName, "user_id")
	_bot.CreatedAt = field.NewTime(tableName, "created_at")
	_bot.UpdatedAt = field.NewTime(tableName, "updated_at")

	_bot.fillFieldMap()

	return _bot
}

type bot struct {
	botDo botDo

	ALL       field.Asterisk
	ID        field.Int64  // 机器人ID
	Name      field.String // 机器人名称
	Avatar    field.String // 机器人头像
	Desc      field.String // 机器人描述
	UserID    field.Int64  // 创建人用户ID 关联 user.id
	CreatedAt field.Time   // 创建时间
	UpdatedAt field.Time   // 更新时间

	fieldMap map[string]field.Expr
}

func (b bot) Table(newTableName string) *bot {
	b.botDo.UseTable(newTableName)
	return b.updateTableName(newTableName)
}

func (b bot) As(alias string) *bot {
	b.botDo.DO = *(b.botDo.As(alias).(*gen.DO))
	return b.updateTableName(alias)
}

func (b *bot) updateTableName(table string) *bot {
	b.ALL = field.NewAsterisk(table)
	b.ID = field.NewInt64(table, "id")
	b.Name = field.NewString(table, "name")
	b.Avatar = field.NewString(table, "avatar")
	b.Desc = field.NewString(table, "desc")
	b.UserID = field.NewInt64(table, "user_id")
	b.CreatedAt = field.NewTime(table, "created_at")
	b.UpdatedAt = field.NewTime(table, "updated_at")

	b.fillFieldMap()

	return b
}

func (b *bot) WithContext(ctx context.Context) *botDo { return b.botDo.WithContext(ctx) }

func (b bot) TableName() string { return b.botDo.TableName() }

func (b bot) Alias() string { return b.botDo.Alias() }

func (b bot) Columns(cols ...field.Expr) gen.Columns { return b.botDo.Columns(cols...) }

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

func (b *bot) fillFieldMap() {
	b.fieldMap = make(map[string]field.Expr, 7)
	b.fieldMap["id"] = b.ID
	b.fieldMap["name"] = b.Name
	b.fieldMap["avatar"] = b.Avatar
	b.fieldMap["desc"] = b.Desc
	b.fieldMap["user_id"] = b.UserID
	b.fieldMap["created_at"] = b.CreatedAt
	b.fieldMap["updated_at"] = b.UpdatedAt
}

func (b bot) clone(db *gorm.DB) bot {
	b.botDo.ReplaceConnPool(db.Statement.ConnPool)
	return b
}

func (b bot) replaceDB(db *gorm.DB) bot {
	b.botDo.ReplaceDB(db)
	return b
}

type botDo struct{ gen.DO }

func (b botDo) Debug() *botDo {
	return b.withDO(b.DO.Debug())
}

func (b botDo) WithContext(ctx context.Context) *botDo {
	return b.withDO(b.DO.WithContext(ctx))
}

func (b botDo) ReadDB() *botDo {
	return b.Clauses(dbresolver.Read)
}

func (b botDo) WriteDB() *botDo {
	return b.Clauses(dbresolver.Write)
}

func (b botDo) Session(config *gorm.Session) *botDo {
	return b.withDO(b.DO.Session(config))
}

func (b botDo) Clauses(conds ...clause.Expression) *botDo {
	return b.withDO(b.DO.Clauses(conds...))
}

func (b botDo) Returning(value interface{}, columns ...string) *botDo {
	return b.withDO(b.DO.Returning(value, columns...))
}

func (b botDo) Not(conds ...gen.Condition) *botDo {
	return b.withDO(b.DO.Not(conds...))
}

func (b botDo) Or(conds ...gen.Condition) *botDo {
	return b.withDO(b.DO.Or(conds...))
}

func (b botDo) Select(conds ...field.Expr) *botDo {
	return b.withDO(b.DO.Select(conds...))
}

func (b botDo) Where(conds ...gen.Condition) *botDo {
	return b.withDO(b.DO.Where(conds...))
}

func (b botDo) Order(conds ...field.Expr) *botDo {
	return b.withDO(b.DO.Order(conds...))
}

func (b botDo) Distinct(cols ...field.Expr) *botDo {
	return b.withDO(b.DO.Distinct(cols...))
}

func (b botDo) Omit(cols ...field.Expr) *botDo {
	return b.withDO(b.DO.Omit(cols...))
}

func (b botDo) Join(table schema.Tabler, on ...field.Expr) *botDo {
	return b.withDO(b.DO.Join(table, on...))
}

func (b botDo) LeftJoin(table schema.Tabler, on ...field.Expr) *botDo {
	return b.withDO(b.DO.LeftJoin(table, on...))
}

func (b botDo) RightJoin(table schema.Tabler, on ...field.Expr) *botDo {
	return b.withDO(b.DO.RightJoin(table, on...))
}

func (b botDo) Group(cols ...field.Expr) *botDo {
	return b.withDO(b.DO.Group(cols...))
}

func (b botDo) Having(conds ...gen.Condition) *botDo {
	return b.withDO(b.DO.Having(conds...))
}

func (b botDo) Limit(limit int) *botDo {
	return b.withDO(b.DO.Limit(limit))
}

func (b botDo) Offset(offset int) *botDo {
	return b.withDO(b.DO.Offset(offset))
}

func (b botDo) Scopes(funcs ...func(gen.Dao) gen.Dao) *botDo {
	return b.withDO(b.DO.Scopes(funcs...))
}

func (b botDo) Unscoped() *botDo {
	return b.withDO(b.DO.Unscoped())
}

func (b botDo) Create(values ...*model.Bot) error {
	if len(values) == 0 {
		return nil
	}
	return b.DO.Create(values)
}

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

func (b botDo) First() (*model.Bot, error) {
	if result, err := b.DO.First(); err != nil {
		return nil, err
	} else {
		return result.(*model.Bot), nil
	}
}

func (b botDo) Take() (*model.Bot, error) {
	if result, err := b.DO.Take(); err != nil {
		return nil, err
	} else {
		return result.(*model.Bot), nil
	}
}

func (b botDo) Last() (*model.Bot, error) {
	if result, err := b.DO.Last(); err != nil {
		return nil, err
	} else {
		return result.(*model.Bot), nil
	}
}

func (b botDo) Find() ([]*model.Bot, error) {
	result, err := b.DO.Find()
	return result.([]*model.Bot), err
}

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

func (b botDo) Attrs(attrs ...field.AssignExpr) *botDo {
	return b.withDO(b.DO.Attrs(attrs...))
}

func (b botDo) Assign(attrs ...field.AssignExpr) *botDo {
	return b.withDO(b.DO.Assign(attrs...))
}

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

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

func (b botDo) FirstOrInit() (*model.Bot, error) {
	if result, err := b.DO.FirstOrInit(); err != nil {
		return nil, err
	} else {
		return result.(*model.Bot), nil
	}
}

func (b botDo) FirstOrCreate() (*model.Bot, error) {
	if result, err := b.DO.FirstOrCreate(); err != nil {
		return nil, err
	} else {
		return result.(*model.Bot), nil
	}
}

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

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

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

func (b botDo) Scan(result interface{}) (err error) {
	return b.DO.Scan(result)
}

func (b botDo) Delete(models ...*model.Bot) (result gen.ResultInfo, err error) {
	return b.DO.Delete(models)
}

func (b *botDo) withDO(do gen.Dao) *botDo {
	b.DO = *do.(*gen.DO)
	return b
}
