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

	_chatConfig.chatConfigDo.UseDB(db, opts...)
	_chatConfig.chatConfigDo.UseModel(&model.ChatConfig{})

	tableName := _chatConfig.chatConfigDo.TableName()
	_chatConfig.ALL = field.NewAsterisk(tableName)
	_chatConfig.ID = field.NewInt64(tableName, "id")
	_chatConfig.User = field.NewString(tableName, "user")
	_chatConfig.AgentID = field.NewInt64(tableName, "agent_id")
	_chatConfig.Model = field.NewString(tableName, "model")
	_chatConfig.Prompt = field.NewString(tableName, "prompt")
	_chatConfig.CreatedAt = field.NewTime(tableName, "created_at")
	_chatConfig.UpdatedAt = field.NewTime(tableName, "updated_at")

	_chatConfig.fillFieldMap()

	return _chatConfig
}

type chatConfig struct {
	chatConfigDo chatConfigDo

	ALL       field.Asterisk
	ID        field.Int64  // 聊天配置ID
	User      field.String // 用户标识
	AgentID   field.Int64  // 应用ID
	Model     field.String // 模型
	Prompt    field.String // 系统初始设置
	CreatedAt field.Time   // 创建时间
	UpdatedAt field.Time   // 更新时间

	fieldMap map[string]field.Expr
}

func (c chatConfig) Table(newTableName string) *chatConfig {
	c.chatConfigDo.UseTable(newTableName)
	return c.updateTableName(newTableName)
}

func (c chatConfig) As(alias string) *chatConfig {
	c.chatConfigDo.DO = *(c.chatConfigDo.As(alias).(*gen.DO))
	return c.updateTableName(alias)
}

func (c *chatConfig) updateTableName(table string) *chatConfig {
	c.ALL = field.NewAsterisk(table)
	c.ID = field.NewInt64(table, "id")
	c.User = field.NewString(table, "user")
	c.AgentID = field.NewInt64(table, "agent_id")
	c.Model = field.NewString(table, "model")
	c.Prompt = field.NewString(table, "prompt")
	c.CreatedAt = field.NewTime(table, "created_at")
	c.UpdatedAt = field.NewTime(table, "updated_at")

	c.fillFieldMap()

	return c
}

func (c *chatConfig) WithContext(ctx context.Context) *chatConfigDo {
	return c.chatConfigDo.WithContext(ctx)
}

func (c chatConfig) TableName() string { return c.chatConfigDo.TableName() }

func (c chatConfig) Alias() string { return c.chatConfigDo.Alias() }

func (c chatConfig) Columns(cols ...field.Expr) gen.Columns { return c.chatConfigDo.Columns(cols...) }

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

func (c *chatConfig) fillFieldMap() {
	c.fieldMap = make(map[string]field.Expr, 7)
	c.fieldMap["id"] = c.ID
	c.fieldMap["user"] = c.User
	c.fieldMap["agent_id"] = c.AgentID
	c.fieldMap["model"] = c.Model
	c.fieldMap["prompt"] = c.Prompt
	c.fieldMap["created_at"] = c.CreatedAt
	c.fieldMap["updated_at"] = c.UpdatedAt
}

func (c chatConfig) clone(db *gorm.DB) chatConfig {
	c.chatConfigDo.ReplaceConnPool(db.Statement.ConnPool)
	return c
}

func (c chatConfig) replaceDB(db *gorm.DB) chatConfig {
	c.chatConfigDo.ReplaceDB(db)
	return c
}

type chatConfigDo struct{ gen.DO }

func (c chatConfigDo) Debug() *chatConfigDo {
	return c.withDO(c.DO.Debug())
}

func (c chatConfigDo) WithContext(ctx context.Context) *chatConfigDo {
	return c.withDO(c.DO.WithContext(ctx))
}

func (c chatConfigDo) ReadDB() *chatConfigDo {
	return c.Clauses(dbresolver.Read)
}

func (c chatConfigDo) WriteDB() *chatConfigDo {
	return c.Clauses(dbresolver.Write)
}

func (c chatConfigDo) Session(config *gorm.Session) *chatConfigDo {
	return c.withDO(c.DO.Session(config))
}

