// 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/bitmagnet-io/bitmagnet/internal/model"
)

func newContentCollectionContent(db *gorm.DB, opts ...gen.DOOption) contentCollectionContent {
	_contentCollectionContent := contentCollectionContent{}

	_contentCollectionContent.contentCollectionContentDo.UseDB(db, opts...)
	_contentCollectionContent.contentCollectionContentDo.UseModel(&model.ContentCollectionContent{})

	tableName := _contentCollectionContent.contentCollectionContentDo.TableName()
	_contentCollectionContent.ALL = field.NewAsterisk(tableName)
	_contentCollectionContent.ContentType = field.NewField(tableName, "content_type")
	_contentCollectionContent.ContentSource = field.NewString(tableName, "content_source")
	_contentCollectionContent.ContentID = field.NewString(tableName, "content_id")
	_contentCollectionContent.ContentCollectionType = field.NewString(tableName, "content_collection_type")
	_contentCollectionContent.ContentCollectionSource = field.NewString(tableName, "content_collection_source")
	_contentCollectionContent.ContentCollectionID = field.NewString(tableName, "content_collection_id")
	_contentCollectionContent.Content = contentCollectionContentBelongsToContent{
		db: db.Session(&gorm.Session{}),

		RelationField: field.NewRelation("Content", "model.Content"),
		Collections: struct {
			field.RelationField
			MetadataSource struct {
				field.RelationField
			}
		}{
			RelationField: field.NewRelation("Content.Collections", "model.ContentCollection"),
			MetadataSource: struct {
				field.RelationField
			}{
				RelationField: field.NewRelation("Content.Collections.MetadataSource", "model.MetadataSource"),
			},
		},
		Attributes: struct {
			field.RelationField
			MetadataSource struct {
				field.RelationField
			}
		}{
			RelationField: field.NewRelation("Content.Attributes", "model.ContentAttribute"),
			MetadataSource: struct {
				field.RelationField
			}{
				RelationField: field.NewRelation("Content.Attributes.MetadataSource", "model.MetadataSource"),
			},
		},
		MetadataSource: struct {
			field.RelationField
		}{
			RelationField: field.NewRelation("Content.MetadataSource", "model.MetadataSource"),
		},
	}

	_contentCollectionContent.Collection = contentCollectionContentBelongsToCollection{
		db: db.Session(&gorm.Session{}),

		RelationField: field.NewRelation("Collection", "model.ContentCollection"),
		MetadataSource: struct {
			field.RelationField
		}{
			RelationField: field.NewRelation("Collection.MetadataSource", "model.MetadataSource"),
		},
	}

	_contentCollectionContent.fillFieldMap()

	return _contentCollectionContent
}

type contentCollectionContent struct {
	contentCollectionContentDo

	ALL                     field.Asterisk
	ContentType             field.Field
	ContentSource           field.String
	ContentID               field.String
	ContentCollectionType   field.String
	ContentCollectionSource field.String
	ContentCollectionID     field.String
	Content                 contentCollectionContentBelongsToContent

	Collection contentCollectionContentBelongsToCollection

	fieldMap map[string]field.Expr
}

func (c contentCollectionContent) Table(newTableName string) *contentCollectionContent {
	c.contentCollectionContentDo.UseTable(newTableName)
	return c.updateTableName(newTableName)
}

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

func (c *contentCollectionContent) updateTableName(table string) *contentCollectionContent {
	c.ALL = field.NewAsterisk(table)
	c.ContentType = field.NewField(table, "content_type")
	c.ContentSource = field.NewString(table, "content_source")
	c.ContentID = field.NewString(table, "content_id")
	c.ContentCollectionType = field.NewString(table, "content_collection_type")
	c.ContentCollectionSource = field.NewString(table, "content_collection_source")
	c.ContentCollectionID = field.NewString(table, "content_collection_id")

	c.fillFieldMap()

	return c
}

func (c *contentCollectionContent) 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 *contentCollectionContent) fillFieldMap() {
	c.fieldMap = make(map[string]field.Expr, 8)
	c.fieldMap["content_type"] = c.ContentType
	c.fieldMap["content_source"] = c.ContentSource
	c.fieldMap["content_id"] = c.ContentID
	c.fieldMap["content_collection_type"] = c.ContentCollectionType
	c.fieldMap["content_collection_source"] = c.ContentCollectionSource
	c.fieldMap["content_collection_id"] = c.ContentCollectionID

}

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

