// 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"

	"mini-chat/internal/repository/mysql/model"
)

func newAppUser(db *gorm.DB, opts ...gen.DOOption) appUser {
	_appUser := appUser{}

	_appUser.appUserDo.UseDB(db, opts...)
	_appUser.appUserDo.UseModel(&model.AppUser{})

	tableName := _appUser.appUserDo.TableName()
	_appUser.ALL = field.NewAsterisk(tableName)
	_appUser.ID = field.NewInt32(tableName, "id")
	_appUser.AppID = field.NewString(tableName, "app_id")
	_appUser.UserID = field.NewString(tableName, "user_id")
	_appUser.UserName = field.NewString(tableName, "user_name")
	_appUser.UserMobile = field.NewString(tableName, "user_mobile")
	_appUser.UserAvatar = field.NewString(tableName, "user_avatar")
	_appUser.CreatedAt = field.NewTime(tableName, "created_at")

	_appUser.fillFieldMap()

	return _appUser
}

// appUser 小程序的用户表
type appUser struct {
	appUserDo

	ALL        field.Asterisk
	ID         field.Int32  // 主键ID
	AppID      field.String // 小程序ID
	UserID     field.String // 用户ID
	UserName   field.String // 用户昵称
	UserMobile field.String // 用户手机号
	UserAvatar field.String // 用户头像
	CreatedAt  field.Time   // 创建时间

	fieldMap map[string]field.Expr
}

func (a appUser) Table(newTableName string) *appUser {
	a.appUserDo.UseTable(newTableName)
	return a.updateTableName(newTableName)
}

func (a appUser) As(alias string) *appUser {
	a.appUserDo.DO = *(a.appUserDo.As(alias).(*gen.DO))
	return a.updateTableName(alias)
}

func (a *appUser) updateTableName(table string) *appUser {
	a.ALL = field.NewAsterisk(table)
	a.ID = field.NewInt32(table, "id")
	a.AppID = field.NewString(table, "app_id")
	a.UserID = field.NewString(table, "user_id")
	a.UserName = field.NewString(table, "user_name")
	a.UserMobile = field.NewString(table, "user_mobile")
	a.UserAvatar = field.NewString(table, "user_avatar")
	a.CreatedAt = field.NewTime(table, "created_at")

	a.fillFieldMap()

	return a
}

func (a *appUser) GetFieldByName(fieldName string) (field.OrderExpr, bool) {
	_f, ok := a.fieldMap[fieldName]
	if !ok || _f == nil {
		return nil, false
	}
	_oe, ok := _f.(field.OrderExpr)
	return _oe, ok
}

func (a *appUser) fillFieldMap() {
	a.fieldMap = make(map[string]field.Expr, 7)
	a.fieldMap["id"] = a.ID
	a.fieldMap["app_id"] = a.AppID
	a.fieldMap["user_id"] = a.UserID
	a.fieldMap["user_name"] = a.UserName
	a.fieldMap["user_mobile"] = a.UserMobile
	a.fieldMap["user_avatar"] = a.UserAvatar
	a.fieldMap["created_at"] = a.CreatedAt
}

func (a appUser) clone(db *gorm.DB) appUser {
	a.appUserDo.ReplaceConnPool(db.Statement.ConnPool)
	return a
}

func (a appUser) replaceDB(db *gorm.DB) appUser {
	a.appUserDo.ReplaceDB(db)
	return a
}

type appUserDo struct{ gen.DO }

func (a appUserDo) Debug() *appUserDo {
	return a.withDO(a.DO.Debug())
}

func (a appUserDo) WithContext(ctx context.Context) *appUserDo {
	return a.withDO(a.DO.WithContext(ctx))
}

func (a appUserDo) ReadDB() *appUserDo {
	return a.Clauses(dbresolver.Read)
}

func (a appUserDo) WriteDB() *appUserDo {
	return a.Clauses(dbresolver.Write)
}

func (a appUserDo) Session(config *gorm.Session) *appUserDo {
	return a.withDO(a.DO.Session(config))
}

func (a appUserDo) Clauses(conds ...clause.Expression) *appUserDo {
	return a.withDO(a.DO.Clauses(conds...))
}

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

func (a appUserDo) Not(conds ...gen.Condition) *appUserDo {
	return a.withDO(a.DO.Not(conds...))
}

