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

	_userCoupons.userCouponsDo.UseDB(db, opts...)
	_userCoupons.userCouponsDo.UseModel(&model.UserCoupons{})

	tableName := _userCoupons.userCouponsDo.TableName()
	_userCoupons.ALL = field.NewAsterisk(tableName)
	_userCoupons.ID = field.NewInt64(tableName, "id")
	_userCoupons.CreatedAt = field.NewTime(tableName, "created_at")
	_userCoupons.UpdatedAt = field.NewTime(tableName, "updated_at")
	_userCoupons.UserID = field.NewInt64(tableName, "user_id")
	_userCoupons.CouponID = field.NewInt64(tableName, "coupon_id")
	_userCoupons.UsedOrderID = field.NewInt64(tableName, "used_order_id")
	_userCoupons.UsedAt = field.NewTime(tableName, "used_at")
	_userCoupons.ValidStart = field.NewTime(tableName, "valid_start")
	_userCoupons.ValidEnd = field.NewTime(tableName, "valid_end")
	_userCoupons.Status = field.NewInt32(tableName, "status")
	_userCoupons.BalanceAmount = field.NewInt64(tableName, "balance_amount")

	_userCoupons.fillFieldMap()

	return _userCoupons
}

// userCoupons 用户持券
type userCoupons struct {
	userCouponsDo

	ALL           field.Asterisk
	ID            field.Int64 // 主键ID
	CreatedAt     field.Time  // 创建时间
	UpdatedAt     field.Time  // 更新时间
	UserID        field.Int64 // 用户ID（user_members.id）
	CouponID      field.Int64 // 券模板ID（system_coupons.id）
	UsedOrderID   field.Int64 // 核销的订单ID（orders.id）
	UsedAt        field.Time  // 核销时间
	ValidStart    field.Time  // 有效期开始
	ValidEnd      field.Time  // 有效期结束
	Status        field.Int32 // 状态：1未使用 2已使用 3已过期
	BalanceAmount field.Int64 // 直金额券剩余额度(分)

	fieldMap map[string]field.Expr
}

func (u userCoupons) Table(newTableName string) *userCoupons {
	u.userCouponsDo.UseTable(newTableName)
	return u.updateTableName(newTableName)
}

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

func (u *userCoupons) updateTableName(table string) *userCoupons {
	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.CouponID = field.NewInt64(table, "coupon_id")
	u.UsedOrderID = field.NewInt64(table, "used_order_id")
	u.UsedAt = field.NewTime(table, "used_at")
	u.ValidStart = field.NewTime(table, "valid_start")
	u.ValidEnd = field.NewTime(table, "valid_end")
	u.Status = field.NewInt32(table, "status")
	u.BalanceAmount = field.NewInt64(table, "balance_amount")

	u.fillFieldMap()

	return u
}

func (u *userCoupons) 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 *userCoupons) fillFieldMap() {
	u.fieldMap = make(map[string]field.Expr, 11)
	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["coupon_id"] = u.CouponID
	u.fieldMap["used_order_id"] = u.UsedOrderID
	u.fieldMap["used_at"] = u.UsedAt
	u.fieldMap["valid_start"] = u.ValidStart
	u.fieldMap["valid_end"] = u.ValidEnd
	u.fieldMap["status"] = u.Status
	u.fieldMap["balance_amount"] = u.BalanceAmount
}

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

func (u userCoupons) replaceDB(db *gorm.DB) userCoupons {
	u.userCouponsDo.ReplaceDB(db)
	return u
}

type userCouponsDo struct{ gen.DO }

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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