// 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 repository

import (
	"context"

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

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

	"gorm.io/plugin/dbresolver"

	"cheemshappy_pay/internal/model"
)

func newMerchantsAPI(db *gorm.DB, opts ...gen.DOOption) merchantsAPI {
	_merchantsAPI := merchantsAPI{}

	_merchantsAPI.merchantsAPIDo.UseDB(db, opts...)
	_merchantsAPI.merchantsAPIDo.UseModel(&model.MerchantsAPI{})

	tableName := _merchantsAPI.merchantsAPIDo.TableName()
	_merchantsAPI.ALL = field.NewAsterisk(tableName)
	_merchantsAPI.ID = field.NewInt64(tableName, "id")
	_merchantsAPI.MID = field.NewInt64(tableName, "m_id")
	_merchantsAPI.Apikey = field.NewString(tableName, "apikey")
	_merchantsAPI.CallbackURL = field.NewString(tableName, "callback_url")
	_merchantsAPI.SecretKey = field.NewString(tableName, "secret_key")
	_merchantsAPI.Remark = field.NewString(tableName, "remark")
	_merchantsAPI.CreatedAt = field.NewTime(tableName, "created_at")
	_merchantsAPI.UpdatedAt = field.NewTime(tableName, "updated_at")
	_merchantsAPI.DeletedAt = field.NewField(tableName, "deleted_at")

	_merchantsAPI.fillFieldMap()

	return _merchantsAPI
}

type merchantsAPI struct {
	merchantsAPIDo

	ALL         field.Asterisk
	ID          field.Int64
	MID         field.Int64
	Apikey      field.String // Token
	CallbackURL field.String // 回调url
	SecretKey   field.String // 回调加密参数
	Remark      field.String // 备注
	CreatedAt   field.Time
	UpdatedAt   field.Time
	DeletedAt   field.Field

	fieldMap map[string]field.Expr
}

func (m merchantsAPI) Table(newTableName string) *merchantsAPI {
	m.merchantsAPIDo.UseTable(newTableName)
	return m.updateTableName(newTableName)
}

func (m merchantsAPI) As(alias string) *merchantsAPI {
	m.merchantsAPIDo.DO = *(m.merchantsAPIDo.As(alias).(*gen.DO))
	return m.updateTableName(alias)
}

func (m *merchantsAPI) updateTableName(table string) *merchantsAPI {
	m.ALL = field.NewAsterisk(table)
	m.ID = field.NewInt64(table, "id")
	m.MID = field.NewInt64(table, "m_id")
	m.Apikey = field.NewString(table, "apikey")
	m.CallbackURL = field.NewString(table, "callback_url")
	m.SecretKey = field.NewString(table, "secret_key")
	m.Remark = field.NewString(table, "remark")
	m.CreatedAt = field.NewTime(table, "created_at")
	m.UpdatedAt = field.NewTime(table, "updated_at")
	m.DeletedAt = field.NewField(table, "deleted_at")

	m.fillFieldMap()

	return m
}

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

func (m *merchantsAPI) fillFieldMap() {
	m.fieldMap = make(map[string]field.Expr, 9)
	m.fieldMap["id"] = m.ID
	m.fieldMap["m_id"] = m.MID
	m.fieldMap["apikey"] = m.Apikey
	m.fieldMap["callback_url"] = m.CallbackURL
	m.fieldMap["secret_key"] = m.SecretKey
	m.fieldMap["remark"] = m.Remark
	m.fieldMap["created_at"] = m.CreatedAt
	m.fieldMap["updated_at"] = m.UpdatedAt
	m.fieldMap["deleted_at"] = m.DeletedAt
}

func (m merchantsAPI) clone(db *gorm.DB) merchantsAPI {
	m.merchantsAPIDo.ReplaceConnPool(db.Statement.ConnPool)
	return m
}

func (m merchantsAPI) replaceDB(db *gorm.DB) merchantsAPI {
	m.merchantsAPIDo.ReplaceDB(db)
	return m
}

type merchantsAPIDo struct{ gen.DO }

