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

	"app/shelfly/internal/models"
)

func newLibrarySection(db *gorm.DB, opts ...gen.DOOption) librarySection {
	_librarySection := librarySection{}

	_librarySection.librarySectionDo.UseDB(db, opts...)
	_librarySection.librarySectionDo.UseModel(&models.LibrarySection{})

	tableName := _librarySection.librarySectionDo.TableName()
	_librarySection.ALL = field.NewAsterisk(tableName)
	_librarySection.ID = field.NewInt64(tableName, "id")
	_librarySection.Name = field.NewString(tableName, "name")
	_librarySection.SectionType = field.NewInt64(tableName, "section_type")
	_librarySection.Language = field.NewString(tableName, "language")
	_librarySection.UUID = field.NewString(tableName, "uuid")
	_librarySection.CreatedAt = field.NewString(tableName, "created_at")
	_librarySection.UpdatedAt = field.NewString(tableName, "updated_at")

	_librarySection.fillFieldMap()

	return _librarySection
}

type librarySection struct {
	librarySectionDo

	ALL         field.Asterisk
	ID          field.Int64
	Name        field.String
	SectionType field.Int64
	Language    field.String
	UUID        field.String
	CreatedAt   field.String
	UpdatedAt   field.String

	fieldMap map[string]field.Expr
}

func (l librarySection) Table(newTableName string) *librarySection {
	l.librarySectionDo.UseTable(newTableName)
	return l.updateTableName(newTableName)
}

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

func (l *librarySection) updateTableName(table string) *librarySection {
	l.ALL = field.NewAsterisk(table)
	l.ID = field.NewInt64(table, "id")
	l.Name = field.NewString(table, "name")
	l.SectionType = field.NewInt64(table, "section_type")
	l.Language = field.NewString(table, "language")
	l.UUID = field.NewString(table, "uuid")
	l.CreatedAt = field.NewString(table, "created_at")
	l.UpdatedAt = field.NewString(table, "updated_at")

	l.fillFieldMap()

	return l
}

func (l *librarySection) 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 *librarySection) fillFieldMap() {
	l.fieldMap = make(map[string]field.Expr, 7)
	l.fieldMap["id"] = l.ID
	l.fieldMap["name"] = l.Name
	l.fieldMap["section_type"] = l.SectionType
	l.fieldMap["language"] = l.Language
	l.fieldMap["uuid"] = l.UUID
	l.fieldMap["created_at"] = l.CreatedAt
	l.fieldMap["updated_at"] = l.UpdatedAt
}

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

func (l librarySection) replaceDB(db *gorm.DB) librarySection {
	l.librarySectionDo.ReplaceDB(db)
	return l
}

type librarySectionDo struct{ gen.DO }

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

	FilterWithNameAndRole(name string, role string) (result []models.LibrarySection, err error)
}

// SELECT * FROM @@table WHERE name = @name{{if role !=""}} AND role = @role{{end}}
func (l librarySectionDo) FilterWithNameAndRole(name string, role string) (result []models.LibrarySection, err error) {
	var params []interface{}

	var generateSQL strings.Builder
	params = append(params, name)
	generateSQL.WriteString("SELECT * FROM library_sections WHERE name = ? ")
	if role != "" {
		params = append(params, role)
		generateSQL.WriteString("AND role = ? ")
	}

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

	return
}

func (l librarySectionDo) Debug() ILibrarySectionDo {
	return l.withDO(l.DO.Debug())
}

func (l librarySectionDo) WithContext(ctx context.Context) ILibrarySectionDo {
	return l.withDO(l.DO.WithContext(ctx))
}

func (l librarySectionDo) ReadDB() ILibrarySectionDo {
	return l.Clauses(dbresolver.Read)
}

func (l librarySectionDo) WriteDB() ILibrarySectionDo {
	return l.Clauses(dbresolver.Write)
}

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

func (l librarySectionDo) Clauses(conds ...clause.Expression) ILibrarySectionDo {
	return l.withDO(l.DO.Clauses(conds...))
}

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

func (l librarySectionDo) Not(conds ...gen.Condition) ILibrarySectionDo {
	return l.withDO(l.DO.Not(conds...))
}

