// 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"
	"strings"

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

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

	"gorm.io/plugin/dbresolver"

	"github.com/0xJacky/Nginx-UI/model"
)

func newLLMSession(db *gorm.DB, opts ...gen.DOOption) lLMSession {
	_lLMSession := lLMSession{}

	_lLMSession.lLMSessionDo.UseDB(db, opts...)
	_lLMSession.lLMSessionDo.UseModel(&model.LLMSession{})

	tableName := _lLMSession.lLMSessionDo.TableName()
	_lLMSession.ALL = field.NewAsterisk(tableName)
	_lLMSession.ID = field.NewInt(tableName, "id")
	_lLMSession.SessionID = field.NewString(tableName, "session_id")
	_lLMSession.Title = field.NewString(tableName, "title")
	_lLMSession.Path = field.NewString(tableName, "path")
	_lLMSession.Messages = field.NewField(tableName, "messages")
	_lLMSession.MessageCount = field.NewInt(tableName, "message_count")
	_lLMSession.IsActive = field.NewBool(tableName, "is_active")
	_lLMSession.CreatedAt = field.NewTime(tableName, "created_at")
	_lLMSession.UpdatedAt = field.NewTime(tableName, "updated_at")
	_lLMSession.DeletedAt = field.NewField(tableName, "deleted_at")

	_lLMSession.fillFieldMap()

	return _lLMSession
}

type lLMSession struct {
	lLMSessionDo

	ALL          field.Asterisk
	ID           field.Int
	SessionID    field.String
	Title        field.String
	Path         field.String
	Messages     field.Field
	MessageCount field.Int
	IsActive     field.Bool
	CreatedAt    field.Time
	UpdatedAt    field.Time
	DeletedAt    field.Field

	fieldMap map[string]field.Expr
}

func (l lLMSession) Table(newTableName string) *lLMSession {
	l.lLMSessionDo.UseTable(newTableName)
	return l.updateTableName(newTableName)
}

func (l lLMSession) As(alias string) *lLMSession {
	l.lLMSessionDo.DO = *(l.lLMSessionDo.As(alias).(*gen.DO))
	return l.updateTableName(alias)
}

func (l *lLMSession) updateTableName(table string) *lLMSession {
	l.ALL = field.NewAsterisk(table)
	l.ID = field.NewInt(table, "id")
	l.SessionID = field.NewString(table, "session_id")
	l.Title = field.NewString(table, "title")
	l.Path = field.NewString(table, "path")
	l.Messages = field.NewField(table, "messages")
	l.MessageCount = field.NewInt(table, "message_count")
	l.IsActive = field.NewBool(table, "is_active")
	l.CreatedAt = field.NewTime(table, "created_at")
	l.UpdatedAt = field.NewTime(table, "updated_at")
	l.DeletedAt = field.NewField(table, "deleted_at")

	l.fillFieldMap()

	return l
}

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

func (l *lLMSession) fillFieldMap() {
	l.fieldMap = make(map[string]field.Expr, 10)
	l.fieldMap["id"] = l.ID
	l.fieldMap["session_id"] = l.SessionID
	l.fieldMap["title"] = l.Title
	l.fieldMap["path"] = l.Path
	l.fieldMap["messages"] = l.Messages
	l.fieldMap["message_count"] = l.MessageCount
	l.fieldMap["is_active"] = l.IsActive
	l.fieldMap["created_at"] = l.CreatedAt
	l.fieldMap["updated_at"] = l.UpdatedAt
	l.fieldMap["deleted_at"] = l.DeletedAt
}

func (l lLMSession) clone(db *gorm.DB) lLMSession {
	l.lLMSessionDo.ReplaceConnPool(db.Statement.ConnPool)
	return l
}

func (l lLMSession) replaceDB(db *gorm.DB) lLMSession {
	l.lLMSessionDo.ReplaceDB(db)
	return l
}

type lLMSessionDo struct{ gen.DO }

// FirstByID Where("id=@id")
func (l lLMSessionDo) FirstByID(id uint64) (result *model.LLMSession, err error) {
	var params []interface{}

	var generateSQL strings.Builder
	params = append(params, id)
	generateSQL.WriteString("id=? ")

	var executeSQL *gorm.DB
	executeSQL = l.UnderlyingDB().Where(generateSQL.String(), params...).Take(&result) // ignore_security_alert
	err = executeSQL.Error

	return
}

// DeleteByID update @@table set deleted_at=strftime('%Y-%m-%d %H:%M:%S','now') where id=@id
func (l lLMSessionDo) DeleteByID(id uint64) (err error) {
	var params []interface{}

	var generateSQL strings.Builder
	params = append(params, id)
	generateSQL.WriteString("update llm_sessions set deleted_at=strftime('%Y-%m-%d %H:%M:%S','now') where id=? ")

	var executeSQL *gorm.DB
	executeSQL = l.UnderlyingDB().Exec(generateSQL.String(), params...) // ignore_security_alert
	err = executeSQL.Error

	return
}

