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

	_userContentActionQuery.userContentActionQueryDo.UseDB(db, opts...)
	_userContentActionQuery.userContentActionQueryDo.UseModel(&UserContentAction{})

	tableName := _userContentActionQuery.userContentActionQueryDo.TableName()
	_userContentActionQuery.ALL = field.NewAsterisk(tableName)
	_userContentActionQuery.ID = field.NewInt64(tableName, "id")
	_userContentActionQuery.UserID = field.NewInt64(tableName, "user_id")
	_userContentActionQuery.ContentID = field.NewInt64(tableName, "content_id")
	_userContentActionQuery.Type = field.NewString(tableName, "type")
	_userContentActionQuery.CreatedAt = field.NewTime(tableName, "created_at")

	_userContentActionQuery.fillFieldMap()

	return _userContentActionQuery
}

type userContentActionQuery struct {
	userContentActionQueryDo userContentActionQueryDo

	ALL       field.Asterisk
	ID        field.Int64
	UserID    field.Int64
	ContentID field.Int64
	Type      field.String
	CreatedAt field.Time

	fieldMap map[string]field.Expr
}

func (u userContentActionQuery) Table(newTableName string) *userContentActionQuery {
	u.userContentActionQueryDo.UseTable(newTableName)
	return u.updateTableName(newTableName)
}

func (u userContentActionQuery) As(alias string) *userContentActionQuery {
	u.userContentActionQueryDo.DO = *(u.userContentActionQueryDo.As(alias).(*gen.DO))
	return u.updateTableName(alias)
}

func (u *userContentActionQuery) updateTableName(table string) *userContentActionQuery {
	u.ALL = field.NewAsterisk(table)
	u.ID = field.NewInt64(table, "id")
	u.UserID = field.NewInt64(table, "user_id")
	u.ContentID = field.NewInt64(table, "content_id")
	u.Type = field.NewString(table, "type")
	u.CreatedAt = field.NewTime(table, "created_at")

	u.fillFieldMap()

	return u
}

func (u *userContentActionQuery) QueryContext(ctx context.Context) (*userContentActionQuery, *userContentActionQueryDo) {
	return u, u.userContentActionQueryDo.WithContext(ctx)
}

func (u *userContentActionQuery) WithContext(ctx context.Context) *userContentActionQueryDo {
	return u.userContentActionQueryDo.WithContext(ctx)
}

func (u userContentActionQuery) TableName() string { return u.userContentActionQueryDo.TableName() }

func (u userContentActionQuery) Alias() string { return u.userContentActionQueryDo.Alias() }

func (u userContentActionQuery) Columns(cols ...field.Expr) gen.Columns {
	return u.userContentActionQueryDo.Columns(cols...)
}

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

func (u *userContentActionQuery) fillFieldMap() {
	u.fieldMap = make(map[string]field.Expr, 5)
	u.fieldMap["id"] = u.ID
	u.fieldMap["user_id"] = u.UserID
	u.fieldMap["content_id"] = u.ContentID
	u.fieldMap["type"] = u.Type
	u.fieldMap["created_at"] = u.CreatedAt
}

func (u userContentActionQuery) clone(db *gorm.DB) userContentActionQuery {
	u.userContentActionQueryDo.ReplaceConnPool(db.Statement.ConnPool)
	return u
}

func (u userContentActionQuery) replaceDB(db *gorm.DB) userContentActionQuery {
	u.userContentActionQueryDo.ReplaceDB(db)
	return u
}

type userContentActionQueryDo struct{ gen.DO }

func (u userContentActionQueryDo) Debug() *userContentActionQueryDo {
	return u.withDO(u.DO.Debug())
}

func (u userContentActionQueryDo) WithContext(ctx context.Context) *userContentActionQueryDo {
	return u.withDO(u.DO.WithContext(ctx))
}

func (u userContentActionQueryDo) ReadDB() *userContentActionQueryDo {
	return u.Clauses(dbresolver.Read)
}

func (u userContentActionQueryDo) WriteDB() *userContentActionQueryDo {
	return u.Clauses(dbresolver.Write)
}

func (u userContentActionQueryDo) Session(config *gorm.Session) *userContentActionQueryDo {
	return u.withDO(u.DO.Session(config))
}

func (u userContentActionQueryDo) Clauses(conds ...clause.Expression) *userContentActionQueryDo {
	return u.withDO(u.DO.Clauses(conds...))
}

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

