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

import (
	"context"

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

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

	"gorm.io/plugin/dbresolver"

	"bindbox-game/internal/repository/mysql/model"
)

func newPaymentPreorders(db *gorm.DB, opts ...gen.DOOption) paymentPreorders {
	_paymentPreorders := paymentPreorders{}

	_paymentPreorders.paymentPreordersDo.UseDB(db, opts...)
	_paymentPreorders.paymentPreordersDo.UseModel(&model.PaymentPreorders{})

	tableName := _paymentPreorders.paymentPreordersDo.TableName()
	_paymentPreorders.ALL = field.NewAsterisk(tableName)
	_paymentPreorders.ID = field.NewInt64(tableName, "id")
	_paymentPreorders.OrderID = field.NewInt64(tableName, "order_id")
	_paymentPreorders.OrderNo = field.NewString(tableName, "order_no")
	_paymentPreorders.Channel = field.NewString(tableName, "channel")
	_paymentPreorders.PrepayID = field.NewString(tableName, "prepay_id")
	_paymentPreorders.OutTradeNo = field.NewString(tableName, "out_trade_no")
	_paymentPreorders.AmountTotal = field.NewInt64(tableName, "amount_total")
	_paymentPreorders.PayerOpenid = field.NewString(tableName, "payer_openid")
	_paymentPreorders.Status = field.NewString(tableName, "status")
	_paymentPreorders.NotifyURL = field.NewString(tableName, "notify_url")
	_paymentPreorders.CreatedAt = field.NewTime(tableName, "created_at")
	_paymentPreorders.ExpiredAt = field.NewTime(tableName, "expired_at")

	_paymentPreorders.fillFieldMap()

	return _paymentPreorders
}

type paymentPreorders struct {
	paymentPreordersDo

	ALL         field.Asterisk
	ID          field.Int64
	OrderID     field.Int64
	OrderNo     field.String
	Channel     field.String
	PrepayID    field.String
	OutTradeNo  field.String
	AmountTotal field.Int64
	PayerOpenid field.String
	Status      field.String
	NotifyURL   field.String
	CreatedAt   field.Time
	ExpiredAt   field.Time

	fieldMap map[string]field.Expr
}

func (p paymentPreorders) Table(newTableName string) *paymentPreorders {
	p.paymentPreordersDo.UseTable(newTableName)
	return p.updateTableName(newTableName)
}

func (p paymentPreorders) As(alias string) *paymentPreorders {
	p.paymentPreordersDo.DO = *(p.paymentPreordersDo.As(alias).(*gen.DO))
	return p.updateTableName(alias)
}

func (p *paymentPreorders) updateTableName(table string) *paymentPreorders {
	p.ALL = field.NewAsterisk(table)
	p.ID = field.NewInt64(table, "id")
	p.OrderID = field.NewInt64(table, "order_id")
	p.OrderNo = field.NewString(table, "order_no")
	p.Channel = field.NewString(table, "channel")
	p.PrepayID = field.NewString(table, "prepay_id")
	p.OutTradeNo = field.NewString(table, "out_trade_no")
	p.AmountTotal = field.NewInt64(table, "amount_total")
	p.PayerOpenid = field.NewString(table, "payer_openid")
	p.Status = field.NewString(table, "status")
	p.NotifyURL = field.NewString(table, "notify_url")
	p.CreatedAt = field.NewTime(table, "created_at")
	p.ExpiredAt = field.NewTime(table, "expired_at")

	p.fillFieldMap()

	return p
}

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

func (p *paymentPreorders) fillFieldMap() {
	p.fieldMap = make(map[string]field.Expr, 12)
	p.fieldMap["id"] = p.ID
	p.fieldMap["order_id"] = p.OrderID
	p.fieldMap["order_no"] = p.OrderNo
	p.fieldMap["channel"] = p.Channel
	p.fieldMap["prepay_id"] = p.PrepayID
	p.fieldMap["out_trade_no"] = p.OutTradeNo
	p.fieldMap["amount_total"] = p.AmountTotal
	p.fieldMap["payer_openid"] = p.PayerOpenid
	p.fieldMap["status"] = p.Status
	p.fieldMap["notify_url"] = p.NotifyURL
	p.fieldMap["created_at"] = p.CreatedAt
	p.fieldMap["expired_at"] = p.ExpiredAt
}

func (p paymentPreorders) clone(db *gorm.DB) paymentPreorders {
	p.paymentPreordersDo.ReplaceConnPool(db.Statement.ConnPool)
	return p
}

func (p paymentPreorders) replaceDB(db *gorm.DB) paymentPreorders {
	p.paymentPreordersDo.ReplaceDB(db)
	return p
}

type paymentPreordersDo struct{ gen.DO }

func (p paymentPreordersDo) Debug() *paymentPreordersDo {
	return p.withDO(p.DO.Debug())
}

func (p paymentPreordersDo) WithContext(ctx context.Context) *paymentPreordersDo {
	return p.withDO(p.DO.WithContext(ctx))
}