func (l lLMSessionDo) Debug() *lLMSessionDo {
	return l.withDO(l.DO.Debug())
}

func (l lLMSessionDo) WithContext(ctx context.Context) *lLMSessionDo {
	return l.withDO(l.DO.WithContext(ctx))
}

func (l lLMSessionDo) ReadDB() *lLMSessionDo {
	return l.Clauses(dbresolver.Read)
}

func (l lLMSessionDo) WriteDB() *lLMSessionDo {
	return l.Clauses(dbresolver.Write)
}

func (l lLMSessionDo) Session(config *gorm.Session) *lLMSessionDo {
	return l.withDO(l.DO.Session(config))
}

func (l lLMSessionDo) Clauses(conds ...clause.Expression) *lLMSessionDo {
	return l.withDO(l.DO.Clauses(conds...))
}

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

func (l lLMSessionDo) Not(conds ...gen.Condition) *lLMSessionDo {
	return l.withDO(l.DO.Not(conds...))
}

func (l lLMSessionDo) Or(conds ...gen.Condition) *lLMSessionDo {
	return l.withDO(l.DO.Or(conds...))
}

func (l lLMSessionDo) Select(conds ...field.Expr) *lLMSessionDo {
	return l.withDO(l.DO.Select(conds...))
}

func (l lLMSessionDo) Where(conds ...gen.Condition) *lLMSessionDo {
	return l.withDO(l.DO.Where(conds...))
}

func (l lLMSessionDo) Order(conds ...field.Expr) *lLMSessionDo {
	return l.withDO(l.DO.Order(conds...))
}

func (l lLMSessionDo) Distinct(cols ...field.Expr) *lLMSessionDo {
	return l.withDO(l.DO.Distinct(cols...))
}

func (l lLMSessionDo) Omit(cols ...field.Expr) *lLMSessionDo {
	return l.withDO(l.DO.Omit(cols...))
}

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

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

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

func (l lLMSessionDo) Group(cols ...field.Expr) *lLMSessionDo {
	return l.withDO(l.DO.Group(cols...))
}

func (l lLMSessionDo) Having(conds ...gen.Condition) *lLMSessionDo {
	return l.withDO(l.DO.Having(conds...))
}

func (l lLMSessionDo) Limit(limit int) *lLMSessionDo {
	return l.withDO(l.DO.Limit(limit))
}

func (l lLMSessionDo) Offset(offset int) *lLMSessionDo {
	return l.withDO(l.DO.Offset(offset))
}

func (l lLMSessionDo) Scopes(funcs ...func(gen.Dao) gen.Dao) *lLMSessionDo {
	return l.withDO(l.DO.Scopes(funcs...))
}

func (l lLMSessionDo) Unscoped() *lLMSessionDo {
	return l.withDO(l.DO.Unscoped())
}

func (l lLMSessionDo) Create(values ...*model.LLMSession) error {
	if len(values) == 0 {
		return nil
	}
	return l.DO.Create(values)
}

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

func (l lLMSessionDo) First() (*model.LLMSession, error) {
	if result, err := l.DO.First(); err != nil {
		return nil, err
	} else {
		return result.(*model.LLMSession), nil
	}
}

func (l lLMSessionDo) Take() (*model.LLMSession, error) {
	if result, err := l.DO.Take(); err != nil {
		return nil, err
	} else {
		return result.(*model.LLMSession), nil
	}
}

func (l lLMSessionDo) Last() (*model.LLMSession, error) {
	if result, err := l.DO.Last(); err != nil {
		return nil, err
	} else {
		return result.(*model.LLMSession), nil
	}
}

func (l lLMSessionDo) Find() ([]*model.LLMSession, error) {
	result, err := l.DO.Find()
	return result.([]*model.LLMSession), err
}

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

func (l lLMSessionDo) Attrs(attrs ...field.AssignExpr) *lLMSessionDo {
	return l.withDO(l.DO.Attrs(attrs...))
}

func (l lLMSessionDo) Assign(attrs ...field.AssignExpr) *lLMSessionDo {
	return l.withDO(l.DO.Assign(attrs...))
}

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

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

func (l lLMSessionDo) FirstOrInit() (*model.LLMSession, error) {
	if result, err := l.DO.FirstOrInit(); err != nil {
		return nil, err
	} else {
		return result.(*model.LLMSession), nil
	}
}

func (l lLMSessionDo) FirstOrCreate() (*model.LLMSession, error) {
	if result, err := l.DO.FirstOrCreate(); err != nil {
		return nil, err
	} else {
		return result.(*model.LLMSession), nil
	}
}

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

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

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

func (l lLMSessionDo) Scan(result interface{}) (err error) {
	return l.DO.Scan(result)
}

func (l lLMSessionDo) Delete(models ...*model.LLMSession) (result gen.ResultInfo, err error) {
	return l.DO.Delete(models)
}

func (l *lLMSessionDo) withDO(do gen.Dao) *lLMSessionDo {
	l.DO = *do.(*gen.DO)
	return l
}
