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

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

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

	"gorm.io/plugin/dbresolver"

	"git.kocoder.xyz/kocoded/vt/model"
)

func newLager(db *gorm.DB, opts ...gen.DOOption) lager {
	_lager := lager{}

	_lager.lagerDo.UseDB(db, opts...)
	_lager.lagerDo.UseModel(&model.Lager{})

	tableName := _lager.lagerDo.TableName()
	_lager.ALL = field.NewAsterisk(tableName)
	_lager.ID = field.NewUint(tableName, "id")
	_lager.CreatedAt = field.NewTime(tableName, "created_at")
	_lager.UpdatedAt = field.NewTime(tableName, "updated_at")
	_lager.DeletedAt = field.NewField(tableName, "deleted_at")

	_lager.fillFieldMap()

	return _lager
}

type lager struct {
	lagerDo

	ALL       field.Asterisk
	ID        field.Uint
	CreatedAt field.Time
	UpdatedAt field.Time
	DeletedAt field.Field

	fieldMap map[string]field.Expr
}

func (l lager) Table(newTableName string) *lager {
	l.lagerDo.UseTable(newTableName)
	return l.updateTableName(newTableName)
}

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

func (l *lager) updateTableName(table string) *lager {
	l.ALL = field.NewAsterisk(table)
	l.ID = field.NewUint(table, "id")
	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 *lager) 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 *lager) fillFieldMap() {
	l.fieldMap = make(map[string]field.Expr, 4)
	l.fieldMap["id"] = l.ID
	l.fieldMap["created_at"] = l.CreatedAt
	l.fieldMap["updated_at"] = l.UpdatedAt
	l.fieldMap["deleted_at"] = l.DeletedAt
}

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

func (l lager) replaceDB(db *gorm.DB) lager {
	l.lagerDo.ReplaceDB(db)
	return l
}

type lagerDo struct{ gen.DO }

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

func (l lagerDo) Debug() ILagerDo {
	return l.withDO(l.DO.Debug())
}

func (l lagerDo) WithContext(ctx context.Context) ILagerDo {
	return l.withDO(l.DO.WithContext(ctx))
}

func (l lagerDo) ReadDB() ILagerDo {
	return l.Clauses(dbresolver.Read)
}

func (l lagerDo) WriteDB() ILagerDo {
	return l.Clauses(dbresolver.Write)
}

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

func (l lagerDo) Clauses(conds ...clause.Expression) ILagerDo {
	return l.withDO(l.DO.Clauses(conds...))
}

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

func (l lagerDo) Not(conds ...gen.Condition) ILagerDo {
	return l.withDO(l.DO.Not(conds...))
}

func (l lagerDo) Or(conds ...gen.Condition) ILagerDo {
	return l.withDO(l.DO.Or(conds...))
}

func (l lagerDo) Select(conds ...field.Expr) ILagerDo {
	return l.withDO(l.DO.Select(conds...))
}

func (l lagerDo) Where(conds ...gen.Condition) ILagerDo {
	return l.withDO(l.DO.Where(conds...))
}

func (l lagerDo) Order(conds ...field.Expr) ILagerDo {
	return l.withDO(l.DO.Order(conds...))
}

func (l lagerDo) Distinct(cols ...field.Expr) ILagerDo {
	return l.withDO(l.DO.Distinct(cols...))
}

func (l lagerDo) Omit(cols ...field.Expr) ILagerDo {
	return l.withDO(l.DO.Omit(cols...))
}

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

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

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

func (l lagerDo) Group(cols ...field.Expr) ILagerDo {
	return l.withDO(l.DO.Group(cols...))
}

func (l lagerDo) Having(conds ...gen.Condition) ILagerDo {
	return l.withDO(l.DO.Having(conds...))
}

func (l lagerDo) Limit(limit int) ILagerDo {
	return l.withDO(l.DO.Limit(limit))
}

func (l lagerDo) Offset(offset int) ILagerDo {
	return l.withDO(l.DO.Offset(offset))
}

func (l lagerDo) Scopes(funcs ...func(gen.Dao) gen.Dao) ILagerDo {
	return l.withDO(l.DO.Scopes(funcs...))
}

func (l lagerDo) Unscoped() ILagerDo {
	return l.withDO(l.DO.Unscoped())
}

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

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

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

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

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

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

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

func (l lagerDo) Attrs(attrs ...field.AssignExpr) ILagerDo {
	return l.withDO(l.DO.Attrs(attrs...))
}

func (l lagerDo) Assign(attrs ...field.AssignExpr) ILagerDo {
	return l.withDO(l.DO.Assign(attrs...))
}

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

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

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

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

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

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

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