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

	_orderItemQuery.orderItemQueryDo.UseDB(db, opts...)
	_orderItemQuery.orderItemQueryDo.UseModel(&OrderItem{})

	tableName := _orderItemQuery.orderItemQueryDo.TableName()
	_orderItemQuery.ALL = field.NewAsterisk(tableName)
	_orderItemQuery.ID = field.NewInt64(tableName, "id")
	_orderItemQuery.TenantID = field.NewInt64(tableName, "tenant_id")
	_orderItemQuery.UserID = field.NewInt64(tableName, "user_id")
	_orderItemQuery.OrderID = field.NewInt64(tableName, "order_id")
	_orderItemQuery.ContentID = field.NewInt64(tableName, "content_id")
	_orderItemQuery.ContentUserID = field.NewInt64(tableName, "content_user_id")
	_orderItemQuery.AmountPaid = field.NewInt64(tableName, "amount_paid")
	_orderItemQuery.Snapshot = field.NewJSONB(tableName, "snapshot")
	_orderItemQuery.CreatedAt = field.NewTime(tableName, "created_at")
	_orderItemQuery.UpdatedAt = field.NewTime(tableName, "updated_at")

	_orderItemQuery.fillFieldMap()

	return _orderItemQuery
}

type orderItemQuery struct {
	orderItemQueryDo orderItemQueryDo

	ALL           field.Asterisk
	ID            field.Int64
	TenantID      field.Int64
	UserID        field.Int64
	OrderID       field.Int64
	ContentID     field.Int64
	ContentUserID field.Int64
	AmountPaid    field.Int64
	Snapshot      field.JSONB
	CreatedAt     field.Time
	UpdatedAt     field.Time

	fieldMap map[string]field.Expr
}

func (o orderItemQuery) Table(newTableName string) *orderItemQuery {
	o.orderItemQueryDo.UseTable(newTableName)
	return o.updateTableName(newTableName)
}

func (o orderItemQuery) As(alias string) *orderItemQuery {
	o.orderItemQueryDo.DO = *(o.orderItemQueryDo.As(alias).(*gen.DO))
	return o.updateTableName(alias)
}

func (o *orderItemQuery) updateTableName(table string) *orderItemQuery {
	o.ALL = field.NewAsterisk(table)
	o.ID = field.NewInt64(table, "id")
	o.TenantID = field.NewInt64(table, "tenant_id")
	o.UserID = field.NewInt64(table, "user_id")
	o.OrderID = field.NewInt64(table, "order_id")
	o.ContentID = field.NewInt64(table, "content_id")
	o.ContentUserID = field.NewInt64(table, "content_user_id")
	o.AmountPaid = field.NewInt64(table, "amount_paid")
	o.Snapshot = field.NewJSONB(table, "snapshot")
	o.CreatedAt = field.NewTime(table, "created_at")
	o.UpdatedAt = field.NewTime(table, "updated_at")

	o.fillFieldMap()

	return o
}

func (o *orderItemQuery) QueryContext(ctx context.Context) (*orderItemQuery, *orderItemQueryDo) {
	return o, o.orderItemQueryDo.WithContext(ctx)
}

func (o *orderItemQuery) WithContext(ctx context.Context) *orderItemQueryDo {
	return o.orderItemQueryDo.WithContext(ctx)
}

func (o orderItemQuery) TableName() string { return o.orderItemQueryDo.TableName() }

func (o orderItemQuery) Alias() string { return o.orderItemQueryDo.Alias() }

func (o orderItemQuery) Columns(cols ...field.Expr) gen.Columns {
	return o.orderItemQueryDo.Columns(cols...)
}

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

func (o *orderItemQuery) fillFieldMap() {
	o.fieldMap = make(map[string]field.Expr, 10)
	o.fieldMap["id"] = o.ID
	o.fieldMap["tenant_id"] = o.TenantID
	o.fieldMap["user_id"] = o.UserID
	o.fieldMap["order_id"] = o.OrderID
	o.fieldMap["content_id"] = o.ContentID
	o.fieldMap["content_user_id"] = o.ContentUserID
	o.fieldMap["amount_paid"] = o.AmountPaid
	o.fieldMap["snapshot"] = o.Snapshot
	o.fieldMap["created_at"] = o.CreatedAt
	o.fieldMap["updated_at"] = o.UpdatedAt
}

func (o orderItemQuery) clone(db *gorm.DB) orderItemQuery {
	o.orderItemQueryDo.ReplaceConnPool(db.Statement.ConnPool)
	return o
}

func (o orderItemQuery) replaceDB(db *gorm.DB) orderItemQuery {
	o.orderItemQueryDo.ReplaceDB(db)
	return o
}

type orderItemQueryDo struct{ gen.DO }

func (o orderItemQueryDo) Debug() *orderItemQueryDo {
	return o.withDO(o.DO.Debug())
}

