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

	_userCouponLedger.userCouponLedgerDo.UseDB(db, opts...)
	_userCouponLedger.userCouponLedgerDo.UseModel(&model.UserCouponLedger{})

	tableName := _userCouponLedger.userCouponLedgerDo.TableName()
	_userCouponLedger.ALL = field.NewAsterisk(tableName)
	_userCouponLedger.ID = field.NewInt64(tableName, "id")
	_userCouponLedger.UserID = field.NewInt64(tableName, "user_id")
	_userCouponLedger.UserCouponID = field.NewInt64(tableName, "user_coupon_id")
	_userCouponLedger.ChangeAmount = field.NewInt64(tableName, "change_amount")
	_userCouponLedger.BalanceAfter = field.NewInt64(tableName, "balance_after")
	_userCouponLedger.OrderID = field.NewInt64(tableName, "order_id")
	_userCouponLedger.Action = field.NewString(tableName, "action")
	_userCouponLedger.CreatedAt = field.NewTime(tableName, "created_at")

	_userCouponLedger.fillFieldMap()

	return _userCouponLedger
}

type userCouponLedger struct {
	userCouponLedgerDo

	ALL          field.Asterisk
	ID           field.Int64  // 主键ID
	UserID       field.Int64  // 用户ID
	UserCouponID field.Int64  // 用户优惠券ID
	ChangeAmount field.Int64  // 变动金额(分，负数表示扣减)
	BalanceAfter field.Int64  // 变动后余额(分)
	OrderID      field.Int64  // 关联订单ID
	Action       field.String // 操作类型
	CreatedAt    field.Time   // 创建时间

	fieldMap map[string]field.Expr
}

func (u userCouponLedger) Table(newTableName string) *userCouponLedger {
	u.userCouponLedgerDo.UseTable(newTableName)
	return u.updateTableName(newTableName)
}

func (u userCouponLedger) As(alias string) *userCouponLedger {
	u.userCouponLedgerDo.DO = *(u.userCouponLedgerDo.As(alias).(*gen.DO))
	return u.updateTableName(alias)
}

func (u *userCouponLedger) updateTableName(table string) *userCouponLedger {
	u.ALL = field.NewAsterisk(table)
	u.ID = field.NewInt64(table, "id")
	u.UserID = field.NewInt64(table, "user_id")
	u.UserCouponID = field.NewInt64(table, "user_coupon_id")
	u.ChangeAmount = field.NewInt64(table, "change_amount")
	u.BalanceAfter = field.NewInt64(table, "balance_after")
	u.OrderID = field.NewInt64(table, "order_id")
	u.Action = field.NewString(table, "action")
	u.CreatedAt = field.NewTime(table, "created_at")

	u.fillFieldMap()

	return u
}

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

func (u *userCouponLedger) fillFieldMap() {
	u.fieldMap = make(map[string]field.Expr, 8)
	u.fieldMap["id"] = u.ID
	u.fieldMap["user_id"] = u.UserID
	u.fieldMap["user_coupon_id"] = u.UserCouponID
	u.fieldMap["change_amount"] = u.ChangeAmount
	u.fieldMap["balance_after"] = u.BalanceAfter
	u.fieldMap["order_id"] = u.OrderID
	u.fieldMap["action"] = u.Action
	u.fieldMap["created_at"] = u.CreatedAt
}

func (u userCouponLedger) clone(db *gorm.DB) userCouponLedger {
	u.userCouponLedgerDo.ReplaceConnPool(db.Statement.ConnPool)
	return u
}

func (u userCouponLedger) replaceDB(db *gorm.DB) userCouponLedger {
	u.userCouponLedgerDo.ReplaceDB(db)
	return u
}

type userCouponLedgerDo struct{ gen.DO }

func (u userCouponLedgerDo) Debug() *userCouponLedgerDo {
	return u.withDO(u.DO.Debug())
}

func (u userCouponLedgerDo) WithContext(ctx context.Context) *userCouponLedgerDo {
	return u.withDO(u.DO.WithContext(ctx))
}

func (u userCouponLedgerDo) ReadDB() *userCouponLedgerDo {
	return u.Clauses(dbresolver.Read)
}

func (u userCouponLedgerDo) WriteDB() *userCouponLedgerDo {
	return u.Clauses(dbresolver.Write)
}

func (u userCouponLedgerDo) Session(config *gorm.Session) *userCouponLedgerDo {
	return u.withDO(u.DO.Session(config))
}

func (u userCouponLedgerDo) Clauses(conds ...clause.Expression) *userCouponLedgerDo {
	return u.withDO(u.DO.Clauses(conds...))
}

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