func (l librarySectionDo) Or(conds ...gen.Condition) ILibrarySectionDo {
	return l.withDO(l.DO.Or(conds...))
}

func (l librarySectionDo) Select(conds ...field.Expr) ILibrarySectionDo {
	return l.withDO(l.DO.Select(conds...))
}

func (l librarySectionDo) Where(conds ...gen.Condition) ILibrarySectionDo {
	return l.withDO(l.DO.Where(conds...))
}

func (l librarySectionDo) Order(conds ...field.Expr) ILibrarySectionDo {
	return l.withDO(l.DO.Order(conds...))
}

func (l librarySectionDo) Distinct(cols ...field.Expr) ILibrarySectionDo {
	return l.withDO(l.DO.Distinct(cols...))
}

func (l librarySectionDo) Omit(cols ...field.Expr) ILibrarySectionDo {
	return l.withDO(l.DO.Omit(cols...))
}

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

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

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

func (l librarySectionDo) Group(cols ...field.Expr) ILibrarySectionDo {
	return l.withDO(l.DO.Group(cols...))
}

func (l librarySectionDo) Having(conds ...gen.Condition) ILibrarySectionDo {
	return l.withDO(l.DO.Having(conds...))
}

func (l librarySectionDo) Limit(limit int) ILibrarySectionDo {
	return l.withDO(l.DO.Limit(limit))
}

func (l librarySectionDo) Offset(offset int) ILibrarySectionDo {
	return l.withDO(l.DO.Offset(offset))
}

func (l librarySectionDo) Scopes(funcs ...func(gen.Dao) gen.Dao) ILibrarySectionDo {
	return l.withDO(l.DO.Scopes(funcs...))
}

func (l librarySectionDo) Unscoped() ILibrarySectionDo {
	return l.withDO(l.DO.Unscoped())
}

func (l librarySectionDo) Create(values ...*models.LibrarySection) error {
	if len(values) == 0 {
		return nil
	}
	return l.DO.Create(values)
}

func (l librarySectionDo) CreateInBatches(values []*models.LibrarySection, 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 librarySectionDo) Save(values ...*models.LibrarySection) error {
	if len(values) == 0 {
		return nil
	}
	return l.DO.Save(values)
}

func (l librarySectionDo) First() (*models.LibrarySection, error) {
	if result, err := l.DO.First(); err != nil {
		return nil, err
	} else {
		return result.(*models.LibrarySection), nil
	}
}

func (l librarySectionDo) Take() (*models.LibrarySection, error) {
	if result, err := l.DO.Take(); err != nil {
		return nil, err
	} else {
		return result.(*models.LibrarySection), nil
	}
}

func (l librarySectionDo) Last() (*models.LibrarySection, error) {
	if result, err := l.DO.Last(); err != nil {
		return nil, err
	} else {
		return result.(*models.LibrarySection), nil
	}
}

func (l librarySectionDo) Find() ([]*models.LibrarySection, error) {
	result, err := l.DO.Find()
	return result.([]*models.LibrarySection), err
}

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

func (l librarySectionDo) Attrs(attrs ...field.AssignExpr) ILibrarySectionDo {
	return l.withDO(l.DO.Attrs(attrs...))
}

func (l librarySectionDo) Assign(attrs ...field.AssignExpr) ILibrarySectionDo {
	return l.withDO(l.DO.Assign(attrs...))
}

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

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

func (l librarySectionDo) FirstOrInit() (*models.LibrarySection, error) {
	if result, err := l.DO.FirstOrInit(); err != nil {
		return nil, err
	} else {
		return result.(*models.LibrarySection), nil
	}
}

func (l librarySectionDo) FirstOrCreate() (*models.LibrarySection, error) {
	if result, err := l.DO.FirstOrCreate(); err != nil {
		return nil, err
	} else {
		return result.(*models.LibrarySection), nil
	}
}

func (l librarySectionDo) FindByPage(offset int, limit int) (result []*models.LibrarySection, 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 librarySectionDo) 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 librarySectionDo) Scan(result interface{}) (err error) {
	return l.DO.Scan(result)
}

func (l librarySectionDo) Delete(models ...*models.LibrarySection) (result gen.ResultInfo, err error) {
	return l.DO.Delete(models)
}

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