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

	_userItemCards.userItemCardsDo.UseDB(db, opts...)
	_userItemCards.userItemCardsDo.UseModel(&model.UserItemCards{})

	tableName := _userItemCards.userItemCardsDo.TableName()
	_userItemCards.ALL = field.NewAsterisk(tableName)
	_userItemCards.ID = field.NewInt64(tableName, "id")
	_userItemCards.CreatedAt = field.NewTime(tableName, "created_at")
	_userItemCards.UpdatedAt = field.NewTime(tableName, "updated_at")
	_userItemCards.UserID = field.NewInt64(tableName, "user_id")
	_userItemCards.CardID = field.NewInt64(tableName, "card_id")
	_userItemCards.ValidStart = field.NewTime(tableName, "valid_start")
	_userItemCards.ValidEnd = field.NewTime(tableName, "valid_end")
	_userItemCards.Status = field.NewInt32(tableName, "status")
	_userItemCards.UsedDrawLogID = field.NewInt64(tableName, "used_draw_log_id")
	_userItemCards.UsedActivityID = field.NewInt64(tableName, "used_activity_id")
	_userItemCards.UsedIssueID = field.NewInt64(tableName, "used_issue_id")
	_userItemCards.UsedAt = field.NewTime(tableName, "used_at")
	_userItemCards.Remark = field.NewString(tableName, "remark")

	_userItemCards.fillFieldMap()

	return _userItemCards
}

// userItemCards 用户持有道具卡
type userItemCards struct {
	userItemCardsDo

	ALL            field.Asterisk
	ID             field.Int64  // 主键ID
	CreatedAt      field.Time   // 创建时间
	UpdatedAt      field.Time   // 更新时间
	UserID         field.Int64  // 用户ID（users.id）
	CardID         field.Int64  // 卡模板ID（system_item_cards.id）
	ValidStart     field.Time   // 有效期开始
	ValidEnd       field.Time   // 有效期结束
	Status         field.Int32  // 状态：1未使用 2已使用 3已过期
	UsedDrawLogID  field.Int64  // 使用关联抽奖日志ID（activity_draw_logs.id）
	UsedActivityID field.Int64  // 使用时活动ID
	UsedIssueID    field.Int64  // 使用时期ID
	UsedAt         field.Time   // 使用时间
	Remark         field.String // 备注

	fieldMap map[string]field.Expr
}

func (u userItemCards) Table(newTableName string) *userItemCards {
	u.userItemCardsDo.UseTable(newTableName)
	return u.updateTableName(newTableName)
}

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

func (u *userItemCards) updateTableName(table string) *userItemCards {
	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.CardID = field.NewInt64(table, "card_id")
	u.ValidStart = field.NewTime(table, "valid_start")
	u.ValidEnd = field.NewTime(table, "valid_end")
	u.Status = field.NewInt32(table, "status")
	u.UsedDrawLogID = field.NewInt64(table, "used_draw_log_id")
	u.UsedActivityID = field.NewInt64(table, "used_activity_id")
	u.UsedIssueID = field.NewInt64(table, "used_issue_id")
	u.UsedAt = field.NewTime(table, "used_at")
	u.Remark = field.NewString(table, "remark")

	u.fillFieldMap()

	return u
}

func (u *userItemCards) 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 *userItemCards) fillFieldMap() {
	u.fieldMap = make(map[string]field.Expr, 13)
	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["card_id"] = u.CardID
	u.fieldMap["valid_start"] = u.ValidStart
	u.fieldMap["valid_end"] = u.ValidEnd
	u.fieldMap["status"] = u.Status
	u.fieldMap["used_draw_log_id"] = u.UsedDrawLogID
	u.fieldMap["used_activity_id"] = u.UsedActivityID
	u.fieldMap["used_issue_id"] = u.UsedIssueID
	u.fieldMap["used_at"] = u.UsedAt
	u.fieldMap["remark"] = u.Remark
}

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

func (u userItemCards) replaceDB(db *gorm.DB) userItemCards {
	u.userItemCardsDo.ReplaceDB(db)
	return u
}

type userItemCardsDo struct{ gen.DO }

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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