// 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/cloudzenith/DouTok/backend/shortVideoCoreService/internal/infrastructure/persistence/model"
)

func newCollectionVideo(db *gorm.DB, opts ...gen.DOOption) collectionVideo {
	_collectionVideo := collectionVideo{}

	_collectionVideo.collectionVideoDo.UseDB(db, opts...)
	_collectionVideo.collectionVideoDo.UseModel(&model.CollectionVideo{})

	tableName := _collectionVideo.collectionVideoDo.TableName()
	_collectionVideo.ALL = field.NewAsterisk(tableName)
	_collectionVideo.ID = field.NewInt64(tableName, "id")
	_collectionVideo.CollectionID = field.NewInt64(tableName, "collection_id")
	_collectionVideo.UserID = field.NewInt64(tableName, "user_id")
	_collectionVideo.VideoID = field.NewInt64(tableName, "video_id")
	_collectionVideo.IsDeleted = field.NewBool(tableName, "is_deleted")
	_collectionVideo.CreateTime = field.NewTime(tableName, "create_time")
	_collectionVideo.UpdateTime = field.NewTime(tableName, "update_time")

	_collectionVideo.fillFieldMap()

	return _collectionVideo
}

type collectionVideo struct {
	collectionVideoDo

	ALL          field.Asterisk
	ID           field.Int64
	CollectionID field.Int64
	UserID       field.Int64
	VideoID      field.Int64
	IsDeleted    field.Bool
	CreateTime   field.Time
	UpdateTime   field.Time

	fieldMap map[string]field.Expr
}

func (c collectionVideo) Table(newTableName string) *collectionVideo {
	c.collectionVideoDo.UseTable(newTableName)
	return c.updateTableName(newTableName)
}

func (c collectionVideo) As(alias string) *collectionVideo {
	c.collectionVideoDo.DO = *(c.collectionVideoDo.As(alias).(*gen.DO))
	return c.updateTableName(alias)
}

func (c *collectionVideo) updateTableName(table string) *collectionVideo {
	c.ALL = field.NewAsterisk(table)
	c.ID = field.NewInt64(table, "id")
	c.CollectionID = field.NewInt64(table, "collection_id")
	c.UserID = field.NewInt64(table, "user_id")
	c.VideoID = field.NewInt64(table, "video_id")
	c.IsDeleted = field.NewBool(table, "is_deleted")
	c.CreateTime = field.NewTime(table, "create_time")
	c.UpdateTime = field.NewTime(table, "update_time")

	c.fillFieldMap()

	return c
}

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

func (c *collectionVideo) fillFieldMap() {
	c.fieldMap = make(map[string]field.Expr, 7)
	c.fieldMap["id"] = c.ID
	c.fieldMap["collection_id"] = c.CollectionID
	c.fieldMap["user_id"] = c.UserID
	c.fieldMap["video_id"] = c.VideoID
	c.fieldMap["is_deleted"] = c.IsDeleted
	c.fieldMap["create_time"] = c.CreateTime
	c.fieldMap["update_time"] = c.UpdateTime
}

func (c collectionVideo) clone(db *gorm.DB) collectionVideo {
	c.collectionVideoDo.ReplaceConnPool(db.Statement.ConnPool)
	return c
}

func (c collectionVideo) replaceDB(db *gorm.DB) collectionVideo {
	c.collectionVideoDo.ReplaceDB(db)
	return c
}

type collectionVideoDo struct{ gen.DO }

