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

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/bangumi/server/dal/dao"
)

func newIndexCollect(db *gorm.DB, opts ...gen.DOOption) indexCollect {
	_indexCollect := indexCollect{}

	_indexCollect.indexCollectDo.UseDB(db, opts...)
	_indexCollect.indexCollectDo.UseModel(&dao.IndexCollect{})

	tableName := _indexCollect.indexCollectDo.TableName()
	_indexCollect.ALL = field.NewAsterisk(tableName)
	_indexCollect.CltID = field.NewUint32(tableName, "idx_clt_id")
	_indexCollect.IndexID = field.NewUint32(tableName, "idx_clt_mid")
	_indexCollect.UserID = field.NewUint32(tableName, "idx_clt_uid")
	_indexCollect.CreatedTime = field.NewUint32(tableName, "idx_clt_dateline")

	_indexCollect.fillFieldMap()

	return _indexCollect
}

// indexCollect 目录收藏
type indexCollect struct {
	indexCollectDo indexCollectDo

	ALL         field.Asterisk
	CltID       field.Uint32
	IndexID     field.Uint32 // 目录ID
	UserID      field.Uint32 // 用户UID
	CreatedTime field.Uint32

	fieldMap map[string]field.Expr
}

func (i indexCollect) Table(newTableName string) *indexCollect {
	i.indexCollectDo.UseTable(newTableName)
	return i.updateTableName(newTableName)
}

func (i indexCollect) As(alias string) *indexCollect {
	i.indexCollectDo.DO = *(i.indexCollectDo.As(alias).(*gen.DO))
	return i.updateTableName(alias)
}

func (i *indexCollect) updateTableName(table string) *indexCollect {
	i.ALL = field.NewAsterisk(table)
	i.CltID = field.NewUint32(table, "idx_clt_id")
	i.IndexID = field.NewUint32(table, "idx_clt_mid")
	i.UserID = field.NewUint32(table, "idx_clt_uid")
	i.CreatedTime = field.NewUint32(table, "idx_clt_dateline")

	i.fillFieldMap()

	return i
}

func (i *indexCollect) WithContext(ctx context.Context) *indexCollectDo {
	return i.indexCollectDo.WithContext(ctx)
}

func (i indexCollect) TableName() string { return i.indexCollectDo.TableName() }

func (i indexCollect) Alias() string { return i.indexCollectDo.Alias() }

func (i indexCollect) Columns(cols ...field.Expr) gen.Columns {
	return i.indexCollectDo.Columns(cols...)
}

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

func (i *indexCollect) fillFieldMap() {
	i.fieldMap = make(map[string]field.Expr, 4)
	i.fieldMap["idx_clt_id"] = i.CltID
	i.fieldMap["idx_clt_mid"] = i.IndexID
	i.fieldMap["idx_clt_uid"] = i.UserID
	i.fieldMap["idx_clt_dateline"] = i.CreatedTime
}

func (i indexCollect) clone(db *gorm.DB) indexCollect {
	i.indexCollectDo.ReplaceConnPool(db.Statement.ConnPool)
	return i
}

func (i indexCollect) replaceDB(db *gorm.DB) indexCollect {
	i.indexCollectDo.ReplaceDB(db)
	return i
}

type indexCollectDo struct{ gen.DO }

func (i indexCollectDo) Debug() *indexCollectDo {
	return i.withDO(i.DO.Debug())
}

func (i indexCollectDo) WithContext(ctx context.Context) *indexCollectDo {
	return i.withDO(i.DO.WithContext(ctx))
}

func (i indexCollectDo) ReadDB() *indexCollectDo {
	return i.Clauses(dbresolver.Read)
}

func (i indexCollectDo) WriteDB() *indexCollectDo {
	return i.Clauses(dbresolver.Write)
}

func (i indexCollectDo) Session(config *gorm.Session) *indexCollectDo {
	return i.withDO(i.DO.Session(config))
}

func (i indexCollectDo) Clauses(conds ...clause.Expression) *indexCollectDo {
	return i.withDO(i.DO.Clauses(conds...))
}

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

func (i indexCollectDo) Not(conds ...gen.Condition) *indexCollectDo {
	return i.withDO(i.DO.Not(conds...))
}

