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

	_sectionLocation.sectionLocationDo.UseDB(db, opts...)
	_sectionLocation.sectionLocationDo.UseModel(&models.SectionLocation{})

	tableName := _sectionLocation.sectionLocationDo.TableName()
	_sectionLocation.ALL = field.NewAsterisk(tableName)
	_sectionLocation.ID = field.NewInt64(tableName, "id")
	_sectionLocation.LibrarySectionID = field.NewInt64(tableName, "library_section_id")
	_sectionLocation.RootPath = field.NewString(tableName, "root_path")
	_sectionLocation.CreatedAt = field.NewString(tableName, "created_at")
	_sectionLocation.UpdatedAt = field.NewString(tableName, "updated_at")

	_sectionLocation.fillFieldMap()

	return _sectionLocation
}

type sectionLocation struct {
	sectionLocationDo

	ALL              field.Asterisk
	ID               field.Int64
	LibrarySectionID field.Int64
	RootPath         field.String
	CreatedAt        field.String
	UpdatedAt        field.String

	fieldMap map[string]field.Expr
}

func (s sectionLocation) Table(newTableName string) *sectionLocation {
	s.sectionLocationDo.UseTable(newTableName)
	return s.updateTableName(newTableName)
}

func (s sectionLocation) As(alias string) *sectionLocation {
	s.sectionLocationDo.DO = *(s.sectionLocationDo.As(alias).(*gen.DO))
	return s.updateTableName(alias)
}

func (s *sectionLocation) updateTableName(table string) *sectionLocation {
	s.ALL = field.NewAsterisk(table)
	s.ID = field.NewInt64(table, "id")
	s.LibrarySectionID = field.NewInt64(table, "library_section_id")
	s.RootPath = field.NewString(table, "root_path")
	s.CreatedAt = field.NewString(table, "created_at")
	s.UpdatedAt = field.NewString(table, "updated_at")

	s.fillFieldMap()

	return s
}

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

func (s *sectionLocation) fillFieldMap() {
	s.fieldMap = make(map[string]field.Expr, 5)
	s.fieldMap["id"] = s.ID
	s.fieldMap["library_section_id"] = s.LibrarySectionID
	s.fieldMap["root_path"] = s.RootPath
	s.fieldMap["created_at"] = s.CreatedAt
	s.fieldMap["updated_at"] = s.UpdatedAt
}

func (s sectionLocation) clone(db *gorm.DB) sectionLocation {
	s.sectionLocationDo.ReplaceConnPool(db.Statement.ConnPool)
	return s
}

func (s sectionLocation) replaceDB(db *gorm.DB) sectionLocation {
	s.sectionLocationDo.ReplaceDB(db)
	return s
}

type sectionLocationDo struct{ gen.DO }

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

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

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

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

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

	return
}

func (s sectionLocationDo) Debug() ISectionLocationDo {
	return s.withDO(s.DO.Debug())
}

func (s sectionLocationDo) WithContext(ctx context.Context) ISectionLocationDo {
	return s.withDO(s.DO.WithContext(ctx))
}

func (s sectionLocationDo) ReadDB() ISectionLocationDo {
	return s.Clauses(dbresolver.Read)
}

func (s sectionLocationDo) WriteDB() ISectionLocationDo {
	return s.Clauses(dbresolver.Write)
}

func (s sectionLocationDo) Session(config *gorm.Session) ISectionLocationDo {
	return s.withDO(s.DO.Session(config))
}

func (s sectionLocationDo) Clauses(conds ...clause.Expression) ISectionLocationDo {
	return s.withDO(s.DO.Clauses(conds...))
}

func (s sectionLocationDo) Returning(value interface{}, columns ...string) ISectionLocationDo {
	return s.withDO(s.DO.Returning(value, columns...))
}

func (s sectionLocationDo) Not(conds ...gen.Condition) ISectionLocationDo {
	return s.withDO(s.DO.Not(conds...))
}

func (s sectionLocationDo) Or(conds ...gen.Condition) ISectionLocationDo {
	return s.withDO(s.DO.Or(conds...))
}

func (s sectionLocationDo) Select(conds ...field.Expr) ISectionLocationDo {
	return s.withDO(s.DO.Select(conds...))
}

func (s sectionLocationDo) Where(conds ...gen.Condition) ISectionLocationDo {
	return s.withDO(s.DO.Where(conds...))
}

