// 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/demo/storage/mysql/model"
)

func newBlock(db *gorm.DB, opts ...gen.DOOption) block {
	_block := block{}

	_block.blockDo.UseDB(db, opts...)
	_block.blockDo.UseModel(&model.Block{})

	tableName := _block.blockDo.TableName()
	_block.ALL = field.NewAsterisk(tableName)
	_block.ChainUniqueID = field.NewInt64(tableName, "chain_unique_id")
	_block.Number = field.NewInt64(tableName, "number")
	_block.Hash = field.NewString(tableName, "hash")
	_block.ParentHash = field.NewString(tableName, "parent_hash")
	_block.Timestamp = field.NewTime(tableName, "timestamp")
	_block.Difficulty = field.NewString(tableName, "difficulty")
	_block.ExtraData = field.NewString(tableName, "extra_data")
	_block.GasLimit = field.NewString(tableName, "gas_limit")
	_block.GasUsed = field.NewString(tableName, "gas_used")
	_block.BaseFeePerGas = field.NewString(tableName, "base_fee_per_gas")
	_block.Miner = field.NewString(tableName, "miner")
	_block.MixHash = field.NewString(tableName, "mix_hash")
	_block.Nonce = field.NewString(tableName, "nonce")
	_block.ReceiptsRoot = field.NewString(tableName, "receipts_root")
	_block.Sha3Uncles = field.NewString(tableName, "sha3_uncles")
	_block.Size = field.NewInt64(tableName, "size")
	_block.StateRoot = field.NewString(tableName, "state_root")
	_block.TransactionsRoot = field.NewString(tableName, "transactions_root")
	_block.TransactionsCount = field.NewInt64(tableName, "transactions_count")
	_block.UnclesCount = field.NewInt64(tableName, "uncles_count")
	_block.ChainID = field.NewInt64(tableName, "chain_id")
	_block.CreatedAt = field.NewTime(tableName, "created_at")
	_block.UpdatedAt = field.NewTime(tableName, "updated_at")

	_block.fillFieldMap()

	return _block
}

type block struct {
	blockDo blockDo

	ALL               field.Asterisk
	ChainUniqueID     field.Int64
	Number            field.Int64 // block number
	Hash              field.String
	ParentHash        field.String
	Timestamp         field.Time // Block Time
	Difficulty        field.String
	ExtraData         field.String
	GasLimit          field.String
	GasUsed           field.String
	BaseFeePerGas     field.String
	Miner             field.String
	MixHash           field.String
	Nonce             field.String
	ReceiptsRoot      field.String
	Sha3Uncles        field.String
	Size              field.Int64
	StateRoot         field.String
	TransactionsRoot  field.String
	TransactionsCount field.Int64
	UnclesCount       field.Int64
	ChainID           field.Int64
	CreatedAt         field.Time // Create Time
	UpdatedAt         field.Time // Update Time

	fieldMap map[string]field.Expr
}

func (b block) Table(newTableName string) *block {
	b.blockDo.UseTable(newTableName)
	return b.updateTableName(newTableName)
}

func (b block) As(alias string) *block {
	b.blockDo.DO = *(b.blockDo.As(alias).(*gen.DO))
	return b.updateTableName(alias)
}

func (b *block) updateTableName(table string) *block {
	b.ALL = field.NewAsterisk(table)
	b.ChainUniqueID = field.NewInt64(table, "chain_unique_id")
	b.Number = field.NewInt64(table, "number")
	b.Hash = field.NewString(table, "hash")
	b.ParentHash = field.NewString(table, "parent_hash")
	b.Timestamp = field.NewTime(table, "timestamp")
	b.Difficulty = field.NewString(table, "difficulty")
	b.ExtraData = field.NewString(table, "extra_data")
	b.GasLimit = field.NewString(table, "gas_limit")
	b.GasUsed = field.NewString(table, "gas_used")
	b.BaseFeePerGas = field.NewString(table, "base_fee_per_gas")
	b.Miner = field.NewString(table, "miner")
	b.MixHash = field.NewString(table, "mix_hash")
	b.Nonce = field.NewString(table, "nonce")
	b.ReceiptsRoot = field.NewString(table, "receipts_root")
	b.Sha3Uncles = field.NewString(table, "sha3_uncles")
	b.Size = field.NewInt64(table, "size")
	b.StateRoot = field.NewString(table, "state_root")
	b.TransactionsRoot = field.NewString(table, "transactions_root")
	b.TransactionsCount = field.NewInt64(table, "transactions_count")
	b.UnclesCount = field.NewInt64(table, "uncles_count")
	b.ChainID = field.NewInt64(table, "chain_id")
	b.CreatedAt = field.NewTime(table, "created_at")
	b.UpdatedAt = field.NewTime(table, "updated_at")

	b.fillFieldMap()

	return b
}

func (b *block) WithContext(ctx context.Context) *blockDo { return b.blockDo.WithContext(ctx) }

func (b block) TableName() string { return b.blockDo.TableName() }

func (b block) Alias() string { return b.blockDo.Alias() }

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

