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

	_contentAssetQuery.contentAssetQueryDo.UseDB(db, opts...)
	_contentAssetQuery.contentAssetQueryDo.UseModel(&ContentAsset{})

	tableName := _contentAssetQuery.contentAssetQueryDo.TableName()
	_contentAssetQuery.ALL = field.NewAsterisk(tableName)
	_contentAssetQuery.ID = field.NewInt64(tableName, "id")
	_contentAssetQuery.TenantID = field.NewInt64(tableName, "tenant_id")
	_contentAssetQuery.UserID = field.NewInt64(tableName, "user_id")
	_contentAssetQuery.ContentID = field.NewInt64(tableName, "content_id")
	_contentAssetQuery.AssetID = field.NewInt64(tableName, "asset_id")
	_contentAssetQuery.Role = field.NewString(tableName, "role")
	_contentAssetQuery.Sort = field.NewInt32(tableName, "sort")
	_contentAssetQuery.CreatedAt = field.NewTime(tableName, "created_at")
	_contentAssetQuery.UpdatedAt = field.NewTime(tableName, "updated_at")

	_contentAssetQuery.fillFieldMap()

	return _contentAssetQuery
}

type contentAssetQuery struct {
	contentAssetQueryDo contentAssetQueryDo

	ALL       field.Asterisk
	ID        field.Int64
	TenantID  field.Int64
	UserID    field.Int64
	ContentID field.Int64
	AssetID   field.Int64
	Role      field.String
	Sort      field.Int32
	CreatedAt field.Time
	UpdatedAt field.Time

	fieldMap map[string]field.Expr
}

func (c contentAssetQuery) Table(newTableName string) *contentAssetQuery {
	c.contentAssetQueryDo.UseTable(newTableName)
	return c.updateTableName(newTableName)
}

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

func (c *contentAssetQuery) updateTableName(table string) *contentAssetQuery {
	c.ALL = field.NewAsterisk(table)
	c.ID = field.NewInt64(table, "id")
	c.TenantID = field.NewInt64(table, "tenant_id")
	c.UserID = field.NewInt64(table, "user_id")
	c.ContentID = field.NewInt64(table, "content_id")
	c.AssetID = field.NewInt64(table, "asset_id")
	c.Role = field.NewString(table, "role")
	c.Sort = field.NewInt32(table, "sort")
	c.CreatedAt = field.NewTime(table, "created_at")
	c.UpdatedAt = field.NewTime(table, "updated_at")

	c.fillFieldMap()

	return c
}

func (c *contentAssetQuery) QueryContext(ctx context.Context) (*contentAssetQuery, *contentAssetQueryDo) {
	return c, c.contentAssetQueryDo.WithContext(ctx)
}

func (c *contentAssetQuery) WithContext(ctx context.Context) *contentAssetQueryDo {
	return c.contentAssetQueryDo.WithContext(ctx)
}

func (c contentAssetQuery) TableName() string { return c.contentAssetQueryDo.TableName() }

func (c contentAssetQuery) Alias() string { return c.contentAssetQueryDo.Alias() }

func (c contentAssetQuery) Columns(cols ...field.Expr) gen.Columns {
	return c.contentAssetQueryDo.Columns(cols...)
}

func (c *contentAssetQuery) 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 *contentAssetQuery) fillFieldMap() {
	c.fieldMap = make(map[string]field.Expr, 9)
	c.fieldMap["id"] = c.ID
	c.fieldMap["tenant_id"] = c.TenantID
	c.fieldMap["user_id"] = c.UserID
	c.fieldMap["content_id"] = c.ContentID
	c.fieldMap["asset_id"] = c.AssetID
	c.fieldMap["role"] = c.Role
	c.fieldMap["sort"] = c.Sort
	c.fieldMap["created_at"] = c.CreatedAt
	c.fieldMap["updated_at"] = c.UpdatedAt
}

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

func (c contentAssetQuery) replaceDB(db *gorm.DB) contentAssetQuery {
	c.contentAssetQueryDo.ReplaceDB(db)
	return c
}

type contentAssetQueryDo struct{ gen.DO }

func (c contentAssetQueryDo) Debug() *contentAssetQueryDo {
	return c.withDO(c.DO.Debug())
}

