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

	_banner.bannerDo.UseDB(db, opts...)
	_banner.bannerDo.UseModel(&model.Banner{})

	tableName := _banner.bannerDo.TableName()
	_banner.ALL = field.NewAsterisk(tableName)
	_banner.ID = field.NewInt64(tableName, "id")
	_banner.Title = field.NewString(tableName, "title")
	_banner.ImageURL = field.NewString(tableName, "image_url")
	_banner.LinkURL = field.NewString(tableName, "link_url")
	_banner.Sort = field.NewInt32(tableName, "sort")
	_banner.Status = field.NewInt32(tableName, "status")
	_banner.CreatedAt = field.NewTime(tableName, "created_at")
	_banner.UpdatedAt = field.NewTime(tableName, "updated_at")
	_banner.DeletedAt = field.NewField(tableName, "deleted_at")

	_banner.fillFieldMap()

	return _banner
}

// banner 轮播图
type banner struct {
	bannerDo

	ALL       field.Asterisk
	ID        field.Int64
	Title     field.String // 标题
	ImageURL  field.String // 图片URL
	LinkURL   field.String // 跳转链接（可选）
	Sort      field.Int32  // 排序，越小越靠前
	Status    field.Int32  // 状态：1启用 0禁用
	CreatedAt field.Time
	UpdatedAt field.Time
	DeletedAt field.Field

	fieldMap map[string]field.Expr
}

func (b banner) Table(newTableName string) *banner {
	b.bannerDo.UseTable(newTableName)
	return b.updateTableName(newTableName)
}

func (b banner) As(alias string) *banner {
	b.bannerDo.DO = *(b.bannerDo.As(alias).(*gen.DO))
	return b.updateTableName(alias)
}

func (b *banner) updateTableName(table string) *banner {
	b.ALL = field.NewAsterisk(table)
	b.ID = field.NewInt64(table, "id")
	b.Title = field.NewString(table, "title")
	b.ImageURL = field.NewString(table, "image_url")
	b.LinkURL = field.NewString(table, "link_url")
	b.Sort = field.NewInt32(table, "sort")
	b.Status = field.NewInt32(table, "status")
	b.CreatedAt = field.NewTime(table, "created_at")
	b.UpdatedAt = field.NewTime(table, "updated_at")
	b.DeletedAt = field.NewField(table, "deleted_at")

	b.fillFieldMap()

	return b
}

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

func (b *banner) fillFieldMap() {
	b.fieldMap = make(map[string]field.Expr, 9)
	b.fieldMap["id"] = b.ID
	b.fieldMap["title"] = b.Title
	b.fieldMap["image_url"] = b.ImageURL
	b.fieldMap["link_url"] = b.LinkURL
	b.fieldMap["sort"] = b.Sort
	b.fieldMap["status"] = b.Status
	b.fieldMap["created_at"] = b.CreatedAt
	b.fieldMap["updated_at"] = b.UpdatedAt
	b.fieldMap["deleted_at"] = b.DeletedAt
}

func (b banner) clone(db *gorm.DB) banner {
	b.bannerDo.ReplaceConnPool(db.Statement.ConnPool)
	return b
}

func (b banner) replaceDB(db *gorm.DB) banner {
	b.bannerDo.ReplaceDB(db)
	return b
}

type bannerDo struct{ gen.DO }

func (b bannerDo) Debug() *bannerDo {
	return b.withDO(b.DO.Debug())
}

func (b bannerDo) WithContext(ctx context.Context) *bannerDo {
	return b.withDO(b.DO.WithContext(ctx))
}

func (b bannerDo) ReadDB() *bannerDo {
	return b.Clauses(dbresolver.Read)
}

func (b bannerDo) WriteDB() *bannerDo {
	return b.Clauses(dbresolver.Write)
}

func (b bannerDo) Session(config *gorm.Session) *bannerDo {
	return b.withDO(b.DO.Session(config))
}

