// Code generated by go.ipao.vip/gen. DO NOT EDIT.
// Code generated by go.ipao.vip/gen. DO NOT EDIT.
// Code generated by go.ipao.vip/gen. DO NOT EDIT.

package models

import (
	"context"

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

	"go.ipao.vip/gen"
	"go.ipao.vip/gen/field"

	"gorm.io/plugin/dbresolver"
)

func newMediaAsset(db *gorm.DB, opts ...gen.DOOption) mediaAssetQuery {
	_mediaAssetQuery := mediaAssetQuery{}

	_mediaAssetQuery.mediaAssetQueryDo.UseDB(db, opts...)
	_mediaAssetQuery.mediaAssetQueryDo.UseModel(&MediaAsset{})

	tableName := _mediaAssetQuery.mediaAssetQueryDo.TableName()
	_mediaAssetQuery.ALL = field.NewAsterisk(tableName)
	_mediaAssetQuery.ID = field.NewInt64(tableName, "id")
	_mediaAssetQuery.TenantID = field.NewInt64(tableName, "tenant_id")
	_mediaAssetQuery.UserID = field.NewInt64(tableName, "user_id")
	_mediaAssetQuery.Type = field.NewField(tableName, "type")
	_mediaAssetQuery.Status = field.NewField(tableName, "status")
	_mediaAssetQuery.Provider = field.NewString(tableName, "provider")
	_mediaAssetQuery.Bucket = field.NewString(tableName, "bucket")
	_mediaAssetQuery.ObjectKey = field.NewString(tableName, "object_key")
	_mediaAssetQuery.Meta = field.NewJSONB(tableName, "meta")
	_mediaAssetQuery.Variant = field.NewField(tableName, "variant")
	_mediaAssetQuery.SourceAssetID = field.NewInt64(tableName, "source_asset_id")
	_mediaAssetQuery.CreatedAt = field.NewTime(tableName, "created_at")
	_mediaAssetQuery.UpdatedAt = field.NewTime(tableName, "updated_at")
	_mediaAssetQuery.DeletedAt = field.NewField(tableName, "deleted_at")
	_mediaAssetQuery.Hash = field.NewString(tableName, "hash")

	_mediaAssetQuery.fillFieldMap()

	return _mediaAssetQuery
}

type mediaAssetQuery struct {
	mediaAssetQueryDo mediaAssetQueryDo

	ALL           field.Asterisk
	ID            field.Int64
	TenantID      field.Int64
	UserID        field.Int64
	Type          field.Field
	Status        field.Field
	Provider      field.String
	Bucket        field.String
	ObjectKey     field.String
	Meta          field.JSONB
	Variant       field.Field
	SourceAssetID field.Int64
	CreatedAt     field.Time
	UpdatedAt     field.Time
	DeletedAt     field.Field
	Hash          field.String // 文件 MD5 哈希

	fieldMap map[string]field.Expr
}

func (m mediaAssetQuery) Table(newTableName string) *mediaAssetQuery {
	m.mediaAssetQueryDo.UseTable(newTableName)
	return m.updateTableName(newTableName)
}

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

func (m *mediaAssetQuery) updateTableName(table string) *mediaAssetQuery {
	m.ALL = field.NewAsterisk(table)
	m.ID = field.NewInt64(table, "id")
	m.TenantID = field.NewInt64(table, "tenant_id")
	m.UserID = field.NewInt64(table, "user_id")
	m.Type = field.NewField(table, "type")
	m.Status = field.NewField(table, "status")
	m.Provider = field.NewString(table, "provider")
	m.Bucket = field.NewString(table, "bucket")
	m.ObjectKey = field.NewString(table, "object_key")
	m.Meta = field.NewJSONB(table, "meta")
	m.Variant = field.NewField(table, "variant")
	m.SourceAssetID = field.NewInt64(table, "source_asset_id")
	m.CreatedAt = field.NewTime(table, "created_at")
	m.UpdatedAt = field.NewTime(table, "updated_at")
	m.DeletedAt = field.NewField(table, "deleted_at")
	m.Hash = field.NewString(table, "hash")

	m.fillFieldMap()

	return m
}

func (m *mediaAssetQuery) QueryContext(ctx context.Context) (*mediaAssetQuery, *mediaAssetQueryDo) {
	return m, m.mediaAssetQueryDo.WithContext(ctx)
}

func (m *mediaAssetQuery) WithContext(ctx context.Context) *mediaAssetQueryDo {
	return m.mediaAssetQueryDo.WithContext(ctx)
}

func (m mediaAssetQuery) TableName() string { return m.mediaAssetQueryDo.TableName() }

func (m mediaAssetQuery) Alias() string { return m.mediaAssetQueryDo.Alias() }

func (m mediaAssetQuery) Columns(cols ...field.Expr) gen.Columns {
	return m.mediaAssetQueryDo.Columns(cols...)
}