func (u userCouponLedgerDo) Not(conds ...gen.Condition) *userCouponLedgerDo {
	return u.withDO(u.DO.Not(conds...))
}

func (u userCouponLedgerDo) Or(conds ...gen.Condition) *userCouponLedgerDo {
	return u.withDO(u.DO.Or(conds...))
}

func (u userCouponLedgerDo) Select(conds ...field.Expr) *userCouponLedgerDo {
	return u.withDO(u.DO.Select(conds...))
}

func (u userCouponLedgerDo) Where(conds ...gen.Condition) *userCouponLedgerDo {
	return u.withDO(u.DO.Where(conds...))
}

func (u userCouponLedgerDo) Order(conds ...field.Expr) *userCouponLedgerDo {
	return u.withDO(u.DO.Order(conds...))
}

func (u userCouponLedgerDo) Distinct(cols ...field.Expr) *userCouponLedgerDo {
	return u.withDO(u.DO.Distinct(cols...))
}

func (u userCouponLedgerDo) Omit(cols ...field.Expr) *userCouponLedgerDo {
	return u.withDO(u.DO.Omit(cols...))
}

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

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

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

func (u userCouponLedgerDo) Group(cols ...field.Expr) *userCouponLedgerDo {
	return u.withDO(u.DO.Group(cols...))
}

func (u userCouponLedgerDo) Having(conds ...gen.Condition) *userCouponLedgerDo {
	return u.withDO(u.DO.Having(conds...))
}

func (u userCouponLedgerDo) Limit(limit int) *userCouponLedgerDo {
	return u.withDO(u.DO.Limit(limit))
}

func (u userCouponLedgerDo) Offset(offset int) *userCouponLedgerDo {
	return u.withDO(u.DO.Offset(offset))
}

func (u userCouponLedgerDo) Scopes(funcs ...func(gen.Dao) gen.Dao) *userCouponLedgerDo {
	return u.withDO(u.DO.Scopes(funcs...))
}

func (u userCouponLedgerDo) Unscoped() *userCouponLedgerDo {
	return u.withDO(u.DO.Unscoped())
}

func (u userCouponLedgerDo) Create(values ...*model.UserCouponLedger) error {
	if len(values) == 0 {
		return nil
	}
	return u.DO.Create(values)
}

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

func (u userCouponLedgerDo) First() (*model.UserCouponLedger, error) {
	if result, err := u.DO.First(); err != nil {
		return nil, err
	} else {
		return result.(*model.UserCouponLedger), nil
	}
}

func (u userCouponLedgerDo) Take() (*model.UserCouponLedger, error) {
	if result, err := u.DO.Take(); err != nil {
		return nil, err
	} else {
		return result.(*model.UserCouponLedger), nil
	}
}

func (u userCouponLedgerDo) Last() (*model.UserCouponLedger, error) {
	if result, err := u.DO.Last(); err != nil {
		return nil, err
	} else {
		return result.(*model.UserCouponLedger), nil
	}
}

func (u userCouponLedgerDo) Find() ([]*model.UserCouponLedger, error) {
	result, err := u.DO.Find()
	return result.([]*model.UserCouponLedger), err
}

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

func (u userCouponLedgerDo) Attrs(attrs ...field.AssignExpr) *userCouponLedgerDo {
	return u.withDO(u.DO.Attrs(attrs...))
}

func (u userCouponLedgerDo) Assign(attrs ...field.AssignExpr) *userCouponLedgerDo {
	return u.withDO(u.DO.Assign(attrs...))
}

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

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

func (u userCouponLedgerDo) FirstOrInit() (*model.UserCouponLedger, error) {
	if result, err := u.DO.FirstOrInit(); err != nil {
		return nil, err
	} else {
		return result.(*model.UserCouponLedger), nil
	}
}

func (u userCouponLedgerDo) FirstOrCreate() (*model.UserCouponLedger, error) {
	if result, err := u.DO.FirstOrCreate(); err != nil {
		return nil, err
	} else {
		return result.(*model.UserCouponLedger), nil
	}
}

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

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

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

func (u userCouponLedgerDo) Scan(result interface{}) (err error) {
	return u.DO.Scan(result)
}

func (u userCouponLedgerDo) Delete(models ...*model.UserCouponLedger) (result gen.ResultInfo, err error) {
	return u.DO.Delete(models)
}

func (u *userCouponLedgerDo) withDO(do gen.Dao) *userCouponLedgerDo {
	u.DO = *do.(*gen.DO)
	return u
}