type IMerchantsAPIDo interface {
	gen.SubQuery
	Debug() IMerchantsAPIDo
	WithContext(ctx context.Context) IMerchantsAPIDo
	WithResult(fc func(tx gen.Dao)) gen.ResultInfo
	ReplaceDB(db *gorm.DB)
	ReadDB() IMerchantsAPIDo
	WriteDB() IMerchantsAPIDo
	As(alias string) gen.Dao
	Session(config *gorm.Session) IMerchantsAPIDo
	Columns(cols ...field.Expr) gen.Columns
	Clauses(conds ...clause.Expression) IMerchantsAPIDo
	Not(conds ...gen.Condition) IMerchantsAPIDo
	Or(conds ...gen.Condition) IMerchantsAPIDo
	Select(conds ...field.Expr) IMerchantsAPIDo
	Where(conds ...gen.Condition) IMerchantsAPIDo
	Order(conds ...field.Expr) IMerchantsAPIDo
	Distinct(cols ...field.Expr) IMerchantsAPIDo
	Omit(cols ...field.Expr) IMerchantsAPIDo
	Join(table schema.Tabler, on ...field.Expr) IMerchantsAPIDo
	LeftJoin(table schema.Tabler, on ...field.Expr) IMerchantsAPIDo
	RightJoin(table schema.Tabler, on ...field.Expr) IMerchantsAPIDo
	Group(cols ...field.Expr) IMerchantsAPIDo
	Having(conds ...gen.Condition) IMerchantsAPIDo
	Limit(limit int) IMerchantsAPIDo
	Offset(offset int) IMerchantsAPIDo
	Count() (count int64, err error)
	Scopes(funcs ...func(gen.Dao) gen.Dao) IMerchantsAPIDo
	Unscoped() IMerchantsAPIDo
	Create(values ...*model.MerchantsAPI) error
	CreateInBatches(values []*model.MerchantsAPI, batchSize int) error
	Save(values ...*model.MerchantsAPI) error
	First() (*model.MerchantsAPI, error)
	Take() (*model.MerchantsAPI, error)
	Last() (*model.MerchantsAPI, error)
	Find() ([]*model.MerchantsAPI, error)
	FindInBatch(batchSize int, fc func(tx gen.Dao, batch int) error) (results []*model.MerchantsAPI, err error)
	FindInBatches(result *[]*model.MerchantsAPI, batchSize int, fc func(tx gen.Dao, batch int) error) error
	Pluck(column field.Expr, dest interface{}) error
	Delete(...*model.MerchantsAPI) (info gen.ResultInfo, err error)
	Update(column field.Expr, value interface{}) (info gen.ResultInfo, err error)
	UpdateSimple(columns ...field.AssignExpr) (info gen.ResultInfo, err error)
	Updates(value interface{}) (info gen.ResultInfo, err error)
	UpdateColumn(column field.Expr, value interface{}) (info gen.ResultInfo, err error)
	UpdateColumnSimple(columns ...field.AssignExpr) (info gen.ResultInfo, err error)
	UpdateColumns(value interface{}) (info gen.ResultInfo, err error)
	UpdateFrom(q gen.SubQuery) gen.Dao
	Attrs(attrs ...field.AssignExpr) IMerchantsAPIDo
	Assign(attrs ...field.AssignExpr) IMerchantsAPIDo
	Joins(fields ...field.RelationField) IMerchantsAPIDo
	Preload(fields ...field.RelationField) IMerchantsAPIDo
	FirstOrInit() (*model.MerchantsAPI, error)
	FirstOrCreate() (*model.MerchantsAPI, error)
	FindByPage(offset int, limit int) (result []*model.MerchantsAPI, count int64, err error)
	ScanByPage(result interface{}, offset int, limit int) (count int64, err error)
	Scan(result interface{}) (err error)
	Returning(value interface{}, columns ...string) IMerchantsAPIDo
	UnderlyingDB() *gorm.DB
	schema.Tabler
}

func (m merchantsAPIDo) Debug() IMerchantsAPIDo {
	return m.withDO(m.DO.Debug())
}

func (m merchantsAPIDo) WithContext(ctx context.Context) IMerchantsAPIDo {
	return m.withDO(m.DO.WithContext(ctx))
}

func (m merchantsAPIDo) ReadDB() IMerchantsAPIDo {
	return m.Clauses(dbresolver.Read)
}

func (m merchantsAPIDo) WriteDB() IMerchantsAPIDo {
	return m.Clauses(dbresolver.Write)
}

func (m merchantsAPIDo) Session(config *gorm.Session) IMerchantsAPIDo {
	return m.withDO(m.DO.Session(config))
}

func (m merchantsAPIDo) Clauses(conds ...clause.Expression) IMerchantsAPIDo {
	return m.withDO(m.DO.Clauses(conds...))
}

func (m merchantsAPIDo) Returning(value interface{}, columns ...string) IMerchantsAPIDo {
	return m.withDO(m.DO.Returning(value, columns...))
}

func (m merchantsAPIDo) Not(conds ...gen.Condition) IMerchantsAPIDo {
	return m.withDO(m.DO.Not(conds...))
}

func (m merchantsAPIDo) Or(conds ...gen.Condition) IMerchantsAPIDo {
	return m.withDO(m.DO.Or(conds...))
}