type ICollectionVideoDo interface {
	gen.SubQuery
	Debug() ICollectionVideoDo
	WithContext(ctx context.Context) ICollectionVideoDo
	WithResult(fc func(tx gen.Dao)) gen.ResultInfo
	ReplaceDB(db *gorm.DB)
	ReadDB() ICollectionVideoDo
	WriteDB() ICollectionVideoDo
	As(alias string) gen.Dao
	Session(config *gorm.Session) ICollectionVideoDo
	Columns(cols ...field.Expr) gen.Columns
	Clauses(conds ...clause.Expression) ICollectionVideoDo
	Not(conds ...gen.Condition) ICollectionVideoDo
	Or(conds ...gen.Condition) ICollectionVideoDo
	Select(conds ...field.Expr) ICollectionVideoDo
	Where(conds ...gen.Condition) ICollectionVideoDo
	Order(conds ...field.Expr) ICollectionVideoDo
	Distinct(cols ...field.Expr) ICollectionVideoDo
	Omit(cols ...field.Expr) ICollectionVideoDo
	Join(table schema.Tabler, on ...field.Expr) ICollectionVideoDo
	LeftJoin(table schema.Tabler, on ...field.Expr) ICollectionVideoDo
	RightJoin(table schema.Tabler, on ...field.Expr) ICollectionVideoDo
	Group(cols ...field.Expr) ICollectionVideoDo
	Having(conds ...gen.Condition) ICollectionVideoDo
	Limit(limit int) ICollectionVideoDo
	Offset(offset int) ICollectionVideoDo
	Count() (count int64, err error)
	Scopes(funcs ...func(gen.Dao) gen.Dao) ICollectionVideoDo
	Unscoped() ICollectionVideoDo
	Create(values ...*model.CollectionVideo) error
	CreateInBatches(values []*model.CollectionVideo, batchSize int) error
	Save(values ...*model.CollectionVideo) error
	First() (*model.CollectionVideo, error)
	Take() (*model.CollectionVideo, error)
	Last() (*model.CollectionVideo, error)
	Find() ([]*model.CollectionVideo, error)
	FindInBatch(batchSize int, fc func(tx gen.Dao, batch int) error) (results []*model.CollectionVideo, err error)
	FindInBatches(result *[]*model.CollectionVideo, batchSize int, fc func(tx gen.Dao, batch int) error) error
	Pluck(column field.Expr, dest interface{}) error
	Delete(...*model.CollectionVideo) (info gen.ResultInfo, err error)
	Update(column field.Expr, value interface{}) (info gen.ResultInfo, err error)
	UpdateSimple(columns ...field.AssignExpr) (info gen.ResultInfo, err error)
	Updates(value interface{}) (info gen.ResultInfo, err error)
	UpdateColumn(column field.Expr, value interface{}) (info gen.ResultInfo, err error)
	UpdateColumnSimple(columns ...field.AssignExpr) (info gen.ResultInfo, err error)
	UpdateColumns(value interface{}) (info gen.ResultInfo, err error)
	UpdateFrom(q gen.SubQuery) gen.Dao
	Attrs(attrs ...field.AssignExpr) ICollectionVideoDo
	Assign(attrs ...field.AssignExpr) ICollectionVideoDo
	Joins(fields ...field.RelationField) ICollectionVideoDo
	Preload(fields ...field.RelationField) ICollectionVideoDo
	FirstOrInit() (*model.CollectionVideo, error)
	FirstOrCreate() (*model.CollectionVideo, error)
	FindByPage(offset int, limit int) (result []*model.CollectionVideo, count int64, err error)
	ScanByPage(result interface{}, offset int, limit int) (count int64, err error)
	Scan(result interface{}) (err error)
	Returning(value interface{}, columns ...string) ICollectionVideoDo
	UnderlyingDB() *gorm.DB
	schema.Tabler
}

func (c collectionVideoDo) Debug() ICollectionVideoDo {
	return c.withDO(c.DO.Debug())
}

func (c collectionVideoDo) WithContext(ctx context.Context) ICollectionVideoDo {
	return c.withDO(c.DO.WithContext(ctx))
}

func (c collectionVideoDo) ReadDB() ICollectionVideoDo {
	return c.Clauses(dbresolver.Read)
}

func (c collectionVideoDo) WriteDB() ICollectionVideoDo {
	return c.Clauses(dbresolver.Write)
}

func (c collectionVideoDo) Session(config *gorm.Session) ICollectionVideoDo {
	return c.withDO(c.DO.Session(config))
}

func (c collectionVideoDo) Clauses(conds ...clause.Expression) ICollectionVideoDo {
	return c.withDO(c.DO.Clauses(conds...))
}

func (c collectionVideoDo) Returning(value interface{}, columns ...string) ICollectionVideoDo {
	return c.withDO(c.DO.Returning(value, columns...))
}

func (c collectionVideoDo) Not(conds ...gen.Condition) ICollectionVideoDo {
	return c.withDO(c.DO.Not(conds...))
}

func (c collectionVideoDo) Or(conds ...gen.Condition) ICollectionVideoDo {
	return c.withDO(c.DO.Or(conds...))
}

func (c collectionVideoDo) Select(conds ...field.Expr) ICollectionVideoDo {
	return c.withDO(c.DO.Select(conds...))
}

