// 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"
	"strings"

	"gorm.io/gorm"
	"gorm.io/gorm/clause"
	"gorm.io/gorm/schema"

	"gorm.io/gen"
	"gorm.io/gen/field"

	"gorm.io/plugin/dbresolver"

	"app/shelfly/internal/models"
)

func newTagging(db *gorm.DB, opts ...gen.DOOption) tagging {
	_tagging := tagging{}

	_tagging.taggingDo.UseDB(db, opts...)
	_tagging.taggingDo.UseModel(&models.Tagging{})

	tableName := _tagging.taggingDo.TableName()
	_tagging.ALL = field.NewAsterisk(tableName)
	_tagging.ID = field.NewInt64(tableName, "id")
	_tagging.MetadataItemID = field.NewInt64(tableName, "metadata_item_id")
	_tagging.TagID = field.NewInt64(tableName, "tag_id")
	_tagging.Index = field.NewInt64(tableName, "index")

	_tagging.fillFieldMap()

	return _tagging
}

type tagging struct {
	taggingDo

	ALL            field.Asterisk
	ID             field.Int64
	MetadataItemID field.Int64
	TagID          field.Int64
	Index          field.Int64

	fieldMap map[string]field.Expr
}

func (t tagging) Table(newTableName string) *tagging {
	t.taggingDo.UseTable(newTableName)
	return t.updateTableName(newTableName)
}

func (t tagging) As(alias string) *tagging {
	t.taggingDo.DO = *(t.taggingDo.As(alias).(*gen.DO))
	return t.updateTableName(alias)
}

func (t *tagging) updateTableName(table string) *tagging {
	t.ALL = field.NewAsterisk(table)
	t.ID = field.NewInt64(table, "id")
	t.MetadataItemID = field.NewInt64(table, "metadata_item_id")
	t.TagID = field.NewInt64(table, "tag_id")
	t.Index = field.NewInt64(table, "index")

	t.fillFieldMap()

	return t
}

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

func (t *tagging) fillFieldMap() {
	t.fieldMap = make(map[string]field.Expr, 4)
	t.fieldMap["id"] = t.ID
	t.fieldMap["metadata_item_id"] = t.MetadataItemID
	t.fieldMap["tag_id"] = t.TagID
	t.fieldMap["index"] = t.Index
}

func (t tagging) clone(db *gorm.DB) tagging {
	t.taggingDo.ReplaceConnPool(db.Statement.ConnPool)
	return t
}

func (t tagging) replaceDB(db *gorm.DB) tagging {
	t.taggingDo.ReplaceDB(db)
	return t
}

type taggingDo struct{ gen.DO }

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

	FilterWithNameAndRole(name string, role string) (result []models.Tagging, err error)
}

// SELECT * FROM @@table WHERE name = @name{{if role !=""}} AND role = @role{{end}}
func (t taggingDo) FilterWithNameAndRole(name string, role string) (result []models.Tagging, err error) {
	var params []interface{}

	var generateSQL strings.Builder
	params = append(params, name)
	generateSQL.WriteString("SELECT * FROM taggings WHERE name = ? ")
	if role != "" {
		params = append(params, role)
		generateSQL.WriteString("AND role = ? ")
	}

	var executeSQL *gorm.DB
	executeSQL = t.UnderlyingDB().Raw(generateSQL.String(), params...).Find(&result) // ignore_security_alert
	err = executeSQL.Error

	return
}

func (t taggingDo) Debug() ITaggingDo {
	return t.withDO(t.DO.Debug())
}

func (t taggingDo) WithContext(ctx context.Context) ITaggingDo {
	return t.withDO(t.DO.WithContext(ctx))
}

func (t taggingDo) ReadDB() ITaggingDo {
	return t.Clauses(dbresolver.Read)
}

func (t taggingDo) WriteDB() ITaggingDo {
	return t.Clauses(dbresolver.Write)
}

func (t taggingDo) Session(config *gorm.Session) ITaggingDo {
	return t.withDO(t.DO.Session(config))
}

func (t taggingDo) Clauses(conds ...clause.Expression) ITaggingDo {
	return t.withDO(t.DO.Clauses(conds...))
}

func (t taggingDo) Returning(value interface{}, columns ...string) ITaggingDo {
	return t.withDO(t.DO.Returning(value, columns...))
}

func (t taggingDo) Not(conds ...gen.Condition) ITaggingDo {
	return t.withDO(t.DO.Not(conds...))
}

