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

	_tenantInviteQuery.tenantInviteQueryDo.UseDB(db, opts...)
	_tenantInviteQuery.tenantInviteQueryDo.UseModel(&TenantInvite{})

	tableName := _tenantInviteQuery.tenantInviteQueryDo.TableName()
	_tenantInviteQuery.ALL = field.NewAsterisk(tableName)
	_tenantInviteQuery.ID = field.NewInt64(tableName, "id")
	_tenantInviteQuery.TenantID = field.NewInt64(tableName, "tenant_id")
	_tenantInviteQuery.UserID = field.NewInt64(tableName, "user_id")
	_tenantInviteQuery.Code = field.NewString(tableName, "code")
	_tenantInviteQuery.Status = field.NewString(tableName, "status")
	_tenantInviteQuery.MaxUses = field.NewInt32(tableName, "max_uses")
	_tenantInviteQuery.UsedCount = field.NewInt32(tableName, "used_count")
	_tenantInviteQuery.ExpiresAt = field.NewTime(tableName, "expires_at")
	_tenantInviteQuery.DisabledAt = field.NewTime(tableName, "disabled_at")
	_tenantInviteQuery.DisabledOperatorUserID = field.NewInt64(tableName, "disabled_operator_user_id")
	_tenantInviteQuery.Remark = field.NewString(tableName, "remark")
	_tenantInviteQuery.CreatedAt = field.NewTime(tableName, "created_at")
	_tenantInviteQuery.UpdatedAt = field.NewTime(tableName, "updated_at")

	_tenantInviteQuery.fillFieldMap()

	return _tenantInviteQuery
}

type tenantInviteQuery struct {
	tenantInviteQueryDo tenantInviteQueryDo

	ALL                    field.Asterisk
	ID                     field.Int64
	TenantID               field.Int64
	UserID                 field.Int64
	Code                   field.String
	Status                 field.String
	MaxUses                field.Int32
	UsedCount              field.Int32
	ExpiresAt              field.Time
	DisabledAt             field.Time
	DisabledOperatorUserID field.Int64
	Remark                 field.String
	CreatedAt              field.Time
	UpdatedAt              field.Time

	fieldMap map[string]field.Expr
}

func (t tenantInviteQuery) Table(newTableName string) *tenantInviteQuery {
	t.tenantInviteQueryDo.UseTable(newTableName)
	return t.updateTableName(newTableName)
}

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

func (t *tenantInviteQuery) updateTableName(table string) *tenantInviteQuery {
	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.Code = field.NewString(table, "code")
	t.Status = field.NewString(table, "status")
	t.MaxUses = field.NewInt32(table, "max_uses")
	t.UsedCount = field.NewInt32(table, "used_count")
	t.ExpiresAt = field.NewTime(table, "expires_at")
	t.DisabledAt = field.NewTime(table, "disabled_at")
	t.DisabledOperatorUserID = field.NewInt64(table, "disabled_operator_user_id")
	t.Remark = field.NewString(table, "remark")
	t.CreatedAt = field.NewTime(table, "created_at")
	t.UpdatedAt = field.NewTime(table, "updated_at")

	t.fillFieldMap()

	return t
}

func (t *tenantInviteQuery) QueryContext(ctx context.Context) (*tenantInviteQuery, *tenantInviteQueryDo) {
	return t, t.tenantInviteQueryDo.WithContext(ctx)
}

func (t *tenantInviteQuery) WithContext(ctx context.Context) *tenantInviteQueryDo {
	return t.tenantInviteQueryDo.WithContext(ctx)
}

func (t tenantInviteQuery) TableName() string { return t.tenantInviteQueryDo.TableName() }

func (t tenantInviteQuery) Alias() string { return t.tenantInviteQueryDo.Alias() }

func (t tenantInviteQuery) Columns(cols ...field.Expr) gen.Columns {
	return t.tenantInviteQueryDo.Columns(cols...)
}

func (t *tenantInviteQuery) 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 *tenantInviteQuery) fillFieldMap() {
	t.fieldMap = make(map[string]field.Expr, 13)
	t.fieldMap["id"] = t.ID
	t.fieldMap["tenant_id"] = t.TenantID
	t.fieldMap["user_id"] = t.UserID
	t.fieldMap["code"] = t.Code
	t.fieldMap["status"] = t.Status
	t.fieldMap["max_uses"] = t.MaxUses
	t.fieldMap["used_count"] = t.UsedCount
	t.fieldMap["expires_at"] = t.ExpiresAt
	t.fieldMap["disabled_at"] = t.DisabledAt
	t.fieldMap["disabled_operator_user_id"] = t.DisabledOperatorUserID
	t.fieldMap["remark"] = t.Remark
	t.fieldMap["created_at"] = t.CreatedAt
	t.fieldMap["updated_at"] = t.UpdatedAt
}

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

func (t tenantInviteQuery) replaceDB(db *gorm.DB) tenantInviteQuery {
	t.tenantInviteQueryDo.ReplaceDB(db)
	return t
}

type tenantInviteQueryDo struct{ gen.DO }

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// Inc increases the given column by step for current scope.
func (t tenantInviteQueryDo) 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 tenantInviteQueryDo) 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 tenantInviteQueryDo) 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 tenantInviteQueryDo) 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 tenantInviteQueryDo) 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 tenantInviteQueryDo) 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 tenantInviteQueryDo) 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 tenantInviteQueryDo) 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 tenantInviteQueryDo) 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 tenantInviteQueryDo) GetByID(id int64) (*TenantInvite, error) {
	pk := field.NewInt64(t.TableName(), "id")
	return t.Where(pk.Eq(id)).First()
}

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

// DeleteByID deletes records by primary key.
func (t tenantInviteQueryDo) 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 tenantInviteQueryDo) 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 *tenantInviteQueryDo) withDO(do gen.Dao) *tenantInviteQueryDo {
	t.DO = *do.(*gen.DO)
	return t
}
