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

	_mediaItem.mediaItemDo.UseDB(db, opts...)
	_mediaItem.mediaItemDo.UseModel(&models.MediaItem{})

	tableName := _mediaItem.mediaItemDo.TableName()
	_mediaItem.ALL = field.NewAsterisk(tableName)
	_mediaItem.ID = field.NewInt64(tableName, "id")
	_mediaItem.MetadataItemID = field.NewInt64(tableName, "metadata_item_id")
	_mediaItem.Duration = field.NewInt64(tableName, "duration")
	_mediaItem.Bitrate = field.NewInt64(tableName, "bitrate")
	_mediaItem.Width = field.NewInt64(tableName, "width")
	_mediaItem.Height = field.NewInt64(tableName, "height")
	_mediaItem.AspectRatio = field.NewFloat64(tableName, "aspect_ratio")
	_mediaItem.AudioCodec = field.NewString(tableName, "audio_codec")
	_mediaItem.VideoCodec = field.NewString(tableName, "video_codec")
	_mediaItem.Container = field.NewString(tableName, "container")
	_mediaItem.CreatedAt = field.NewString(tableName, "created_at")
	_mediaItem.UpdatedAt = field.NewString(tableName, "updated_at")

	_mediaItem.fillFieldMap()

	return _mediaItem
}

type mediaItem struct {
	mediaItemDo

	ALL            field.Asterisk
	ID             field.Int64
	MetadataItemID field.Int64
	Duration       field.Int64
	Bitrate        field.Int64
	Width          field.Int64
	Height         field.Int64
	AspectRatio    field.Float64
	AudioCodec     field.String
	VideoCodec     field.String
	Container      field.String
	CreatedAt      field.String
	UpdatedAt      field.String

	fieldMap map[string]field.Expr
}

func (m mediaItem) Table(newTableName string) *mediaItem {
	m.mediaItemDo.UseTable(newTableName)
	return m.updateTableName(newTableName)
}

func (m mediaItem) As(alias string) *mediaItem {
	m.mediaItemDo.DO = *(m.mediaItemDo.As(alias).(*gen.DO))
	return m.updateTableName(alias)
}

func (m *mediaItem) updateTableName(table string) *mediaItem {
	m.ALL = field.NewAsterisk(table)
	m.ID = field.NewInt64(table, "id")
	m.MetadataItemID = field.NewInt64(table, "metadata_item_id")
	m.Duration = field.NewInt64(table, "duration")
	m.Bitrate = field.NewInt64(table, "bitrate")
	m.Width = field.NewInt64(table, "width")
	m.Height = field.NewInt64(table, "height")
	m.AspectRatio = field.NewFloat64(table, "aspect_ratio")
	m.AudioCodec = field.NewString(table, "audio_codec")
	m.VideoCodec = field.NewString(table, "video_codec")
	m.Container = field.NewString(table, "container")
	m.CreatedAt = field.NewString(table, "created_at")
	m.UpdatedAt = field.NewString(table, "updated_at")

	m.fillFieldMap()

	return m
}

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

func (m *mediaItem) fillFieldMap() {
	m.fieldMap = make(map[string]field.Expr, 12)
	m.fieldMap["id"] = m.ID
	m.fieldMap["metadata_item_id"] = m.MetadataItemID
	m.fieldMap["duration"] = m.Duration
	m.fieldMap["bitrate"] = m.Bitrate
	m.fieldMap["width"] = m.Width
	m.fieldMap["height"] = m.Height
	m.fieldMap["aspect_ratio"] = m.AspectRatio
	m.fieldMap["audio_codec"] = m.AudioCodec
	m.fieldMap["video_codec"] = m.VideoCodec
	m.fieldMap["container"] = m.Container
	m.fieldMap["created_at"] = m.CreatedAt
	m.fieldMap["updated_at"] = m.UpdatedAt
}

func (m mediaItem) clone(db *gorm.DB) mediaItem {
	m.mediaItemDo.ReplaceConnPool(db.Statement.ConnPool)
	return m
}

func (m mediaItem) replaceDB(db *gorm.DB) mediaItem {
	m.mediaItemDo.ReplaceDB(db)
	return m
}

type mediaItemDo struct{ gen.DO }

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

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

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

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

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

	return
}

func (m mediaItemDo) Debug() IMediaItemDo {
	return m.withDO(m.DO.Debug())
}

func (m mediaItemDo) WithContext(ctx context.Context) IMediaItemDo {
	return m.withDO(m.DO.WithContext(ctx))
}

func (m mediaItemDo) ReadDB() IMediaItemDo {
	return m.Clauses(dbresolver.Read)
}

func (m mediaItemDo) WriteDB() IMediaItemDo {
	return m.Clauses(dbresolver.Write)
}

func (m mediaItemDo) Session(config *gorm.Session) IMediaItemDo {
	return m.withDO(m.DO.Session(config))
}

