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

	_users.usersDo.UseDB(db, opts...)
	_users.usersDo.UseModel(&model.Users{})

	tableName := _users.usersDo.TableName()
	_users.ALL = field.NewAsterisk(tableName)
	_users.ID = field.NewInt64(tableName, "id")
	_users.CreatedAt = field.NewTime(tableName, "created_at")
	_users.UpdatedAt = field.NewTime(tableName, "updated_at")
	_users.DeletedAt = field.NewField(tableName, "deleted_at")
	_users.Nickname = field.NewString(tableName, "nickname")
	_users.Avatar = field.NewString(tableName, "avatar")
	_users.Mobile = field.NewString(tableName, "mobile")
	_users.Openid = field.NewString(tableName, "openid")
	_users.Unionid = field.NewString(tableName, "unionid")
	_users.InviteCode = field.NewString(tableName, "invite_code")
	_users.InviterID = field.NewInt64(tableName, "inviter_id")
	_users.Status = field.NewInt32(tableName, "status")
	_users.DouyinID = field.NewString(tableName, "douyin_id")
	_users.ChannelID = field.NewInt64(tableName, "channel_id")
	_users.DouyinUserID = field.NewString(tableName, "douyin_user_id")

	_users.fillFieldMap()

	return _users
}

// users 用户表
type users struct {
	usersDo

	ALL          field.Asterisk
	ID           field.Int64  // 主键ID
	CreatedAt    field.Time   // 创建时间
	UpdatedAt    field.Time   // 更新时间
	DeletedAt    field.Field  // 删除时间（软删）
	Nickname     field.String // 昵称
	Avatar       field.String // 头像URL
	Mobile       field.String // 手机号
	Openid       field.String // 微信openid
	Unionid      field.String // 微信unionid
	InviteCode   field.String // 用户唯一邀请码
	InviterID    field.Int64  // 邀请人用户ID
	Status       field.Int32  // 状态：1正常 2禁用
	DouyinID     field.String
	ChannelID    field.Int64 // 渠道ID
	DouyinUserID field.String

	fieldMap map[string]field.Expr
}

func (u users) Table(newTableName string) *users {
	u.usersDo.UseTable(newTableName)
	return u.updateTableName(newTableName)
}

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

func (u *users) updateTableName(table string) *users {
	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.DeletedAt = field.NewField(table, "deleted_at")
	u.Nickname = field.NewString(table, "nickname")
	u.Avatar = field.NewString(table, "avatar")
	u.Mobile = field.NewString(table, "mobile")
	u.Openid = field.NewString(table, "openid")
	u.Unionid = field.NewString(table, "unionid")
	u.InviteCode = field.NewString(table, "invite_code")
	u.InviterID = field.NewInt64(table, "inviter_id")
	u.Status = field.NewInt32(table, "status")
	u.DouyinID = field.NewString(table, "douyin_id")
	u.ChannelID = field.NewInt64(table, "channel_id")
	u.DouyinUserID = field.NewString(table, "douyin_user_id")

	u.fillFieldMap()

	return u
}

func (u *users) 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 *users) fillFieldMap() {
	u.fieldMap = make(map[string]field.Expr, 15)
	u.fieldMap["id"] = u.ID
	u.fieldMap["created_at"] = u.CreatedAt
	u.fieldMap["updated_at"] = u.UpdatedAt
	u.fieldMap["deleted_at"] = u.DeletedAt
	u.fieldMap["nickname"] = u.Nickname
	u.fieldMap["avatar"] = u.Avatar
	u.fieldMap["mobile"] = u.Mobile
	u.fieldMap["openid"] = u.Openid
	u.fieldMap["unionid"] = u.Unionid
	u.fieldMap["invite_code"] = u.InviteCode
	u.fieldMap["inviter_id"] = u.InviterID
	u.fieldMap["status"] = u.Status
	u.fieldMap["douyin_id"] = u.DouyinID
	u.fieldMap["channel_id"] = u.ChannelID
	u.fieldMap["douyin_user_id"] = u.DouyinUserID
}

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

func (u users) replaceDB(db *gorm.DB) users {
	u.usersDo.ReplaceDB(db)
	return u
}

type usersDo struct{ gen.DO }

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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