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

	_userAddresses.userAddressesDo.UseDB(db, opts...)
	_userAddresses.userAddressesDo.UseModel(&model.UserAddresses{})

	tableName := _userAddresses.userAddressesDo.TableName()
	_userAddresses.ALL = field.NewAsterisk(tableName)
	_userAddresses.ID = field.NewInt64(tableName, "id")
	_userAddresses.CreatedAt = field.NewTime(tableName, "created_at")
	_userAddresses.UpdatedAt = field.NewTime(tableName, "updated_at")
	_userAddresses.UserID = field.NewInt64(tableName, "user_id")
	_userAddresses.Name = field.NewString(tableName, "name")
	_userAddresses.Mobile = field.NewString(tableName, "mobile")
	_userAddresses.Province = field.NewString(tableName, "province")
	_userAddresses.City = field.NewString(tableName, "city")
	_userAddresses.District = field.NewString(tableName, "district")
	_userAddresses.Address = field.NewString(tableName, "address")
	_userAddresses.IsDefault = field.NewInt32(tableName, "is_default")
	_userAddresses.DefaultUserUnique = field.NewInt64(tableName, "default_user_unique")

	_userAddresses.fillFieldMap()

	return _userAddresses
}

// userAddresses 用户地址
type userAddresses struct {
	userAddressesDo

	ALL               field.Asterisk
	ID                field.Int64  // 主键ID
	CreatedAt         field.Time   // 创建时间
	UpdatedAt         field.Time   // 更新时间
	UserID            field.Int64  // 用户ID（user_members.id）
	Name              field.String // 收件人姓名
	Mobile            field.String // 收件人手机号
	Province          field.String // 省
	City              field.String // 市
	District          field.String // 区/县
	Address           field.String // 详细地址
	IsDefault         field.Int32  // 是否默认地址：0否 1是
	DefaultUserUnique field.Int64  // 用于唯一约束的生成列：默认地址→user_id

	fieldMap map[string]field.Expr
}

func (u userAddresses) Table(newTableName string) *userAddresses {
	u.userAddressesDo.UseTable(newTableName)
	return u.updateTableName(newTableName)
}

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

func (u *userAddresses) updateTableName(table string) *userAddresses {
	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.Name = field.NewString(table, "name")
	u.Mobile = field.NewString(table, "mobile")
	u.Province = field.NewString(table, "province")
	u.City = field.NewString(table, "city")
	u.District = field.NewString(table, "district")
	u.Address = field.NewString(table, "address")
	u.IsDefault = field.NewInt32(table, "is_default")
	u.DefaultUserUnique = field.NewInt64(table, "default_user_unique")

	u.fillFieldMap()

	return u
}

func (u *userAddresses) 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 *userAddresses) fillFieldMap() {
	u.fieldMap = make(map[string]field.Expr, 12)
	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["name"] = u.Name
	u.fieldMap["mobile"] = u.Mobile
	u.fieldMap["province"] = u.Province
	u.fieldMap["city"] = u.City
	u.fieldMap["district"] = u.District
	u.fieldMap["address"] = u.Address
	u.fieldMap["is_default"] = u.IsDefault
	u.fieldMap["default_user_unique"] = u.DefaultUserUnique
}

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

func (u userAddresses) replaceDB(db *gorm.DB) userAddresses {
	u.userAddressesDo.ReplaceDB(db)
	return u
}

type userAddressesDo struct{ gen.DO }

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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