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

	_userTitles.userTitlesDo.UseDB(db, opts...)
	_userTitles.userTitlesDo.UseModel(&model.UserTitles{})

	tableName := _userTitles.userTitlesDo.TableName()
	_userTitles.ALL = field.NewAsterisk(tableName)
	_userTitles.ID = field.NewInt64(tableName, "id")
	_userTitles.UserID = field.NewInt64(tableName, "user_id")
	_userTitles.TitleID = field.NewInt64(tableName, "title_id")
	_userTitles.Active = field.NewInt32(tableName, "active")
	_userTitles.ObtainedAt = field.NewTime(tableName, "obtained_at")
	_userTitles.ExpiresAt = field.NewTime(tableName, "expires_at")
	_userTitles.Source = field.NewString(tableName, "source")
	_userTitles.Remark = field.NewString(tableName, "remark")
	_userTitles.CreatedAt = field.NewTime(tableName, "created_at")

	_userTitles.fillFieldMap()

	return _userTitles
}

// userTitles 用户持有头衔表
type userTitles struct {
	userTitlesDo

	ALL        field.Asterisk
	ID         field.Int64  // 用户持有头衔主键ID
	UserID     field.Int64  // 用户ID（users.id）
	TitleID    field.Int64  // 头衔ID（system_titles.id）
	Active     field.Int32  // 是否激活：1激活 0停用
	ObtainedAt field.Time   // 获得时间
	ExpiresAt  field.Time   // 过期时间（可空）
	Source     field.String // 来源标识（活动、运营发放等）
	Remark     field.String // 备注信息
	CreatedAt  field.Time   // 创建时间

	fieldMap map[string]field.Expr
}

func (u userTitles) Table(newTableName string) *userTitles {
	u.userTitlesDo.UseTable(newTableName)
	return u.updateTableName(newTableName)
}

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

func (u *userTitles) updateTableName(table string) *userTitles {
	u.ALL = field.NewAsterisk(table)
	u.ID = field.NewInt64(table, "id")
	u.UserID = field.NewInt64(table, "user_id")
	u.TitleID = field.NewInt64(table, "title_id")
	u.Active = field.NewInt32(table, "active")
	u.ObtainedAt = field.NewTime(table, "obtained_at")
	u.ExpiresAt = field.NewTime(table, "expires_at")
	u.Source = field.NewString(table, "source")
	u.Remark = field.NewString(table, "remark")
	u.CreatedAt = field.NewTime(table, "created_at")

	u.fillFieldMap()

	return u
}

func (u *userTitles) 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 *userTitles) fillFieldMap() {
	u.fieldMap = make(map[string]field.Expr, 9)
	u.fieldMap["id"] = u.ID
	u.fieldMap["user_id"] = u.UserID
	u.fieldMap["title_id"] = u.TitleID
	u.fieldMap["active"] = u.Active
	u.fieldMap["obtained_at"] = u.ObtainedAt
	u.fieldMap["expires_at"] = u.ExpiresAt
	u.fieldMap["source"] = u.Source
	u.fieldMap["remark"] = u.Remark
	u.fieldMap["created_at"] = u.CreatedAt
}

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

func (u userTitles) replaceDB(db *gorm.DB) userTitles {
	u.userTitlesDo.ReplaceDB(db)
	return u
}

type userTitlesDo struct{ gen.DO }

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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