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

	_guild.guildDo.UseDB(db, opts...)
	_guild.guildDo.UseModel(&model.Guild{})

	tableName := _guild.guildDo.TableName()
	_guild.ALL = field.NewAsterisk(tableName)
	_guild.ID = field.NewInt64(tableName, "id")
	_guild.CreatedAt = field.NewTime(tableName, "created_at")
	_guild.Name = field.NewString(tableName, "name")
	_guild.OwnerID = field.NewInt64(tableName, "owner_id")
	_guild.Status = field.NewInt32(tableName, "status")
	_guild.Description = field.NewString(tableName, "description")
	_guild.JoinMode = field.NewInt32(tableName, "join_mode")
	_guild.ConsumeLimit = field.NewInt64(tableName, "consume_limit")
	_guild.AvatarURL = field.NewString(tableName, "avatar_url")
	_guild.IsOpen = field.NewInt32(tableName, "is_open")

	_guild.fillFieldMap()

	return _guild
}

// guild 公会
type guild struct {
	guildDo

	ALL          field.Asterisk
	ID           field.Int64  // 主键ID
	CreatedAt    field.Time   // 创建时间
	Name         field.String // 公会名称
	OwnerID      field.Int64  // 会长用户ID
	Status       field.Int32  // 状态：1正常 2解散
	Description  field.String // 工会描述
	JoinMode     field.Int32  // 加入方式 1: 审核通过 2: 自动通过 3. 消费流水
	ConsumeLimit field.Int64  // 加入工会消费流水最低限额(分)
	AvatarURL    field.String // 工会头像
	IsOpen       field.Int32  // 1 公开 2 私有

	fieldMap map[string]field.Expr
}

func (g guild) Table(newTableName string) *guild {
	g.guildDo.UseTable(newTableName)
	return g.updateTableName(newTableName)
}

func (g guild) As(alias string) *guild {
	g.guildDo.DO = *(g.guildDo.As(alias).(*gen.DO))
	return g.updateTableName(alias)
}

func (g *guild) updateTableName(table string) *guild {
	g.ALL = field.NewAsterisk(table)
	g.ID = field.NewInt64(table, "id")
	g.CreatedAt = field.NewTime(table, "created_at")
	g.Name = field.NewString(table, "name")
	g.OwnerID = field.NewInt64(table, "owner_id")
	g.Status = field.NewInt32(table, "status")
	g.Description = field.NewString(table, "description")
	g.JoinMode = field.NewInt32(table, "join_mode")
	g.ConsumeLimit = field.NewInt64(table, "consume_limit")
	g.AvatarURL = field.NewString(table, "avatar_url")
	g.IsOpen = field.NewInt32(table, "is_open")

	g.fillFieldMap()

	return g
}

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

func (g *guild) fillFieldMap() {
	g.fieldMap = make(map[string]field.Expr, 10)
	g.fieldMap["id"] = g.ID
	g.fieldMap["created_at"] = g.CreatedAt
	g.fieldMap["name"] = g.Name
	g.fieldMap["owner_id"] = g.OwnerID
	g.fieldMap["status"] = g.Status
	g.fieldMap["description"] = g.Description
	g.fieldMap["join_mode"] = g.JoinMode
	g.fieldMap["consume_limit"] = g.ConsumeLimit
	g.fieldMap["avatar_url"] = g.AvatarURL
	g.fieldMap["is_open"] = g.IsOpen
}

func (g guild) clone(db *gorm.DB) guild {
	g.guildDo.ReplaceConnPool(db.Statement.ConnPool)
	return g
}

func (g guild) replaceDB(db *gorm.DB) guild {
	g.guildDo.ReplaceDB(db)
	return g
}

type guildDo struct{ gen.DO }

func (g guildDo) Debug() *guildDo {
	return g.withDO(g.DO.Debug())
}

func (g guildDo) WithContext(ctx context.Context) *guildDo {
	return g.withDO(g.DO.WithContext(ctx))
}

func (g guildDo) ReadDB() *guildDo {
	return g.Clauses(dbresolver.Read)
}

