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

	_payoutAccountQuery.payoutAccountQueryDo.UseDB(db, opts...)
	_payoutAccountQuery.payoutAccountQueryDo.UseModel(&PayoutAccount{})

	tableName := _payoutAccountQuery.payoutAccountQueryDo.TableName()
	_payoutAccountQuery.ALL = field.NewAsterisk(tableName)
	_payoutAccountQuery.ID = field.NewInt64(tableName, "id")
	_payoutAccountQuery.TenantID = field.NewInt64(tableName, "tenant_id")
	_payoutAccountQuery.UserID = field.NewInt64(tableName, "user_id")
	_payoutAccountQuery.Type = field.NewString(tableName, "type")
	_payoutAccountQuery.Name = field.NewString(tableName, "name")
	_payoutAccountQuery.Account = field.NewString(tableName, "account")
	_payoutAccountQuery.Realname = field.NewString(tableName, "realname")
	_payoutAccountQuery.CreatedAt = field.NewTime(tableName, "created_at")
	_payoutAccountQuery.UpdatedAt = field.NewTime(tableName, "updated_at")

	_payoutAccountQuery.fillFieldMap()

	return _payoutAccountQuery
}

type payoutAccountQuery struct {
	payoutAccountQueryDo payoutAccountQueryDo

	ALL       field.Asterisk
	ID        field.Int64
	TenantID  field.Int64
	UserID    field.Int64
	Type      field.String
	Name      field.String
	Account   field.String
	Realname  field.String
	CreatedAt field.Time
	UpdatedAt field.Time

	fieldMap map[string]field.Expr
}

func (p payoutAccountQuery) Table(newTableName string) *payoutAccountQuery {
	p.payoutAccountQueryDo.UseTable(newTableName)
	return p.updateTableName(newTableName)
}

func (p payoutAccountQuery) As(alias string) *payoutAccountQuery {
	p.payoutAccountQueryDo.DO = *(p.payoutAccountQueryDo.As(alias).(*gen.DO))
	return p.updateTableName(alias)
}

func (p *payoutAccountQuery) updateTableName(table string) *payoutAccountQuery {
	p.ALL = field.NewAsterisk(table)
	p.ID = field.NewInt64(table, "id")
	p.TenantID = field.NewInt64(table, "tenant_id")
	p.UserID = field.NewInt64(table, "user_id")
	p.Type = field.NewString(table, "type")
	p.Name = field.NewString(table, "name")
	p.Account = field.NewString(table, "account")
	p.Realname = field.NewString(table, "realname")
	p.CreatedAt = field.NewTime(table, "created_at")
	p.UpdatedAt = field.NewTime(table, "updated_at")

	p.fillFieldMap()

	return p
}

func (p *payoutAccountQuery) QueryContext(ctx context.Context) (*payoutAccountQuery, *payoutAccountQueryDo) {
	return p, p.payoutAccountQueryDo.WithContext(ctx)
}

func (p *payoutAccountQuery) WithContext(ctx context.Context) *payoutAccountQueryDo {
	return p.payoutAccountQueryDo.WithContext(ctx)
}

func (p payoutAccountQuery) TableName() string { return p.payoutAccountQueryDo.TableName() }

func (p payoutAccountQuery) Alias() string { return p.payoutAccountQueryDo.Alias() }

func (p payoutAccountQuery) Columns(cols ...field.Expr) gen.Columns {
	return p.payoutAccountQueryDo.Columns(cols...)
}

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

func (p *payoutAccountQuery) fillFieldMap() {
	p.fieldMap = make(map[string]field.Expr, 9)
	p.fieldMap["id"] = p.ID
	p.fieldMap["tenant_id"] = p.TenantID
	p.fieldMap["user_id"] = p.UserID
	p.fieldMap["type"] = p.Type
	p.fieldMap["name"] = p.Name
	p.fieldMap["account"] = p.Account
	p.fieldMap["realname"] = p.Realname
	p.fieldMap["created_at"] = p.CreatedAt
	p.fieldMap["updated_at"] = p.UpdatedAt
}

func (p payoutAccountQuery) clone(db *gorm.DB) payoutAccountQuery {
	p.payoutAccountQueryDo.ReplaceConnPool(db.Statement.ConnPool)
	return p
}

func (p payoutAccountQuery) replaceDB(db *gorm.DB) payoutAccountQuery {
	p.payoutAccountQueryDo.ReplaceDB(db)
	return p
}

type payoutAccountQueryDo struct{ gen.DO }

func (p payoutAccountQueryDo) Debug() *payoutAccountQueryDo {
	return p.withDO(p.DO.Debug())
}

func (p payoutAccountQueryDo) WithContext(ctx context.Context) *payoutAccountQueryDo {
	return p.withDO(p.DO.WithContext(ctx))
}

func (p payoutAccountQueryDo) ReadDB() *payoutAccountQueryDo {
	return p.Clauses(dbresolver.Read)
}

func (p payoutAccountQueryDo) WriteDB() *payoutAccountQueryDo {
	return p.Clauses(dbresolver.Write)
}

func (p payoutAccountQueryDo) Session(config *gorm.Session) *payoutAccountQueryDo {
	return p.withDO(p.DO.Session(config))
}