func (i indexCollectDo) Or(conds ...gen.Condition) *indexCollectDo {
	return i.withDO(i.DO.Or(conds...))
}

func (i indexCollectDo) Select(conds ...field.Expr) *indexCollectDo {
	return i.withDO(i.DO.Select(conds...))
}

func (i indexCollectDo) Where(conds ...gen.Condition) *indexCollectDo {
	return i.withDO(i.DO.Where(conds...))
}

func (i indexCollectDo) Order(conds ...field.Expr) *indexCollectDo {
	return i.withDO(i.DO.Order(conds...))
}

func (i indexCollectDo) Distinct(cols ...field.Expr) *indexCollectDo {
	return i.withDO(i.DO.Distinct(cols...))
}

func (i indexCollectDo) Omit(cols ...field.Expr) *indexCollectDo {
	return i.withDO(i.DO.Omit(cols...))
}

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

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

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

func (i indexCollectDo) Group(cols ...field.Expr) *indexCollectDo {
	return i.withDO(i.DO.Group(cols...))
}

func (i indexCollectDo) Having(conds ...gen.Condition) *indexCollectDo {
	return i.withDO(i.DO.Having(conds...))
}

func (i indexCollectDo) Limit(limit int) *indexCollectDo {
	return i.withDO(i.DO.Limit(limit))
}

func (i indexCollectDo) Offset(offset int) *indexCollectDo {
	return i.withDO(i.DO.Offset(offset))
}

func (i indexCollectDo) Scopes(funcs ...func(gen.Dao) gen.Dao) *indexCollectDo {
	return i.withDO(i.DO.Scopes(funcs...))
}

func (i indexCollectDo) Unscoped() *indexCollectDo {
	return i.withDO(i.DO.Unscoped())
}

func (i indexCollectDo) Create(values ...*dao.IndexCollect) error {
	if len(values) == 0 {
		return nil
	}
	return i.DO.Create(values)
}

func (i indexCollectDo) CreateInBatches(values []*dao.IndexCollect, batchSize int) error {
	return i.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 (i indexCollectDo) Save(values ...*dao.IndexCollect) error {
	if len(values) == 0 {
		return nil
	}
	return i.DO.Save(values)
}

func (i indexCollectDo) First() (*dao.IndexCollect, error) {
	if result, err := i.DO.First(); err != nil {
		return nil, err
	} else {
		return result.(*dao.IndexCollect), nil
	}
}

func (i indexCollectDo) Take() (*dao.IndexCollect, error) {
	if result, err := i.DO.Take(); err != nil {
		return nil, err
	} else {
		return result.(*dao.IndexCollect), nil
	}
}

func (i indexCollectDo) Last() (*dao.IndexCollect, error) {
	if result, err := i.DO.Last(); err != nil {
		return nil, err
	} else {
		return result.(*dao.IndexCollect), nil
	}
}

func (i indexCollectDo) Find() ([]*dao.IndexCollect, error) {
	result, err := i.DO.Find()
	return result.([]*dao.IndexCollect), err
}

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

func (i indexCollectDo) Attrs(attrs ...field.AssignExpr) *indexCollectDo {
	return i.withDO(i.DO.Attrs(attrs...))
}

func (i indexCollectDo) Assign(attrs ...field.AssignExpr) *indexCollectDo {
	return i.withDO(i.DO.Assign(attrs...))
}

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

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

func (i indexCollectDo) FirstOrInit() (*dao.IndexCollect, error) {
	if result, err := i.DO.FirstOrInit(); err != nil {
		return nil, err
	} else {
		return result.(*dao.IndexCollect), nil
	}
}

func (i indexCollectDo) FirstOrCreate() (*dao.IndexCollect, error) {
	if result, err := i.DO.FirstOrCreate(); err != nil {
		return nil, err
	} else {
		return result.(*dao.IndexCollect), nil
	}
}

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

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

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

func (i indexCollectDo) Scan(result interface{}) (err error) {
	return i.DO.Scan(result)
}

func (i indexCollectDo) Delete(models ...*dao.IndexCollect) (result gen.ResultInfo, err error) {
	return i.DO.Delete(models)
}

func (i *indexCollectDo) withDO(do gen.Dao) *indexCollectDo {
	i.DO = *do.(*gen.DO)
	return i
}
