// 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 query

import (
	"context"

	"gorm.io/gorm"
	"gorm.io/gorm/clause"
	"gorm.io/gorm/schema"

	"gorm.io/gen"
	"gorm.io/gen/field"

	"gorm.io/plugin/dbresolver"

	"wechat-api/database/dao/wechat/model"
)

func newChatRecord(db *gorm.DB, opts ...gen.DOOption) chatRecord {
	_chatRecord := chatRecord{}

	_chatRecord.chatRecordDo.UseDB(db, opts...)
	_chatRecord.chatRecordDo.UseModel(&model.ChatRecord{})

	tableName := _chatRecord.chatRecordDo.TableName()
	_chatRecord.ALL = field.NewAsterisk(tableName)
	_chatRecord.ID = field.NewInt64(tableName, "id")
	_chatRecord.CreatedAt = field.NewTime(tableName, "created_at")
	_chatRecord.UpdatedAt = field.NewTime(tableName, "updated_at")
	_chatRecord.DeletedAt = field.NewField(tableName, "deleted_at")
	_chatRecord.Content = field.NewString(tableName, "content")
	_chatRecord.ContentType = field.NewInt64(tableName, "content_type")
	_chatRecord.SessionID = field.NewInt64(tableName, "session_id")
	_chatRecord.UserID = field.NewInt64(tableName, "user_id")
	_chatRecord.BotID = field.NewInt64(tableName, "bot_id")
	_chatRecord.BotType = field.NewInt64(tableName, "bot_type")

	_chatRecord.fillFieldMap()

	return _chatRecord
}

// chatRecord 聊天历史记录
type chatRecord struct {
	chatRecordDo

	ALL         field.Asterisk
	ID          field.Int64
	CreatedAt   field.Time   // Create Time | 创建日期
	UpdatedAt   field.Time   // Update Time | 修改日期
	DeletedAt   field.Field  // Delete Time | 删除日期
	Content     field.String // 内容
	ContentType field.Int64  // 类型：1-提问 2-回答
	SessionID   field.Int64  // 会话ID
	UserID      field.Int64  // 用户ID
	BotID       field.Int64  // 聊天ID
	BotType     field.Int64  // 用户类型:1-微信 2-小程序 3-智能体

	fieldMap map[string]field.Expr
}

func (c chatRecord) Table(newTableName string) *chatRecord {
	c.chatRecordDo.UseTable(newTableName)
	return c.updateTableName(newTableName)
}

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

func (c *chatRecord) updateTableName(table string) *chatRecord {
	c.ALL = field.NewAsterisk(table)
	c.ID = field.NewInt64(table, "id")
	c.CreatedAt = field.NewTime(table, "created_at")
	c.UpdatedAt = field.NewTime(table, "updated_at")
	c.DeletedAt = field.NewField(table, "deleted_at")
	c.Content = field.NewString(table, "content")
	c.ContentType = field.NewInt64(table, "content_type")
	c.SessionID = field.NewInt64(table, "session_id")
	c.UserID = field.NewInt64(table, "user_id")
	c.BotID = field.NewInt64(table, "bot_id")
	c.BotType = field.NewInt64(table, "bot_type")

	c.fillFieldMap()

	return c
}

func (c *chatRecord) 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 *chatRecord) fillFieldMap() {
	c.fieldMap = make(map[string]field.Expr, 10)
	c.fieldMap["id"] = c.ID
	c.fieldMap["created_at"] = c.CreatedAt
	c.fieldMap["updated_at"] = c.UpdatedAt
	c.fieldMap["deleted_at"] = c.DeletedAt
	c.fieldMap["content"] = c.Content
	c.fieldMap["content_type"] = c.ContentType
	c.fieldMap["session_id"] = c.SessionID
	c.fieldMap["user_id"] = c.UserID
	c.fieldMap["bot_id"] = c.BotID
	c.fieldMap["bot_type"] = c.BotType
}

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

func (c chatRecord) replaceDB(db *gorm.DB) chatRecord {
	c.chatRecordDo.ReplaceDB(db)
	return c
}

type chatRecordDo struct{ gen.DO }

