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

	_commentQuery.commentQueryDo.UseDB(db, opts...)
	_commentQuery.commentQueryDo.UseModel(&Comment{})

	tableName := _commentQuery.commentQueryDo.TableName()
	_commentQuery.ALL = field.NewAsterisk(tableName)
	_commentQuery.ID = field.NewInt64(tableName, "id")
	_commentQuery.TenantID = field.NewInt64(tableName, "tenant_id")
	_commentQuery.UserID = field.NewInt64(tableName, "user_id")
	_commentQuery.ContentID = field.NewInt64(tableName, "content_id")
	_commentQuery.ReplyTo = field.NewInt64(tableName, "reply_to")
	_commentQuery.Content = field.NewString(tableName, "content")
	_commentQuery.Likes = field.NewInt32(tableName, "likes")
	_commentQuery.CreatedAt = field.NewTime(tableName, "created_at")
	_commentQuery.UpdatedAt = field.NewTime(tableName, "updated_at")
	_commentQuery.DeletedAt = field.NewField(tableName, "deleted_at")

	_commentQuery.fillFieldMap()

	return _commentQuery
}

type commentQuery struct {
	commentQueryDo commentQueryDo

	ALL       field.Asterisk
	ID        field.Int64
	TenantID  field.Int64
	UserID    field.Int64
	ContentID field.Int64
	ReplyTo   field.Int64
	Content   field.String
	Likes     field.Int32
	CreatedAt field.Time
	UpdatedAt field.Time
	DeletedAt field.Field

	fieldMap map[string]field.Expr
}

func (c commentQuery) Table(newTableName string) *commentQuery {
	c.commentQueryDo.UseTable(newTableName)
	return c.updateTableName(newTableName)
}

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

func (c *commentQuery) updateTableName(table string) *commentQuery {
	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.ReplyTo = field.NewInt64(table, "reply_to")
	c.Content = field.NewString(table, "content")
	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 *commentQuery) QueryContext(ctx context.Context) (*commentQuery, *commentQueryDo) {
	return c, c.commentQueryDo.WithContext(ctx)
}

func (c *commentQuery) WithContext(ctx context.Context) *commentQueryDo {
	return c.commentQueryDo.WithContext(ctx)
}

func (c commentQuery) TableName() string { return c.commentQueryDo.TableName() }

func (c commentQuery) Alias() string { return c.commentQueryDo.Alias() }

func (c commentQuery) Columns(cols ...field.Expr) gen.Columns {
	return c.commentQueryDo.Columns(cols...)
}

func (c *commentQuery) 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 *commentQuery) fillFieldMap() {
	c.fieldMap = make(map[string]field.Expr, 10)
	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["reply_to"] = c.ReplyTo
	c.fieldMap["content"] = c.Content
	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 commentQuery) clone(db *gorm.DB) commentQuery {
	c.commentQueryDo.ReplaceConnPool(db.Statement.ConnPool)
	return c
}

func (c commentQuery) replaceDB(db *gorm.DB) commentQuery {
	c.commentQueryDo.ReplaceDB(db)
	return c
}

type commentQueryDo struct{ gen.DO }

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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