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

	_userPoints.userPointsDo.UseDB(db, opts...)
	_userPoints.userPointsDo.UseModel(&model.UserPoints{})

	tableName := _userPoints.userPointsDo.TableName()
	_userPoints.ALL = field.NewAsterisk(tableName)
	_userPoints.ID = field.NewInt64(tableName, "id")
	_userPoints.CreatedAt = field.NewTime(tableName, "created_at")
	_userPoints.UpdatedAt = field.NewTime(tableName, "updated_at")
	_userPoints.UserID = field.NewInt64(tableName, "user_id")
	_userPoints.Kind = field.NewString(tableName, "kind")
	_userPoints.Points = field.NewInt64(tableName, "points")
	_userPoints.ValidStart = field.NewTime(tableName, "valid_start")
	_userPoints.ValidEnd = field.NewTime(tableName, "valid_end")
	_userPoints.Version = field.NewInt32(tableName, "version")

	_userPoints.fillFieldMap()

	return _userPoints
}

// userPoints 用户积分（余额表）
type userPoints struct {
	userPointsDo

	ALL        field.Asterisk
	ID         field.Int64  // 主键ID
	CreatedAt  field.Time   // 创建时间
	UpdatedAt  field.Time   // 更新时间
	UserID     field.Int64  // 用户ID（
	Kind       field.String // 积分类别：signin、task、exchange等
	Points     field.Int64  // 积分值（整数点）
	ValidStart field.Time   // 有效期开始（用于每日一次等约束）
	ValidEnd   field.Time   // 有效期结束
	Version    field.Int32  // 乐观锁版本号

	fieldMap map[string]field.Expr
}

func (u userPoints) Table(newTableName string) *userPoints {
	u.userPointsDo.UseTable(newTableName)
	return u.updateTableName(newTableName)
}

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

func (u *userPoints) updateTableName(table string) *userPoints {
	u.ALL = field.NewAsterisk(table)
	u.ID = field.NewInt64(table, "id")
	u.CreatedAt = field.NewTime(table, "created_at")
	u.UpdatedAt = field.NewTime(table, "updated_at")
	u.UserID = field.NewInt64(table, "user_id")
	u.Kind = field.NewString(table, "kind")
	u.Points = field.NewInt64(table, "points")
	u.ValidStart = field.NewTime(table, "valid_start")
	u.ValidEnd = field.NewTime(table, "valid_end")
	u.Version = field.NewInt32(table, "version")

	u.fillFieldMap()

	return u
}

func (u *userPoints) 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 *userPoints) fillFieldMap() {
	u.fieldMap = make(map[string]field.Expr, 9)
	u.fieldMap["id"] = u.ID
	u.fieldMap["created_at"] = u.CreatedAt
	u.fieldMap["updated_at"] = u.UpdatedAt
	u.fieldMap["user_id"] = u.UserID
	u.fieldMap["kind"] = u.Kind
	u.fieldMap["points"] = u.Points
	u.fieldMap["valid_start"] = u.ValidStart
	u.fieldMap["valid_end"] = u.ValidEnd
	u.fieldMap["version"] = u.Version
}

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

func (u userPoints) replaceDB(db *gorm.DB) userPoints {
	u.userPointsDo.ReplaceDB(db)
	return u
}

type userPointsDo struct{ gen.DO }

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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