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

	_promptConfig.promptConfigDo.UseDB(db, opts...)
	_promptConfig.promptConfigDo.UseModel(&model.PromptConfig{})

	tableName := _promptConfig.promptConfigDo.TableName()
	_promptConfig.ALL = field.NewAsterisk(tableName)
	_promptConfig.ID = field.NewInt64(tableName, "id")
	_promptConfig.Key = field.NewString(tableName, "key")
	_promptConfig.Value = field.NewString(tableName, "value")
	_promptConfig.CreatedAt = field.NewTime(tableName, "created_at")
	_promptConfig.UpdatedAt = field.NewTime(tableName, "updated_at")

	_promptConfig.fillFieldMap()

	return _promptConfig
}

type promptConfig struct {
	promptConfigDo promptConfigDo

	ALL       field.Asterisk
	ID        field.Int64  // 用户全局唯一主键
	Key       field.String // prompt 关键词
	Value     field.String // prompt 详细文本
	CreatedAt field.Time   // 创建时间
	UpdatedAt field.Time   // 更新时间

	fieldMap map[string]field.Expr
}

func (p promptConfig) Table(newTableName string) *promptConfig {
	p.promptConfigDo.UseTable(newTableName)
	return p.updateTableName(newTableName)
}

func (p promptConfig) As(alias string) *promptConfig {
	p.promptConfigDo.DO = *(p.promptConfigDo.As(alias).(*gen.DO))
	return p.updateTableName(alias)
}

func (p *promptConfig) updateTableName(table string) *promptConfig {
	p.ALL = field.NewAsterisk(table)
	p.ID = field.NewInt64(table, "id")
	p.Key = field.NewString(table, "key")
	p.Value = field.NewString(table, "value")
	p.CreatedAt = field.NewTime(table, "created_at")
	p.UpdatedAt = field.NewTime(table, "updated_at")

	p.fillFieldMap()

	return p
}

func (p *promptConfig) WithContext(ctx context.Context) *promptConfigDo {
	return p.promptConfigDo.WithContext(ctx)
}

func (p promptConfig) TableName() string { return p.promptConfigDo.TableName() }

func (p promptConfig) Alias() string { return p.promptConfigDo.Alias() }

func (p promptConfig) Columns(cols ...field.Expr) gen.Columns {
	return p.promptConfigDo.Columns(cols...)
}

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

func (p *promptConfig) fillFieldMap() {
	p.fieldMap = make(map[string]field.Expr, 5)
	p.fieldMap["id"] = p.ID
	p.fieldMap["key"] = p.Key
	p.fieldMap["value"] = p.Value
	p.fieldMap["created_at"] = p.CreatedAt
	p.fieldMap["updated_at"] = p.UpdatedAt
}

func (p promptConfig) clone(db *gorm.DB) promptConfig {
	p.promptConfigDo.ReplaceConnPool(db.Statement.ConnPool)
	return p
}

func (p promptConfig) replaceDB(db *gorm.DB) promptConfig {
	p.promptConfigDo.ReplaceDB(db)
	return p
}

type promptConfigDo struct{ gen.DO }

func (p promptConfigDo) Debug() *promptConfigDo {
	return p.withDO(p.DO.Debug())
}

func (p promptConfigDo) WithContext(ctx context.Context) *promptConfigDo {
	return p.withDO(p.DO.WithContext(ctx))
}

func (p promptConfigDo) ReadDB() *promptConfigDo {
	return p.Clauses(dbresolver.Read)
}

func (p promptConfigDo) WriteDB() *promptConfigDo {
	return p.Clauses(dbresolver.Write)
}

func (p promptConfigDo) Session(config *gorm.Session) *promptConfigDo {
	return p.withDO(p.DO.Session(config))
}

func (p promptConfigDo) Clauses(conds ...clause.Expression) *promptConfigDo {
	return p.withDO(p.DO.Clauses(conds...))
}

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

func (p promptConfigDo) Not(conds ...gen.Condition) *promptConfigDo {
	return p.withDO(p.DO.Not(conds...))
}

