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

	_merchant.merchantDo.UseDB(db, opts...)
	_merchant.merchantDo.UseModel(&model.Merchant{})

	tableName := _merchant.merchantDo.TableName()
	_merchant.ALL = field.NewAsterisk(tableName)
	_merchant.ID = field.NewInt64(tableName, "id")
	_merchant.Nickname = field.NewString(tableName, "nickname")
	_merchant.Avatar = field.NewString(tableName, "avatar")
	_merchant.Introduction = field.NewString(tableName, "introduction")
	_merchant.Password = field.NewString(tableName, "password")
	_merchant.Email = field.NewString(tableName, "email")
	_merchant.Phone = field.NewString(tableName, "phone")
	_merchant.CreatedBy = field.NewString(tableName, "created_by")
	_merchant.CreatedAt = field.NewTime(tableName, "created_at")
	_merchant.UpdatedAt = field.NewTime(tableName, "updated_at")
	_merchant.DeletedAt = field.NewField(tableName, "deleted_at")

	_merchant.fillFieldMap()

	return _merchant
}

type merchant struct {
	merchantDo

	ALL          field.Asterisk
	ID           field.Int64
	Nickname     field.String // 昵称
	Avatar       field.String // 头像
	Introduction field.String // 简介
	Password     field.String
	Email        field.String
	Phone        field.String
	CreatedBy    field.String
	CreatedAt    field.Time
	UpdatedAt    field.Time
	DeletedAt    field.Field

	fieldMap map[string]field.Expr
}

func (m merchant) Table(newTableName string) *merchant {
	m.merchantDo.UseTable(newTableName)
	return m.updateTableName(newTableName)
}

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

func (m *merchant) updateTableName(table string) *merchant {
	m.ALL = field.NewAsterisk(table)
	m.ID = field.NewInt64(table, "id")
	m.Nickname = field.NewString(table, "nickname")
	m.Avatar = field.NewString(table, "avatar")
	m.Introduction = field.NewString(table, "introduction")
	m.Password = field.NewString(table, "password")
	m.Email = field.NewString(table, "email")
	m.Phone = field.NewString(table, "phone")
	m.CreatedBy = field.NewString(table, "created_by")
	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 *merchant) 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 *merchant) fillFieldMap() {
	m.fieldMap = make(map[string]field.Expr, 11)
	m.fieldMap["id"] = m.ID
	m.fieldMap["nickname"] = m.Nickname
	m.fieldMap["avatar"] = m.Avatar
	m.fieldMap["introduction"] = m.Introduction
	m.fieldMap["password"] = m.Password
	m.fieldMap["email"] = m.Email
	m.fieldMap["phone"] = m.Phone
	m.fieldMap["created_by"] = m.CreatedBy
	m.fieldMap["created_at"] = m.CreatedAt
	m.fieldMap["updated_at"] = m.UpdatedAt
	m.fieldMap["deleted_at"] = m.DeletedAt
}

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

func (m merchant) replaceDB(db *gorm.DB) merchant {
	m.merchantDo.ReplaceDB(db)
	return m
}

type merchantDo struct{ gen.DO }

