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

	"github.com/flowline-io/flowbot/internal/store/model"
)

func newPlatformUser(db *gorm.DB, opts ...gen.DOOption) platformUser {
	_platformUser := platformUser{}

	_platformUser.platformUserDo.UseDB(db, opts...)
	_platformUser.platformUserDo.UseModel(&model.PlatformUser{})

	tableName := _platformUser.platformUserDo.TableName()
	_platformUser.ALL = field.NewAsterisk(tableName)
	_platformUser.ID = field.NewInt64(tableName, "id")
	_platformUser.PlatformID = field.NewInt64(tableName, "platform_id")
	_platformUser.UserID = field.NewInt64(tableName, "user_id")
	_platformUser.Flag = field.NewString(tableName, "flag")
	_platformUser.Name = field.NewString(tableName, "name")
	_platformUser.Email = field.NewString(tableName, "email")
	_platformUser.AvatarURL = field.NewString(tableName, "avatar_url")
	_platformUser.IsBot = field.NewBool(tableName, "is_bot")
	_platformUser.CreatedAt = field.NewTime(tableName, "created_at")
	_platformUser.UpdatedAt = field.NewTime(tableName, "updated_at")

	_platformUser.fillFieldMap()

	return _platformUser
}

type platformUser struct {
	platformUserDo

	ALL        field.Asterisk
	ID         field.Int64
	PlatformID field.Int64
	UserID     field.Int64
	Flag       field.String
	Name       field.String
	Email      field.String
	AvatarURL  field.String
	IsBot      field.Bool
	CreatedAt  field.Time
	UpdatedAt  field.Time

	fieldMap map[string]field.Expr
}

func (p platformUser) Table(newTableName string) *platformUser {
	p.platformUserDo.UseTable(newTableName)
	return p.updateTableName(newTableName)
}

func (p platformUser) As(alias string) *platformUser {
	p.platformUserDo.DO = *(p.platformUserDo.As(alias).(*gen.DO))
	return p.updateTableName(alias)
}

func (p *platformUser) updateTableName(table string) *platformUser {
	p.ALL = field.NewAsterisk(table)
	p.ID = field.NewInt64(table, "id")
	p.PlatformID = field.NewInt64(table, "platform_id")
	p.UserID = field.NewInt64(table, "user_id")
	p.Flag = field.NewString(table, "flag")
	p.Name = field.NewString(table, "name")
	p.Email = field.NewString(table, "email")
	p.AvatarURL = field.NewString(table, "avatar_url")
	p.IsBot = field.NewBool(table, "is_bot")
	p.CreatedAt = field.NewTime(table, "created_at")
	p.UpdatedAt = field.NewTime(table, "updated_at")

	p.fillFieldMap()

	return p
}

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

func (p *platformUser) fillFieldMap() {
	p.fieldMap = make(map[string]field.Expr, 10)
	p.fieldMap["id"] = p.ID
	p.fieldMap["platform_id"] = p.PlatformID
	p.fieldMap["user_id"] = p.UserID
	p.fieldMap["flag"] = p.Flag
	p.fieldMap["name"] = p.Name
	p.fieldMap["email"] = p.Email
	p.fieldMap["avatar_url"] = p.AvatarURL
	p.fieldMap["is_bot"] = p.IsBot
	p.fieldMap["created_at"] = p.CreatedAt
	p.fieldMap["updated_at"] = p.UpdatedAt
}

func (p platformUser) clone(db *gorm.DB) platformUser {
	p.platformUserDo.ReplaceConnPool(db.Statement.ConnPool)
	return p
}

func (p platformUser) replaceDB(db *gorm.DB) platformUser {
	p.platformUserDo.ReplaceDB(db)
	return p
}

type platformUserDo struct{ gen.DO }

func (p platformUserDo) Debug() *platformUserDo {
	return p.withDO(p.DO.Debug())
}

func (p platformUserDo) WithContext(ctx context.Context) *platformUserDo {
	return p.withDO(p.DO.WithContext(ctx))
}

func (p platformUserDo) ReadDB() *platformUserDo {
	return p.Clauses(dbresolver.Read)
}

func (p platformUserDo) WriteDB() *platformUserDo {
	return p.Clauses(dbresolver.Write)
}

