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

	_notificationQuery.notificationQueryDo.UseDB(db, opts...)
	_notificationQuery.notificationQueryDo.UseModel(&Notification{})

	tableName := _notificationQuery.notificationQueryDo.TableName()
	_notificationQuery.ALL = field.NewAsterisk(tableName)
	_notificationQuery.ID = field.NewInt64(tableName, "id")
	_notificationQuery.UserID = field.NewInt64(tableName, "user_id")
	_notificationQuery.TenantID = field.NewInt64(tableName, "tenant_id")
	_notificationQuery.Type = field.NewString(tableName, "type")
	_notificationQuery.Title = field.NewString(tableName, "title")
	_notificationQuery.Content = field.NewString(tableName, "content")
	_notificationQuery.IsRead = field.NewBool(tableName, "is_read")
	_notificationQuery.CreatedAt = field.NewTime(tableName, "created_at")

	_notificationQuery.fillFieldMap()

	return _notificationQuery
}

type notificationQuery struct {
	notificationQueryDo notificationQueryDo

	ALL       field.Asterisk
	ID        field.Int64
	UserID    field.Int64
	TenantID  field.Int64
	Type      field.String
	Title     field.String
	Content   field.String
	IsRead    field.Bool
	CreatedAt field.Time

	fieldMap map[string]field.Expr
}

func (n notificationQuery) Table(newTableName string) *notificationQuery {
	n.notificationQueryDo.UseTable(newTableName)
	return n.updateTableName(newTableName)
}

func (n notificationQuery) As(alias string) *notificationQuery {
	n.notificationQueryDo.DO = *(n.notificationQueryDo.As(alias).(*gen.DO))
	return n.updateTableName(alias)
}

func (n *notificationQuery) updateTableName(table string) *notificationQuery {
	n.ALL = field.NewAsterisk(table)
	n.ID = field.NewInt64(table, "id")
	n.UserID = field.NewInt64(table, "user_id")
	n.TenantID = field.NewInt64(table, "tenant_id")
	n.Type = field.NewString(table, "type")
	n.Title = field.NewString(table, "title")
	n.Content = field.NewString(table, "content")
	n.IsRead = field.NewBool(table, "is_read")
	n.CreatedAt = field.NewTime(table, "created_at")

	n.fillFieldMap()

	return n
}

func (n *notificationQuery) QueryContext(ctx context.Context) (*notificationQuery, *notificationQueryDo) {
	return n, n.notificationQueryDo.WithContext(ctx)
}

func (n *notificationQuery) WithContext(ctx context.Context) *notificationQueryDo {
	return n.notificationQueryDo.WithContext(ctx)
}

func (n notificationQuery) TableName() string { return n.notificationQueryDo.TableName() }

func (n notificationQuery) Alias() string { return n.notificationQueryDo.Alias() }

func (n notificationQuery) Columns(cols ...field.Expr) gen.Columns {
	return n.notificationQueryDo.Columns(cols...)
}

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

func (n *notificationQuery) fillFieldMap() {
	n.fieldMap = make(map[string]field.Expr, 8)
	n.fieldMap["id"] = n.ID
	n.fieldMap["user_id"] = n.UserID
	n.fieldMap["tenant_id"] = n.TenantID
	n.fieldMap["type"] = n.Type
	n.fieldMap["title"] = n.Title
	n.fieldMap["content"] = n.Content
	n.fieldMap["is_read"] = n.IsRead
	n.fieldMap["created_at"] = n.CreatedAt
}

func (n notificationQuery) clone(db *gorm.DB) notificationQuery {
	n.notificationQueryDo.ReplaceConnPool(db.Statement.ConnPool)
	return n
}

func (n notificationQuery) replaceDB(db *gorm.DB) notificationQuery {
	n.notificationQueryDo.ReplaceDB(db)
	return n
}

type notificationQueryDo struct{ gen.DO }

func (n notificationQueryDo) Debug() *notificationQueryDo {
	return n.withDO(n.DO.Debug())
}

func (n notificationQueryDo) WithContext(ctx context.Context) *notificationQueryDo {
	return n.withDO(n.DO.WithContext(ctx))
}

func (n notificationQueryDo) ReadDB() *notificationQueryDo {
	return n.Clauses(dbresolver.Read)
}

func (n notificationQueryDo) WriteDB() *notificationQueryDo {
	return n.Clauses(dbresolver.Write)
}

