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

	_contentQuery.contentQueryDo.UseDB(db, opts...)
	_contentQuery.contentQueryDo.UseModel(&Content{})

	tableName := _contentQuery.contentQueryDo.TableName()
	_contentQuery.ALL = field.NewAsterisk(tableName)
	_contentQuery.ID = field.NewInt64(tableName, "id")
	_contentQuery.TenantID = field.NewInt64(tableName, "tenant_id")
	_contentQuery.UserID = field.NewInt64(tableName, "user_id")
	_contentQuery.Title = field.NewString(tableName, "title")
	_contentQuery.Description = field.NewString(tableName, "description")
	_contentQuery.Status = field.NewString(tableName, "status")
	_contentQuery.Visibility = field.NewString(tableName, "visibility")
	_contentQuery.PreviewSeconds = field.NewInt32(tableName, "preview_seconds")
	_contentQuery.PreviewDownloadable = field.NewBool(tableName, "preview_downloadable")
	_contentQuery.PublishedAt = field.NewTime(tableName, "published_at")
	_contentQuery.Summary = field.NewString(tableName, "summary")
	_contentQuery.Tags = field.NewJSONB(tableName, "tags")
	_contentQuery.Body = field.NewString(tableName, "body")
	_contentQuery.Genre = field.NewString(tableName, "genre")
	_contentQuery.Views = field.NewInt32(tableName, "views")
	_contentQuery.Likes = field.NewInt32(tableName, "likes")
	_contentQuery.CreatedAt = field.NewTime(tableName, "created_at")
	_contentQuery.UpdatedAt = field.NewTime(tableName, "updated_at")
	_contentQuery.DeletedAt = field.NewField(tableName, "deleted_at")

	_contentQuery.fillFieldMap()

	return _contentQuery
}

type contentQuery struct {
	contentQueryDo contentQueryDo

	ALL                 field.Asterisk
	ID                  field.Int64
	TenantID            field.Int64
	UserID              field.Int64
	Title               field.String
	Description         field.String
	Status              field.String
	Visibility          field.String
	PreviewSeconds      field.Int32
	PreviewDownloadable field.Bool
	PublishedAt         field.Time
	Summary             field.String
	Tags                field.JSONB
	Body                field.String
	Genre               field.String
	Views               field.Int32
	Likes               field.Int32
	CreatedAt           field.Time
	UpdatedAt           field.Time
	DeletedAt           field.Field

	fieldMap map[string]field.Expr
}

func (c contentQuery) Table(newTableName string) *contentQuery {
	c.contentQueryDo.UseTable(newTableName)
	return c.updateTableName(newTableName)
}

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

func (c *contentQuery) updateTableName(table string) *contentQuery {
	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.Title = field.NewString(table, "title")
	c.Description = field.NewString(table, "description")
	c.Status = field.NewString(table, "status")
	c.Visibility = field.NewString(table, "visibility")
	c.PreviewSeconds = field.NewInt32(table, "preview_seconds")
	c.PreviewDownloadable = field.NewBool(table, "preview_downloadable")
	c.PublishedAt = field.NewTime(table, "published_at")
	c.Summary = field.NewString(table, "summary")
	c.Tags = field.NewJSONB(table, "tags")
	c.Body = field.NewString(table, "body")
	c.Genre = field.NewString(table, "genre")
	c.Views = field.NewInt32(table, "views")
	c.Likes = field.NewInt32(table, "likes")
	c.CreatedAt = field.NewTime(table, "created_at")
	c.UpdatedAt = field.NewTime(table, "updated_at")
	c.DeletedAt = field.NewField(table, "deleted_at")

	c.fillFieldMap()

	return c
}

func (c *contentQuery) QueryContext(ctx context.Context) (*contentQuery, *contentQueryDo) {
	return c, c.contentQueryDo.WithContext(ctx)
}

func (c *contentQuery) WithContext(ctx context.Context) *contentQueryDo {
	return c.contentQueryDo.WithContext(ctx)
}

func (c contentQuery) TableName() string { return c.contentQueryDo.TableName() }

func (c contentQuery) Alias() string { return c.contentQueryDo.Alias() }

func (c contentQuery) Columns(cols ...field.Expr) gen.Columns {
	return c.contentQueryDo.Columns(cols...)
}

func (c *contentQuery) 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 *contentQuery) fillFieldMap() {
	c.fieldMap = make(map[string]field.Expr, 19)
	c.fieldMap["id"] = c.ID
	c.fieldMap["tenant_id"] = c.TenantID
	c.fieldMap["user_id"] = c.UserID
	c.fieldMap["title"] = c.Title
	c.fieldMap["description"] = c.Description
	c.fieldMap["status"] = c.Status
	c.fieldMap["visibility"] = c.Visibility
	c.fieldMap["preview_seconds"] = c.PreviewSeconds
	c.fieldMap["preview_downloadable"] = c.PreviewDownloadable
	c.fieldMap["published_at"] = c.PublishedAt
	c.fieldMap["summary"] = c.Summary
	c.fieldMap["tags"] = c.Tags
	c.fieldMap["body"] = c.Body
	c.fieldMap["genre"] = c.Genre
	c.fieldMap["views"] = c.Views
	c.fieldMap["likes"] = c.Likes
	c.fieldMap["created_at"] = c.CreatedAt
	c.fieldMap["updated_at"] = c.UpdatedAt
	c.fieldMap["deleted_at"] = c.DeletedAt
}

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

func (c contentQuery) replaceDB(db *gorm.DB) contentQuery {
	c.contentQueryDo.ReplaceDB(db)
	return c
}

type contentQueryDo struct{ gen.DO }

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// DeleteByID deletes records by primary key.
func (c contentQueryDo) 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 contentQueryDo) 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()
}

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

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

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