func (m *mediaAssetQuery) 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 *mediaAssetQuery) fillFieldMap() {
	m.fieldMap = make(map[string]field.Expr, 15)
	m.fieldMap["id"] = m.ID
	m.fieldMap["tenant_id"] = m.TenantID
	m.fieldMap["user_id"] = m.UserID
	m.fieldMap["type"] = m.Type
	m.fieldMap["status"] = m.Status
	m.fieldMap["provider"] = m.Provider
	m.fieldMap["bucket"] = m.Bucket
	m.fieldMap["object_key"] = m.ObjectKey
	m.fieldMap["meta"] = m.Meta
	m.fieldMap["variant"] = m.Variant
	m.fieldMap["source_asset_id"] = m.SourceAssetID
	m.fieldMap["created_at"] = m.CreatedAt
	m.fieldMap["updated_at"] = m.UpdatedAt
	m.fieldMap["deleted_at"] = m.DeletedAt
	m.fieldMap["hash"] = m.Hash
}

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

func (m mediaAssetQuery) replaceDB(db *gorm.DB) mediaAssetQuery {
	m.mediaAssetQueryDo.ReplaceDB(db)
	return m
}

type mediaAssetQueryDo struct{ gen.DO }

func (m mediaAssetQueryDo) Debug() *mediaAssetQueryDo {
	return m.withDO(m.DO.Debug())
}

func (m mediaAssetQueryDo) WithContext(ctx context.Context) *mediaAssetQueryDo {
	return m.withDO(m.DO.WithContext(ctx))
}

func (m mediaAssetQueryDo) ReadDB() *mediaAssetQueryDo {
	return m.Clauses(dbresolver.Read)
}

func (m mediaAssetQueryDo) WriteDB() *mediaAssetQueryDo {
	return m.Clauses(dbresolver.Write)
}

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

func (m mediaAssetQueryDo) Clauses(conds ...clause.Expression) *mediaAssetQueryDo {
	return m.withDO(m.DO.Clauses(conds...))
}

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

func (m mediaAssetQueryDo) Not(conds ...gen.Condition) *mediaAssetQueryDo {
	return m.withDO(m.DO.Not(conds...))
}

func (m mediaAssetQueryDo) Or(conds ...gen.Condition) *mediaAssetQueryDo {
	return m.withDO(m.DO.Or(conds...))
}

func (m mediaAssetQueryDo) Select(conds ...field.Expr) *mediaAssetQueryDo {
	return m.withDO(m.DO.Select(conds...))
}

func (m mediaAssetQueryDo) Where(conds ...gen.Condition) *mediaAssetQueryDo {
	return m.withDO(m.DO.Where(conds...))
}

func (m mediaAssetQueryDo) Order(conds ...field.Expr) *mediaAssetQueryDo {
	return m.withDO(m.DO.Order(conds...))
}

func (m mediaAssetQueryDo) Distinct(cols ...field.Expr) *mediaAssetQueryDo {
	return m.withDO(m.DO.Distinct(cols...))
}

func (m mediaAssetQueryDo) Omit(cols ...field.Expr) *mediaAssetQueryDo {
	return m.withDO(m.DO.Omit(cols...))
}

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

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

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

func (m mediaAssetQueryDo) Group(cols ...field.Expr) *mediaAssetQueryDo {
	return m.withDO(m.DO.Group(cols...))
}

func (m mediaAssetQueryDo) Having(conds ...gen.Condition) *mediaAssetQueryDo {
	return m.withDO(m.DO.Having(conds...))
}

func (m mediaAssetQueryDo) Limit(limit int) *mediaAssetQueryDo {
	return m.withDO(m.DO.Limit(limit))
}

func (m mediaAssetQueryDo) Offset(offset int) *mediaAssetQueryDo {
	return m.withDO(m.DO.Offset(offset))
}

func (m mediaAssetQueryDo) Scopes(funcs ...func(gen.Dao) gen.Dao) *mediaAssetQueryDo {
	return m.withDO(m.DO.Scopes(funcs...))
}

func (m mediaAssetQueryDo) Unscoped() *mediaAssetQueryDo {
	return m.withDO(m.DO.Unscoped())
}

func (m mediaAssetQueryDo) Create(values ...*MediaAsset) error {
	if len(values) == 0 {
		return nil
	}
	return m.DO.Create(values)
}

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

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

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

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

func (m mediaAssetQueryDo) Find() ([]*MediaAsset, error) {
	result, err := m.DO.Find()
	return result.([]*MediaAsset), err
}

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

func (m mediaAssetQueryDo) Attrs(attrs ...field.AssignExpr) *mediaAssetQueryDo {
	return m.withDO(m.DO.Attrs(attrs...))
}

func (m mediaAssetQueryDo) Assign(attrs ...field.AssignExpr) *mediaAssetQueryDo {
	return m.withDO(m.DO.Assign(attrs...))
}

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

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

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

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

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

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

// ForceDelete performs a permanent delete (ignores soft-delete) for current scope.
func (m mediaAssetQueryDo) ForceDelete() (gen.ResultInfo, error) {
	return m.Unscoped().Delete()
}

