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

	_userPointsLedger.userPointsLedgerDo.UseDB(db, opts...)
	_userPointsLedger.userPointsLedgerDo.UseModel(&model.UserPointsLedger{})

	tableName := _userPointsLedger.userPointsLedgerDo.TableName()
	_userPointsLedger.ALL = field.NewAsterisk(tableName)
	_userPointsLedger.ID = field.NewInt64(tableName, "id")
	_userPointsLedger.CreatedAt = field.NewTime(tableName, "created_at")
	_userPointsLedger.UserID = field.NewInt64(tableName, "user_id")
	_userPointsLedger.Action = field.NewString(tableName, "action")
	_userPointsLedger.Points = field.NewInt64(tableName, "points")
	_userPointsLedger.RefTable = field.NewString(tableName, "ref_table")
	_userPointsLedger.RefID = field.NewString(tableName, "ref_id")
	_userPointsLedger.Remark = field.NewString(tableName, "remark")

	_userPointsLedger.fillFieldMap()

	return _userPointsLedger
}

// userPointsLedger 用户积分流水（总账）
type userPointsLedger struct {
	userPointsLedgerDo

	ALL       field.Asterisk
	ID        field.Int64  // 主键ID
	CreatedAt field.Time   // 创建时间
	UserID    field.Int64  // 用户ID（user_members.id）
	Action    field.String // 变更动作：signin/order_deduct/refund_restore/manual
	Points    field.Int64  // 积分变动值（正增负减）
	RefTable  field.String // 关联表名（如orders/payment_refund）
	RefID     field.String // 关联ID（如订单号/记录ID）
	Remark    field.String // 备注

	fieldMap map[string]field.Expr
}

func (u userPointsLedger) Table(newTableName string) *userPointsLedger {
	u.userPointsLedgerDo.UseTable(newTableName)
	return u.updateTableName(newTableName)
}

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

func (u *userPointsLedger) updateTableName(table string) *userPointsLedger {
	u.ALL = field.NewAsterisk(table)
	u.ID = field.NewInt64(table, "id")
	u.CreatedAt = field.NewTime(table, "created_at")
	u.UserID = field.NewInt64(table, "user_id")
	u.Action = field.NewString(table, "action")
	u.Points = field.NewInt64(table, "points")
	u.RefTable = field.NewString(table, "ref_table")
	u.RefID = field.NewString(table, "ref_id")
	u.Remark = field.NewString(table, "remark")

	u.fillFieldMap()

	return u
}

func (u *userPointsLedger) 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 *userPointsLedger) fillFieldMap() {
	u.fieldMap = make(map[string]field.Expr, 8)
	u.fieldMap["id"] = u.ID
	u.fieldMap["created_at"] = u.CreatedAt
	u.fieldMap["user_id"] = u.UserID
	u.fieldMap["action"] = u.Action
	u.fieldMap["points"] = u.Points
	u.fieldMap["ref_table"] = u.RefTable
	u.fieldMap["ref_id"] = u.RefID
	u.fieldMap["remark"] = u.Remark
}

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

func (u userPointsLedger) replaceDB(db *gorm.DB) userPointsLedger {
	u.userPointsLedgerDo.ReplaceDB(db)
	return u
}

type userPointsLedgerDo struct{ gen.DO }

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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