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

func newChainClient(db *gorm.DB, opts ...gen.DOOption) chainClient {
	_chainClient := chainClient{}

	_chainClient.chainClientDo.UseDB(db, opts...)
	_chainClient.chainClientDo.UseModel(&model.ChainClient{})

	tableName := _chainClient.chainClientDo.TableName()
	_chainClient.ALL = field.NewAsterisk(tableName)
	_chainClient.ID = field.NewInt64(tableName, "id")
	_chainClient.ChainUniqueID = field.NewInt64(tableName, "chain_unique_id")
	_chainClient.ChainID = field.NewInt64(tableName, "chain_id")
	_chainClient.ProviderName = field.NewString(tableName, "provider_name")
	_chainClient.ProviderID = field.NewInt64(tableName, "provider_id")
	_chainClient.TransportSchema = field.NewString(tableName, "transport_schema")
	_chainClient.TransportURL = field.NewString(tableName, "transport_url")
	_chainClient.Priority = field.NewInt64(tableName, "priority")
	_chainClient.CreatedAt = field.NewTime(tableName, "created_at")
	_chainClient.UpdatedAt = field.NewTime(tableName, "updated_at")

	_chainClient.fillFieldMap()

	return _chainClient
}

type chainClient struct {
	chainClientDo chainClientDo

	ALL             field.Asterisk
	ID              field.Int64
	ChainUniqueID   field.Int64
	ChainID         field.Int64
	ProviderName    field.String
	ProviderID      field.Int64
	TransportSchema field.String
	TransportURL    field.String
	Priority        field.Int64
	CreatedAt       field.Time // Create Time
	UpdatedAt       field.Time // Update Time

	fieldMap map[string]field.Expr
}

func (c chainClient) Table(newTableName string) *chainClient {
	c.chainClientDo.UseTable(newTableName)
	return c.updateTableName(newTableName)
}

func (c chainClient) As(alias string) *chainClient {
	c.chainClientDo.DO = *(c.chainClientDo.As(alias).(*gen.DO))
	return c.updateTableName(alias)
}

func (c *chainClient) updateTableName(table string) *chainClient {
	c.ALL = field.NewAsterisk(table)
	c.ID = field.NewInt64(table, "id")
	c.ChainUniqueID = field.NewInt64(table, "chain_unique_id")
	c.ChainID = field.NewInt64(table, "chain_id")
	c.ProviderName = field.NewString(table, "provider_name")
	c.ProviderID = field.NewInt64(table, "provider_id")
	c.TransportSchema = field.NewString(table, "transport_schema")
	c.TransportURL = field.NewString(table, "transport_url")
	c.Priority = field.NewInt64(table, "priority")
	c.CreatedAt = field.NewTime(table, "created_at")
	c.UpdatedAt = field.NewTime(table, "updated_at")

	c.fillFieldMap()

	return c
}

func (c *chainClient) WithContext(ctx context.Context) *chainClientDo {
	return c.chainClientDo.WithContext(ctx)
}

func (c chainClient) TableName() string { return c.chainClientDo.TableName() }

func (c chainClient) Alias() string { return c.chainClientDo.Alias() }

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

func (c *chainClient) fillFieldMap() {
	c.fieldMap = make(map[string]field.Expr, 10)
	c.fieldMap["id"] = c.ID
	c.fieldMap["chain_unique_id"] = c.ChainUniqueID
	c.fieldMap["chain_id"] = c.ChainID
	c.fieldMap["provider_name"] = c.ProviderName
	c.fieldMap["provider_id"] = c.ProviderID
	c.fieldMap["transport_schema"] = c.TransportSchema
	c.fieldMap["transport_url"] = c.TransportURL
	c.fieldMap["priority"] = c.Priority
	c.fieldMap["created_at"] = c.CreatedAt
	c.fieldMap["updated_at"] = c.UpdatedAt
}

func (c chainClient) clone(db *gorm.DB) chainClient {
	c.chainClientDo.ReplaceConnPool(db.Statement.ConnPool)
	return c
}

func (c chainClient) replaceDB(db *gorm.DB) chainClient {
	c.chainClientDo.ReplaceDB(db)
	return c
}

type chainClientDo struct{ gen.DO }

func (c chainClientDo) Debug() *chainClientDo {
	return c.withDO(c.DO.Debug())
}

func (c chainClientDo) WithContext(ctx context.Context) *chainClientDo {
	return c.withDO(c.DO.WithContext(ctx))
}

