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

	_contentPriceQuery.contentPriceQueryDo.UseDB(db, opts...)
	_contentPriceQuery.contentPriceQueryDo.UseModel(&ContentPrice{})

	tableName := _contentPriceQuery.contentPriceQueryDo.TableName()
	_contentPriceQuery.ALL = field.NewAsterisk(tableName)
	_contentPriceQuery.ID = field.NewInt64(tableName, "id")
	_contentPriceQuery.TenantID = field.NewInt64(tableName, "tenant_id")
	_contentPriceQuery.UserID = field.NewInt64(tableName, "user_id")
	_contentPriceQuery.ContentID = field.NewInt64(tableName, "content_id")
	_contentPriceQuery.Currency = field.NewString(tableName, "currency")
	_contentPriceQuery.PriceAmount = field.NewInt64(tableName, "price_amount")
	_contentPriceQuery.DiscountType = field.NewString(tableName, "discount_type")
	_contentPriceQuery.DiscountValue = field.NewInt64(tableName, "discount_value")
	_contentPriceQuery.DiscountStartAt = field.NewTime(tableName, "discount_start_at")
	_contentPriceQuery.DiscountEndAt = field.NewTime(tableName, "discount_end_at")
	_contentPriceQuery.CreatedAt = field.NewTime(tableName, "created_at")
	_contentPriceQuery.UpdatedAt = field.NewTime(tableName, "updated_at")

	_contentPriceQuery.fillFieldMap()

	return _contentPriceQuery
}

type contentPriceQuery struct {
	contentPriceQueryDo contentPriceQueryDo

	ALL             field.Asterisk
	ID              field.Int64
	TenantID        field.Int64
	UserID          field.Int64
	ContentID       field.Int64
	Currency        field.String
	PriceAmount     field.Int64
	DiscountType    field.String
	DiscountValue   field.Int64
	DiscountStartAt field.Time
	DiscountEndAt   field.Time
	CreatedAt       field.Time
	UpdatedAt       field.Time

	fieldMap map[string]field.Expr
}

func (c contentPriceQuery) Table(newTableName string) *contentPriceQuery {
	c.contentPriceQueryDo.UseTable(newTableName)
	return c.updateTableName(newTableName)
}

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

func (c *contentPriceQuery) updateTableName(table string) *contentPriceQuery {
	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.Currency = field.NewString(table, "currency")
	c.PriceAmount = field.NewInt64(table, "price_amount")
	c.DiscountType = field.NewString(table, "discount_type")
	c.DiscountValue = field.NewInt64(table, "discount_value")
	c.DiscountStartAt = field.NewTime(table, "discount_start_at")
	c.DiscountEndAt = field.NewTime(table, "discount_end_at")
	c.CreatedAt = field.NewTime(table, "created_at")
	c.UpdatedAt = field.NewTime(table, "updated_at")

	c.fillFieldMap()

	return c
}

func (c *contentPriceQuery) QueryContext(ctx context.Context) (*contentPriceQuery, *contentPriceQueryDo) {
	return c, c.contentPriceQueryDo.WithContext(ctx)
}

func (c *contentPriceQuery) WithContext(ctx context.Context) *contentPriceQueryDo {
	return c.contentPriceQueryDo.WithContext(ctx)
}

func (c contentPriceQuery) TableName() string { return c.contentPriceQueryDo.TableName() }

func (c contentPriceQuery) Alias() string { return c.contentPriceQueryDo.Alias() }

func (c contentPriceQuery) Columns(cols ...field.Expr) gen.Columns {
	return c.contentPriceQueryDo.Columns(cols...)
}

func (c *contentPriceQuery) 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 *contentPriceQuery) fillFieldMap() {
	c.fieldMap = make(map[string]field.Expr, 12)
	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["currency"] = c.Currency
	c.fieldMap["price_amount"] = c.PriceAmount
	c.fieldMap["discount_type"] = c.DiscountType
	c.fieldMap["discount_value"] = c.DiscountValue
	c.fieldMap["discount_start_at"] = c.DiscountStartAt
	c.fieldMap["discount_end_at"] = c.DiscountEndAt
	c.fieldMap["created_at"] = c.CreatedAt
	c.fieldMap["updated_at"] = c.UpdatedAt
}

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

func (c contentPriceQuery) replaceDB(db *gorm.DB) contentPriceQuery {
	c.contentPriceQueryDo.ReplaceDB(db)
	return c
}

type contentPriceQueryDo struct{ gen.DO }

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// Inc increases the given column by step for current scope.
func (c contentPriceQueryDo) 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 contentPriceQueryDo) 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 contentPriceQueryDo) 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 contentPriceQueryDo) 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 contentPriceQueryDo) 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 contentPriceQueryDo) 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 contentPriceQueryDo) 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 contentPriceQueryDo) 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 contentPriceQueryDo) 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 contentPriceQueryDo) GetByID(id int64) (*ContentPrice, error) {
	pk := field.NewInt64(c.TableName(), "id")
	return c.Where(pk.Eq(id)).First()
}

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

// DeleteByID deletes records by primary key.
func (c contentPriceQueryDo) 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 contentPriceQueryDo) 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 *contentPriceQueryDo) withDO(do gen.Dao) *contentPriceQueryDo {
	c.DO = *do.(*gen.DO)
	return c
}