func (b *block) fillFieldMap() {
	b.fieldMap = make(map[string]field.Expr, 23)
	b.fieldMap["chain_unique_id"] = b.ChainUniqueID
	b.fieldMap["number"] = b.Number
	b.fieldMap["hash"] = b.Hash
	b.fieldMap["parent_hash"] = b.ParentHash
	b.fieldMap["timestamp"] = b.Timestamp
	b.fieldMap["difficulty"] = b.Difficulty
	b.fieldMap["extra_data"] = b.ExtraData
	b.fieldMap["gas_limit"] = b.GasLimit
	b.fieldMap["gas_used"] = b.GasUsed
	b.fieldMap["base_fee_per_gas"] = b.BaseFeePerGas
	b.fieldMap["miner"] = b.Miner
	b.fieldMap["mix_hash"] = b.MixHash
	b.fieldMap["nonce"] = b.Nonce
	b.fieldMap["receipts_root"] = b.ReceiptsRoot
	b.fieldMap["sha3_uncles"] = b.Sha3Uncles
	b.fieldMap["size"] = b.Size
	b.fieldMap["state_root"] = b.StateRoot
	b.fieldMap["transactions_root"] = b.TransactionsRoot
	b.fieldMap["transactions_count"] = b.TransactionsCount
	b.fieldMap["uncles_count"] = b.UnclesCount
	b.fieldMap["chain_id"] = b.ChainID
	b.fieldMap["created_at"] = b.CreatedAt
	b.fieldMap["updated_at"] = b.UpdatedAt
}

func (b block) clone(db *gorm.DB) block {
	b.blockDo.ReplaceConnPool(db.Statement.ConnPool)
	return b
}

func (b block) replaceDB(db *gorm.DB) block {
	b.blockDo.ReplaceDB(db)
	return b
}

type blockDo struct{ gen.DO }

func (b blockDo) Debug() *blockDo {
	return b.withDO(b.DO.Debug())
}

func (b blockDo) WithContext(ctx context.Context) *blockDo {
	return b.withDO(b.DO.WithContext(ctx))
}

func (b blockDo) ReadDB() *blockDo {
	return b.Clauses(dbresolver.Read)
}

func (b blockDo) WriteDB() *blockDo {
	return b.Clauses(dbresolver.Write)
}

func (b blockDo) Session(config *gorm.Session) *blockDo {
	return b.withDO(b.DO.Session(config))
}

func (b blockDo) Clauses(conds ...clause.Expression) *blockDo {
	return b.withDO(b.DO.Clauses(conds...))
}

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

func (b blockDo) Not(conds ...gen.Condition) *blockDo {
	return b.withDO(b.DO.Not(conds...))
}

func (b blockDo) Or(conds ...gen.Condition) *blockDo {
	return b.withDO(b.DO.Or(conds...))
}

func (b blockDo) Select(conds ...field.Expr) *blockDo {
	return b.withDO(b.DO.Select(conds...))
}

func (b blockDo) Where(conds ...gen.Condition) *blockDo {
	return b.withDO(b.DO.Where(conds...))
}

func (b blockDo) Exists(subquery interface{ UnderlyingDB() *gorm.DB }) *blockDo {
	return b.Where(field.CompareSubQuery(field.ExistsOp, nil, subquery.UnderlyingDB()))
}

func (b blockDo) Order(conds ...field.Expr) *blockDo {
	return b.withDO(b.DO.Order(conds...))
}

func (b blockDo) Distinct(cols ...field.Expr) *blockDo {
	return b.withDO(b.DO.Distinct(cols...))
}

func (b blockDo) Omit(cols ...field.Expr) *blockDo {
	return b.withDO(b.DO.Omit(cols...))
}

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

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

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

func (b blockDo) Group(cols ...field.Expr) *blockDo {
	return b.withDO(b.DO.Group(cols...))
}

func (b blockDo) Having(conds ...gen.Condition) *blockDo {
	return b.withDO(b.DO.Having(conds...))
}

func (b blockDo) Limit(limit int) *blockDo {
	return b.withDO(b.DO.Limit(limit))
}

func (b blockDo) Offset(offset int) *blockDo {
	return b.withDO(b.DO.Offset(offset))
}

func (b blockDo) Scopes(funcs ...func(gen.Dao) gen.Dao) *blockDo {
	return b.withDO(b.DO.Scopes(funcs...))
}

func (b blockDo) Unscoped() *blockDo {
	return b.withDO(b.DO.Unscoped())
}

func (b blockDo) Create(values ...*model.Block) error {
	if len(values) == 0 {
		return nil
	}
	return b.DO.Create(values)
}

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

func (b blockDo) First() (*model.Block, error) {
	if result, err := b.DO.First(); err != nil {
		return nil, err
	} else {
		return result.(*model.Block), nil
	}
}

func (b blockDo) Take() (*model.Block, error) {
	if result, err := b.DO.Take(); err != nil {
		return nil, err
	} else {
		return result.(*model.Block), nil
	}
}

func (b blockDo) Last() (*model.Block, error) {
	if result, err := b.DO.Last(); err != nil {
		return nil, err
	} else {
		return result.(*model.Block), nil
	}
}

func (b blockDo) Find() ([]*model.Block, error) {
	result, err := b.DO.Find()
	return result.([]*model.Block), err
}

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

func (b blockDo) Attrs(attrs ...field.AssignExpr) *blockDo {
	return b.withDO(b.DO.Attrs(attrs...))
}

func (b blockDo) Assign(attrs ...field.AssignExpr) *blockDo {
	return b.withDO(b.DO.Assign(attrs...))
}

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

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

func (b blockDo) FirstOrInit() (*model.Block, error) {
	if result, err := b.DO.FirstOrInit(); err != nil {
		return nil, err
	} else {
		return result.(*model.Block), nil
	}
}

func (b blockDo) FirstOrCreate() (*model.Block, error) {
	if result, err := b.DO.FirstOrCreate(); err != nil {
		return nil, err
	} else {
		return result.(*model.Block), nil
	}
}

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

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

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

func (b blockDo) Scan(result interface{}) (err error) {
	return b.DO.Scan(result)
}

func (b blockDo) Delete(models ...*model.Block) (result gen.ResultInfo, err error) {
	return b.DO.Delete(models)
}

func (b *blockDo) withDO(do gen.Dao) *blockDo {
	b.DO = *do.(*gen.DO)
	return b
}