func (o orderItemQueryDo) WithContext(ctx context.Context) *orderItemQueryDo {
	return o.withDO(o.DO.WithContext(ctx))
}

func (o orderItemQueryDo) ReadDB() *orderItemQueryDo {
	return o.Clauses(dbresolver.Read)
}

func (o orderItemQueryDo) WriteDB() *orderItemQueryDo {
	return o.Clauses(dbresolver.Write)
}

func (o orderItemQueryDo) Session(config *gorm.Session) *orderItemQueryDo {
	return o.withDO(o.DO.Session(config))
}

func (o orderItemQueryDo) Clauses(conds ...clause.Expression) *orderItemQueryDo {
	return o.withDO(o.DO.Clauses(conds...))
}

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

func (o orderItemQueryDo) Not(conds ...gen.Condition) *orderItemQueryDo {
	return o.withDO(o.DO.Not(conds...))
}

func (o orderItemQueryDo) Or(conds ...gen.Condition) *orderItemQueryDo {
	return o.withDO(o.DO.Or(conds...))
}

func (o orderItemQueryDo) Select(conds ...field.Expr) *orderItemQueryDo {
	return o.withDO(o.DO.Select(conds...))
}

func (o orderItemQueryDo) Where(conds ...gen.Condition) *orderItemQueryDo {
	return o.withDO(o.DO.Where(conds...))
}

func (o orderItemQueryDo) Order(conds ...field.Expr) *orderItemQueryDo {
	return o.withDO(o.DO.Order(conds...))
}

func (o orderItemQueryDo) Distinct(cols ...field.Expr) *orderItemQueryDo {
	return o.withDO(o.DO.Distinct(cols...))
}

func (o orderItemQueryDo) Omit(cols ...field.Expr) *orderItemQueryDo {
	return o.withDO(o.DO.Omit(cols...))
}

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

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

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

func (o orderItemQueryDo) Group(cols ...field.Expr) *orderItemQueryDo {
	return o.withDO(o.DO.Group(cols...))
}

func (o orderItemQueryDo) Having(conds ...gen.Condition) *orderItemQueryDo {
	return o.withDO(o.DO.Having(conds...))
}

func (o orderItemQueryDo) Limit(limit int) *orderItemQueryDo {
	return o.withDO(o.DO.Limit(limit))
}

func (o orderItemQueryDo) Offset(offset int) *orderItemQueryDo {
	return o.withDO(o.DO.Offset(offset))
}

func (o orderItemQueryDo) Scopes(funcs ...func(gen.Dao) gen.Dao) *orderItemQueryDo {
	return o.withDO(o.DO.Scopes(funcs...))
}

func (o orderItemQueryDo) Unscoped() *orderItemQueryDo {
	return o.withDO(o.DO.Unscoped())
}

func (o orderItemQueryDo) Create(values ...*OrderItem) error {
	if len(values) == 0 {
		return nil
	}
	return o.DO.Create(values)
}

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

func (o orderItemQueryDo) First() (*OrderItem, error) {
	if result, err := o.DO.First(); err != nil {
		return nil, err
	} else {
		return result.(*OrderItem), nil
	}
}

func (o orderItemQueryDo) Take() (*OrderItem, error) {
	if result, err := o.DO.Take(); err != nil {
		return nil, err
	} else {
		return result.(*OrderItem), nil
	}
}

func (o orderItemQueryDo) Last() (*OrderItem, error) {
	if result, err := o.DO.Last(); err != nil {
		return nil, err
	} else {
		return result.(*OrderItem), nil
	}
}

func (o orderItemQueryDo) Find() ([]*OrderItem, error) {
	result, err := o.DO.Find()
	return result.([]*OrderItem), err
}

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

func (o orderItemQueryDo) Attrs(attrs ...field.AssignExpr) *orderItemQueryDo {
	return o.withDO(o.DO.Attrs(attrs...))
}

func (o orderItemQueryDo) Assign(attrs ...field.AssignExpr) *orderItemQueryDo {
	return o.withDO(o.DO.Assign(attrs...))
}

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

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

func (o orderItemQueryDo) FirstOrInit() (*OrderItem, error) {
	if result, err := o.DO.FirstOrInit(); err != nil {
		return nil, err
	} else {
		return result.(*OrderItem), nil
	}
}

func (o orderItemQueryDo) FirstOrCreate() (*OrderItem, error) {
	if result, err := o.DO.FirstOrCreate(); err != nil {
		return nil, err
	} else {
		return result.(*OrderItem), nil
	}
}

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

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

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

func (o orderItemQueryDo) Scan(result interface{}) (err error) {
	return o.DO.Scan(result)
}

func (o orderItemQueryDo) Delete(models ...*OrderItem) (result gen.ResultInfo, err error) {
	return o.DO.Delete(models)
}

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

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

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

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

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

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

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

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

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

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

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

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

func (o *orderItemQueryDo) withDO(do gen.Dao) *orderItemQueryDo {
	o.DO = *do.(*gen.DO)
	return o
}