func (p payoutAccountQueryDo) Clauses(conds ...clause.Expression) *payoutAccountQueryDo {
	return p.withDO(p.DO.Clauses(conds...))
}

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

func (p payoutAccountQueryDo) Not(conds ...gen.Condition) *payoutAccountQueryDo {
	return p.withDO(p.DO.Not(conds...))
}

func (p payoutAccountQueryDo) Or(conds ...gen.Condition) *payoutAccountQueryDo {
	return p.withDO(p.DO.Or(conds...))
}

func (p payoutAccountQueryDo) Select(conds ...field.Expr) *payoutAccountQueryDo {
	return p.withDO(p.DO.Select(conds...))
}

func (p payoutAccountQueryDo) Where(conds ...gen.Condition) *payoutAccountQueryDo {
	return p.withDO(p.DO.Where(conds...))
}

func (p payoutAccountQueryDo) Order(conds ...field.Expr) *payoutAccountQueryDo {
	return p.withDO(p.DO.Order(conds...))
}

func (p payoutAccountQueryDo) Distinct(cols ...field.Expr) *payoutAccountQueryDo {
	return p.withDO(p.DO.Distinct(cols...))
}

func (p payoutAccountQueryDo) Omit(cols ...field.Expr) *payoutAccountQueryDo {
	return p.withDO(p.DO.Omit(cols...))
}

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

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

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

func (p payoutAccountQueryDo) Group(cols ...field.Expr) *payoutAccountQueryDo {
	return p.withDO(p.DO.Group(cols...))
}

func (p payoutAccountQueryDo) Having(conds ...gen.Condition) *payoutAccountQueryDo {
	return p.withDO(p.DO.Having(conds...))
}

func (p payoutAccountQueryDo) Limit(limit int) *payoutAccountQueryDo {
	return p.withDO(p.DO.Limit(limit))
}

func (p payoutAccountQueryDo) Offset(offset int) *payoutAccountQueryDo {
	return p.withDO(p.DO.Offset(offset))
}

func (p payoutAccountQueryDo) Scopes(funcs ...func(gen.Dao) gen.Dao) *payoutAccountQueryDo {
	return p.withDO(p.DO.Scopes(funcs...))
}

func (p payoutAccountQueryDo) Unscoped() *payoutAccountQueryDo {
	return p.withDO(p.DO.Unscoped())
}

func (p payoutAccountQueryDo) Create(values ...*PayoutAccount) error {
	if len(values) == 0 {
		return nil
	}
	return p.DO.Create(values)
}

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

func (p payoutAccountQueryDo) First() (*PayoutAccount, error) {
	if result, err := p.DO.First(); err != nil {
		return nil, err
	} else {
		return result.(*PayoutAccount), nil
	}
}

func (p payoutAccountQueryDo) Take() (*PayoutAccount, error) {
	if result, err := p.DO.Take(); err != nil {
		return nil, err
	} else {
		return result.(*PayoutAccount), nil
	}
}

func (p payoutAccountQueryDo) Last() (*PayoutAccount, error) {
	if result, err := p.DO.Last(); err != nil {
		return nil, err
	} else {
		return result.(*PayoutAccount), nil
	}
}

func (p payoutAccountQueryDo) Find() ([]*PayoutAccount, error) {
	result, err := p.DO.Find()
	return result.([]*PayoutAccount), err
}

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

func (p payoutAccountQueryDo) Attrs(attrs ...field.AssignExpr) *payoutAccountQueryDo {
	return p.withDO(p.DO.Attrs(attrs...))
}

func (p payoutAccountQueryDo) Assign(attrs ...field.AssignExpr) *payoutAccountQueryDo {
	return p.withDO(p.DO.Assign(attrs...))
}

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

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

func (p payoutAccountQueryDo) FirstOrInit() (*PayoutAccount, error) {
	if result, err := p.DO.FirstOrInit(); err != nil {
		return nil, err
	} else {
		return result.(*PayoutAccount), nil
	}
}

func (p payoutAccountQueryDo) FirstOrCreate() (*PayoutAccount, error) {
	if result, err := p.DO.FirstOrCreate(); err != nil {
		return nil, err
	} else {
		return result.(*PayoutAccount), nil
	}
}

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

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

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

func (p payoutAccountQueryDo) Scan(result interface{}) (err error) {
	return p.DO.Scan(result)
}

func (p payoutAccountQueryDo) Delete(models ...*PayoutAccount) (result gen.ResultInfo, err error) {
	return p.DO.Delete(models)
}

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

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

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

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

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

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

// Max returns MAX(column) for current scope.
func (p payoutAccountQueryDo) Max(column field.Expr) (float64, error) {
	var _v float64
	agg := field.NewUnsafeFieldRaw("MAX(?)", column.RawExpr())
	if err := p.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 (p payoutAccountQueryDo) PluckMap(key, val field.Expr) (map[interface{}]interface{}, error) {
	do := p.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 (p payoutAccountQueryDo) Exists(conds ...gen.Condition) (bool, error) {
	cnt, err := p.Where(conds...).Count()
	if err != nil {
		return false, err
	}
	return cnt > 0, nil
}

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

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

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

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

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

func (p *payoutAccountQueryDo) withDO(do gen.Dao) *payoutAccountQueryDo {
	p.DO = *do.(*gen.DO)
	return p
}
