// Code generated by gorm.io/gen. DO NOT EDIT.
// Code generated by gorm.io/gen. DO NOT EDIT.
// Code generated by gorm.io/gen. DO NOT EDIT.

package query

import (
	"context"

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

	"gorm.io/gen"
	"gorm.io/gen/field"

	"gorm.io/plugin/dbresolver"

	"web3/app/service/dex-finance/storage/mysql/model"
)

func newAccountTrade(db *gorm.DB, opts ...gen.DOOption) accountTrade {
	_accountTrade := accountTrade{}

	_accountTrade.accountTradeDo.UseDB(db, opts...)
	_accountTrade.accountTradeDo.UseModel(&model.AccountTrade{})

	tableName := _accountTrade.accountTradeDo.TableName()
	_accountTrade.ALL = field.NewAsterisk(tableName)
	_accountTrade.ID = field.NewInt64(tableName, "id")
	_accountTrade.UserID = field.NewInt64(tableName, "user_id")
	_accountTrade.CoinID = field.NewInt64(tableName, "coin_id")
	_accountTrade.AccountID = field.NewInt64(tableName, "account_id")
	_accountTrade.StreamID = field.NewString(tableName, "stream_id")
	_accountTrade.AccountType = field.NewString(tableName, "account_type")
	_accountTrade.TradeType = field.NewString(tableName, "trade_type")
	_accountTrade.Direction = field.NewInt64(tableName, "direction")
	_accountTrade.Amount = field.NewFloat64(tableName, "amount")
	_accountTrade.Status = field.NewInt64(tableName, "status")
	_accountTrade.Remark = field.NewString(tableName, "remark")
	_accountTrade.ExtraData = field.NewString(tableName, "extra_data")
	_accountTrade.TradeTime = field.NewTime(tableName, "trade_time")
	_accountTrade.CreatedAt = field.NewTime(tableName, "created_at")
	_accountTrade.UpdatedAt = field.NewTime(tableName, "updated_at")

	_accountTrade.fillFieldMap()

	return _accountTrade
}

type accountTrade struct {
	accountTradeDo accountTradeDo

	ALL         field.Asterisk
	ID          field.Int64
	UserID      field.Int64   // 用户ID
	CoinID      field.Int64   // 币种ID
	AccountID   field.Int64   // 账户ID
	StreamID    field.String  // 交易流水ID
	AccountType field.String  // 账户类型
	TradeType   field.String  // 交易类型
	Direction   field.Int64   // 资金流转方向:  1:入账  2:出账  3:其他
	Amount      field.Float64 // 资产数量
	Status      field.Int64   // 流水状态  0:默认 1:执行中 2:执行完成 3:回滚中 4:回滚完成
	Remark      field.String  // 备注
	ExtraData   field.String
	TradeTime   field.Time // 交易时间
	CreatedAt   field.Time // 创建时间
	UpdatedAt   field.Time // 更新时间

	fieldMap map[string]field.Expr
}

func (a accountTrade) Table(newTableName string) *accountTrade {
	a.accountTradeDo.UseTable(newTableName)
	return a.updateTableName(newTableName)
}

func (a accountTrade) As(alias string) *accountTrade {
	a.accountTradeDo.DO = *(a.accountTradeDo.As(alias).(*gen.DO))
	return a.updateTableName(alias)
}

func (a *accountTrade) updateTableName(table string) *accountTrade {
	a.ALL = field.NewAsterisk(table)
	a.ID = field.NewInt64(table, "id")
	a.UserID = field.NewInt64(table, "user_id")
	a.CoinID = field.NewInt64(table, "coin_id")
	a.AccountID = field.NewInt64(table, "account_id")
	a.StreamID = field.NewString(table, "stream_id")
	a.AccountType = field.NewString(table, "account_type")
	a.TradeType = field.NewString(table, "trade_type")
	a.Direction = field.NewInt64(table, "direction")
	a.Amount = field.NewFloat64(table, "amount")
	a.Status = field.NewInt64(table, "status")
	a.Remark = field.NewString(table, "remark")
	a.ExtraData = field.NewString(table, "extra_data")
	a.TradeTime = field.NewTime(table, "trade_time")
	a.CreatedAt = field.NewTime(table, "created_at")
	a.UpdatedAt = field.NewTime(table, "updated_at")

	a.fillFieldMap()

	return a
}

func (a *accountTrade) WithContext(ctx context.Context) *accountTradeDo {
	return a.accountTradeDo.WithContext(ctx)
}

func (a accountTrade) TableName() string { return a.accountTradeDo.TableName() }

func (a accountTrade) Alias() string { return a.accountTradeDo.Alias() }

func (a accountTrade) Columns(cols ...field.Expr) gen.Columns {
	return a.accountTradeDo.Columns(cols...)
}

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

func (a *accountTrade) fillFieldMap() {
	a.fieldMap = make(map[string]field.Expr, 15)
	a.fieldMap["id"] = a.ID
	a.fieldMap["user_id"] = a.UserID
	a.fieldMap["coin_id"] = a.CoinID
	a.fieldMap["account_id"] = a.AccountID
	a.fieldMap["stream_id"] = a.StreamID
	a.fieldMap["account_type"] = a.AccountType
	a.fieldMap["trade_type"] = a.TradeType
	a.fieldMap["direction"] = a.Direction
	a.fieldMap["amount"] = a.Amount
	a.fieldMap["status"] = a.Status
	a.fieldMap["remark"] = a.Remark
	a.fieldMap["extra_data"] = a.ExtraData
	a.fieldMap["trade_time"] = a.TradeTime
	a.fieldMap["created_at"] = a.CreatedAt
	a.fieldMap["updated_at"] = a.UpdatedAt
}