func (a appUserDo) Or(conds ...gen.Condition) *appUserDo {
	return a.withDO(a.DO.Or(conds...))
}

func (a appUserDo) Select(conds ...field.Expr) *appUserDo {
	return a.withDO(a.DO.Select(conds...))
}

func (a appUserDo) Where(conds ...gen.Condition) *appUserDo {
	return a.withDO(a.DO.Where(conds...))
}

func (a appUserDo) Order(conds ...field.Expr) *appUserDo {
	return a.withDO(a.DO.Order(conds...))
}

func (a appUserDo) Distinct(cols ...field.Expr) *appUserDo {
	return a.withDO(a.DO.Distinct(cols...))
}

func (a appUserDo) Omit(cols ...field.Expr) *appUserDo {
	return a.withDO(a.DO.Omit(cols...))
}

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

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

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

func (a appUserDo) Group(cols ...field.Expr) *appUserDo {
	return a.withDO(a.DO.Group(cols...))
}

func (a appUserDo) Having(conds ...gen.Condition) *appUserDo {
	return a.withDO(a.DO.Having(conds...))
}

func (a appUserDo) Limit(limit int) *appUserDo {
	return a.withDO(a.DO.Limit(limit))
}

func (a appUserDo) Offset(offset int) *appUserDo {
	return a.withDO(a.DO.Offset(offset))
}

func (a appUserDo) Scopes(funcs ...func(gen.Dao) gen.Dao) *appUserDo {
	return a.withDO(a.DO.Scopes(funcs...))
}

func (a appUserDo) Unscoped() *appUserDo {
	return a.withDO(a.DO.Unscoped())
}

func (a appUserDo) Create(values ...*model.AppUser) error {
	if len(values) == 0 {
		return nil
	}
	return a.DO.Create(values)
}

func (a appUserDo) CreateInBatches(values []*model.AppUser, batchSize int) error {
	return a.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 (a appUserDo) Save(values ...*model.AppUser) error {
	if len(values) == 0 {
		return nil
	}
	return a.DO.Save(values)
}

func (a appUserDo) First() (*model.AppUser, error) {
	if result, err := a.DO.First(); err != nil {
		return nil, err
	} else {
		return result.(*model.AppUser), nil
	}
}

func (a appUserDo) Take() (*model.AppUser, error) {
	if result, err := a.DO.Take(); err != nil {
		return nil, err
	} else {
		return result.(*model.AppUser), nil
	}
}

func (a appUserDo) Last() (*model.AppUser, error) {
	if result, err := a.DO.Last(); err != nil {
		return nil, err
	} else {
		return result.(*model.AppUser), nil
	}
}

func (a appUserDo) Find() ([]*model.AppUser, error) {
	result, err := a.DO.Find()
	return result.([]*model.AppUser), err
}

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

func (a appUserDo) Attrs(attrs ...field.AssignExpr) *appUserDo {
	return a.withDO(a.DO.Attrs(attrs...))
}

func (a appUserDo) Assign(attrs ...field.AssignExpr) *appUserDo {
	return a.withDO(a.DO.Assign(attrs...))
}

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

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

func (a appUserDo) FirstOrInit() (*model.AppUser, error) {
	if result, err := a.DO.FirstOrInit(); err != nil {
		return nil, err
	} else {
		return result.(*model.AppUser), nil
	}
}

func (a appUserDo) FirstOrCreate() (*model.AppUser, error) {
	if result, err := a.DO.FirstOrCreate(); err != nil {
		return nil, err
	} else {
		return result.(*model.AppUser), nil
	}
}

func (a appUserDo) FindByPage(offset int, limit int) (result []*model.AppUser, count int64, err error) {
	result, err = a.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 = a.Offset(-1).Limit(-1).Count()
	return
}

func (a appUserDo) ScanByPage(result interface{}, offset int, limit int) (count int64, err error) {
	count, err = a.Count()
	if err != nil {
		return
	}

	err = a.Offset(offset).Limit(limit).Scan(result)
	return
}

func (a appUserDo) Scan(result interface{}) (err error) {
	return a.DO.Scan(result)
}

func (a appUserDo) Delete(models ...*model.AppUser) (result gen.ResultInfo, err error) {
	return a.DO.Delete(models)
}

func (a *appUserDo) withDO(do gen.Dao) *appUserDo {
	a.DO = *do.(*gen.DO)
	return a
}