type IMerchantDo interface {
	gen.SubQuery
	Debug() IMerchantDo
	WithContext(ctx context.Context) IMerchantDo
	WithResult(fc func(tx gen.Dao)) gen.ResultInfo
	ReplaceDB(db *gorm.DB)
	ReadDB() IMerchantDo
	WriteDB() IMerchantDo
	As(alias string) gen.Dao
	Session(config *gorm.Session) IMerchantDo
	Columns(cols ...field.Expr) gen.Columns
	Clauses(conds ...clause.Expression) IMerchantDo
	Not(conds ...gen.Condition) IMerchantDo
	Or(conds ...gen.Condition) IMerchantDo
	Select(conds ...field.Expr) IMerchantDo
	Where(conds ...gen.Condition) IMerchantDo
	Order(conds ...field.Expr) IMerchantDo
	Distinct(cols ...field.Expr) IMerchantDo
	Omit(cols ...field.Expr) IMerchantDo
	Join(table schema.Tabler, on ...field.Expr) IMerchantDo
	LeftJoin(table schema.Tabler, on ...field.Expr) IMerchantDo
	RightJoin(table schema.Tabler, on ...field.Expr) IMerchantDo
	Group(cols ...field.Expr) IMerchantDo
	Having(conds ...gen.Condition) IMerchantDo
	Limit(limit int) IMerchantDo
	Offset(offset int) IMerchantDo
	Count() (count int64, err error)
	Scopes(funcs ...func(gen.Dao) gen.Dao) IMerchantDo
	Unscoped() IMerchantDo
	Create(values ...*model.Merchant) error
	CreateInBatches(values []*model.Merchant, batchSize int) error
	Save(values ...*model.Merchant) error
	First() (*model.Merchant, error)
	Take() (*model.Merchant, error)
	Last() (*model.Merchant, error)
	Find() ([]*model.Merchant, error)
	FindInBatch(batchSize int, fc func(tx gen.Dao, batch int) error) (results []*model.Merchant, err error)
	FindInBatches(result *[]*model.Merchant, batchSize int, fc func(tx gen.Dao, batch int) error) error
	Pluck(column field.Expr, dest interface{}) error
	Delete(...*model.Merchant) (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) IMerchantDo
	Assign(attrs ...field.AssignExpr) IMerchantDo
	Joins(fields ...field.RelationField) IMerchantDo
	Preload(fields ...field.RelationField) IMerchantDo
	FirstOrInit() (*model.Merchant, error)
	FirstOrCreate() (*model.Merchant, error)
	FindByPage(offset int, limit int) (result []*model.Merchant, 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) IMerchantDo
	UnderlyingDB() *gorm.DB
	schema.Tabler
}

func (m merchantDo) Debug() IMerchantDo {
	return m.withDO(m.DO.Debug())
}

func (m merchantDo) WithContext(ctx context.Context) IMerchantDo {
	return m.withDO(m.DO.WithContext(ctx))
}

func (m merchantDo) ReadDB() IMerchantDo {
	return m.Clauses(dbresolver.Read)
}

func (m merchantDo) WriteDB() IMerchantDo {
	return m.Clauses(dbresolver.Write)
}

func (m merchantDo) Session(config *gorm.Session) IMerchantDo {
	return m.withDO(m.DO.Session(config))
}

func (m merchantDo) Clauses(conds ...clause.Expression) IMerchantDo {
	return m.withDO(m.DO.Clauses(conds...))
}

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

func (m merchantDo) Not(conds ...gen.Condition) IMerchantDo {
	return m.withDO(m.DO.Not(conds...))
}

func (m merchantDo) Or(conds ...gen.Condition) IMerchantDo {
	return m.withDO(m.DO.Or(conds...))
}

func (m merchantDo) Select(conds ...field.Expr) IMerchantDo {
	return m.withDO(m.DO.Select(conds...))
}

func (m merchantDo) Where(conds ...gen.Condition) IMerchantDo {
	return m.withDO(m.DO.Where(conds...))
}

func (m merchantDo) Order(conds ...field.Expr) IMerchantDo {
	return m.withDO(m.DO.Order(conds...))
}

func (m merchantDo) Distinct(cols ...field.Expr) IMerchantDo {
	return m.withDO(m.DO.Distinct(cols...))
}

func (m merchantDo) Omit(cols ...field.Expr) IMerchantDo {
	return m.withDO(m.DO.Omit(cols...))
}

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

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

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

func (m merchantDo) Group(cols ...field.Expr) IMerchantDo {
	return m.withDO(m.DO.Group(cols...))
}

func (m merchantDo) Having(conds ...gen.Condition) IMerchantDo {
	return m.withDO(m.DO.Having(conds...))
}

func (m merchantDo) Limit(limit int) IMerchantDo {
	return m.withDO(m.DO.Limit(limit))
}

func (m merchantDo) Offset(offset int) IMerchantDo {
	return m.withDO(m.DO.Offset(offset))
}

func (m merchantDo) Scopes(funcs ...func(gen.Dao) gen.Dao) IMerchantDo {
	return m.withDO(m.DO.Scopes(funcs...))
}

func (m merchantDo) Unscoped() IMerchantDo {
	return m.withDO(m.DO.Unscoped())
}

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

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

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

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

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

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

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

func (m merchantDo) Attrs(attrs ...field.AssignExpr) IMerchantDo {
	return m.withDO(m.DO.Attrs(attrs...))
}

func (m merchantDo) Assign(attrs ...field.AssignExpr) IMerchantDo {
	return m.withDO(m.DO.Assign(attrs...))
}

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

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

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

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

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

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

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