// Inc increases the given column by step for current scope.
func (m mediaAssetQueryDo) Inc(column field.Expr, step int64) (gen.ResultInfo, error) {
	// column = column + step
	e := field.NewUnsafeFieldRaw("?+?", column.RawExpr(), step)
	return m.DO.UpdateColumn(column, e)
}

// Dec decreases the given column by step for current scope.
func (m mediaAssetQueryDo) Dec(column field.Expr, step int64) (gen.ResultInfo, error) {
	// column = column - step
	e := field.NewUnsafeFieldRaw("?-?", column.RawExpr(), step)
	return m.DO.UpdateColumn(column, e)
}

// Sum returns SUM(column) for current scope.
func (m mediaAssetQueryDo) Sum(column field.Expr) (float64, error) {
	var _v float64
	agg := field.NewUnsafeFieldRaw("SUM(?)", column.RawExpr())
	if err := m.Select(agg).Scan(&_v); err != nil {
		return 0, err
	}
	return _v, nil
}

// Avg returns AVG(column) for current scope.
func (m mediaAssetQueryDo) Avg(column field.Expr) (float64, error) {
	var _v float64
	agg := field.NewUnsafeFieldRaw("AVG(?)", column.RawExpr())
	if err := m.Select(agg).Scan(&_v); err != nil {
		return 0, err
	}
	return _v, nil
}

// Min returns MIN(column) for current scope.
func (m mediaAssetQueryDo) Min(column field.Expr) (float64, error) {
	var _v float64
	agg := field.NewUnsafeFieldRaw("MIN(?)", column.RawExpr())
	if err := m.Select(agg).Scan(&_v); err != nil {
		return 0, err
	}
	return _v, nil
}

// Max returns MAX(column) for current scope.
func (m mediaAssetQueryDo) Max(column field.Expr) (float64, error) {
	var _v float64
	agg := field.NewUnsafeFieldRaw("MAX(?)", column.RawExpr())
	if err := m.Select(agg).Scan(&_v); err != nil {
		return 0, err
	}
	return _v, nil
}

// PluckMap returns a map[key]value for selected key/value expressions within current scope.
func (m mediaAssetQueryDo) PluckMap(key, val field.Expr) (map[interface{}]interface{}, error) {
	do := m.Select(key, val)
	rows, err := do.DO.Rows()
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	mm := make(map[interface{}]interface{})
	for rows.Next() {
		var k interface{}
		var v interface{}
		if err := rows.Scan(&k, &v); err != nil {
			return nil, err
		}
		mm[k] = v
	}
	return mm, rows.Err()
}

// Exists returns true if any record matches the given conditions.
func (m mediaAssetQueryDo) Exists(conds ...gen.Condition) (bool, error) {
	cnt, err := m.Where(conds...).Count()
	if err != nil {
		return false, err
	}
	return cnt > 0, nil
}

// PluckIDs returns all primary key values under current scope.
func (m mediaAssetQueryDo) PluckIDs() ([]int64, error) {
	ids := make([]int64, 0, 16)
	pk := field.NewInt64(m.TableName(), "id")
	if err := m.DO.Pluck(pk, &ids); err != nil {
		return nil, err
	}
	return ids, nil
}

// GetByID finds a single record by primary key.
func (m mediaAssetQueryDo) GetByID(id int64) (*MediaAsset, error) {
	pk := field.NewInt64(m.TableName(), "id")
	return m.Where(pk.Eq(id)).First()
}

// GetByIDs finds records by primary key list.
func (m mediaAssetQueryDo) GetByIDs(ids ...int64) ([]*MediaAsset, error) {
	if len(ids) == 0 {
		return []*MediaAsset{}, nil
	}
	pk := field.NewInt64(m.TableName(), "id")
	return m.Where(pk.In(ids...)).Find()
}

// DeleteByID deletes records by primary key.
func (m mediaAssetQueryDo) DeleteByID(id int64) (gen.ResultInfo, error) {
	pk := field.NewInt64(m.TableName(), "id")
	return m.Where(pk.Eq(id)).Delete()
}

// DeleteByIDs deletes records by a list of primary keys.
func (m mediaAssetQueryDo) DeleteByIDs(ids ...int64) (gen.ResultInfo, error) {
	if len(ids) == 0 {
		return gen.ResultInfo{RowsAffected: 0, Error: nil}, nil
	}
	pk := field.NewInt64(m.TableName(), "id")
	return m.Where(pk.In(ids...)).Delete()
}

// RestoreWhere sets deleted_at to NULL for rows matching current scope + conds.
func (m mediaAssetQueryDo) RestoreWhere(conds ...gen.Condition) (gen.ResultInfo, error) {
	col := field.NewField(m.TableName(), "deleted_at")
	return m.Unscoped().Where(conds...).UpdateColumn(col, nil)
}

// RestoreByID sets deleted_at to NULL for the given primary key.
func (m mediaAssetQueryDo) RestoreByID(id int64) (gen.ResultInfo, error) {
	pk := field.NewInt64(m.TableName(), "id")
	col := field.NewField(m.TableName(), "deleted_at")
	return m.Unscoped().Where(pk.Eq(id)).UpdateColumn(col, nil)
}

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