func (u userContentActionQueryDo) Not(conds ...gen.Condition) *userContentActionQueryDo {
	return u.withDO(u.DO.Not(conds...))
}

func (u userContentActionQueryDo) Or(conds ...gen.Condition) *userContentActionQueryDo {
	return u.withDO(u.DO.Or(conds...))
}

func (u userContentActionQueryDo) Select(conds ...field.Expr) *userContentActionQueryDo {
	return u.withDO(u.DO.Select(conds...))
}

func (u userContentActionQueryDo) Where(conds ...gen.Condition) *userContentActionQueryDo {
	return u.withDO(u.DO.Where(conds...))
}

func (u userContentActionQueryDo) Order(conds ...field.Expr) *userContentActionQueryDo {
	return u.withDO(u.DO.Order(conds...))
}

func (u userContentActionQueryDo) Distinct(cols ...field.Expr) *userContentActionQueryDo {
	return u.withDO(u.DO.Distinct(cols...))
}

func (u userContentActionQueryDo) Omit(cols ...field.Expr) *userContentActionQueryDo {
	return u.withDO(u.DO.Omit(cols...))
}

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

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

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

func (u userContentActionQueryDo) Group(cols ...field.Expr) *userContentActionQueryDo {
	return u.withDO(u.DO.Group(cols...))
}

func (u userContentActionQueryDo) Having(conds ...gen.Condition) *userContentActionQueryDo {
	return u.withDO(u.DO.Having(conds...))
}

func (u userContentActionQueryDo) Limit(limit int) *userContentActionQueryDo {
	return u.withDO(u.DO.Limit(limit))
}

func (u userContentActionQueryDo) Offset(offset int) *userContentActionQueryDo {
	return u.withDO(u.DO.Offset(offset))
}

func (u userContentActionQueryDo) Scopes(funcs ...func(gen.Dao) gen.Dao) *userContentActionQueryDo {
	return u.withDO(u.DO.Scopes(funcs...))
}

func (u userContentActionQueryDo) Unscoped() *userContentActionQueryDo {
	return u.withDO(u.DO.Unscoped())
}

func (u userContentActionQueryDo) Create(values ...*UserContentAction) error {
	if len(values) == 0 {
		return nil
	}
	return u.DO.Create(values)
}

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

func (u userContentActionQueryDo) First() (*UserContentAction, error) {
	if result, err := u.DO.First(); err != nil {
		return nil, err
	} else {
		return result.(*UserContentAction), nil
	}
}

func (u userContentActionQueryDo) Take() (*UserContentAction, error) {
	if result, err := u.DO.Take(); err != nil {
		return nil, err
	} else {
		return result.(*UserContentAction), nil
	}
}

func (u userContentActionQueryDo) Last() (*UserContentAction, error) {
	if result, err := u.DO.Last(); err != nil {
		return nil, err
	} else {
		return result.(*UserContentAction), nil
	}
}

func (u userContentActionQueryDo) Find() ([]*UserContentAction, error) {
	result, err := u.DO.Find()
	return result.([]*UserContentAction), err
}

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

func (u userContentActionQueryDo) Attrs(attrs ...field.AssignExpr) *userContentActionQueryDo {
	return u.withDO(u.DO.Attrs(attrs...))
}

func (u userContentActionQueryDo) Assign(attrs ...field.AssignExpr) *userContentActionQueryDo {
	return u.withDO(u.DO.Assign(attrs...))
}

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

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

func (u userContentActionQueryDo) FirstOrInit() (*UserContentAction, error) {
	if result, err := u.DO.FirstOrInit(); err != nil {
		return nil, err
	} else {
		return result.(*UserContentAction), nil
	}
}

func (u userContentActionQueryDo) FirstOrCreate() (*UserContentAction, error) {
	if result, err := u.DO.FirstOrCreate(); err != nil {
		return nil, err
	} else {
		return result.(*UserContentAction), nil
	}
}

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

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

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

func (u userContentActionQueryDo) Scan(result interface{}) (err error) {
	return u.DO.Scan(result)
}

func (u userContentActionQueryDo) Delete(models ...*UserContentAction) (result gen.ResultInfo, err error) {
	return u.DO.Delete(models)
}

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

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

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

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

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

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

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

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

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

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

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

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

func (u *userContentActionQueryDo) withDO(do gen.Dao) *userContentActionQueryDo {
	u.DO = *do.(*gen.DO)
	return u
}