func (g guildDo) WriteDB() *guildDo {
	return g.Clauses(dbresolver.Write)
}

func (g guildDo) Session(config *gorm.Session) *guildDo {
	return g.withDO(g.DO.Session(config))
}

func (g guildDo) Clauses(conds ...clause.Expression) *guildDo {
	return g.withDO(g.DO.Clauses(conds...))
}

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

func (g guildDo) Not(conds ...gen.Condition) *guildDo {
	return g.withDO(g.DO.Not(conds...))
}

func (g guildDo) Or(conds ...gen.Condition) *guildDo {
	return g.withDO(g.DO.Or(conds...))
}

func (g guildDo) Select(conds ...field.Expr) *guildDo {
	return g.withDO(g.DO.Select(conds...))
}

func (g guildDo) Where(conds ...gen.Condition) *guildDo {
	return g.withDO(g.DO.Where(conds...))
}

func (g guildDo) Order(conds ...field.Expr) *guildDo {
	return g.withDO(g.DO.Order(conds...))
}

func (g guildDo) Distinct(cols ...field.Expr) *guildDo {
	return g.withDO(g.DO.Distinct(cols...))
}

func (g guildDo) Omit(cols ...field.Expr) *guildDo {
	return g.withDO(g.DO.Omit(cols...))
}

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

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

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

func (g guildDo) Group(cols ...field.Expr) *guildDo {
	return g.withDO(g.DO.Group(cols...))
}

func (g guildDo) Having(conds ...gen.Condition) *guildDo {
	return g.withDO(g.DO.Having(conds...))
}

func (g guildDo) Limit(limit int) *guildDo {
	return g.withDO(g.DO.Limit(limit))
}

func (g guildDo) Offset(offset int) *guildDo {
	return g.withDO(g.DO.Offset(offset))
}

func (g guildDo) Scopes(funcs ...func(gen.Dao) gen.Dao) *guildDo {
	return g.withDO(g.DO.Scopes(funcs...))
}

func (g guildDo) Unscoped() *guildDo {
	return g.withDO(g.DO.Unscoped())
}

func (g guildDo) Create(values ...*model.Guild) error {
	if len(values) == 0 {
		return nil
	}
	return g.DO.Create(values)
}

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

func (g guildDo) First() (*model.Guild, error) {
	if result, err := g.DO.First(); err != nil {
		return nil, err
	} else {
		return result.(*model.Guild), nil
	}
}

func (g guildDo) Take() (*model.Guild, error) {
	if result, err := g.DO.Take(); err != nil {
		return nil, err
	} else {
		return result.(*model.Guild), nil
	}
}

func (g guildDo) Last() (*model.Guild, error) {
	if result, err := g.DO.Last(); err != nil {
		return nil, err
	} else {
		return result.(*model.Guild), nil
	}
}

func (g guildDo) Find() ([]*model.Guild, error) {
	result, err := g.DO.Find()
	return result.([]*model.Guild), err
}

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

func (g guildDo) Attrs(attrs ...field.AssignExpr) *guildDo {
	return g.withDO(g.DO.Attrs(attrs...))
}

func (g guildDo) Assign(attrs ...field.AssignExpr) *guildDo {
	return g.withDO(g.DO.Assign(attrs...))
}

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

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

func (g guildDo) FirstOrInit() (*model.Guild, error) {
	if result, err := g.DO.FirstOrInit(); err != nil {
		return nil, err
	} else {
		return result.(*model.Guild), nil
	}
}

func (g guildDo) FirstOrCreate() (*model.Guild, error) {
	if result, err := g.DO.FirstOrCreate(); err != nil {
		return nil, err
	} else {
		return result.(*model.Guild), nil
	}
}

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

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

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

func (g guildDo) Scan(result interface{}) (err error) {
	return g.DO.Scan(result)
}

func (g guildDo) Delete(models ...*model.Guild) (result gen.ResultInfo, err error) {
	return g.DO.Delete(models)
}

func (g *guildDo) withDO(do gen.Dao) *guildDo {
	g.DO = *do.(*gen.DO)
	return g
}
