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

	_tenantJoinRequestQuery.tenantJoinRequestQueryDo.UseDB(db, opts...)
	_tenantJoinRequestQuery.tenantJoinRequestQueryDo.UseModel(&TenantJoinRequest{})

	tableName := _tenantJoinRequestQuery.tenantJoinRequestQueryDo.TableName()
	_tenantJoinRequestQuery.ALL = field.NewAsterisk(tableName)
	_tenantJoinRequestQuery.ID = field.NewInt64(tableName, "id")
	_tenantJoinRequestQuery.TenantID = field.NewInt64(tableName, "tenant_id")
	_tenantJoinRequestQuery.UserID = field.NewInt64(tableName, "user_id")
	_tenantJoinRequestQuery.Status = field.NewString(tableName, "status")
	_tenantJoinRequestQuery.Reason = field.NewString(tableName, "reason")
	_tenantJoinRequestQuery.DecidedAt = field.NewTime(tableName, "decided_at")
	_tenantJoinRequestQuery.DecidedOperatorUserID = field.NewInt64(tableName, "decided_operator_user_id")
	_tenantJoinRequestQuery.DecidedReason = field.NewString(tableName, "decided_reason")
	_tenantJoinRequestQuery.CreatedAt = field.NewTime(tableName, "created_at")
	_tenantJoinRequestQuery.UpdatedAt = field.NewTime(tableName, "updated_at")

	_tenantJoinRequestQuery.fillFieldMap()

	return _tenantJoinRequestQuery
}

type tenantJoinRequestQuery struct {
	tenantJoinRequestQueryDo tenantJoinRequestQueryDo

	ALL                   field.Asterisk
	ID                    field.Int64
	TenantID              field.Int64
	UserID                field.Int64
	Status                field.String
	Reason                field.String
	DecidedAt             field.Time
	DecidedOperatorUserID field.Int64
	DecidedReason         field.String
	CreatedAt             field.Time
	UpdatedAt             field.Time

	fieldMap map[string]field.Expr
}

func (t tenantJoinRequestQuery) Table(newTableName string) *tenantJoinRequestQuery {
	t.tenantJoinRequestQueryDo.UseTable(newTableName)
	return t.updateTableName(newTableName)
}

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

func (t *tenantJoinRequestQuery) updateTableName(table string) *tenantJoinRequestQuery {
	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.Status = field.NewString(table, "status")
	t.Reason = field.NewString(table, "reason")
	t.DecidedAt = field.NewTime(table, "decided_at")
	t.DecidedOperatorUserID = field.NewInt64(table, "decided_operator_user_id")
	t.DecidedReason = field.NewString(table, "decided_reason")
	t.CreatedAt = field.NewTime(table, "created_at")
	t.UpdatedAt = field.NewTime(table, "updated_at")

	t.fillFieldMap()

	return t
}

func (t *tenantJoinRequestQuery) QueryContext(ctx context.Context) (*tenantJoinRequestQuery, *tenantJoinRequestQueryDo) {
	return t, t.tenantJoinRequestQueryDo.WithContext(ctx)
}

func (t *tenantJoinRequestQuery) WithContext(ctx context.Context) *tenantJoinRequestQueryDo {
	return t.tenantJoinRequestQueryDo.WithContext(ctx)
}

func (t tenantJoinRequestQuery) TableName() string { return t.tenantJoinRequestQueryDo.TableName() }

func (t tenantJoinRequestQuery) Alias() string { return t.tenantJoinRequestQueryDo.Alias() }

func (t tenantJoinRequestQuery) Columns(cols ...field.Expr) gen.Columns {
	return t.tenantJoinRequestQueryDo.Columns(cols...)
}

func (t *tenantJoinRequestQuery) 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 *tenantJoinRequestQuery) fillFieldMap() {
	t.fieldMap = make(map[string]field.Expr, 10)
	t.fieldMap["id"] = t.ID
	t.fieldMap["tenant_id"] = t.TenantID
	t.fieldMap["user_id"] = t.UserID
	t.fieldMap["status"] = t.Status
	t.fieldMap["reason"] = t.Reason
	t.fieldMap["decided_at"] = t.DecidedAt
	t.fieldMap["decided_operator_user_id"] = t.DecidedOperatorUserID
	t.fieldMap["decided_reason"] = t.DecidedReason
	t.fieldMap["created_at"] = t.CreatedAt
	t.fieldMap["updated_at"] = t.UpdatedAt
}

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

func (t tenantJoinRequestQuery) replaceDB(db *gorm.DB) tenantJoinRequestQuery {
	t.tenantJoinRequestQueryDo.ReplaceDB(db)
	return t
}

type tenantJoinRequestQueryDo struct{ gen.DO }

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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