func (c chatConfigDo) Clauses(conds ...clause.Expression) *chatConfigDo {
	return c.withDO(c.DO.Clauses(conds...))
}

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

func (c chatConfigDo) Not(conds ...gen.Condition) *chatConfigDo {
	return c.withDO(c.DO.Not(conds...))
}

func (c chatConfigDo) Or(conds ...gen.Condition) *chatConfigDo {
	return c.withDO(c.DO.Or(conds...))
}

func (c chatConfigDo) Select(conds ...field.Expr) *chatConfigDo {
	return c.withDO(c.DO.Select(conds...))
}

func (c chatConfigDo) Where(conds ...gen.Condition) *chatConfigDo {
	return c.withDO(c.DO.Where(conds...))
}

func (c chatConfigDo) Order(conds ...field.Expr) *chatConfigDo {
	return c.withDO(c.DO.Order(conds...))
}

func (c chatConfigDo) Distinct(cols ...field.Expr) *chatConfigDo {
	return c.withDO(c.DO.Distinct(cols...))
}

func (c chatConfigDo) Omit(cols ...field.Expr) *chatConfigDo {
	return c.withDO(c.DO.Omit(cols...))
}

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

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

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

func (c chatConfigDo) Group(cols ...field.Expr) *chatConfigDo {
	return c.withDO(c.DO.Group(cols...))
}

func (c chatConfigDo) Having(conds ...gen.Condition) *chatConfigDo {
	return c.withDO(c.DO.Having(conds...))
}

func (c chatConfigDo) Limit(limit int) *chatConfigDo {
	return c.withDO(c.DO.Limit(limit))
}

func (c chatConfigDo) Offset(offset int) *chatConfigDo {
	return c.withDO(c.DO.Offset(offset))
}

func (c chatConfigDo) Scopes(funcs ...func(gen.Dao) gen.Dao) *chatConfigDo {
	return c.withDO(c.DO.Scopes(funcs...))
}

func (c chatConfigDo) Unscoped() *chatConfigDo {
	return c.withDO(c.DO.Unscoped())
}

func (c chatConfigDo) Create(values ...*model.ChatConfig) error {
	if len(values) == 0 {
		return nil
	}
	return c.DO.Create(values)
}

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

func (c chatConfigDo) First() (*model.ChatConfig, error) {
	if result, err := c.DO.First(); err != nil {
		return nil, err
	} else {
		return result.(*model.ChatConfig), nil
	}
}

func (c chatConfigDo) Take() (*model.ChatConfig, error) {
	if result, err := c.DO.Take(); err != nil {
		return nil, err
	} else {
		return result.(*model.ChatConfig), nil
	}
}

func (c chatConfigDo) Last() (*model.ChatConfig, error) {
	if result, err := c.DO.Last(); err != nil {
		return nil, err
	} else {
		return result.(*model.ChatConfig), nil
	}
}

func (c chatConfigDo) Find() ([]*model.ChatConfig, error) {
	result, err := c.DO.Find()
	return result.([]*model.ChatConfig), err
}

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

func (c chatConfigDo) Attrs(attrs ...field.AssignExpr) *chatConfigDo {
	return c.withDO(c.DO.Attrs(attrs...))
}

func (c chatConfigDo) Assign(attrs ...field.AssignExpr) *chatConfigDo {
	return c.withDO(c.DO.Assign(attrs...))
}

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

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

func (c chatConfigDo) FirstOrInit() (*model.ChatConfig, error) {
	if result, err := c.DO.FirstOrInit(); err != nil {
		return nil, err
	} else {
		return result.(*model.ChatConfig), nil
	}
}

func (c chatConfigDo) FirstOrCreate() (*model.ChatConfig, error) {
	if result, err := c.DO.FirstOrCreate(); err != nil {
		return nil, err
	} else {
		return result.(*model.ChatConfig), nil
	}
}

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

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

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

func (c chatConfigDo) Scan(result interface{}) (err error) {
	return c.DO.Scan(result)
}

func (c chatConfigDo) Delete(models ...*model.ChatConfig) (result gen.ResultInfo, err error) {
	return c.DO.Delete(models)
}

func (c *chatConfigDo) withDO(do gen.Dao) *chatConfigDo {
	c.DO = *do.(*gen.DO)
	return c
}
