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

	_knowledgeUnitSegment.knowledgeUnitSegmentDo.UseDB(db, opts...)
	_knowledgeUnitSegment.knowledgeUnitSegmentDo.UseModel(&model.KnowledgeUnitSegment{})

	tableName := _knowledgeUnitSegment.knowledgeUnitSegmentDo.TableName()
	_knowledgeUnitSegment.ALL = field.NewAsterisk(tableName)
	_knowledgeUnitSegment.ID = field.NewInt64(tableName, "id")
	_knowledgeUnitSegment.KnowledgeID = field.NewInt64(tableName, "knowledge_id")
	_knowledgeUnitSegment.KnowledgeUnitID = field.NewInt64(tableName, "knowledge_unit_id")
	_knowledgeUnitSegment.Value = field.NewString(tableName, "value")
	_knowledgeUnitSegment.Embedding = field.NewString(tableName, "embedding")
	_knowledgeUnitSegment.CreatedAt = field.NewTime(tableName, "created_at")
	_knowledgeUnitSegment.UpdatedAt = field.NewTime(tableName, "updated_at")

	_knowledgeUnitSegment.fillFieldMap()

	return _knowledgeUnitSegment
}

type knowledgeUnitSegment struct {
	knowledgeUnitSegmentDo knowledgeUnitSegmentDo

	ALL             field.Asterisk
	ID              field.Int64  // 主键
	KnowledgeID     field.Int64  // 知识库ID
	KnowledgeUnitID field.Int64  // 知识单元ID
	Value           field.String // 知识单元分段内容
	Embedding       field.String // 知识单元分段内容向量
	CreatedAt       field.Time
	UpdatedAt       field.Time

	fieldMap map[string]field.Expr
}

func (k knowledgeUnitSegment) Table(newTableName string) *knowledgeUnitSegment {
	k.knowledgeUnitSegmentDo.UseTable(newTableName)
	return k.updateTableName(newTableName)
}

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

func (k *knowledgeUnitSegment) updateTableName(table string) *knowledgeUnitSegment {
	k.ALL = field.NewAsterisk(table)
	k.ID = field.NewInt64(table, "id")
	k.KnowledgeID = field.NewInt64(table, "knowledge_id")
	k.KnowledgeUnitID = field.NewInt64(table, "knowledge_unit_id")
	k.Value = field.NewString(table, "value")
	k.Embedding = field.NewString(table, "embedding")
	k.CreatedAt = field.NewTime(table, "created_at")
	k.UpdatedAt = field.NewTime(table, "updated_at")

	k.fillFieldMap()

	return k
}

func (k *knowledgeUnitSegment) WithContext(ctx context.Context) *knowledgeUnitSegmentDo {
	return k.knowledgeUnitSegmentDo.WithContext(ctx)
}

func (k knowledgeUnitSegment) TableName() string { return k.knowledgeUnitSegmentDo.TableName() }

func (k knowledgeUnitSegment) Alias() string { return k.knowledgeUnitSegmentDo.Alias() }

func (k knowledgeUnitSegment) Columns(cols ...field.Expr) gen.Columns {
	return k.knowledgeUnitSegmentDo.Columns(cols...)
}

func (k *knowledgeUnitSegment) 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 *knowledgeUnitSegment) fillFieldMap() {
	k.fieldMap = make(map[string]field.Expr, 7)
	k.fieldMap["id"] = k.ID
	k.fieldMap["knowledge_id"] = k.KnowledgeID
	k.fieldMap["knowledge_unit_id"] = k.KnowledgeUnitID
	k.fieldMap["value"] = k.Value
	k.fieldMap["embedding"] = k.Embedding
	k.fieldMap["created_at"] = k.CreatedAt
	k.fieldMap["updated_at"] = k.UpdatedAt
}

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

func (k knowledgeUnitSegment) replaceDB(db *gorm.DB) knowledgeUnitSegment {
	k.knowledgeUnitSegmentDo.ReplaceDB(db)
	return k
}

type knowledgeUnitSegmentDo struct{ gen.DO }

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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