func (b bannerDo) Clauses(conds ...clause.Expression) *bannerDo {
	return b.withDO(b.DO.Clauses(conds...))
}

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

func (b bannerDo) Not(conds ...gen.Condition) *bannerDo {
	return b.withDO(b.DO.Not(conds...))
}

func (b bannerDo) Or(conds ...gen.Condition) *bannerDo {
	return b.withDO(b.DO.Or(conds...))
}

func (b bannerDo) Select(conds ...field.Expr) *bannerDo {
	return b.withDO(b.DO.Select(conds...))
}

func (b bannerDo) Where(conds ...gen.Condition) *bannerDo {
	return b.withDO(b.DO.Where(conds...))
}

func (b bannerDo) Order(conds ...field.Expr) *bannerDo {
	return b.withDO(b.DO.Order(conds...))
}

func (b bannerDo) Distinct(cols ...field.Expr) *bannerDo {
	return b.withDO(b.DO.Distinct(cols...))
}

func (b bannerDo) Omit(cols ...field.Expr) *bannerDo {
	return b.withDO(b.DO.Omit(cols...))
}

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

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

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

func (b bannerDo) Group(cols ...field.Expr) *bannerDo {
	return b.withDO(b.DO.Group(cols...))
}

func (b bannerDo) Having(conds ...gen.Condition) *bannerDo {
	return b.withDO(b.DO.Having(conds...))
}

func (b bannerDo) Limit(limit int) *bannerDo {
	return b.withDO(b.DO.Limit(limit))
}

func (b bannerDo) Offset(offset int) *bannerDo {
	return b.withDO(b.DO.Offset(offset))
}

func (b bannerDo) Scopes(funcs ...func(gen.Dao) gen.Dao) *bannerDo {
	return b.withDO(b.DO.Scopes(funcs...))
}

func (b bannerDo) Unscoped() *bannerDo {
	return b.withDO(b.DO.Unscoped())
}

func (b bannerDo) Create(values ...*model.Banner) error {
	if len(values) == 0 {
		return nil
	}
	return b.DO.Create(values)
}

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

func (b bannerDo) First() (*model.Banner, error) {
	if result, err := b.DO.First(); err != nil {
		return nil, err
	} else {
		return result.(*model.Banner), nil
	}
}

func (b bannerDo) Take() (*model.Banner, error) {
	if result, err := b.DO.Take(); err != nil {
		return nil, err
	} else {
		return result.(*model.Banner), nil
	}
}

func (b bannerDo) Last() (*model.Banner, error) {
	if result, err := b.DO.Last(); err != nil {
		return nil, err
	} else {
		return result.(*model.Banner), nil
	}
}

func (b bannerDo) Find() ([]*model.Banner, error) {
	result, err := b.DO.Find()
	return result.([]*model.Banner), err
}

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

func (b bannerDo) Attrs(attrs ...field.AssignExpr) *bannerDo {
	return b.withDO(b.DO.Attrs(attrs...))
}

func (b bannerDo) Assign(attrs ...field.AssignExpr) *bannerDo {
	return b.withDO(b.DO.Assign(attrs...))
}

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

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

func (b bannerDo) FirstOrInit() (*model.Banner, error) {
	if result, err := b.DO.FirstOrInit(); err != nil {
		return nil, err
	} else {
		return result.(*model.Banner), nil
	}
}

func (b bannerDo) FirstOrCreate() (*model.Banner, error) {
	if result, err := b.DO.FirstOrCreate(); err != nil {
		return nil, err
	} else {
		return result.(*model.Banner), nil
	}
}

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

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

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

func (b bannerDo) Scan(result interface{}) (err error) {
	return b.DO.Scan(result)
}

func (b bannerDo) Delete(models ...*model.Banner) (result gen.ResultInfo, err error) {
	return b.DO.Delete(models)
}

func (b *bannerDo) withDO(do gen.Dao) *bannerDo {
	b.DO = *do.(*gen.DO)
	return b
}
