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

	_contentAttribute.contentAttributeDo.UseDB(db, opts...)
	_contentAttribute.contentAttributeDo.UseModel(&model.ContentAttribute{})

	tableName := _contentAttribute.contentAttributeDo.TableName()
	_contentAttribute.ALL = field.NewAsterisk(tableName)
	_contentAttribute.ContentType = field.NewField(tableName, "content_type")
	_contentAttribute.ContentSource = field.NewString(tableName, "content_source")
	_contentAttribute.ContentID = field.NewString(tableName, "content_id")
	_contentAttribute.Source = field.NewString(tableName, "source")
	_contentAttribute.Key = field.NewString(tableName, "key")
	_contentAttribute.Value = field.NewString(tableName, "value")
	_contentAttribute.CreatedAt = field.NewTime(tableName, "created_at")
	_contentAttribute.UpdatedAt = field.NewTime(tableName, "updated_at")
	_contentAttribute.MetadataSource = contentAttributeBelongsToMetadataSource{
		db: db.Session(&gorm.Session{}),

		RelationField: field.NewRelation("MetadataSource", "model.MetadataSource"),
	}

	_contentAttribute.fillFieldMap()

	return _contentAttribute
}

type contentAttribute struct {
	contentAttributeDo

	ALL            field.Asterisk
	ContentType    field.Field
	ContentSource  field.String
	ContentID      field.String
	Source         field.String
	Key            field.String
	Value          field.String
	CreatedAt      field.Time
	UpdatedAt      field.Time
	MetadataSource contentAttributeBelongsToMetadataSource

	fieldMap map[string]field.Expr
}

func (c contentAttribute) Table(newTableName string) *contentAttribute {
	c.contentAttributeDo.UseTable(newTableName)
	return c.updateTableName(newTableName)
}

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

func (c *contentAttribute) updateTableName(table string) *contentAttribute {
	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.Source = field.NewString(table, "source")
	c.Key = field.NewString(table, "key")
	c.Value = field.NewString(table, "value")
	c.CreatedAt = field.NewTime(table, "created_at")
	c.UpdatedAt = field.NewTime(table, "updated_at")

	c.fillFieldMap()

	return c
}

func (c *contentAttribute) 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 *contentAttribute) fillFieldMap() {
	c.fieldMap = make(map[string]field.Expr, 9)
	c.fieldMap["content_type"] = c.ContentType
	c.fieldMap["content_source"] = c.ContentSource
	c.fieldMap["content_id"] = c.ContentID
	c.fieldMap["source"] = c.Source
	c.fieldMap["key"] = c.Key
	c.fieldMap["value"] = c.Value
	c.fieldMap["created_at"] = c.CreatedAt
	c.fieldMap["updated_at"] = c.UpdatedAt

}

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

func (c contentAttribute) replaceDB(db *gorm.DB) contentAttribute {
	c.contentAttributeDo.ReplaceDB(db)
	return c
}

type contentAttributeBelongsToMetadataSource struct {
	db *gorm.DB

	field.RelationField
}

func (a contentAttributeBelongsToMetadataSource) Where(conds ...field.Expr) *contentAttributeBelongsToMetadataSource {
	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 contentAttributeBelongsToMetadataSource) WithContext(ctx context.Context) *contentAttributeBelongsToMetadataSource {
	a.db = a.db.WithContext(ctx)
	return &a
}

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

func (a contentAttributeBelongsToMetadataSource) Model(m *model.ContentAttribute) *contentAttributeBelongsToMetadataSourceTx {
	return &contentAttributeBelongsToMetadataSourceTx{a.db.Model(m).Association(a.Name())}
}

type contentAttributeBelongsToMetadataSourceTx struct{ tx *gorm.Association }

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

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

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

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

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

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

type contentAttributeDo struct{ gen.DO }

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

func (c contentAttributeDo) Debug() IContentAttributeDo {
	return c.withDO(c.DO.Debug())
}

func (c contentAttributeDo) WithContext(ctx context.Context) IContentAttributeDo {
	return c.withDO(c.DO.WithContext(ctx))
}

func (c contentAttributeDo) ReadDB() IContentAttributeDo {
	return c.Clauses(dbresolver.Read)
}

func (c contentAttributeDo) WriteDB() IContentAttributeDo {
	return c.Clauses(dbresolver.Write)
}

func (c contentAttributeDo) Session(config *gorm.Session) IContentAttributeDo {
	return c.withDO(c.DO.Session(config))
}

func (c contentAttributeDo) Clauses(conds ...clause.Expression) IContentAttributeDo {
	return c.withDO(c.DO.Clauses(conds...))
}

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

func (c contentAttributeDo) Not(conds ...gen.Condition) IContentAttributeDo {
	return c.withDO(c.DO.Not(conds...))
}

func (c contentAttributeDo) Or(conds ...gen.Condition) IContentAttributeDo {
	return c.withDO(c.DO.Or(conds...))
}

func (c contentAttributeDo) Select(conds ...field.Expr) IContentAttributeDo {
	return c.withDO(c.DO.Select(conds...))
}

func (c contentAttributeDo) Where(conds ...gen.Condition) IContentAttributeDo {
	return c.withDO(c.DO.Where(conds...))
}

func (c contentAttributeDo) Order(conds ...field.Expr) IContentAttributeDo {
	return c.withDO(c.DO.Order(conds...))
}

func (c contentAttributeDo) Distinct(cols ...field.Expr) IContentAttributeDo {
	return c.withDO(c.DO.Distinct(cols...))
}

func (c contentAttributeDo) Omit(cols ...field.Expr) IContentAttributeDo {
	return c.withDO(c.DO.Omit(cols...))
}

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

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

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

func (c contentAttributeDo) Group(cols ...field.Expr) IContentAttributeDo {
	return c.withDO(c.DO.Group(cols...))
}

func (c contentAttributeDo) Having(conds ...gen.Condition) IContentAttributeDo {
	return c.withDO(c.DO.Having(conds...))
}

func (c contentAttributeDo) Limit(limit int) IContentAttributeDo {
	return c.withDO(c.DO.Limit(limit))
}

func (c contentAttributeDo) Offset(offset int) IContentAttributeDo {
	return c.withDO(c.DO.Offset(offset))
}

func (c contentAttributeDo) Scopes(funcs ...func(gen.Dao) gen.Dao) IContentAttributeDo {
	return c.withDO(c.DO.Scopes(funcs...))
}

func (c contentAttributeDo) Unscoped() IContentAttributeDo {
	return c.withDO(c.DO.Unscoped())
}

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

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

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

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

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

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

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

func (c contentAttributeDo) Attrs(attrs ...field.AssignExpr) IContentAttributeDo {
	return c.withDO(c.DO.Attrs(attrs...))
}

func (c contentAttributeDo) Assign(attrs ...field.AssignExpr) IContentAttributeDo {
	return c.withDO(c.DO.Assign(attrs...))
}

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

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

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

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

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

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

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