func (a accountTrade) clone(db *gorm.DB) accountTrade {
	a.accountTradeDo.ReplaceConnPool(db.Statement.ConnPool)
	return a
}

func (a accountTrade) replaceDB(db *gorm.DB) accountTrade {
	a.accountTradeDo.ReplaceDB(db)
	return a
}

type accountTradeDo struct{ gen.DO }

func (a accountTradeDo) Debug() *accountTradeDo {
	return a.withDO(a.DO.Debug())
}

func (a accountTradeDo) WithContext(ctx context.Context) *accountTradeDo {
	return a.withDO(a.DO.WithContext(ctx))
}

func (a accountTradeDo) ReadDB() *accountTradeDo {
	return a.Clauses(dbresolver.Read)
}

func (a accountTradeDo) WriteDB() *accountTradeDo {
	return a.Clauses(dbresolver.Write)
}

func (a accountTradeDo) Session(config *gorm.Session) *accountTradeDo {
	return a.withDO(a.DO.Session(config))
}

func (a accountTradeDo) Clauses(conds ...clause.Expression) *accountTradeDo {
	return a.withDO(a.DO.Clauses(conds...))
}

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

func (a accountTradeDo) Not(conds ...gen.Condition) *accountTradeDo {
	return a.withDO(a.DO.Not(conds...))
}

func (a accountTradeDo) Or(conds ...gen.Condition) *accountTradeDo {
	return a.withDO(a.DO.Or(conds...))
}

func (a accountTradeDo) Select(conds ...field.Expr) *accountTradeDo {
	return a.withDO(a.DO.Select(conds...))
}

func (a accountTradeDo) Where(conds ...gen.Condition) *accountTradeDo {
	return a.withDO(a.DO.Where(conds...))
}

func (a accountTradeDo) Order(conds ...field.Expr) *accountTradeDo {
	return a.withDO(a.DO.Order(conds...))
}

func (a accountTradeDo) Distinct(cols ...field.Expr) *accountTradeDo {
	return a.withDO(a.DO.Distinct(cols...))
}

func (a accountTradeDo) Omit(cols ...field.Expr) *accountTradeDo {
	return a.withDO(a.DO.Omit(cols...))
}

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

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

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

func (a accountTradeDo) Group(cols ...field.Expr) *accountTradeDo {
	return a.withDO(a.DO.Group(cols...))
}

func (a accountTradeDo) Having(conds ...gen.Condition) *accountTradeDo {
	return a.withDO(a.DO.Having(conds...))
}

func (a accountTradeDo) Limit(limit int) *accountTradeDo {
	return a.withDO(a.DO.Limit(limit))
}

func (a accountTradeDo) Offset(offset int) *accountTradeDo {
	return a.withDO(a.DO.Offset(offset))
}

func (a accountTradeDo) Scopes(funcs ...func(gen.Dao) gen.Dao) *accountTradeDo {
	return a.withDO(a.DO.Scopes(funcs...))
}

func (a accountTradeDo) Unscoped() *accountTradeDo {
	return a.withDO(a.DO.Unscoped())
}

func (a accountTradeDo) Create(values ...*model.AccountTrade) error {
	if len(values) == 0 {
		return nil
	}
	return a.DO.Create(values)
}

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

func (a accountTradeDo) First() (*model.AccountTrade, error) {
	if result, err := a.DO.First(); err != nil {
		return nil, err
	} else {
		return result.(*model.AccountTrade), nil
	}
}

func (a accountTradeDo) Take() (*model.AccountTrade, error) {
	if result, err := a.DO.Take(); err != nil {
		return nil, err
	} else {
		return result.(*model.AccountTrade), nil
	}
}

func (a accountTradeDo) Last() (*model.AccountTrade, error) {
	if result, err := a.DO.Last(); err != nil {
		return nil, err
	} else {
		return result.(*model.AccountTrade), nil
	}
}

func (a accountTradeDo) Find() ([]*model.AccountTrade, error) {
	result, err := a.DO.Find()
	return result.([]*model.AccountTrade), err
}

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

func (a accountTradeDo) Attrs(attrs ...field.AssignExpr) *accountTradeDo {
	return a.withDO(a.DO.Attrs(attrs...))
}

func (a accountTradeDo) Assign(attrs ...field.AssignExpr) *accountTradeDo {
	return a.withDO(a.DO.Assign(attrs...))
}

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

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

func (a accountTradeDo) FirstOrInit() (*model.AccountTrade, error) {
	if result, err := a.DO.FirstOrInit(); err != nil {
		return nil, err
	} else {
		return result.(*model.AccountTrade), nil
	}
}

func (a accountTradeDo) FirstOrCreate() (*model.AccountTrade, error) {
	if result, err := a.DO.FirstOrCreate(); err != nil {
		return nil, err
	} else {
		return result.(*model.AccountTrade), nil
	}
}

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

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

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

func (a accountTradeDo) Scan(result interface{}) (err error) {
	return a.DO.Scan(result)
}

func (a accountTradeDo) Delete(models ...*model.AccountTrade) (result gen.ResultInfo, err error) {
	return a.DO.Delete(models)
}

func (a *accountTradeDo) withDO(do gen.Dao) *accountTradeDo {
	a.DO = *do.(*gen.DO)
	return a
}