func (p platformUserDo) Session(config *gorm.Session) *platformUserDo {
	return p.withDO(p.DO.Session(config))
}

func (p platformUserDo) Clauses(conds ...clause.Expression) *platformUserDo {
	return p.withDO(p.DO.Clauses(conds...))
}

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

func (p platformUserDo) Not(conds ...gen.Condition) *platformUserDo {
	return p.withDO(p.DO.Not(conds...))
}

func (p platformUserDo) Or(conds ...gen.Condition) *platformUserDo {
	return p.withDO(p.DO.Or(conds...))
}

func (p platformUserDo) Select(conds ...field.Expr) *platformUserDo {
	return p.withDO(p.DO.Select(conds...))
}

func (p platformUserDo) Where(conds ...gen.Condition) *platformUserDo {
	return p.withDO(p.DO.Where(conds...))
}

func (p platformUserDo) Order(conds ...field.Expr) *platformUserDo {
	return p.withDO(p.DO.Order(conds...))
}

func (p platformUserDo) Distinct(cols ...field.Expr) *platformUserDo {
	return p.withDO(p.DO.Distinct(cols...))
}

func (p platformUserDo) Omit(cols ...field.Expr) *platformUserDo {
	return p.withDO(p.DO.Omit(cols...))
}

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

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

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

func (p platformUserDo) Group(cols ...field.Expr) *platformUserDo {
	return p.withDO(p.DO.Group(cols...))
}

func (p platformUserDo) Having(conds ...gen.Condition) *platformUserDo {
	return p.withDO(p.DO.Having(conds...))
}

func (p platformUserDo) Limit(limit int) *platformUserDo {
	return p.withDO(p.DO.Limit(limit))
}

func (p platformUserDo) Offset(offset int) *platformUserDo {
	return p.withDO(p.DO.Offset(offset))
}

func (p platformUserDo) Scopes(funcs ...func(gen.Dao) gen.Dao) *platformUserDo {
	return p.withDO(p.DO.Scopes(funcs...))
}

func (p platformUserDo) Unscoped() *platformUserDo {
	return p.withDO(p.DO.Unscoped())
}

func (p platformUserDo) Create(values ...*model.PlatformUser) error {
	if len(values) == 0 {
		return nil
	}
	return p.DO.Create(values)
}

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

func (p platformUserDo) First() (*model.PlatformUser, error) {
	if result, err := p.DO.First(); err != nil {
		return nil, err
	} else {
		return result.(*model.PlatformUser), nil
	}
}

func (p platformUserDo) Take() (*model.PlatformUser, error) {
	if result, err := p.DO.Take(); err != nil {
		return nil, err
	} else {
		return result.(*model.PlatformUser), nil
	}
}

func (p platformUserDo) Last() (*model.PlatformUser, error) {
	if result, err := p.DO.Last(); err != nil {
		return nil, err
	} else {
		return result.(*model.PlatformUser), nil
	}
}

func (p platformUserDo) Find() ([]*model.PlatformUser, error) {
	result, err := p.DO.Find()
	return result.([]*model.PlatformUser), err
}

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

func (p platformUserDo) Attrs(attrs ...field.AssignExpr) *platformUserDo {
	return p.withDO(p.DO.Attrs(attrs...))
}

func (p platformUserDo) Assign(attrs ...field.AssignExpr) *platformUserDo {
	return p.withDO(p.DO.Assign(attrs...))
}

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

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

func (p platformUserDo) FirstOrInit() (*model.PlatformUser, error) {
	if result, err := p.DO.FirstOrInit(); err != nil {
		return nil, err
	} else {
		return result.(*model.PlatformUser), nil
	}
}

func (p platformUserDo) FirstOrCreate() (*model.PlatformUser, error) {
	if result, err := p.DO.FirstOrCreate(); err != nil {
		return nil, err
	} else {
		return result.(*model.PlatformUser), nil
	}
}

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

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

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

func (p platformUserDo) Scan(result interface{}) (err error) {
	return p.DO.Scan(result)
}

func (p platformUserDo) Delete(models ...*model.PlatformUser) (result gen.ResultInfo, err error) {
	return p.DO.Delete(models)
}

func (p *platformUserDo) withDO(do gen.Dao) *platformUserDo {
	p.DO = *do.(*gen.DO)
	return p
}