func (m merchantsAPIDo) Select(conds ...field.Expr) IMerchantsAPIDo {
	return m.withDO(m.DO.Select(conds...))
}

func (m merchantsAPIDo) Where(conds ...gen.Condition) IMerchantsAPIDo {
	return m.withDO(m.DO.Where(conds...))
}

func (m merchantsAPIDo) Order(conds ...field.Expr) IMerchantsAPIDo {
	return m.withDO(m.DO.Order(conds...))
}

func (m merchantsAPIDo) Distinct(cols ...field.Expr) IMerchantsAPIDo {
	return m.withDO(m.DO.Distinct(cols...))
}

func (m merchantsAPIDo) Omit(cols ...field.Expr) IMerchantsAPIDo {
	return m.withDO(m.DO.Omit(cols...))
}

func (m merchantsAPIDo) Join(table schema.Tabler, on ...field.Expr) IMerchantsAPIDo {
	return m.withDO(m.DO.Join(table, on...))
}

func (m merchantsAPIDo) LeftJoin(table schema.Tabler, on ...field.Expr) IMerchantsAPIDo {
	return m.withDO(m.DO.LeftJoin(table, on...))
}

func (m merchantsAPIDo) RightJoin(table schema.Tabler, on ...field.Expr) IMerchantsAPIDo {
	return m.withDO(m.DO.RightJoin(table, on...))
}

func (m merchantsAPIDo) Group(cols ...field.Expr) IMerchantsAPIDo {
	return m.withDO(m.DO.Group(cols...))
}

func (m merchantsAPIDo) Having(conds ...gen.Condition) IMerchantsAPIDo {
	return m.withDO(m.DO.Having(conds...))
}

func (m merchantsAPIDo) Limit(limit int) IMerchantsAPIDo {
	return m.withDO(m.DO.Limit(limit))
}

func (m merchantsAPIDo) Offset(offset int) IMerchantsAPIDo {
	return m.withDO(m.DO.Offset(offset))
}

func (m merchantsAPIDo) Scopes(funcs ...func(gen.Dao) gen.Dao) IMerchantsAPIDo {
	return m.withDO(m.DO.Scopes(funcs...))
}

func (m merchantsAPIDo) Unscoped() IMerchantsAPIDo {
	return m.withDO(m.DO.Unscoped())
}

func (m merchantsAPIDo) Create(values ...*model.MerchantsAPI) error {
	if len(values) == 0 {
		return nil
	}
	return m.DO.Create(values)
}

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

func (m merchantsAPIDo) First() (*model.MerchantsAPI, error) {
	if result, err := m.DO.First(); err != nil {
		return nil, err
	} else {
		return result.(*model.MerchantsAPI), nil
	}
}

func (m merchantsAPIDo) Take() (*model.MerchantsAPI, error) {
	if result, err := m.DO.Take(); err != nil {
		return nil, err
	} else {
		return result.(*model.MerchantsAPI), nil
	}
}

func (m merchantsAPIDo) Last() (*model.MerchantsAPI, error) {
	if result, err := m.DO.Last(); err != nil {
		return nil, err
	} else {
		return result.(*model.MerchantsAPI), nil
	}
}

func (m merchantsAPIDo) Find() ([]*model.MerchantsAPI, error) {
	result, err := m.DO.Find()
	return result.([]*model.MerchantsAPI), err
}

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

func (m merchantsAPIDo) Attrs(attrs ...field.AssignExpr) IMerchantsAPIDo {
	return m.withDO(m.DO.Attrs(attrs...))
}

func (m merchantsAPIDo) Assign(attrs ...field.AssignExpr) IMerchantsAPIDo {
	return m.withDO(m.DO.Assign(attrs...))
}

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

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

func (m merchantsAPIDo) FirstOrInit() (*model.MerchantsAPI, error) {
	if result, err := m.DO.FirstOrInit(); err != nil {
		return nil, err
	} else {
		return result.(*model.MerchantsAPI), nil
	}
}

func (m merchantsAPIDo) FirstOrCreate() (*model.MerchantsAPI, error) {
	if result, err := m.DO.FirstOrCreate(); err != nil {
		return nil, err
	} else {
		return result.(*model.MerchantsAPI), nil
	}
}

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

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

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

func (m merchantsAPIDo) Scan(result interface{}) (err error) {
	return m.DO.Scan(result)
}

func (m merchantsAPIDo) Delete(models ...*model.MerchantsAPI) (result gen.ResultInfo, err error) {
	return m.DO.Delete(models)
}

func (m *merchantsAPIDo) withDO(do gen.Dao) *merchantsAPIDo {
	m.DO = *do.(*gen.DO)
	return m
}