type IChatRecordDo interface {
	gen.SubQuery
	Debug() IChatRecordDo
	WithContext(ctx context.Context) IChatRecordDo
	WithResult(fc func(tx gen.Dao)) gen.ResultInfo
	ReplaceDB(db *gorm.DB)
	ReadDB() IChatRecordDo
	WriteDB() IChatRecordDo
	As(alias string) gen.Dao
	Session(config *gorm.Session) IChatRecordDo
	Columns(cols ...field.Expr) gen.Columns
	Clauses(conds ...clause.Expression) IChatRecordDo
	Not(conds ...gen.Condition) IChatRecordDo
	Or(conds ...gen.Condition) IChatRecordDo
	Select(conds ...field.Expr) IChatRecordDo
	Where(conds ...gen.Condition) IChatRecordDo
	Order(conds ...field.Expr) IChatRecordDo
	Distinct(cols ...field.Expr) IChatRecordDo
	Omit(cols ...field.Expr) IChatRecordDo
	Join(table schema.Tabler, on ...field.Expr) IChatRecordDo
	LeftJoin(table schema.Tabler, on ...field.Expr) IChatRecordDo
	RightJoin(table schema.Tabler, on ...field.Expr) IChatRecordDo
	Group(cols ...field.Expr) IChatRecordDo
	Having(conds ...gen.Condition) IChatRecordDo
	Limit(limit int) IChatRecordDo
	Offset(offset int) IChatRecordDo
	Count() (count int64, err error)
	Scopes(funcs ...func(gen.Dao) gen.Dao) IChatRecordDo
	Unscoped() IChatRecordDo
	Create(values ...*model.ChatRecord) error
	CreateInBatches(values []*model.ChatRecord, batchSize int) error
	Save(values ...*model.ChatRecord) error
	First() (*model.ChatRecord, error)
	Take() (*model.ChatRecord, error)
	Last() (*model.ChatRecord, error)
	Find() ([]*model.ChatRecord, error)
	FindInBatch(batchSize int, fc func(tx gen.Dao, batch int) error) (results []*model.ChatRecord, err error)
	FindInBatches(result *[]*model.ChatRecord, batchSize int, fc func(tx gen.Dao, batch int) error) error
	Pluck(column field.Expr, dest interface{}) error
	Delete(...*model.ChatRecord) (info gen.ResultInfo, err error)
	Update(column field.Expr, value interface{}) (info gen.ResultInfo, err error)
	UpdateSimple(columns ...field.AssignExpr) (info gen.ResultInfo, err error)
	Updates(value interface{}) (info gen.ResultInfo, err error)
	UpdateColumn(column field.Expr, value interface{}) (info gen.ResultInfo, err error)
	UpdateColumnSimple(columns ...field.AssignExpr) (info gen.ResultInfo, err error)
	UpdateColumns(value interface{}) (info gen.ResultInfo, err error)
	UpdateFrom(q gen.SubQuery) gen.Dao
	Attrs(attrs ...field.AssignExpr) IChatRecordDo
	Assign(attrs ...field.AssignExpr) IChatRecordDo
	Joins(fields ...field.RelationField) IChatRecordDo
	Preload(fields ...field.RelationField) IChatRecordDo
	FirstOrInit() (*model.ChatRecord, error)
	FirstOrCreate() (*model.ChatRecord, error)
	FindByPage(offset int, limit int) (result []*model.ChatRecord, count int64, err error)
	ScanByPage(result interface{}, offset int, limit int) (count int64, err error)
	Scan(result interface{}) (err error)
	Returning(value interface{}, columns ...string) IChatRecordDo
	UnderlyingDB() *gorm.DB
	schema.Tabler
}

func (c chatRecordDo) Debug() IChatRecordDo {
	return c.withDO(c.DO.Debug())
}

func (c chatRecordDo) WithContext(ctx context.Context) IChatRecordDo {
	return c.withDO(c.DO.WithContext(ctx))
}

func (c chatRecordDo) ReadDB() IChatRecordDo {
	return c.Clauses(dbresolver.Read)
}

func (c chatRecordDo) WriteDB() IChatRecordDo {
	return c.Clauses(dbresolver.Write)
}

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

func (c chatRecordDo) Clauses(conds ...clause.Expression) IChatRecordDo {
	return c.withDO(c.DO.Clauses(conds...))
}

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

func (c chatRecordDo) Not(conds ...gen.Condition) IChatRecordDo {
	return c.withDO(c.DO.Not(conds...))
}

func (c chatRecordDo) Or(conds ...gen.Condition) IChatRecordDo {
	return c.withDO(c.DO.Or(conds...))
}

func (c chatRecordDo) Select(conds ...field.Expr) IChatRecordDo {
	return c.withDO(c.DO.Select(conds...))
}

func (c chatRecordDo) Where(conds ...gen.Condition) IChatRecordDo {
	return c.withDO(c.DO.Where(conds...))
}

func (c chatRecordDo) Order(conds ...field.Expr) IChatRecordDo {
	return c.withDO(c.DO.Order(conds...))
}

func (c chatRecordDo) Distinct(cols ...field.Expr) IChatRecordDo {
	return c.withDO(c.DO.Distinct(cols...))
}

func (c chatRecordDo) Omit(cols ...field.Expr) IChatRecordDo {
	return c.withDO(c.DO.Omit(cols...))
}

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

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

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

func (c chatRecordDo) Group(cols ...field.Expr) IChatRecordDo {
	return c.withDO(c.DO.Group(cols...))
}

func (c chatRecordDo) Having(conds ...gen.Condition) IChatRecordDo {
	return c.withDO(c.DO.Having(conds...))
}

func (c chatRecordDo) Limit(limit int) IChatRecordDo {
	return c.withDO(c.DO.Limit(limit))
}

func (c chatRecordDo) Offset(offset int) IChatRecordDo {
	return c.withDO(c.DO.Offset(offset))
}

func (c chatRecordDo) Scopes(funcs ...func(gen.Dao) gen.Dao) IChatRecordDo {
	return c.withDO(c.DO.Scopes(funcs...))
}

func (c chatRecordDo) Unscoped() IChatRecordDo {
	return c.withDO(c.DO.Unscoped())
}

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

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

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

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

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

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

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

func (c chatRecordDo) Attrs(attrs ...field.AssignExpr) IChatRecordDo {
	return c.withDO(c.DO.Attrs(attrs...))
}

func (c chatRecordDo) Assign(attrs ...field.AssignExpr) IChatRecordDo {
	return c.withDO(c.DO.Assign(attrs...))
}

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

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

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

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

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

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

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