func (c contentAssetQueryDo) WithContext(ctx context.Context) *contentAssetQueryDo {
	return c.withDO(c.DO.WithContext(ctx))
}

func (c contentAssetQueryDo) ReadDB() *contentAssetQueryDo {
	return c.Clauses(dbresolver.Read)
}

func (c contentAssetQueryDo) WriteDB() *contentAssetQueryDo {
	return c.Clauses(dbresolver.Write)
}

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

func (c contentAssetQueryDo) Clauses(conds ...clause.Expression) *contentAssetQueryDo {
	return c.withDO(c.DO.Clauses(conds...))
}

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

func (c contentAssetQueryDo) Not(conds ...gen.Condition) *contentAssetQueryDo {
	return c.withDO(c.DO.Not(conds...))
}

func (c contentAssetQueryDo) Or(conds ...gen.Condition) *contentAssetQueryDo {
	return c.withDO(c.DO.Or(conds...))
}

func (c contentAssetQueryDo) Select(conds ...field.Expr) *contentAssetQueryDo {
	return c.withDO(c.DO.Select(conds...))
}

func (c contentAssetQueryDo) Where(conds ...gen.Condition) *contentAssetQueryDo {
	return c.withDO(c.DO.Where(conds...))
}

func (c contentAssetQueryDo) Order(conds ...field.Expr) *contentAssetQueryDo {
	return c.withDO(c.DO.Order(conds...))
}

func (c contentAssetQueryDo) Distinct(cols ...field.Expr) *contentAssetQueryDo {
	return c.withDO(c.DO.Distinct(cols...))
}

func (c contentAssetQueryDo) Omit(cols ...field.Expr) *contentAssetQueryDo {
	return c.withDO(c.DO.Omit(cols...))
}

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

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

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

func (c contentAssetQueryDo) Group(cols ...field.Expr) *contentAssetQueryDo {
	return c.withDO(c.DO.Group(cols...))
}

func (c contentAssetQueryDo) Having(conds ...gen.Condition) *contentAssetQueryDo {
	return c.withDO(c.DO.Having(conds...))
}

func (c contentAssetQueryDo) Limit(limit int) *contentAssetQueryDo {
	return c.withDO(c.DO.Limit(limit))
}

func (c contentAssetQueryDo) Offset(offset int) *contentAssetQueryDo {
	return c.withDO(c.DO.Offset(offset))
}

func (c contentAssetQueryDo) Scopes(funcs ...func(gen.Dao) gen.Dao) *contentAssetQueryDo {
	return c.withDO(c.DO.Scopes(funcs...))
}

func (c contentAssetQueryDo) Unscoped() *contentAssetQueryDo {
	return c.withDO(c.DO.Unscoped())
}

func (c contentAssetQueryDo) Create(values ...*ContentAsset) error {
	if len(values) == 0 {
		return nil
	}
	return c.DO.Create(values)
}

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

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

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

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

func (c contentAssetQueryDo) Find() ([]*ContentAsset, error) {
	result, err := c.DO.Find()
	return result.([]*ContentAsset), err
}

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

func (c contentAssetQueryDo) Attrs(attrs ...field.AssignExpr) *contentAssetQueryDo {
	return c.withDO(c.DO.Attrs(attrs...))
}

func (c contentAssetQueryDo) Assign(attrs ...field.AssignExpr) *contentAssetQueryDo {
	return c.withDO(c.DO.Assign(attrs...))
}

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

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

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

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

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

func (c contentAssetQueryDo) Delete(models ...*ContentAsset) (result gen.ResultInfo, err error) {
	return c.DO.Delete(models)
}

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

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

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

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

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

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

// Max returns MAX(column) for current scope.
func (c contentAssetQueryDo) Max(column field.Expr) (float64, error) {
	var _v float64
	agg := field.NewUnsafeFieldRaw("MAX(?)", column.RawExpr())
	if err := c.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 (c contentAssetQueryDo) PluckMap(key, val field.Expr) (map[interface{}]interface{}, error) {
	do := c.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 (c contentAssetQueryDo) Exists(conds ...gen.Condition) (bool, error) {
	cnt, err := c.Where(conds...).Count()
	if err != nil {
		return false, err
	}
	return cnt > 0, nil
}

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

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

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

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

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

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