func (c collectionVideoDo) Where(conds ...gen.Condition) ICollectionVideoDo {
	return c.withDO(c.DO.Where(conds...))
}

func (c collectionVideoDo) Order(conds ...field.Expr) ICollectionVideoDo {
	return c.withDO(c.DO.Order(conds...))
}

func (c collectionVideoDo) Distinct(cols ...field.Expr) ICollectionVideoDo {
	return c.withDO(c.DO.Distinct(cols...))
}

func (c collectionVideoDo) Omit(cols ...field.Expr) ICollectionVideoDo {
	return c.withDO(c.DO.Omit(cols...))
}

func (c collectionVideoDo) Join(table schema.Tabler, on ...field.Expr) ICollectionVideoDo {
	return c.withDO(c.DO.Join(table, on...))
}

func (c collectionVideoDo) LeftJoin(table schema.Tabler, on ...field.Expr) ICollectionVideoDo {
	return c.withDO(c.DO.LeftJoin(table, on...))
}

func (c collectionVideoDo) RightJoin(table schema.Tabler, on ...field.Expr) ICollectionVideoDo {
	return c.withDO(c.DO.RightJoin(table, on...))
}

func (c collectionVideoDo) Group(cols ...field.Expr) ICollectionVideoDo {
	return c.withDO(c.DO.Group(cols...))
}

func (c collectionVideoDo) Having(conds ...gen.Condition) ICollectionVideoDo {
	return c.withDO(c.DO.Having(conds...))
}

func (c collectionVideoDo) Limit(limit int) ICollectionVideoDo {
	return c.withDO(c.DO.Limit(limit))
}

func (c collectionVideoDo) Offset(offset int) ICollectionVideoDo {
	return c.withDO(c.DO.Offset(offset))
}

func (c collectionVideoDo) Scopes(funcs ...func(gen.Dao) gen.Dao) ICollectionVideoDo {
	return c.withDO(c.DO.Scopes(funcs...))
}

func (c collectionVideoDo) Unscoped() ICollectionVideoDo {
	return c.withDO(c.DO.Unscoped())
}

func (c collectionVideoDo) Create(values ...*model.CollectionVideo) error {
	if len(values) == 0 {
		return nil
	}
	return c.DO.Create(values)
}

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

func (c collectionVideoDo) First() (*model.CollectionVideo, error) {
	if result, err := c.DO.First(); err != nil {
		return nil, err
	} else {
		return result.(*model.CollectionVideo), nil
	}
}

func (c collectionVideoDo) Take() (*model.CollectionVideo, error) {
	if result, err := c.DO.Take(); err != nil {
		return nil, err
	} else {
		return result.(*model.CollectionVideo), nil
	}
}

func (c collectionVideoDo) Last() (*model.CollectionVideo, error) {
	if result, err := c.DO.Last(); err != nil {
		return nil, err
	} else {
		return result.(*model.CollectionVideo), nil
	}
}

func (c collectionVideoDo) Find() ([]*model.CollectionVideo, error) {
	result, err := c.DO.Find()
	return result.([]*model.CollectionVideo), err
}

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

func (c collectionVideoDo) Attrs(attrs ...field.AssignExpr) ICollectionVideoDo {
	return c.withDO(c.DO.Attrs(attrs...))
}

func (c collectionVideoDo) Assign(attrs ...field.AssignExpr) ICollectionVideoDo {
	return c.withDO(c.DO.Assign(attrs...))
}

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

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

func (c collectionVideoDo) FirstOrInit() (*model.CollectionVideo, error) {
	if result, err := c.DO.FirstOrInit(); err != nil {
		return nil, err
	} else {
		return result.(*model.CollectionVideo), nil
	}
}

func (c collectionVideoDo) FirstOrCreate() (*model.CollectionVideo, error) {
	if result, err := c.DO.FirstOrCreate(); err != nil {
		return nil, err
	} else {
		return result.(*model.CollectionVideo), nil
	}
}

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

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

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

func (c collectionVideoDo) Scan(result interface{}) (err error) {
	return c.DO.Scan(result)
}

func (c collectionVideoDo) Delete(models ...*model.CollectionVideo) (result gen.ResultInfo, err error) {
	return c.DO.Delete(models)
}

func (c *collectionVideoDo) withDO(do gen.Dao) *collectionVideoDo {
	c.DO = *do.(*gen.DO)
	return c
}