func (s sectionLocationDo) Order(conds ...field.Expr) ISectionLocationDo {
	return s.withDO(s.DO.Order(conds...))
}

func (s sectionLocationDo) Distinct(cols ...field.Expr) ISectionLocationDo {
	return s.withDO(s.DO.Distinct(cols...))
}

func (s sectionLocationDo) Omit(cols ...field.Expr) ISectionLocationDo {
	return s.withDO(s.DO.Omit(cols...))
}

func (s sectionLocationDo) Join(table schema.Tabler, on ...field.Expr) ISectionLocationDo {
	return s.withDO(s.DO.Join(table, on...))
}

func (s sectionLocationDo) LeftJoin(table schema.Tabler, on ...field.Expr) ISectionLocationDo {
	return s.withDO(s.DO.LeftJoin(table, on...))
}

func (s sectionLocationDo) RightJoin(table schema.Tabler, on ...field.Expr) ISectionLocationDo {
	return s.withDO(s.DO.RightJoin(table, on...))
}

func (s sectionLocationDo) Group(cols ...field.Expr) ISectionLocationDo {
	return s.withDO(s.DO.Group(cols...))
}

func (s sectionLocationDo) Having(conds ...gen.Condition) ISectionLocationDo {
	return s.withDO(s.DO.Having(conds...))
}

func (s sectionLocationDo) Limit(limit int) ISectionLocationDo {
	return s.withDO(s.DO.Limit(limit))
}

func (s sectionLocationDo) Offset(offset int) ISectionLocationDo {
	return s.withDO(s.DO.Offset(offset))
}

func (s sectionLocationDo) Scopes(funcs ...func(gen.Dao) gen.Dao) ISectionLocationDo {
	return s.withDO(s.DO.Scopes(funcs...))
}

func (s sectionLocationDo) Unscoped() ISectionLocationDo {
	return s.withDO(s.DO.Unscoped())
}

func (s sectionLocationDo) Create(values ...*models.SectionLocation) error {
	if len(values) == 0 {
		return nil
	}
	return s.DO.Create(values)
}

func (s sectionLocationDo) CreateInBatches(values []*models.SectionLocation, batchSize int) error {
	return s.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 (s sectionLocationDo) Save(values ...*models.SectionLocation) error {
	if len(values) == 0 {
		return nil
	}
	return s.DO.Save(values)
}

func (s sectionLocationDo) First() (*models.SectionLocation, error) {
	if result, err := s.DO.First(); err != nil {
		return nil, err
	} else {
		return result.(*models.SectionLocation), nil
	}
}

func (s sectionLocationDo) Take() (*models.SectionLocation, error) {
	if result, err := s.DO.Take(); err != nil {
		return nil, err
	} else {
		return result.(*models.SectionLocation), nil
	}
}

func (s sectionLocationDo) Last() (*models.SectionLocation, error) {
	if result, err := s.DO.Last(); err != nil {
		return nil, err
	} else {
		return result.(*models.SectionLocation), nil
	}
}

func (s sectionLocationDo) Find() ([]*models.SectionLocation, error) {
	result, err := s.DO.Find()
	return result.([]*models.SectionLocation), err
}

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

func (s sectionLocationDo) Attrs(attrs ...field.AssignExpr) ISectionLocationDo {
	return s.withDO(s.DO.Attrs(attrs...))
}

func (s sectionLocationDo) Assign(attrs ...field.AssignExpr) ISectionLocationDo {
	return s.withDO(s.DO.Assign(attrs...))
}

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

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

func (s sectionLocationDo) FirstOrInit() (*models.SectionLocation, error) {
	if result, err := s.DO.FirstOrInit(); err != nil {
		return nil, err
	} else {
		return result.(*models.SectionLocation), nil
	}
}

func (s sectionLocationDo) FirstOrCreate() (*models.SectionLocation, error) {
	if result, err := s.DO.FirstOrCreate(); err != nil {
		return nil, err
	} else {
		return result.(*models.SectionLocation), nil
	}
}

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

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

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

func (s sectionLocationDo) Scan(result interface{}) (err error) {
	return s.DO.Scan(result)
}

func (s sectionLocationDo) Delete(models ...*models.SectionLocation) (result gen.ResultInfo, err error) {
	return s.DO.Delete(models)
}

func (s *sectionLocationDo) withDO(do gen.Dao) *sectionLocationDo {
	s.DO = *do.(*gen.DO)
	return s
}
