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

	_chatSession.chatSessionDo.UseDB(db, opts...)
	_chatSession.chatSessionDo.UseModel(&model.ChatSession{})

	tableName := _chatSession.chatSessionDo.TableName()
	_chatSession.ALL = field.NewAsterisk(tableName)
	_chatSession.ID = field.NewInt64(tableName, "id")
	_chatSession.CreatedAt = field.NewTime(tableName, "created_at")
	_chatSession.UpdatedAt = field.NewTime(tableName, "updated_at")
	_chatSession.DeletedAt = field.NewField(tableName, "deleted_at")
	_chatSession.Name = field.NewString(tableName, "name")
	_chatSession.UserID = field.NewInt64(tableName, "user_id")
	_chatSession.BotID = field.NewInt64(tableName, "bot_id")
	_chatSession.BotType = field.NewInt64(tableName, "bot_type")

	_chatSession.fillFieldMap()

	return _chatSession
}

// chatSession 聊天会话表
type chatSession struct {
	chatSessionDo

	ALL       field.Asterisk
	ID        field.Int64
	CreatedAt field.Time   // Create Time | 创建日期
	UpdatedAt field.Time   // Update Time | 修改日期
	DeletedAt field.Field  // Delete Time | 删除日期
	Name      field.String // 会话名称
	UserID    field.Int64  // 用户ID
	BotID     field.Int64  // 聊天 ID
	BotType   field.Int64  // 类型:1-微信 2-小程序 3-智能体

	fieldMap map[string]field.Expr
}

func (c chatSession) Table(newTableName string) *chatSession {
	c.chatSessionDo.UseTable(newTableName)
	return c.updateTableName(newTableName)
}

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

func (c *chatSession) updateTableName(table string) *chatSession {
	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.Name = field.NewString(table, "name")
	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 *chatSession) 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 *chatSession) fillFieldMap() {
	c.fieldMap = make(map[string]field.Expr, 8)
	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["name"] = c.Name
	c.fieldMap["user_id"] = c.UserID
	c.fieldMap["bot_id"] = c.BotID
	c.fieldMap["bot_type"] = c.BotType
}

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

func (c chatSession) replaceDB(db *gorm.DB) chatSession {
	c.chatSessionDo.ReplaceDB(db)
	return c
}

type chatSessionDo struct{ gen.DO }

type IChatSessionDo interface {
	gen.SubQuery
	Debug() IChatSessionDo
	WithContext(ctx context.Context) IChatSessionDo
	WithResult(fc func(tx gen.Dao)) gen.ResultInfo
	ReplaceDB(db *gorm.DB)
	ReadDB() IChatSessionDo
	WriteDB() IChatSessionDo
	As(alias string) gen.Dao
	Session(config *gorm.Session) IChatSessionDo
	Columns(cols ...field.Expr) gen.Columns
	Clauses(conds ...clause.Expression) IChatSessionDo
	Not(conds ...gen.Condition) IChatSessionDo
	Or(conds ...gen.Condition) IChatSessionDo
	Select(conds ...field.Expr) IChatSessionDo
	Where(conds ...gen.Condition) IChatSessionDo
	Order(conds ...field.Expr) IChatSessionDo
	Distinct(cols ...field.Expr) IChatSessionDo
	Omit(cols ...field.Expr) IChatSessionDo
	Join(table schema.Tabler, on ...field.Expr) IChatSessionDo
	LeftJoin(table schema.Tabler, on ...field.Expr) IChatSessionDo
	RightJoin(table schema.Tabler, on ...field.Expr) IChatSessionDo
	Group(cols ...field.Expr) IChatSessionDo
	Having(conds ...gen.Condition) IChatSessionDo
	Limit(limit int) IChatSessionDo
	Offset(offset int) IChatSessionDo
	Count() (count int64, err error)
	Scopes(funcs ...func(gen.Dao) gen.Dao) IChatSessionDo
	Unscoped() IChatSessionDo
	Create(values ...*model.ChatSession) error
	CreateInBatches(values []*model.ChatSession, batchSize int) error
	Save(values ...*model.ChatSession) error
	First() (*model.ChatSession, error)
	Take() (*model.ChatSession, error)
	Last() (*model.ChatSession, error)
	Find() ([]*model.ChatSession, error)
	FindInBatch(batchSize int, fc func(tx gen.Dao, batch int) error) (results []*model.ChatSession, err error)
	FindInBatches(result *[]*model.ChatSession, batchSize int, fc func(tx gen.Dao, batch int) error) error
	Pluck(column field.Expr, dest interface{}) error
	Delete(...*model.ChatSession) (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) IChatSessionDo
	Assign(attrs ...field.AssignExpr) IChatSessionDo
	Joins(fields ...field.RelationField) IChatSessionDo
	Preload(fields ...field.RelationField) IChatSessionDo
	FirstOrInit() (*model.ChatSession, error)
	FirstOrCreate() (*model.ChatSession, error)
	FindByPage(offset int, limit int) (result []*model.ChatSession, 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) IChatSessionDo
	UnderlyingDB() *gorm.DB
	schema.Tabler
}

