package query

import (
	"box/dal/model"
	"context"

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

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

func newTblUser(db *gorm.DB) tblUser {
	_tblUser := tblUser{}

	_tblUser.tblUserDo.UseDB(db)
	_tblUser.tblUserDo.UseModel(&model.TblUser{})

	tableName := _tblUser.tblUserDo.TableName()
	_tblUser.ALL = field.NewField(tableName, "*")
	_tblUser.ID = field.NewUint(tableName, "id")
	_tblUser.Mail = field.NewString(tableName, "mail")
	_tblUser.Password = field.NewString(tableName, "password")
	_tblUser.CreatedAt = field.NewTime(tableName, "created_at")
	_tblUser.UpdatedAt = field.NewTime(tableName, "updated_at")
	_tblUser.DeletedAt = field.NewField(tableName, "deleted_at")

	_tblUser.fillFieldMap()

	return _tblUser
}

type tblUser struct {
	tblUserDo tblUserDo

	ALL       field.Field
	ID        field.Uint
	Mail      field.String
	Password  field.String
	CreatedAt field.Time
	UpdatedAt field.Time
	DeletedAt field.Field

	fieldMap map[string]field.Expr
}

func (t tblUser) As(alias string) *tblUser {
	t.tblUserDo.DO = *(t.tblUserDo.As(alias).(*gen.DO))

	t.ALL = field.NewField(alias, "*")
	t.ID = field.NewUint(alias, "id")
	t.Mail = field.NewString(alias, "mail")
	t.Password = field.NewString(alias, "password")
	t.CreatedAt = field.NewTime(alias, "created_at")
	t.UpdatedAt = field.NewTime(alias, "updated_at")
	t.DeletedAt = field.NewField(alias, "deleted_at")

	t.fillFieldMap()

	return &t
}

func (t *tblUser) WithContext(ctx context.Context) *tblUserDo { return t.tblUserDo.WithContext(ctx) }

func (t tblUser) TableName() string { return t.tblUserDo.TableName() }

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

func (t *tblUser) fillFieldMap() {
	t.fieldMap = make(map[string]field.Expr, 6)
	t.fieldMap["id"] = t.ID
	t.fieldMap["mail"] = t.Mail
	t.fieldMap["password"] = t.Password
	t.fieldMap["created_at"] = t.CreatedAt
	t.fieldMap["updated_at"] = t.UpdatedAt
	t.fieldMap["deleted_at"] = t.DeletedAt
}

func (t tblUser) clone(db *gorm.DB) tblUser {
	t.tblUserDo.ReplaceDB(db)
	return t
}

type tblUserDo struct{ gen.DO }

func (t tblUserDo) Debug() *tblUserDo {
	return t.withDO(t.DO.Debug())
}

func (t tblUserDo) WithContext(ctx context.Context) *tblUserDo {
	return t.withDO(t.DO.WithContext(ctx))
}

func (t tblUserDo) Clauses(conds ...clause.Expression) *tblUserDo {
	return t.withDO(t.DO.Clauses(conds...))
}

func (t tblUserDo) Not(conds ...gen.Condition) *tblUserDo {
	return t.withDO(t.DO.Not(conds...))
}

func (t tblUserDo) Or(conds ...gen.Condition) *tblUserDo {
	return t.withDO(t.DO.Or(conds...))
}

func (t tblUserDo) Select(conds ...field.Expr) *tblUserDo {
	return t.withDO(t.DO.Select(conds...))
}

func (t tblUserDo) Where(conds ...gen.Condition) *tblUserDo {
	return t.withDO(t.DO.Where(conds...))
}

func (t tblUserDo) Order(conds ...field.Expr) *tblUserDo {
	return t.withDO(t.DO.Order(conds...))
}

func (t tblUserDo) Distinct(cols ...field.Expr) *tblUserDo {
	return t.withDO(t.DO.Distinct(cols...))
}

func (t tblUserDo) Omit(cols ...field.Expr) *tblUserDo {
	return t.withDO(t.DO.Omit(cols...))
}

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

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

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

func (t tblUserDo) Group(cols ...field.Expr) *tblUserDo {
	return t.withDO(t.DO.Group(cols...))
}

func (t tblUserDo) Having(conds ...gen.Condition) *tblUserDo {
	return t.withDO(t.DO.Having(conds...))
}

func (t tblUserDo) Limit(limit int) *tblUserDo {
	return t.withDO(t.DO.Limit(limit))
}

func (t tblUserDo) Offset(offset int) *tblUserDo {
	return t.withDO(t.DO.Offset(offset))
}

func (t tblUserDo) Scopes(funcs ...func(gen.Dao) gen.Dao) *tblUserDo {
	return t.withDO(t.DO.Scopes(funcs...))
}

func (t tblUserDo) Unscoped() *tblUserDo {
	return t.withDO(t.DO.Unscoped())
}

func (t tblUserDo) Create(values ...*model.TblUser) error {
	if len(values) == 0 {
		return nil
	}
	return t.DO.Create(values)
}

func (t tblUserDo) CreateInBatches(values []*model.TblUser, batchSize int) error {
	return t.DO.CreateInBatches(values, batchSize)
}

func (t tblUserDo) Save(values ...*model.TblUser) error {
	if len(values) == 0 {
		return nil
	}
	return t.DO.Save(values)
}

func (t tblUserDo) First() (*model.TblUser, error) {
	if result, err := t.DO.First(); err != nil {
		return nil, err
	} else {
		return result.(*model.TblUser), nil
	}
}

func (t tblUserDo) Take() (*model.TblUser, error) {
	if result, err := t.DO.Take(); err != nil {
		return nil, err
	} else {
		return result.(*model.TblUser), nil
	}
}

func (t tblUserDo) Last() (*model.TblUser, error) {
	if result, err := t.DO.Last(); err != nil {
		return nil, err
	} else {
		return result.(*model.TblUser), nil
	}
}

func (t tblUserDo) Find() ([]*model.TblUser, error) {
	result, err := t.DO.Find()
	return result.([]*model.TblUser), err
}

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

func (t tblUserDo) Attrs(attrs ...field.AssignExpr) *tblUserDo {
	return t.withDO(t.DO.Attrs(attrs...))
}

func (t tblUserDo) Assign(attrs ...field.AssignExpr) *tblUserDo {
	return t.withDO(t.DO.Assign(attrs...))
}

func (t tblUserDo) Joins(field field.RelationField) *tblUserDo {
	return t.withDO(t.DO.Joins(field))
}

func (t tblUserDo) Preload(field field.RelationField) *tblUserDo {
	return t.withDO(t.DO.Preload(field))
}

func (t tblUserDo) FirstOrInit() (*model.TblUser, error) {
	if result, err := t.DO.FirstOrInit(); err != nil {
		return nil, err
	} else {
		return result.(*model.TblUser), nil
	}
}

func (t tblUserDo) FirstOrCreate() (*model.TblUser, error) {
	if result, err := t.DO.FirstOrCreate(); err != nil {
		return nil, err
	} else {
		return result.(*model.TblUser), nil
	}
}

func (t tblUserDo) FindByPage(offset int, limit int) (result []*model.TblUser, count int64, err error) {
	count, err = t.Count()
	if err != nil {
		return
	}

	if limit <= 0 {
		return
	}

	result, err = t.Offset(offset).Limit(limit).Find()
	return
}

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

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

func (t *tblUserDo) withDO(do gen.Dao) *tblUserDo {
	t.DO = *do.(*gen.DO)
	return t
}
// 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 (
	"box/dal/model"
	"context"

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

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

func newTblUser(db *gorm.DB) tblUser {
	_tblUser := tblUser{}

	_tblUser.tblUserDo.UseDB(db)
	_tblUser.tblUserDo.UseModel(&model.TblUser{})

	tableName := _tblUser.tblUserDo.TableName()
	_tblUser.ALL = field.NewField(tableName, "*")
	_tblUser.ID = field.NewUint(tableName, "id")
	_tblUser.Mail = field.NewString(tableName, "mail")
	_tblUser.Password = field.NewString(tableName, "password")
	_tblUser.CreatedAt = field.NewTime(tableName, "created_at")
	_tblUser.UpdatedAt = field.NewTime(tableName, "updated_at")
	_tblUser.DeletedAt = field.NewField(tableName, "deleted_at")

	_tblUser.fillFieldMap()

	return _tblUser
}

type tblUser struct {
	tblUserDo tblUserDo

	ALL       field.Field
	ID        field.Uint
	Mail      field.String
	Password  field.String
	CreatedAt field.Time
	UpdatedAt field.Time
	DeletedAt field.Field

	fieldMap map[string]field.Expr
}

func (t tblUser) As(alias string) *tblUser {
	t.tblUserDo.DO = *(t.tblUserDo.As(alias).(*gen.DO))

	t.ALL = field.NewField(alias, "*")
	t.ID = field.NewUint(alias, "id")
	t.Mail = field.NewString(alias, "mail")
	t.Password = field.NewString(alias, "password")
	t.CreatedAt = field.NewTime(alias, "created_at")
	t.UpdatedAt = field.NewTime(alias, "updated_at")
	t.DeletedAt = field.NewField(alias, "deleted_at")

	t.fillFieldMap()

	return &t
}

func (t *tblUser) WithContext(ctx context.Context) *tblUserDo { return t.tblUserDo.WithContext(ctx) }

func (t tblUser) TableName() string { return t.tblUserDo.TableName() }

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

func (t *tblUser) fillFieldMap() {
	t.fieldMap = make(map[string]field.Expr, 6)
	t.fieldMap["id"] = t.ID
	t.fieldMap["mail"] = t.Mail
	t.fieldMap["password"] = t.Password
	t.fieldMap["created_at"] = t.CreatedAt
	t.fieldMap["updated_at"] = t.UpdatedAt
	t.fieldMap["deleted_at"] = t.DeletedAt
}

func (t tblUser) clone(db *gorm.DB) tblUser {
	t.tblUserDo.ReplaceDB(db)
	return t
}

type tblUserDo struct{ gen.DO }

func (t tblUserDo) Debug() *tblUserDo {
	return t.withDO(t.DO.Debug())
}

func (t tblUserDo) WithContext(ctx context.Context) *tblUserDo {
	return t.withDO(t.DO.WithContext(ctx))
}

func (t tblUserDo) Clauses(conds ...clause.Expression) *tblUserDo {
	return t.withDO(t.DO.Clauses(conds...))
}

func (t tblUserDo) Not(conds ...gen.Condition) *tblUserDo {
	return t.withDO(t.DO.Not(conds...))
}

func (t tblUserDo) Or(conds ...gen.Condition) *tblUserDo {
	return t.withDO(t.DO.Or(conds...))
}

func (t tblUserDo) Select(conds ...field.Expr) *tblUserDo {
	return t.withDO(t.DO.Select(conds...))
}

func (t tblUserDo) Where(conds ...gen.Condition) *tblUserDo {
	return t.withDO(t.DO.Where(conds...))
}

func (t tblUserDo) Order(conds ...field.Expr) *tblUserDo {
	return t.withDO(t.DO.Order(conds...))
}

func (t tblUserDo) Distinct(cols ...field.Expr) *tblUserDo {
	return t.withDO(t.DO.Distinct(cols...))
}

func (t tblUserDo) Omit(cols ...field.Expr) *tblUserDo {
	return t.withDO(t.DO.Omit(cols...))
}

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

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

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

func (t tblUserDo) Group(cols ...field.Expr) *tblUserDo {
	return t.withDO(t.DO.Group(cols...))
}

func (t tblUserDo) Having(conds ...gen.Condition) *tblUserDo {
	return t.withDO(t.DO.Having(conds...))
}

func (t tblUserDo) Limit(limit int) *tblUserDo {
	return t.withDO(t.DO.Limit(limit))
}

func (t tblUserDo) Offset(offset int) *tblUserDo {
	return t.withDO(t.DO.Offset(offset))
}

func (t tblUserDo) Scopes(funcs ...func(gen.Dao) gen.Dao) *tblUserDo {
	return t.withDO(t.DO.Scopes(funcs...))
}

func (t tblUserDo) Unscoped() *tblUserDo {
	return t.withDO(t.DO.Unscoped())
}

func (t tblUserDo) Create(values ...*model.TblUser) error {
	if len(values) == 0 {
		return nil
	}
	return t.DO.Create(values)
}

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

func (t tblUserDo) First() (*model.TblUser, error) {
	if result, err := t.DO.First(); err != nil {
		return nil, err
	} else {
		return result.(*model.TblUser), nil
	}
}

func (t tblUserDo) Take() (*model.TblUser, error) {
	if result, err := t.DO.Take(); err != nil {
		return nil, err
	} else {
		return result.(*model.TblUser), nil
	}
}

func (t tblUserDo) Last() (*model.TblUser, error) {
	if result, err := t.DO.Last(); err != nil {
		return nil, err
	} else {
		return result.(*model.TblUser), nil
	}
}

func (t tblUserDo) Find() ([]*model.TblUser, error) {
	result, err := t.DO.Find()
	return result.([]*model.TblUser), err
}

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

func (t tblUserDo) Attrs(attrs ...field.AssignExpr) *tblUserDo {
	return t.withDO(t.DO.Attrs(attrs...))
}

func (t tblUserDo) Assign(attrs ...field.AssignExpr) *tblUserDo {
	return t.withDO(t.DO.Assign(attrs...))
}

func (t tblUserDo) Joins(field field.RelationField) *tblUserDo {
	return t.withDO(t.DO.Joins(field))
}

func (t tblUserDo) Preload(field field.RelationField) *tblUserDo {
	return t.withDO(t.DO.Preload(field))
}

func (t tblUserDo) FirstOrInit() (*model.TblUser, error) {
	if result, err := t.DO.FirstOrInit(); err != nil {
		return nil, err
	} else {
		return result.(*model.TblUser), nil
	}
}

func (t tblUserDo) FirstOrCreate() (*model.TblUser, error) {
	if result, err := t.DO.FirstOrCreate(); err != nil {
		return nil, err
	} else {
		return result.(*model.TblUser), nil
	}
}

func (t tblUserDo) FindByPage(offset int, limit int) (result []*model.TblUser, count int64, err error) {
	count, err = t.Count()
	if err != nil {
		return
	}

	if limit <= 0 {
		return
	}

	result, err = t.Offset(offset).Limit(limit).Find()
	return
}

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

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

func (t *tblUserDo) withDO(do gen.Dao) *tblUserDo {
	t.DO = *do.(*gen.DO)
	return t
}
