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

	_tenantLedgerQuery.tenantLedgerQueryDo.UseDB(db, opts...)
	_tenantLedgerQuery.tenantLedgerQueryDo.UseModel(&TenantLedger{})

	tableName := _tenantLedgerQuery.tenantLedgerQueryDo.TableName()
	_tenantLedgerQuery.ALL = field.NewAsterisk(tableName)
	_tenantLedgerQuery.ID = field.NewInt64(tableName, "id")
	_tenantLedgerQuery.TenantID = field.NewInt64(tableName, "tenant_id")
	_tenantLedgerQuery.UserID = field.NewInt64(tableName, "user_id")
	_tenantLedgerQuery.OrderID = field.NewInt64(tableName, "order_id")
	_tenantLedgerQuery.Type = field.NewField(tableName, "type")
	_tenantLedgerQuery.Amount = field.NewInt64(tableName, "amount")
	_tenantLedgerQuery.BalanceBefore = field.NewInt64(tableName, "balance_before")
	_tenantLedgerQuery.BalanceAfter = field.NewInt64(tableName, "balance_after")
	_tenantLedgerQuery.FrozenBefore = field.NewInt64(tableName, "frozen_before")
	_tenantLedgerQuery.FrozenAfter = field.NewInt64(tableName, "frozen_after")
	_tenantLedgerQuery.IdempotencyKey = field.NewString(tableName, "idempotency_key")
	_tenantLedgerQuery.Remark = field.NewString(tableName, "remark")
	_tenantLedgerQuery.OperatorUserID = field.NewInt64(tableName, "operator_user_id")
	_tenantLedgerQuery.BizRefType = field.NewString(tableName, "biz_ref_type")
	_tenantLedgerQuery.BizRefID = field.NewInt64(tableName, "biz_ref_id")
	_tenantLedgerQuery.CreatedAt = field.NewTime(tableName, "created_at")
	_tenantLedgerQuery.UpdatedAt = field.NewTime(tableName, "updated_at")

	_tenantLedgerQuery.fillFieldMap()

	return _tenantLedgerQuery
}

type tenantLedgerQuery struct {
	tenantLedgerQueryDo tenantLedgerQueryDo

	ALL            field.Asterisk
	ID             field.Int64
	TenantID       field.Int64
	UserID         field.Int64
	OrderID        field.Int64
	Type           field.Field
	Amount         field.Int64
	BalanceBefore  field.Int64
	BalanceAfter   field.Int64
	FrozenBefore   field.Int64
	FrozenAfter    field.Int64
	IdempotencyKey field.String
	Remark         field.String
	OperatorUserID field.Int64
	BizRefType     field.String
	BizRefID       field.Int64
	CreatedAt      field.Time
	UpdatedAt      field.Time

	fieldMap map[string]field.Expr
}

func (t tenantLedgerQuery) Table(newTableName string) *tenantLedgerQuery {
	t.tenantLedgerQueryDo.UseTable(newTableName)
	return t.updateTableName(newTableName)
}

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

func (t *tenantLedgerQuery) updateTableName(table string) *tenantLedgerQuery {
	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.OrderID = field.NewInt64(table, "order_id")
	t.Type = field.NewField(table, "type")
	t.Amount = field.NewInt64(table, "amount")
	t.BalanceBefore = field.NewInt64(table, "balance_before")
	t.BalanceAfter = field.NewInt64(table, "balance_after")
	t.FrozenBefore = field.NewInt64(table, "frozen_before")
	t.FrozenAfter = field.NewInt64(table, "frozen_after")
	t.IdempotencyKey = field.NewString(table, "idempotency_key")
	t.Remark = field.NewString(table, "remark")
	t.OperatorUserID = field.NewInt64(table, "operator_user_id")
	t.BizRefType = field.NewString(table, "biz_ref_type")
	t.BizRefID = field.NewInt64(table, "biz_ref_id")
	t.CreatedAt = field.NewTime(table, "created_at")
	t.UpdatedAt = field.NewTime(table, "updated_at")

	t.fillFieldMap()

	return t
}

func (t *tenantLedgerQuery) QueryContext(ctx context.Context) (*tenantLedgerQuery, *tenantLedgerQueryDo) {
	return t, t.tenantLedgerQueryDo.WithContext(ctx)
}

func (t *tenantLedgerQuery) WithContext(ctx context.Context) *tenantLedgerQueryDo {
	return t.tenantLedgerQueryDo.WithContext(ctx)
}

func (t tenantLedgerQuery) TableName() string { return t.tenantLedgerQueryDo.TableName() }

func (t tenantLedgerQuery) Alias() string { return t.tenantLedgerQueryDo.Alias() }

func (t tenantLedgerQuery) Columns(cols ...field.Expr) gen.Columns {
	return t.tenantLedgerQueryDo.Columns(cols...)
}

func (t *tenantLedgerQuery) 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 *tenantLedgerQuery) fillFieldMap() {
	t.fieldMap = make(map[string]field.Expr, 17)
	t.fieldMap["id"] = t.ID
	t.fieldMap["tenant_id"] = t.TenantID
	t.fieldMap["user_id"] = t.UserID
	t.fieldMap["order_id"] = t.OrderID
	t.fieldMap["type"] = t.Type
	t.fieldMap["amount"] = t.Amount
	t.fieldMap["balance_before"] = t.BalanceBefore
	t.fieldMap["balance_after"] = t.BalanceAfter
	t.fieldMap["frozen_before"] = t.FrozenBefore
	t.fieldMap["frozen_after"] = t.FrozenAfter
	t.fieldMap["idempotency_key"] = t.IdempotencyKey
	t.fieldMap["remark"] = t.Remark
	t.fieldMap["operator_user_id"] = t.OperatorUserID
	t.fieldMap["biz_ref_type"] = t.BizRefType
	t.fieldMap["biz_ref_id"] = t.BizRefID
	t.fieldMap["created_at"] = t.CreatedAt
	t.fieldMap["updated_at"] = t.UpdatedAt
}

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

func (t tenantLedgerQuery) replaceDB(db *gorm.DB) tenantLedgerQuery {
	t.tenantLedgerQueryDo.ReplaceDB(db)
	return t
}

type tenantLedgerQueryDo struct{ gen.DO }

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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