func (p paymentPreordersDo) ReadDB() *paymentPreordersDo {
	return p.Clauses(dbresolver.Read)
}

func (p paymentPreordersDo) WriteDB() *paymentPreordersDo {
	return p.Clauses(dbresolver.Write)
}

func (p paymentPreordersDo) Session(config *gorm.Session) *paymentPreordersDo {
	return p.withDO(p.DO.Session(config))
}

func (p paymentPreordersDo) Clauses(conds ...clause.Expression) *paymentPreordersDo {
	return p.withDO(p.DO.Clauses(conds...))
}

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

func (p paymentPreordersDo) Not(conds ...gen.Condition) *paymentPreordersDo {
	return p.withDO(p.DO.Not(conds...))
}

func (p paymentPreordersDo) Or(conds ...gen.Condition) *paymentPreordersDo {
	return p.withDO(p.DO.Or(conds...))
}

func (p paymentPreordersDo) Select(conds ...field.Expr) *paymentPreordersDo {
	return p.withDO(p.DO.Select(conds...))
}

func (p paymentPreordersDo) Where(conds ...gen.Condition) *paymentPreordersDo {
	return p.withDO(p.DO.Where(conds...))
}

func (p paymentPreordersDo) Order(conds ...field.Expr) *paymentPreordersDo {
	return p.withDO(p.DO.Order(conds...))
}

func (p paymentPreordersDo) Distinct(cols ...field.Expr) *paymentPreordersDo {
	return p.withDO(p.DO.Distinct(cols...))
}

func (p paymentPreordersDo) Omit(cols ...field.Expr) *paymentPreordersDo {
	return p.withDO(p.DO.Omit(cols...))
}

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

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

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

func (p paymentPreordersDo) Group(cols ...field.Expr) *paymentPreordersDo {
	return p.withDO(p.DO.Group(cols...))
}

func (p paymentPreordersDo) Having(conds ...gen.Condition) *paymentPreordersDo {
	return p.withDO(p.DO.Having(conds...))
}

func (p paymentPreordersDo) Limit(limit int) *paymentPreordersDo {
	return p.withDO(p.DO.Limit(limit))
}

func (p paymentPreordersDo) Offset(offset int) *paymentPreordersDo {
	return p.withDO(p.DO.Offset(offset))
}

func (p paymentPreordersDo) Scopes(funcs ...func(gen.Dao) gen.Dao) *paymentPreordersDo {
	return p.withDO(p.DO.Scopes(funcs...))
}

func (p paymentPreordersDo) Unscoped() *paymentPreordersDo {
	return p.withDO(p.DO.Unscoped())
}

func (p paymentPreordersDo) Create(values ...*model.PaymentPreorders) error {
	if len(values) == 0 {
		return nil
	}
	return p.DO.Create(values)
}

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

func (p paymentPreordersDo) First() (*model.PaymentPreorders, error) {
	if result, err := p.DO.First(); err != nil {
		return nil, err
	} else {
		return result.(*model.PaymentPreorders), nil
	}
}

func (p paymentPreordersDo) Take() (*model.PaymentPreorders, error) {
	if result, err := p.DO.Take(); err != nil {
		return nil, err
	} else {
		return result.(*model.PaymentPreorders), nil
	}
}

func (p paymentPreordersDo) Last() (*model.PaymentPreorders, error) {
	if result, err := p.DO.Last(); err != nil {
		return nil, err
	} else {
		return result.(*model.PaymentPreorders), nil
	}
}

func (p paymentPreordersDo) Find() ([]*model.PaymentPreorders, error) {
	result, err := p.DO.Find()
	return result.([]*model.PaymentPreorders), err
}

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

func (p paymentPreordersDo) Attrs(attrs ...field.AssignExpr) *paymentPreordersDo {
	return p.withDO(p.DO.Attrs(attrs...))
}

func (p paymentPreordersDo) Assign(attrs ...field.AssignExpr) *paymentPreordersDo {
	return p.withDO(p.DO.Assign(attrs...))
}

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

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

func (p paymentPreordersDo) FirstOrInit() (*model.PaymentPreorders, error) {
	if result, err := p.DO.FirstOrInit(); err != nil {
		return nil, err
	} else {
		return result.(*model.PaymentPreorders), nil
	}
}

func (p paymentPreordersDo) FirstOrCreate() (*model.PaymentPreorders, error) {
	if result, err := p.DO.FirstOrCreate(); err != nil {
		return nil, err
	} else {
		return result.(*model.PaymentPreorders), nil
	}
}

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

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

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

func (p paymentPreordersDo) Scan(result interface{}) (err error) {
	return p.DO.Scan(result)
}

func (p paymentPreordersDo) Delete(models ...*model.PaymentPreorders) (result gen.ResultInfo, err error) {
	return p.DO.Delete(models)
}

func (p *paymentPreordersDo) withDO(do gen.Dao) *paymentPreordersDo {
	p.DO = *do.(*gen.DO)
	return p
}
