// Code generated by go.ipao.vip/gen. DO NOT EDIT.
// Code generated by go.ipao.vip/gen. DO NOT EDIT.
// Code generated by go.ipao.vip/gen. DO NOT EDIT.

package models

import (
	"context"

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

	"go.ipao.vip/gen"
	"go.ipao.vip/gen/field"

	"gorm.io/plugin/dbresolver"
)

func newTenantUser(db *gorm.DB, opts ...gen.DOOption) tenantUserQuery {
	_tenantUserQuery := tenantUserQuery{}

	_tenantUserQuery.tenantUserQueryDo.UseDB(db, opts...)
	_tenantUserQuery.tenantUserQueryDo.UseModel(&TenantUser{})

	tableName := _tenantUserQuery.tenantUserQueryDo.TableName()
	_tenantUserQuery.ALL = field.NewAsterisk(tableName)
	_tenantUserQuery.ID = field.NewInt64(tableName, "id")
	_tenantUserQuery.TenantID = field.NewInt64(tableName, "tenant_id")
	_tenantUserQuery.UserID = field.NewInt64(tableName, "user_id")
	_tenantUserQuery.Role = field.NewArray(tableName, "role")
	_tenantUserQuery.Status = field.NewField(tableName, "status")
	_tenantUserQuery.CreatedAt = field.NewTime(tableName, "created_at")
	_tenantUserQuery.UpdatedAt = field.NewTime(tableName, "updated_at")

	_tenantUserQuery.fillFieldMap()

	return _tenantUserQuery
}

type tenantUserQuery struct {
	tenantUserQueryDo tenantUserQueryDo

	ALL       field.Asterisk
	ID        field.Int64
	TenantID  field.Int64
	UserID    field.Int64
	Role      field.Array
	Status    field.Field
	CreatedAt field.Time
	UpdatedAt field.Time

	fieldMap map[string]field.Expr
}

func (t tenantUserQuery) Table(newTableName string) *tenantUserQuery {
	t.tenantUserQueryDo.UseTable(newTableName)
	return t.updateTableName(newTableName)
}

func (t tenantUserQuery) As(alias string) *tenantUserQuery {
	t.tenantUserQueryDo.DO = *(t.tenantUserQueryDo.As(alias).(*gen.DO))
	return t.updateTableName(alias)
}

func (t *tenantUserQuery) updateTableName(table string) *tenantUserQuery {
	t.ALL = field.NewAsterisk(table)
	t.ID = field.NewInt64(table, "id")
	t.TenantID = field.NewInt64(table, "tenant_id")
	t.UserID = field.NewInt64(table, "user_id")
	t.Role = field.NewArray(table, "role")
	t.Status = field.NewField(table, "status")
	t.CreatedAt = field.NewTime(table, "created_at")
	t.UpdatedAt = field.NewTime(table, "updated_at")

	t.fillFieldMap()

	return t
}

func (t *tenantUserQuery) QueryContext(ctx context.Context) (*tenantUserQuery, *tenantUserQueryDo) {
	return t, t.tenantUserQueryDo.WithContext(ctx)
}

func (t *tenantUserQuery) WithContext(ctx context.Context) *tenantUserQueryDo {
	return t.tenantUserQueryDo.WithContext(ctx)
}

func (t tenantUserQuery) TableName() string { return t.tenantUserQueryDo.TableName() }

func (t tenantUserQuery) Alias() string { return t.tenantUserQueryDo.Alias() }

func (t tenantUserQuery) Columns(cols ...field.Expr) gen.Columns {
	return t.tenantUserQueryDo.Columns(cols...)
}

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

func (t *tenantUserQuery) fillFieldMap() {
	t.fieldMap = make(map[string]field.Expr, 7)
	t.fieldMap["id"] = t.ID
	t.fieldMap["tenant_id"] = t.TenantID
	t.fieldMap["user_id"] = t.UserID
	t.fieldMap["role"] = t.Role
	t.fieldMap["status"] = t.Status
	t.fieldMap["created_at"] = t.CreatedAt
	t.fieldMap["updated_at"] = t.UpdatedAt
}

func (t tenantUserQuery) clone(db *gorm.DB) tenantUserQuery {
	t.tenantUserQueryDo.ReplaceConnPool(db.Statement.ConnPool)
	return t
}

func (t tenantUserQuery) replaceDB(db *gorm.DB) tenantUserQuery {
	t.tenantUserQueryDo.ReplaceDB(db)
	return t
}

type tenantUserQueryDo struct{ gen.DO }

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

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

func (t tenantUserQueryDo) ReadDB() *tenantUserQueryDo {
	return t.Clauses(dbresolver.Read)
}

func (t tenantUserQueryDo) WriteDB() *tenantUserQueryDo {
	return t.Clauses(dbresolver.Write)
}

