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

	_knowledgeUnit.knowledgeUnitDo.UseDB(db, opts...)
	_knowledgeUnit.knowledgeUnitDo.UseModel(&model.KnowledgeUnit{})

	tableName := _knowledgeUnit.knowledgeUnitDo.TableName()
	_knowledgeUnit.ALL = field.NewAsterisk(tableName)
	_knowledgeUnit.ID = field.NewInt64(tableName, "id")
	_knowledgeUnit.KnowledgeID = field.NewInt64(tableName, "knowledge_id")
	_knowledgeUnit.Name = field.NewString(tableName, "name")
	_knowledgeUnit.Type = field.NewString(tableName, "type")
	_knowledgeUnit.Source = field.NewString(tableName, "source")
	_knowledgeUnit.HitCounts = field.NewInt32(tableName, "hit_counts")
	_knowledgeUnit.Enable = field.NewBool(tableName, "enable")
	_knowledgeUnit.CreatedAt = field.NewTime(tableName, "created_at")
	_knowledgeUnit.UpdatedAt = field.NewTime(tableName, "updated_at")

	_knowledgeUnit.fillFieldMap()

	return _knowledgeUnit
}

type knowledgeUnit struct {
	knowledgeUnitDo knowledgeUnitDo

	ALL         field.Asterisk
	ID          field.Int64  // 主键
	KnowledgeID field.Int64  // 知识库ID
	Name        field.String // 知识单元名称
	Type        field.String // 知识单元类型(txt)
	Source      field.String // 知识单元来源
	HitCounts   field.Int32  // 命中次数
	Enable      field.Bool   // 是否启用
	CreatedAt   field.Time
	UpdatedAt   field.Time

	fieldMap map[string]field.Expr
}

func (k knowledgeUnit) Table(newTableName string) *knowledgeUnit {
	k.knowledgeUnitDo.UseTable(newTableName)
	return k.updateTableName(newTableName)
}

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

func (k *knowledgeUnit) updateTableName(table string) *knowledgeUnit {
	k.ALL = field.NewAsterisk(table)
	k.ID = field.NewInt64(table, "id")
	k.KnowledgeID = field.NewInt64(table, "knowledge_id")
	k.Name = field.NewString(table, "name")
	k.Type = field.NewString(table, "type")
	k.Source = field.NewString(table, "source")
	k.HitCounts = field.NewInt32(table, "hit_counts")
	k.Enable = field.NewBool(table, "enable")
	k.CreatedAt = field.NewTime(table, "created_at")
	k.UpdatedAt = field.NewTime(table, "updated_at")

	k.fillFieldMap()

	return k
}

func (k *knowledgeUnit) WithContext(ctx context.Context) *knowledgeUnitDo {
	return k.knowledgeUnitDo.WithContext(ctx)
}

func (k knowledgeUnit) TableName() string { return k.knowledgeUnitDo.TableName() }

func (k knowledgeUnit) Alias() string { return k.knowledgeUnitDo.Alias() }

func (k knowledgeUnit) Columns(cols ...field.Expr) gen.Columns {
	return k.knowledgeUnitDo.Columns(cols...)
}

func (k *knowledgeUnit) 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 *knowledgeUnit) fillFieldMap() {
	k.fieldMap = make(map[string]field.Expr, 9)
	k.fieldMap["id"] = k.ID
	k.fieldMap["knowledge_id"] = k.KnowledgeID
	k.fieldMap["name"] = k.Name
	k.fieldMap["type"] = k.Type
	k.fieldMap["source"] = k.Source
	k.fieldMap["hit_counts"] = k.HitCounts
	k.fieldMap["enable"] = k.Enable
	k.fieldMap["created_at"] = k.CreatedAt
	k.fieldMap["updated_at"] = k.UpdatedAt
}

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

func (k knowledgeUnit) replaceDB(db *gorm.DB) knowledgeUnit {
	k.knowledgeUnitDo.ReplaceDB(db)
	return k
}

type knowledgeUnitDo struct{ gen.DO }

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

func (k knowledgeUnitDo) FindByPage(offset int, limit int) (result []*model.KnowledgeUnit, 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 knowledgeUnitDo) 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 knowledgeUnitDo) Scan(result interface{}) (err error) {
	return k.DO.Scan(result)
}

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

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