func (p promptConfigDo) Or(conds ...gen.Condition) *promptConfigDo {
	return p.withDO(p.DO.Or(conds...))
}

func (p promptConfigDo) Select(conds ...field.Expr) *promptConfigDo {
	return p.withDO(p.DO.Select(conds...))
}

func (p promptConfigDo) Where(conds ...gen.Condition) *promptConfigDo {
	return p.withDO(p.DO.Where(conds...))
}

func (p promptConfigDo) Order(conds ...field.Expr) *promptConfigDo {
	return p.withDO(p.DO.Order(conds...))
}

func (p promptConfigDo) Distinct(cols ...field.Expr) *promptConfigDo {
	return p.withDO(p.DO.Distinct(cols...))
}

func (p promptConfigDo) Omit(cols ...field.Expr) *promptConfigDo {
	return p.withDO(p.DO.Omit(cols...))
}

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

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

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

func (p promptConfigDo) Group(cols ...field.Expr) *promptConfigDo {
	return p.withDO(p.DO.Group(cols...))
}

func (p promptConfigDo) Having(conds ...gen.Condition) *promptConfigDo {
	return p.withDO(p.DO.Having(conds...))
}

func (p promptConfigDo) Limit(limit int) *promptConfigDo {
	return p.withDO(p.DO.Limit(limit))
}

func (p promptConfigDo) Offset(offset int) *promptConfigDo {
	return p.withDO(p.DO.Offset(offset))
}

func (p promptConfigDo) Scopes(funcs ...func(gen.Dao) gen.Dao) *promptConfigDo {
	return p.withDO(p.DO.Scopes(funcs...))
}

func (p promptConfigDo) Unscoped() *promptConfigDo {
	return p.withDO(p.DO.Unscoped())
}

func (p promptConfigDo) Create(values ...*model.PromptConfig) error {
	if len(values) == 0 {
		return nil
	}
	return p.DO.Create(values)
}

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

func (p promptConfigDo) First() (*model.PromptConfig, error) {
	if result, err := p.DO.First(); err != nil {
		return nil, err
	} else {
		return result.(*model.PromptConfig), nil
	}
}

func (p promptConfigDo) Take() (*model.PromptConfig, error) {
	if result, err := p.DO.Take(); err != nil {
		return nil, err
	} else {
		return result.(*model.PromptConfig), nil
	}
}

func (p promptConfigDo) Last() (*model.PromptConfig, error) {
	if result, err := p.DO.Last(); err != nil {
		return nil, err
	} else {
		return result.(*model.PromptConfig), nil
	}
}

func (p promptConfigDo) Find() ([]*model.PromptConfig, error) {
	result, err := p.DO.Find()
	return result.([]*model.PromptConfig), err
}

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

func (p promptConfigDo) Attrs(attrs ...field.AssignExpr) *promptConfigDo {
	return p.withDO(p.DO.Attrs(attrs...))
}

func (p promptConfigDo) Assign(attrs ...field.AssignExpr) *promptConfigDo {
	return p.withDO(p.DO.Assign(attrs...))
}

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

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

func (p promptConfigDo) FirstOrInit() (*model.PromptConfig, error) {
	if result, err := p.DO.FirstOrInit(); err != nil {
		return nil, err
	} else {
		return result.(*model.PromptConfig), nil
	}
}

func (p promptConfigDo) FirstOrCreate() (*model.PromptConfig, error) {
	if result, err := p.DO.FirstOrCreate(); err != nil {
		return nil, err
	} else {
		return result.(*model.PromptConfig), nil
	}
}

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

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

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

func (p promptConfigDo) Scan(result interface{}) (err error) {
	return p.DO.Scan(result)
}

func (p promptConfigDo) Delete(models ...*model.PromptConfig) (result gen.ResultInfo, err error) {
	return p.DO.Delete(models)
}

func (p *promptConfigDo) withDO(do gen.Dao) *promptConfigDo {
	p.DO = *do.(*gen.DO)
	return p
}
