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

	_mediaPart.mediaPartDo.UseDB(db, opts...)
	_mediaPart.mediaPartDo.UseModel(&models.MediaPart{})

	tableName := _mediaPart.mediaPartDo.TableName()
	_mediaPart.ALL = field.NewAsterisk(tableName)
	_mediaPart.ID = field.NewInt64(tableName, "id")
	_mediaPart.MediaItemID = field.NewInt64(tableName, "media_item_id")
	_mediaPart.File = field.NewString(tableName, "file")
	_mediaPart.Duration = field.NewInt64(tableName, "duration")
	_mediaPart.Size = field.NewInt64(tableName, "size")
	_mediaPart.Indexes = field.NewString(tableName, "indexes")
	_mediaPart.CreatedAt = field.NewString(tableName, "created_at")
	_mediaPart.UpdatedAt = field.NewString(tableName, "updated_at")

	_mediaPart.fillFieldMap()

	return _mediaPart
}

type mediaPart struct {
	mediaPartDo

	ALL         field.Asterisk
	ID          field.Int64
	MediaItemID field.Int64
	File        field.String
	Duration    field.Int64
	Size        field.Int64
	Indexes     field.String
	CreatedAt   field.String
	UpdatedAt   field.String

	fieldMap map[string]field.Expr
}

func (m mediaPart) Table(newTableName string) *mediaPart {
	m.mediaPartDo.UseTable(newTableName)
	return m.updateTableName(newTableName)
}

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

func (m *mediaPart) updateTableName(table string) *mediaPart {
	m.ALL = field.NewAsterisk(table)
	m.ID = field.NewInt64(table, "id")
	m.MediaItemID = field.NewInt64(table, "media_item_id")
	m.File = field.NewString(table, "file")
	m.Duration = field.NewInt64(table, "duration")
	m.Size = field.NewInt64(table, "size")
	m.Indexes = field.NewString(table, "indexes")
	m.CreatedAt = field.NewString(table, "created_at")
	m.UpdatedAt = field.NewString(table, "updated_at")

	m.fillFieldMap()

	return m
}

func (m *mediaPart) 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 *mediaPart) fillFieldMap() {
	m.fieldMap = make(map[string]field.Expr, 8)
	m.fieldMap["id"] = m.ID
	m.fieldMap["media_item_id"] = m.MediaItemID
	m.fieldMap["file"] = m.File
	m.fieldMap["duration"] = m.Duration
	m.fieldMap["size"] = m.Size
	m.fieldMap["indexes"] = m.Indexes
	m.fieldMap["created_at"] = m.CreatedAt
	m.fieldMap["updated_at"] = m.UpdatedAt
}

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

func (m mediaPart) replaceDB(db *gorm.DB) mediaPart {
	m.mediaPartDo.ReplaceDB(db)
	return m
}

type mediaPartDo struct{ gen.DO }

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

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

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

	var generateSQL strings.Builder
	params = append(params, name)
	generateSQL.WriteString("SELECT * FROM media_parts 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 mediaPartDo) Debug() IMediaPartDo {
	return m.withDO(m.DO.Debug())
}

func (m mediaPartDo) WithContext(ctx context.Context) IMediaPartDo {
	return m.withDO(m.DO.WithContext(ctx))
}

func (m mediaPartDo) ReadDB() IMediaPartDo {
	return m.Clauses(dbresolver.Read)
}

func (m mediaPartDo) WriteDB() IMediaPartDo {
	return m.Clauses(dbresolver.Write)
}

func (m mediaPartDo) Session(config *gorm.Session) IMediaPartDo {
	return m.withDO(m.DO.Session(config))
}

func (m mediaPartDo) Clauses(conds ...clause.Expression) IMediaPartDo {
	return m.withDO(m.DO.Clauses(conds...))
}

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

func (m mediaPartDo) Not(conds ...gen.Condition) IMediaPartDo {
	return m.withDO(m.DO.Not(conds...))
}

func (m mediaPartDo) Or(conds ...gen.Condition) IMediaPartDo {
	return m.withDO(m.DO.Or(conds...))
}

func (m mediaPartDo) Select(conds ...field.Expr) IMediaPartDo {
	return m.withDO(m.DO.Select(conds...))
}

func (m mediaPartDo) Where(conds ...gen.Condition) IMediaPartDo {
	return m.withDO(m.DO.Where(conds...))
}

func (m mediaPartDo) Order(conds ...field.Expr) IMediaPartDo {
	return m.withDO(m.DO.Order(conds...))
}

func (m mediaPartDo) Distinct(cols ...field.Expr) IMediaPartDo {
	return m.withDO(m.DO.Distinct(cols...))
}

func (m mediaPartDo) Omit(cols ...field.Expr) IMediaPartDo {
	return m.withDO(m.DO.Omit(cols...))
}

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

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

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

func (m mediaPartDo) Group(cols ...field.Expr) IMediaPartDo {
	return m.withDO(m.DO.Group(cols...))
}

func (m mediaPartDo) Having(conds ...gen.Condition) IMediaPartDo {
	return m.withDO(m.DO.Having(conds...))
}

func (m mediaPartDo) Limit(limit int) IMediaPartDo {
	return m.withDO(m.DO.Limit(limit))
}

func (m mediaPartDo) Offset(offset int) IMediaPartDo {
	return m.withDO(m.DO.Offset(offset))
}

func (m mediaPartDo) Scopes(funcs ...func(gen.Dao) gen.Dao) IMediaPartDo {
	return m.withDO(m.DO.Scopes(funcs...))
}

func (m mediaPartDo) Unscoped() IMediaPartDo {
	return m.withDO(m.DO.Unscoped())
}

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

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

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

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

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

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

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

func (m mediaPartDo) Attrs(attrs ...field.AssignExpr) IMediaPartDo {
	return m.withDO(m.DO.Attrs(attrs...))
}

func (m mediaPartDo) Assign(attrs ...field.AssignExpr) IMediaPartDo {
	return m.withDO(m.DO.Assign(attrs...))
}

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

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

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

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

func (m mediaPartDo) FindByPage(offset int, limit int) (result []*models.MediaPart, 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 mediaPartDo) 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 mediaPartDo) Scan(result interface{}) (err error) {
	return m.DO.Scan(result)
}

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

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