func (c chatSessionDo) Debug() IChatSessionDo {
	return c.withDO(c.DO.Debug())
}

func (c chatSessionDo) WithContext(ctx context.Context) IChatSessionDo {
	return c.withDO(c.DO.WithContext(ctx))
}

func (c chatSessionDo) ReadDB() IChatSessionDo {
	return c.Clauses(dbresolver.Read)
}

func (c chatSessionDo) WriteDB() IChatSessionDo {
	return c.Clauses(dbresolver.Write)
}

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

func (c chatSessionDo) Clauses(conds ...clause.Expression) IChatSessionDo {
	return c.withDO(c.DO.Clauses(conds...))
}

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

func (c chatSessionDo) Not(conds ...gen.Condition) IChatSessionDo {
	return c.withDO(c.DO.Not(conds...))
}

func (c chatSessionDo) Or(conds ...gen.Condition) IChatSessionDo {
	return c.withDO(c.DO.Or(conds...))
}

func (c chatSessionDo) Select(conds ...field.Expr) IChatSessionDo {
	return c.withDO(c.DO.Select(conds...))
}

func (c chatSessionDo) Where(conds ...gen.Condition) IChatSessionDo {
	return c.withDO(c.DO.Where(conds...))
}

func (c chatSessionDo) Order(conds ...field.Expr) IChatSessionDo {
	return c.withDO(c.DO.Order(conds...))
}

func (c chatSessionDo) Distinct(cols ...field.Expr) IChatSessionDo {
	return c.withDO(c.DO.Distinct(cols...))
}

func (c chatSessionDo) Omit(cols ...field.Expr) IChatSessionDo {
	return c.withDO(c.DO.Omit(cols...))
}

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

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

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

func (c chatSessionDo) Group(cols ...field.Expr) IChatSessionDo {
	return c.withDO(c.DO.Group(cols...))
}

func (c chatSessionDo) Having(conds ...gen.Condition) IChatSessionDo {
	return c.withDO(c.DO.Having(conds...))
}

func (c chatSessionDo) Limit(limit int) IChatSessionDo {
	return c.withDO(c.DO.Limit(limit))
}

func (c chatSessionDo) Offset(offset int) IChatSessionDo {
	return c.withDO(c.DO.Offset(offset))
}

func (c chatSessionDo) Scopes(funcs ...func(gen.Dao) gen.Dao) IChatSessionDo {
	return c.withDO(c.DO.Scopes(funcs...))
}

func (c chatSessionDo) Unscoped() IChatSessionDo {
	return c.withDO(c.DO.Unscoped())
}

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

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

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

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

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

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

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

func (c chatSessionDo) Attrs(attrs ...field.AssignExpr) IChatSessionDo {
	return c.withDO(c.DO.Attrs(attrs...))
}

func (c chatSessionDo) Assign(attrs ...field.AssignExpr) IChatSessionDo {
	return c.withDO(c.DO.Assign(attrs...))
}

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

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

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

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

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

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

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