func (c chainClientDo) ReadDB() *chainClientDo {
	return c.Clauses(dbresolver.Read)
}

func (c chainClientDo) WriteDB() *chainClientDo {
	return c.Clauses(dbresolver.Write)
}

func (c chainClientDo) Session(config *gorm.Session) *chainClientDo {
	return c.withDO(c.DO.Session(config))
}

func (c chainClientDo) Clauses(conds ...clause.Expression) *chainClientDo {
	return c.withDO(c.DO.Clauses(conds...))
}

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

func (c chainClientDo) Not(conds ...gen.Condition) *chainClientDo {
	return c.withDO(c.DO.Not(conds...))
}

func (c chainClientDo) Or(conds ...gen.Condition) *chainClientDo {
	return c.withDO(c.DO.Or(conds...))
}

func (c chainClientDo) Select(conds ...field.Expr) *chainClientDo {
	return c.withDO(c.DO.Select(conds...))
}

func (c chainClientDo) Where(conds ...gen.Condition) *chainClientDo {
	return c.withDO(c.DO.Where(conds...))
}

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

func (c chainClientDo) Order(conds ...field.Expr) *chainClientDo {
	return c.withDO(c.DO.Order(conds...))
}

func (c chainClientDo) Distinct(cols ...field.Expr) *chainClientDo {
	return c.withDO(c.DO.Distinct(cols...))
}

func (c chainClientDo) Omit(cols ...field.Expr) *chainClientDo {
	return c.withDO(c.DO.Omit(cols...))
}

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

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

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

func (c chainClientDo) Group(cols ...field.Expr) *chainClientDo {
	return c.withDO(c.DO.Group(cols...))
}

func (c chainClientDo) Having(conds ...gen.Condition) *chainClientDo {
	return c.withDO(c.DO.Having(conds...))
}

func (c chainClientDo) Limit(limit int) *chainClientDo {
	return c.withDO(c.DO.Limit(limit))
}

func (c chainClientDo) Offset(offset int) *chainClientDo {
	return c.withDO(c.DO.Offset(offset))
}

func (c chainClientDo) Scopes(funcs ...func(gen.Dao) gen.Dao) *chainClientDo {
	return c.withDO(c.DO.Scopes(funcs...))
}

func (c chainClientDo) Unscoped() *chainClientDo {
	return c.withDO(c.DO.Unscoped())
}

func (c chainClientDo) Create(values ...*model.ChainClient) error {
	if len(values) == 0 {
		return nil
	}
	return c.DO.Create(values)
}

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

func (c chainClientDo) First() (*model.ChainClient, error) {
	if result, err := c.DO.First(); err != nil {
		return nil, err
	} else {
		return result.(*model.ChainClient), nil
	}
}

func (c chainClientDo) Take() (*model.ChainClient, error) {
	if result, err := c.DO.Take(); err != nil {
		return nil, err
	} else {
		return result.(*model.ChainClient), nil
	}
}

func (c chainClientDo) Last() (*model.ChainClient, error) {
	if result, err := c.DO.Last(); err != nil {
		return nil, err
	} else {
		return result.(*model.ChainClient), nil
	}
}

func (c chainClientDo) Find() ([]*model.ChainClient, error) {
	result, err := c.DO.Find()
	return result.([]*model.ChainClient), err
}

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

func (c chainClientDo) Attrs(attrs ...field.AssignExpr) *chainClientDo {
	return c.withDO(c.DO.Attrs(attrs...))
}

func (c chainClientDo) Assign(attrs ...field.AssignExpr) *chainClientDo {
	return c.withDO(c.DO.Assign(attrs...))
}

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

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

func (c chainClientDo) FirstOrInit() (*model.ChainClient, error) {
	if result, err := c.DO.FirstOrInit(); err != nil {
		return nil, err
	} else {
		return result.(*model.ChainClient), nil
	}
}

func (c chainClientDo) FirstOrCreate() (*model.ChainClient, error) {
	if result, err := c.DO.FirstOrCreate(); err != nil {
		return nil, err
	} else {
		return result.(*model.ChainClient), nil
	}
}

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

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

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

func (c chainClientDo) Scan(result interface{}) (err error) {
	return c.DO.Scan(result)
}

func (c chainClientDo) Delete(models ...*model.ChainClient) (result gen.ResultInfo, err error) {
	return c.DO.Delete(models)
}

func (c *chainClientDo) withDO(do gen.Dao) *chainClientDo {
	c.DO = *do.(*gen.DO)
	return c
}