func (c contentCollectionContent) replaceDB(db *gorm.DB) contentCollectionContent {
	c.contentCollectionContentDo.ReplaceDB(db)
	return c
}

type contentCollectionContentBelongsToContent struct {
	db *gorm.DB

	field.RelationField

	Collections struct {
		field.RelationField
		MetadataSource struct {
			field.RelationField
		}
	}
	Attributes struct {
		field.RelationField
		MetadataSource struct {
			field.RelationField
		}
	}
	MetadataSource struct {
		field.RelationField
	}
}

func (a contentCollectionContentBelongsToContent) Where(conds ...field.Expr) *contentCollectionContentBelongsToContent {
	if len(conds) == 0 {
		return &a
	}

	exprs := make([]clause.Expression, 0, len(conds))
	for _, cond := range conds {
		exprs = append(exprs, cond.BeCond().(clause.Expression))
	}
	a.db = a.db.Clauses(clause.Where{Exprs: exprs})
	return &a
}

func (a contentCollectionContentBelongsToContent) WithContext(ctx context.Context) *contentCollectionContentBelongsToContent {
	a.db = a.db.WithContext(ctx)
	return &a
}

func (a contentCollectionContentBelongsToContent) Session(session *gorm.Session) *contentCollectionContentBelongsToContent {
	a.db = a.db.Session(session)
	return &a
}

func (a contentCollectionContentBelongsToContent) Model(m *model.ContentCollectionContent) *contentCollectionContentBelongsToContentTx {
	return &contentCollectionContentBelongsToContentTx{a.db.Model(m).Association(a.Name())}
}

type contentCollectionContentBelongsToContentTx struct{ tx *gorm.Association }

func (a contentCollectionContentBelongsToContentTx) Find() (result *model.Content, err error) {
	return result, a.tx.Find(&result)
}

func (a contentCollectionContentBelongsToContentTx) Append(values ...*model.Content) (err error) {
	targetValues := make([]interface{}, len(values))
	for i, v := range values {
		targetValues[i] = v
	}
	return a.tx.Append(targetValues...)
}

func (a contentCollectionContentBelongsToContentTx) Replace(values ...*model.Content) (err error) {
	targetValues := make([]interface{}, len(values))
	for i, v := range values {
		targetValues[i] = v
	}
	return a.tx.Replace(targetValues...)
}

func (a contentCollectionContentBelongsToContentTx) Delete(values ...*model.Content) (err error) {
	targetValues := make([]interface{}, len(values))
	for i, v := range values {
		targetValues[i] = v
	}
	return a.tx.Delete(targetValues...)
}

func (a contentCollectionContentBelongsToContentTx) Clear() error {
	return a.tx.Clear()
}

func (a contentCollectionContentBelongsToContentTx) Count() int64 {
	return a.tx.Count()
}

type contentCollectionContentBelongsToCollection struct {
	db *gorm.DB

	field.RelationField

	MetadataSource struct {
		field.RelationField
	}
}

func (a contentCollectionContentBelongsToCollection) Where(conds ...field.Expr) *contentCollectionContentBelongsToCollection {
	if len(conds) == 0 {
		return &a
	}

	exprs := make([]clause.Expression, 0, len(conds))
	for _, cond := range conds {
		exprs = append(exprs, cond.BeCond().(clause.Expression))
	}
	a.db = a.db.Clauses(clause.Where{Exprs: exprs})
	return &a
}

func (a contentCollectionContentBelongsToCollection) WithContext(ctx context.Context) *contentCollectionContentBelongsToCollection {
	a.db = a.db.WithContext(ctx)
	return &a
}

func (a contentCollectionContentBelongsToCollection) Session(session *gorm.Session) *contentCollectionContentBelongsToCollection {
	a.db = a.db.Session(session)
	return &a
}