func (m mediaItemDo) Clauses(conds ...clause.Expression) IMediaItemDo {
	return m.withDO(m.DO.Clauses(conds...))
}

func (m mediaItemDo) Returning(value interface{}, columns ...string) IMediaItemDo {
	return m.withDO(m.DO.Returning(value, columns...))
}

func (m mediaItemDo) Not(conds ...gen.Condition) IMediaItemDo {
	return m.withDO(m.DO.Not(conds...))
}

func (m mediaItemDo) Or(conds ...gen.Condition) IMediaItemDo {
	return m.withDO(m.DO.Or(conds...))
}

func (m mediaItemDo) Select(conds ...field.Expr) IMediaItemDo {
	return m.withDO(m.DO.Select(conds...))
}

func (m mediaItemDo) Where(conds ...gen.Condition) IMediaItemDo {
	return m.withDO(m.DO.Where(conds...))
}

func (m mediaItemDo) Order(conds ...field.Expr) IMediaItemDo {
	return m.withDO(m.DO.Order(conds...))
}

func (m mediaItemDo) Distinct(cols ...field.Expr) IMediaItemDo {
	return m.withDO(m.DO.Distinct(cols...))
}

func (m mediaItemDo) Omit(cols ...field.Expr) IMediaItemDo {
	return m.withDO(m.DO.Omit(cols...))
}

func (m mediaItemDo) Join(table schema.Tabler, on ...field.Expr) IMediaItemDo {
	return m.withDO(m.DO.Join(table, on...))
}

func (m mediaItemDo) LeftJoin(table schema.Tabler, on ...field.Expr) IMediaItemDo {
	return m.withDO(m.DO.LeftJoin(table, on...))
}

func (m mediaItemDo) RightJoin(table schema.Tabler, on ...field.Expr) IMediaItemDo {
	return m.withDO(m.DO.RightJoin(table, on...))
}

func (m mediaItemDo) Group(cols ...field.Expr) IMediaItemDo {
	return m.withDO(m.DO.Group(cols...))
}

func (m mediaItemDo) Having(conds ...gen.Condition) IMediaItemDo {
	return m.withDO(m.DO.Having(conds...))
}

func (m mediaItemDo) Limit(limit int) IMediaItemDo {
	return m.withDO(m.DO.Limit(limit))
}

func (m mediaItemDo) Offset(offset int) IMediaItemDo {
	return m.withDO(m.DO.Offset(offset))
}

func (m mediaItemDo) Scopes(funcs ...func(gen.Dao) gen.Dao) IMediaItemDo {
	return m.withDO(m.DO.Scopes(funcs...))
}

func (m mediaItemDo) Unscoped() IMediaItemDo {
	return m.withDO(m.DO.Unscoped())
}

func (m mediaItemDo) Create(values ...*models.MediaItem) error {
	if len(values) == 0 {
		return nil
	}
	return m.DO.Create(values)
}

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

func (m mediaItemDo) First() (*models.MediaItem, error) {
	if result, err := m.DO.First(); err != nil {
		return nil, err
	} else {
		return result.(*models.MediaItem), nil
	}
}

func (m mediaItemDo) Take() (*models.MediaItem, error) {
	if result, err := m.DO.Take(); err != nil {
		return nil, err
	} else {
		return result.(*models.MediaItem), nil
	}
}

func (m mediaItemDo) Last() (*models.MediaItem, error) {
	if result, err := m.DO.Last(); err != nil {
		return nil, err
	} else {
		return result.(*models.MediaItem), nil
	}
}

func (m mediaItemDo) Find() ([]*models.MediaItem, error) {
	result, err := m.DO.Find()
	return result.([]*models.MediaItem), err
}

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

func (m mediaItemDo) Attrs(attrs ...field.AssignExpr) IMediaItemDo {
	return m.withDO(m.DO.Attrs(attrs...))
}

func (m mediaItemDo) Assign(attrs ...field.AssignExpr) IMediaItemDo {
	return m.withDO(m.DO.Assign(attrs...))
}

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

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

func (m mediaItemDo) FirstOrInit() (*models.MediaItem, error) {
	if result, err := m.DO.FirstOrInit(); err != nil {
		return nil, err
	} else {
		return result.(*models.MediaItem), nil
	}
}

func (m mediaItemDo) FirstOrCreate() (*models.MediaItem, error) {
	if result, err := m.DO.FirstOrCreate(); err != nil {
		return nil, err
	} else {
		return result.(*models.MediaItem), nil
	}
}

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

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

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

func (m mediaItemDo) Scan(result interface{}) (err error) {
	return m.DO.Scan(result)
}

func (m mediaItemDo) Delete(models ...*models.MediaItem) (result gen.ResultInfo, err error) {
	return m.DO.Delete(models)
}

func (m *mediaItemDo) withDO(do gen.Dao) *mediaItemDo {
	m.DO = *do.(*gen.DO)
	return m
}