func (t taggingDo) Or(conds ...gen.Condition) ITaggingDo {
	return t.withDO(t.DO.Or(conds...))
}

func (t taggingDo) Select(conds ...field.Expr) ITaggingDo {
	return t.withDO(t.DO.Select(conds...))
}

func (t taggingDo) Where(conds ...gen.Condition) ITaggingDo {
	return t.withDO(t.DO.Where(conds...))
}

func (t taggingDo) Order(conds ...field.Expr) ITaggingDo {
	return t.withDO(t.DO.Order(conds...))
}

func (t taggingDo) Distinct(cols ...field.Expr) ITaggingDo {
	return t.withDO(t.DO.Distinct(cols...))
}

func (t taggingDo) Omit(cols ...field.Expr) ITaggingDo {
	return t.withDO(t.DO.Omit(cols...))
}

func (t taggingDo) Join(table schema.Tabler, on ...field.Expr) ITaggingDo {
	return t.withDO(t.DO.Join(table, on...))
}

func (t taggingDo) LeftJoin(table schema.Tabler, on ...field.Expr) ITaggingDo {
	return t.withDO(t.DO.LeftJoin(table, on...))
}

func (t taggingDo) RightJoin(table schema.Tabler, on ...field.Expr) ITaggingDo {
	return t.withDO(t.DO.RightJoin(table, on...))
}

func (t taggingDo) Group(cols ...field.Expr) ITaggingDo {
	return t.withDO(t.DO.Group(cols...))
}

func (t taggingDo) Having(conds ...gen.Condition) ITaggingDo {
	return t.withDO(t.DO.Having(conds...))
}

func (t taggingDo) Limit(limit int) ITaggingDo {
	return t.withDO(t.DO.Limit(limit))
}

func (t taggingDo) Offset(offset int) ITaggingDo {
	return t.withDO(t.DO.Offset(offset))
}

func (t taggingDo) Scopes(funcs ...func(gen.Dao) gen.Dao) ITaggingDo {
	return t.withDO(t.DO.Scopes(funcs...))
}

func (t taggingDo) Unscoped() ITaggingDo {
	return t.withDO(t.DO.Unscoped())
}

func (t taggingDo) Create(values ...*models.Tagging) error {
	if len(values) == 0 {
		return nil
	}
	return t.DO.Create(values)
}

func (t taggingDo) CreateInBatches(values []*models.Tagging, batchSize int) error {
	return t.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 (t taggingDo) Save(values ...*models.Tagging) error {
	if len(values) == 0 {
		return nil
	}
	return t.DO.Save(values)
}

func (t taggingDo) First() (*models.Tagging, error) {
	if result, err := t.DO.First(); err != nil {
		return nil, err
	} else {
		return result.(*models.Tagging), nil
	}
}

func (t taggingDo) Take() (*models.Tagging, error) {
	if result, err := t.DO.Take(); err != nil {
		return nil, err
	} else {
		return result.(*models.Tagging), nil
	}
}

func (t taggingDo) Last() (*models.Tagging, error) {
	if result, err := t.DO.Last(); err != nil {
		return nil, err
	} else {
		return result.(*models.Tagging), nil
	}
}

func (t taggingDo) Find() ([]*models.Tagging, error) {
	result, err := t.DO.Find()
	return result.([]*models.Tagging), err
}

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

func (t taggingDo) Attrs(attrs ...field.AssignExpr) ITaggingDo {
	return t.withDO(t.DO.Attrs(attrs...))
}

func (t taggingDo) Assign(attrs ...field.AssignExpr) ITaggingDo {
	return t.withDO(t.DO.Assign(attrs...))
}

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

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

func (t taggingDo) FirstOrInit() (*models.Tagging, error) {
	if result, err := t.DO.FirstOrInit(); err != nil {
		return nil, err
	} else {
		return result.(*models.Tagging), nil
	}
}

func (t taggingDo) FirstOrCreate() (*models.Tagging, error) {
	if result, err := t.DO.FirstOrCreate(); err != nil {
		return nil, err
	} else {
		return result.(*models.Tagging), nil
	}
}

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

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

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

func (t taggingDo) Scan(result interface{}) (err error) {
	return t.DO.Scan(result)
}

func (t taggingDo) Delete(models ...*models.Tagging) (result gen.ResultInfo, err error) {
	return t.DO.Delete(models)
}

func (t *taggingDo) withDO(do gen.Dao) *taggingDo {
	t.DO = *do.(*gen.DO)
	return t
}