func (a contentCollectionContentBelongsToCollection) Model(m *model.ContentCollectionContent) *contentCollectionContentBelongsToCollectionTx {
	return &contentCollectionContentBelongsToCollectionTx{a.db.Model(m).Association(a.Name())}
}

type contentCollectionContentBelongsToCollectionTx struct{ tx *gorm.Association }

func (a contentCollectionContentBelongsToCollectionTx) Find() (result *model.ContentCollection, err error) {
	return result, a.tx.Find(&result)
}

func (a contentCollectionContentBelongsToCollectionTx) Append(values ...*model.ContentCollection) (err error) {
	targetValues := make([]interface{}, len(values))
	for i, v := range values {
		targetValues[i] = v
	}
	return a.tx.Append(targetValues...)
}

func (a contentCollectionContentBelongsToCollectionTx) Replace(values ...*model.ContentCollection) (err error) {
	targetValues := make([]interface{}, len(values))
	for i, v := range values {
		targetValues[i] = v
	}
	return a.tx.Replace(targetValues...)
}

func (a contentCollectionContentBelongsToCollectionTx) Delete(values ...*model.ContentCollection) (err error) {
	targetValues := make([]interface{}, len(values))
	for i, v := range values {
		targetValues[i] = v
	}
	return a.tx.Delete(targetValues...)
}

func (a contentCollectionContentBelongsToCollectionTx) Clear() error {
	return a.tx.Clear()
}

func (a contentCollectionContentBelongsToCollectionTx) Count() int64 {
	return a.tx.Count()
}

type contentCollectionContentDo struct{ gen.DO }

type IContentCollectionContentDo interface {
	gen.SubQuery
	Debug() IContentCollectionContentDo
	WithContext(ctx context.Context) IContentCollectionContentDo
	WithResult(fc func(tx gen.Dao)) gen.ResultInfo
	ReplaceDB(db *gorm.DB)
	ReadDB() IContentCollectionContentDo
	WriteDB() IContentCollectionContentDo
	As(alias string) gen.Dao
	Session(config *gorm.Session) IContentCollectionContentDo
	Columns(cols ...field.Expr) gen.Columns
	Clauses(conds ...clause.Expression) IContentCollectionContentDo
	Not(conds ...gen.Condition) IContentCollectionContentDo
	Or(conds ...gen.Condition) IContentCollectionContentDo
	Select(conds ...field.Expr) IContentCollectionContentDo
	Where(conds ...gen.Condition) IContentCollectionContentDo
	Order(conds ...field.Expr) IContentCollectionContentDo
	Distinct(cols ...field.Expr) IContentCollectionContentDo
	Omit(cols ...field.Expr) IContentCollectionContentDo
	Join(table schema.Tabler, on ...field.Expr) IContentCollectionContentDo
	LeftJoin(table schema.Tabler, on ...field.Expr) IContentCollectionContentDo
	RightJoin(table schema.Tabler, on ...field.Expr) IContentCollectionContentDo
	Group(cols ...field.Expr) IContentCollectionContentDo
	Having(conds ...gen.Condition) IContentCollectionContentDo
	Limit(limit int) IContentCollectionContentDo
	Offset(offset int) IContentCollectionContentDo
	Count() (count int64, err error)
	Scopes(funcs ...func(gen.Dao) gen.Dao) IContentCollectionContentDo
	Unscoped() IContentCollectionContentDo
	Create(values ...*model.ContentCollectionContent) error
	CreateInBatches(values []*model.ContentCollectionContent, batchSize int) error
	Save(values ...*model.ContentCollectionContent) error
	First() (*model.ContentCollectionContent, error)
	Take() (*model.ContentCollectionContent, error)
	Last() (*model.ContentCollectionContent, error)
	Find() ([]*model.ContentCollectionContent, error)
	FindInBatch(batchSize int, fc func(tx gen.Dao, batch int) error) (results []*model.ContentCollectionContent, err error)
	FindInBatches(result *[]*model.ContentCollectionContent, batchSize int, fc func(tx gen.Dao, batch int) error) error
	Pluck(column field.Expr, dest interface{}) error
	Delete(...*model.ContentCollectionContent) (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) IContentCollectionContentDo
	Assign(attrs ...field.AssignExpr) IContentCollectionContentDo
	Joins(fields ...field.RelationField) IContentCollectionContentDo
	Preload(fields ...field.RelationField) IContentCollectionContentDo
	FirstOrInit() (*model.ContentCollectionContent, error)
	FirstOrCreate() (*model.ContentCollectionContent, error)
	FindByPage(offset int, limit int) (result []*model.ContentCollectionContent, 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) IContentCollectionContentDo
	UnderlyingDB() *gorm.DB
	schema.Tabler
}