func (n notificationQueryDo) Session(config *gorm.Session) *notificationQueryDo {
	return n.withDO(n.DO.Session(config))
}

func (n notificationQueryDo) Clauses(conds ...clause.Expression) *notificationQueryDo {
	return n.withDO(n.DO.Clauses(conds...))
}

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

func (n notificationQueryDo) Not(conds ...gen.Condition) *notificationQueryDo {
	return n.withDO(n.DO.Not(conds...))
}

func (n notificationQueryDo) Or(conds ...gen.Condition) *notificationQueryDo {
	return n.withDO(n.DO.Or(conds...))
}

func (n notificationQueryDo) Select(conds ...field.Expr) *notificationQueryDo {
	return n.withDO(n.DO.Select(conds...))
}

func (n notificationQueryDo) Where(conds ...gen.Condition) *notificationQueryDo {
	return n.withDO(n.DO.Where(conds...))
}

func (n notificationQueryDo) Order(conds ...field.Expr) *notificationQueryDo {
	return n.withDO(n.DO.Order(conds...))
}

func (n notificationQueryDo) Distinct(cols ...field.Expr) *notificationQueryDo {
	return n.withDO(n.DO.Distinct(cols...))
}

func (n notificationQueryDo) Omit(cols ...field.Expr) *notificationQueryDo {
	return n.withDO(n.DO.Omit(cols...))
}

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

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

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

func (n notificationQueryDo) Group(cols ...field.Expr) *notificationQueryDo {
	return n.withDO(n.DO.Group(cols...))
}

func (n notificationQueryDo) Having(conds ...gen.Condition) *notificationQueryDo {
	return n.withDO(n.DO.Having(conds...))
}

func (n notificationQueryDo) Limit(limit int) *notificationQueryDo {
	return n.withDO(n.DO.Limit(limit))
}

func (n notificationQueryDo) Offset(offset int) *notificationQueryDo {
	return n.withDO(n.DO.Offset(offset))
}

func (n notificationQueryDo) Scopes(funcs ...func(gen.Dao) gen.Dao) *notificationQueryDo {
	return n.withDO(n.DO.Scopes(funcs...))
}

func (n notificationQueryDo) Unscoped() *notificationQueryDo {
	return n.withDO(n.DO.Unscoped())
}

func (n notificationQueryDo) Create(values ...*Notification) error {
	if len(values) == 0 {
		return nil
	}
	return n.DO.Create(values)
}

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

func (n notificationQueryDo) First() (*Notification, error) {
	if result, err := n.DO.First(); err != nil {
		return nil, err
	} else {
		return result.(*Notification), nil
	}
}

func (n notificationQueryDo) Take() (*Notification, error) {
	if result, err := n.DO.Take(); err != nil {
		return nil, err
	} else {
		return result.(*Notification), nil
	}
}

func (n notificationQueryDo) Last() (*Notification, error) {
	if result, err := n.DO.Last(); err != nil {
		return nil, err
	} else {
		return result.(*Notification), nil
	}
}

func (n notificationQueryDo) Find() ([]*Notification, error) {
	result, err := n.DO.Find()
	return result.([]*Notification), err
}

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

func (n notificationQueryDo) Attrs(attrs ...field.AssignExpr) *notificationQueryDo {
	return n.withDO(n.DO.Attrs(attrs...))
}

func (n notificationQueryDo) Assign(attrs ...field.AssignExpr) *notificationQueryDo {
	return n.withDO(n.DO.Assign(attrs...))
}

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

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

func (n notificationQueryDo) FirstOrInit() (*Notification, error) {
	if result, err := n.DO.FirstOrInit(); err != nil {
		return nil, err
	} else {
		return result.(*Notification), nil
	}
}

func (n notificationQueryDo) FirstOrCreate() (*Notification, error) {
	if result, err := n.DO.FirstOrCreate(); err != nil {
		return nil, err
	} else {
		return result.(*Notification), nil
	}
}

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

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

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

func (n notificationQueryDo) Scan(result interface{}) (err error) {
	return n.DO.Scan(result)
}

func (n notificationQueryDo) Delete(models ...*Notification) (result gen.ResultInfo, err error) {
	return n.DO.Delete(models)
}

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

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

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

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

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

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

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

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

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

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

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

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

func (n *notificationQueryDo) withDO(do gen.Dao) *notificationQueryDo {
	n.DO = *do.(*gen.DO)
	return n
}
