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

	_knowledge.knowledgeDo.UseDB(db, opts...)
	_knowledge.knowledgeDo.UseModel(&model.Knowledge{})

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

	_knowledge.fillFieldMap()

	return _knowledge
}

type knowledge struct {
	knowledgeDo knowledgeDo

	ALL       field.Asterisk
	ID        field.Int64  // 主键
	UserID    field.Int64  // 用户ID
	Name      field.String // 知识库名称
	Avatar    field.String // 知识库头像
	Desc      field.String // 知识库描述
	CreatedAt field.Time
	UpdatedAt field.Time

	fieldMap map[string]field.Expr
}

func (k knowledge) Table(newTableName string) *knowledge {
	k.knowledgeDo.UseTable(newTableName)
	return k.updateTableName(newTableName)
}

func (k knowledge) As(alias string) *knowledge {
	k.knowledgeDo.DO = *(k.knowledgeDo.As(alias).(*gen.DO))
	return k.updateTableName(alias)
}

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

	k.fillFieldMap()

	return k
}

func (k *knowledge) WithContext(ctx context.Context) *knowledgeDo {
	return k.knowledgeDo.WithContext(ctx)
}

func (k knowledge) TableName() string { return k.knowledgeDo.TableName() }

func (k knowledge) Alias() string { return k.knowledgeDo.Alias() }

func (k knowledge) Columns(cols ...field.Expr) gen.Columns { return k.knowledgeDo.Columns(cols...) }

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

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

func (k knowledge) clone(db *gorm.DB) knowledge {
	k.knowledgeDo.ReplaceConnPool(db.Statement.ConnPool)
	return k
}

func (k knowledge) replaceDB(db *gorm.DB) knowledge {
	k.knowledgeDo.ReplaceDB(db)
	return k
}

type knowledgeDo struct{ gen.DO }

func (k knowledgeDo) Debug() *knowledgeDo {
	return k.withDO(k.DO.Debug())
}

func (k knowledgeDo) WithContext(ctx context.Context) *knowledgeDo {
	return k.withDO(k.DO.WithContext(ctx))
}

func (k knowledgeDo) ReadDB() *knowledgeDo {
	return k.Clauses(dbresolver.Read)
}

func (k knowledgeDo) WriteDB() *knowledgeDo {
	return k.Clauses(dbresolver.Write)
}

func (k knowledgeDo) Session(config *gorm.Session) *knowledgeDo {
	return k.withDO(k.DO.Session(config))
}

func (k knowledgeDo) Clauses(conds ...clause.Expression) *knowledgeDo {
	return k.withDO(k.DO.Clauses(conds...))
}

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

func (k knowledgeDo) Not(conds ...gen.Condition) *knowledgeDo {
	return k.withDO(k.DO.Not(conds...))
}

func (k knowledgeDo) Or(conds ...gen.Condition) *knowledgeDo {
	return k.withDO(k.DO.Or(conds...))
}

func (k knowledgeDo) Select(conds ...field.Expr) *knowledgeDo {
	return k.withDO(k.DO.Select(conds...))
}

func (k knowledgeDo) Where(conds ...gen.Condition) *knowledgeDo {
	return k.withDO(k.DO.Where(conds...))
}

func (k knowledgeDo) Order(conds ...field.Expr) *knowledgeDo {
	return k.withDO(k.DO.Order(conds...))
}

func (k knowledgeDo) Distinct(cols ...field.Expr) *knowledgeDo {
	return k.withDO(k.DO.Distinct(cols...))
}

func (k knowledgeDo) Omit(cols ...field.Expr) *knowledgeDo {
	return k.withDO(k.DO.Omit(cols...))
}

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

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

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

func (k knowledgeDo) Group(cols ...field.Expr) *knowledgeDo {
	return k.withDO(k.DO.Group(cols...))
}

func (k knowledgeDo) Having(conds ...gen.Condition) *knowledgeDo {
	return k.withDO(k.DO.Having(conds...))
}

func (k knowledgeDo) Limit(limit int) *knowledgeDo {
	return k.withDO(k.DO.Limit(limit))
}

func (k knowledgeDo) Offset(offset int) *knowledgeDo {
	return k.withDO(k.DO.Offset(offset))
}

func (k knowledgeDo) Scopes(funcs ...func(gen.Dao) gen.Dao) *knowledgeDo {
	return k.withDO(k.DO.Scopes(funcs...))
}

func (k knowledgeDo) Unscoped() *knowledgeDo {
	return k.withDO(k.DO.Unscoped())
}

func (k knowledgeDo) Create(values ...*model.Knowledge) error {
	if len(values) == 0 {
		return nil
	}
	return k.DO.Create(values)
}

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

func (k knowledgeDo) First() (*model.Knowledge, error) {
	if result, err := k.DO.First(); err != nil {
		return nil, err
	} else {
		return result.(*model.Knowledge), nil
	}
}

func (k knowledgeDo) Take() (*model.Knowledge, error) {
	if result, err := k.DO.Take(); err != nil {
		return nil, err
	} else {
		return result.(*model.Knowledge), nil
	}
}

func (k knowledgeDo) Last() (*model.Knowledge, error) {
	if result, err := k.DO.Last(); err != nil {
		return nil, err
	} else {
		return result.(*model.Knowledge), nil
	}
}

func (k knowledgeDo) Find() ([]*model.Knowledge, error) {
	result, err := k.DO.Find()
	return result.([]*model.Knowledge), err
}

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

func (k knowledgeDo) Attrs(attrs ...field.AssignExpr) *knowledgeDo {
	return k.withDO(k.DO.Attrs(attrs...))
}

func (k knowledgeDo) Assign(attrs ...field.AssignExpr) *knowledgeDo {
	return k.withDO(k.DO.Assign(attrs...))
}

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

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

func (k knowledgeDo) FirstOrInit() (*model.Knowledge, error) {
	if result, err := k.DO.FirstOrInit(); err != nil {
		return nil, err
	} else {
		return result.(*model.Knowledge), nil
	}
}

func (k knowledgeDo) FirstOrCreate() (*model.Knowledge, error) {
	if result, err := k.DO.FirstOrCreate(); err != nil {
		return nil, err
	} else {
		return result.(*model.Knowledge), nil
	}
}

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

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

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

func (k knowledgeDo) Scan(result interface{}) (err error) {
	return k.DO.Scan(result)
}

func (k knowledgeDo) Delete(models ...*model.Knowledge) (result gen.ResultInfo, err error) {
	return k.DO.Delete(models)
}

func (k *knowledgeDo) withDO(do gen.Dao) *knowledgeDo {
	k.DO = *do.(*gen.DO)
	return k
}