func (c contentCollectionContentDo) Debug() IContentCollectionContentDo {
	return c.withDO(c.DO.Debug())
}

func (c contentCollectionContentDo) WithContext(ctx context.Context) IContentCollectionContentDo {
	return c.withDO(c.DO.WithContext(ctx))
}

func (c contentCollectionContentDo) ReadDB() IContentCollectionContentDo {
	return c.Clauses(dbresolver.Read)
}

func (c contentCollectionContentDo) WriteDB() IContentCollectionContentDo {
	return c.Clauses(dbresolver.Write)
}

func (c contentCollectionContentDo) Session(config *gorm.Session) IContentCollectionContentDo {
	return c.withDO(c.DO.Session(config))
}

func (c contentCollectionContentDo) Clauses(conds ...clause.Expression) IContentCollectionContentDo {
	return c.withDO(c.DO.Clauses(conds...))
}

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

func (c contentCollectionContentDo) Not(conds ...gen.Condition) IContentCollectionContentDo {
	return c.withDO(c.DO.Not(conds...))
}

func (c contentCollectionContentDo) Or(conds ...gen.Condition) IContentCollectionContentDo {
	return c.withDO(c.DO.Or(conds...))
}

func (c contentCollectionContentDo) Select(conds ...field.Expr) IContentCollectionContentDo {
	return c.withDO(c.DO.Select(conds...))
}

func (c contentCollectionContentDo) Where(conds ...gen.Condition) IContentCollectionContentDo {
	return c.withDO(c.DO.Where(conds...))
}

func (c contentCollectionContentDo) Order(conds ...field.Expr) IContentCollectionContentDo {
	return c.withDO(c.DO.Order(conds...))
}

func (c contentCollectionContentDo) Distinct(cols ...field.Expr) IContentCollectionContentDo {
	return c.withDO(c.DO.Distinct(cols...))
}

func (c contentCollectionContentDo) Omit(cols ...field.Expr) IContentCollectionContentDo {
	return c.withDO(c.DO.Omit(cols...))
}

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

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

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

func (c contentCollectionContentDo) Group(cols ...field.Expr) IContentCollectionContentDo {
	return c.withDO(c.DO.Group(cols...))
}

func (c contentCollectionContentDo) Having(conds ...gen.Condition) IContentCollectionContentDo {
	return c.withDO(c.DO.Having(conds...))
}

func (c contentCollectionContentDo) Limit(limit int) IContentCollectionContentDo {
	return c.withDO(c.DO.Limit(limit))
}

func (c contentCollectionContentDo) Offset(offset int) IContentCollectionContentDo {
	return c.withDO(c.DO.Offset(offset))
}

func (c contentCollectionContentDo) Scopes(funcs ...func(gen.Dao) gen.Dao) IContentCollectionContentDo {
	return c.withDO(c.DO.Scopes(funcs...))
}

func (c contentCollectionContentDo) Unscoped() IContentCollectionContentDo {
	return c.withDO(c.DO.Unscoped())
}

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

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

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

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

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

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

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

func (c contentCollectionContentDo) Attrs(attrs ...field.AssignExpr) IContentCollectionContentDo {
	return c.withDO(c.DO.Attrs(attrs...))
}

func (c contentCollectionContentDo) Assign(attrs ...field.AssignExpr) IContentCollectionContentDo {
	return c.withDO(c.DO.Assign(attrs...))
}

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

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

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

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

func (c contentCollectionContentDo) FindByPage(offset int, limit int) (result []*model.ContentCollectionContent, 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 contentCollectionContentDo) 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 contentCollectionContentDo) Scan(result interface{}) (err error) {
	return c.DO.Scan(result)
}

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

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