func (t tenantUserQueryDo) Session(config *gorm.Session) *tenantUserQueryDo {
	return t.withDO(t.DO.Session(config))
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

func (t tenantUserQueryDo) Create(values ...*TenantUser) error {
	if len(values) == 0 {
		return nil
	}
	return t.DO.Create(values)
}

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

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

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

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

func (t tenantUserQueryDo) Find() ([]*TenantUser, error) {
	result, err := t.DO.Find()
	return result.([]*TenantUser), err
}

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

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

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

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

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

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

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

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

func (t tenantUserQueryDo) 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 tenantUserQueryDo) Scan(result interface{}) (err error) {
	return t.DO.Scan(result)
}

func (t tenantUserQueryDo) Delete(models ...*TenantUser) (result gen.ResultInfo, err error) {
	return t.DO.Delete(models)
}

// ForceDelete performs a permanent delete (ignores soft-delete) for current scope.
func (t tenantUserQueryDo) ForceDelete() (gen.ResultInfo, error) {
	return t.Unscoped().Delete()
}

// Inc increases the given column by step for current scope.
func (t tenantUserQueryDo) Inc(column field.Expr, step int64) (gen.ResultInfo, error) {
	// column = column + step
	e := field.NewUnsafeFieldRaw("?+?", column.RawExpr(), step)
	return t.DO.UpdateColumn(column, e)
}

// Dec decreases the given column by step for current scope.
func (t tenantUserQueryDo) Dec(column field.Expr, step int64) (gen.ResultInfo, error) {
	// column = column - step
	e := field.NewUnsafeFieldRaw("?-?", column.RawExpr(), step)
	return t.DO.UpdateColumn(column, e)
}

// Sum returns SUM(column) for current scope.
func (t tenantUserQueryDo) Sum(column field.Expr) (float64, error) {
	var _v float64
	agg := field.NewUnsafeFieldRaw("SUM(?)", column.RawExpr())
	if err := t.Select(agg).Scan(&_v); err != nil {
		return 0, err
	}
	return _v, nil
}

// Avg returns AVG(column) for current scope.
func (t tenantUserQueryDo) Avg(column field.Expr) (float64, error) {
	var _v float64
	agg := field.NewUnsafeFieldRaw("AVG(?)", column.RawExpr())
	if err := t.Select(agg).Scan(&_v); err != nil {
		return 0, err
	}
	return _v, nil
}

// Min returns MIN(column) for current scope.
func (t tenantUserQueryDo) Min(column field.Expr) (float64, error) {
	var _v float64
	agg := field.NewUnsafeFieldRaw("MIN(?)", column.RawExpr())
	if err := t.Select(agg).Scan(&_v); err != nil {
		return 0, err
	}
	return _v, nil
}

// Max returns MAX(column) for current scope.
func (t tenantUserQueryDo) Max(column field.Expr) (float64, error) {
	var _v float64
	agg := field.NewUnsafeFieldRaw("MAX(?)", column.RawExpr())
	if err := t.Select(agg).Scan(&_v); err != nil {
		return 0, err
	}
	return _v, nil
}

// PluckMap returns a map[key]value for selected key/value expressions within current scope.
func (t tenantUserQueryDo) PluckMap(key, val field.Expr) (map[interface{}]interface{}, error) {
	do := t.Select(key, val)
	rows, err := do.DO.Rows()
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	mm := make(map[interface{}]interface{})
	for rows.Next() {
		var k interface{}
		var v interface{}
		if err := rows.Scan(&k, &v); err != nil {
			return nil, err
		}
		mm[k] = v
	}
	return mm, rows.Err()
}

// Exists returns true if any record matches the given conditions.
func (t tenantUserQueryDo) Exists(conds ...gen.Condition) (bool, error) {
	cnt, err := t.Where(conds...).Count()
	if err != nil {
		return false, err
	}
	return cnt > 0, nil
}

// PluckIDs returns all primary key values under current scope.
func (t tenantUserQueryDo) PluckIDs() ([]int64, error) {
	ids := make([]int64, 0, 16)
	pk := field.NewInt64(t.TableName(), "id")
	if err := t.DO.Pluck(pk, &ids); err != nil {
		return nil, err
	}
	return ids, nil
}

// GetByID finds a single record by primary key.
func (t tenantUserQueryDo) GetByID(id int64) (*TenantUser, error) {
	pk := field.NewInt64(t.TableName(), "id")
	return t.Where(pk.Eq(id)).First()
}

// GetByIDs finds records by primary key list.
func (t tenantUserQueryDo) GetByIDs(ids ...int64) ([]*TenantUser, error) {
	if len(ids) == 0 {
		return []*TenantUser{}, nil
	}
	pk := field.NewInt64(t.TableName(), "id")
	return t.Where(pk.In(ids...)).Find()
}

// DeleteByID deletes records by primary key.
func (t tenantUserQueryDo) DeleteByID(id int64) (gen.ResultInfo, error) {
	pk := field.NewInt64(t.TableName(), "id")
	return t.Where(pk.Eq(id)).Delete()
}

// DeleteByIDs deletes records by a list of primary keys.
func (t tenantUserQueryDo) DeleteByIDs(ids ...int64) (gen.ResultInfo, error) {
	if len(ids) == 0 {
		return gen.ResultInfo{RowsAffected: 0, Error: nil}, nil
	}
	pk := field.NewInt64(t.TableName(), "id")
	return t.Where(pk.In(ids...)).Delete()
}

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