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

	_chat.chatDo.UseDB(db, opts...)
	_chat.chatDo.UseModel(&model.Chat{})

	tableName := _chat.chatDo.TableName()
	_chat.ALL = field.NewAsterisk(tableName)
	_chat.ID = field.NewInt64(tableName, "id")
	_chat.User = field.NewString(tableName, "user")
	_chat.MessageID = field.NewString(tableName, "message_id")
	_chat.OpenKfID = field.NewString(tableName, "open_kf_id")
	_chat.AgentID = field.NewInt64(tableName, "agent_id")
	_chat.ReqContent = field.NewString(tableName, "req_content")
	_chat.ResContent = field.NewString(tableName, "res_content")
	_chat.CreatedAt = field.NewTime(tableName, "created_at")
	_chat.UpdatedAt = field.NewTime(tableName, "updated_at")

	_chat.fillFieldMap()

	return _chat
}

type chat struct {
	chatDo chatDo

	ALL        field.Asterisk
	ID         field.Int64  // 聊天记录ID
	User       field.String // weCom用户标识/customer用户标识
	MessageID  field.String // message_id customer消息唯一ID
	OpenKfID   field.String // 客服标识
	AgentID    field.Int64  // 应用ID
	ReqContent field.String // 用户发送内容
	ResContent field.String // openai响应内容
	CreatedAt  field.Time   // 创建时间
	UpdatedAt  field.Time   // 更新时间

	fieldMap map[string]field.Expr
}

func (c chat) Table(newTableName string) *chat {
	c.chatDo.UseTable(newTableName)
	return c.updateTableName(newTableName)
}

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

func (c *chat) updateTableName(table string) *chat {
	c.ALL = field.NewAsterisk(table)
	c.ID = field.NewInt64(table, "id")
	c.User = field.NewString(table, "user")
	c.MessageID = field.NewString(table, "message_id")
	c.OpenKfID = field.NewString(table, "open_kf_id")
	c.AgentID = field.NewInt64(table, "agent_id")
	c.ReqContent = field.NewString(table, "req_content")
	c.ResContent = field.NewString(table, "res_content")
	c.CreatedAt = field.NewTime(table, "created_at")
	c.UpdatedAt = field.NewTime(table, "updated_at")

	c.fillFieldMap()

	return c
}

func (c *chat) WithContext(ctx context.Context) *chatDo { return c.chatDo.WithContext(ctx) }

func (c chat) TableName() string { return c.chatDo.TableName() }

func (c chat) Alias() string { return c.chatDo.Alias() }

func (c chat) Columns(cols ...field.Expr) gen.Columns { return c.chatDo.Columns(cols...) }

func (c *chat) 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 *chat) fillFieldMap() {
	c.fieldMap = make(map[string]field.Expr, 9)
	c.fieldMap["id"] = c.ID
	c.fieldMap["user"] = c.User
	c.fieldMap["message_id"] = c.MessageID
	c.fieldMap["open_kf_id"] = c.OpenKfID
	c.fieldMap["agent_id"] = c.AgentID
	c.fieldMap["req_content"] = c.ReqContent
	c.fieldMap["res_content"] = c.ResContent
	c.fieldMap["created_at"] = c.CreatedAt
	c.fieldMap["updated_at"] = c.UpdatedAt
}

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

func (c chat) replaceDB(db *gorm.DB) chat {
	c.chatDo.ReplaceDB(db)
	return c
}

type chatDo struct{ gen.DO }

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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