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

	_contentAccessQuery.contentAccessQueryDo.UseDB(db, opts...)
	_contentAccessQuery.contentAccessQueryDo.UseModel(&ContentAccess{})

	tableName := _contentAccessQuery.contentAccessQueryDo.TableName()
	_contentAccessQuery.ALL = field.NewAsterisk(tableName)
	_contentAccessQuery.ID = field.NewInt64(tableName, "id")
	_contentAccessQuery.TenantID = field.NewInt64(tableName, "tenant_id")
	_contentAccessQuery.UserID = field.NewInt64(tableName, "user_id")
	_contentAccessQuery.ContentID = field.NewInt64(tableName, "content_id")
	_contentAccessQuery.OrderID = field.NewInt64(tableName, "order_id")
	_contentAccessQuery.Status = field.NewField(tableName, "status")
	_contentAccessQuery.RevokedAt = field.NewTime(tableName, "revoked_at")
	_contentAccessQuery.CreatedAt = field.NewTime(tableName, "created_at")
	_contentAccessQuery.UpdatedAt = field.NewTime(tableName, "updated_at")

	_contentAccessQuery.fillFieldMap()

	return _contentAccessQuery
}

type contentAccessQuery struct {
	contentAccessQueryDo contentAccessQueryDo

	ALL       field.Asterisk
	ID        field.Int64
	TenantID  field.Int64
	UserID    field.Int64
	ContentID field.Int64
	OrderID   field.Int64
	Status    field.Field
	RevokedAt field.Time
	CreatedAt field.Time
	UpdatedAt field.Time

	fieldMap map[string]field.Expr
}

func (c contentAccessQuery) Table(newTableName string) *contentAccessQuery {
	c.contentAccessQueryDo.UseTable(newTableName)
	return c.updateTableName(newTableName)
}

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

func (c *contentAccessQuery) updateTableName(table string) *contentAccessQuery {
	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.OrderID = field.NewInt64(table, "order_id")
	c.Status = field.NewField(table, "status")
	c.RevokedAt = field.NewTime(table, "revoked_at")
	c.CreatedAt = field.NewTime(table, "created_at")
	c.UpdatedAt = field.NewTime(table, "updated_at")

	c.fillFieldMap()

	return c
}

func (c *contentAccessQuery) QueryContext(ctx context.Context) (*contentAccessQuery, *contentAccessQueryDo) {
	return c, c.contentAccessQueryDo.WithContext(ctx)
}

func (c *contentAccessQuery) WithContext(ctx context.Context) *contentAccessQueryDo {
	return c.contentAccessQueryDo.WithContext(ctx)
}

func (c contentAccessQuery) TableName() string { return c.contentAccessQueryDo.TableName() }

func (c contentAccessQuery) Alias() string { return c.contentAccessQueryDo.Alias() }

func (c contentAccessQuery) Columns(cols ...field.Expr) gen.Columns {
	return c.contentAccessQueryDo.Columns(cols...)
}

func (c *contentAccessQuery) 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 *contentAccessQuery) 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["order_id"] = c.OrderID
	c.fieldMap["status"] = c.Status
	c.fieldMap["revoked_at"] = c.RevokedAt
	c.fieldMap["created_at"] = c.CreatedAt
	c.fieldMap["updated_at"] = c.UpdatedAt
}

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

func (c contentAccessQuery) replaceDB(db *gorm.DB) contentAccessQuery {
	c.contentAccessQueryDo.ReplaceDB(db)
	return c
}

type contentAccessQueryDo struct{ gen.DO }

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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