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

	_userCouponQuery.userCouponQueryDo.UseDB(db, opts...)
	_userCouponQuery.userCouponQueryDo.UseModel(&UserCoupon{})

	tableName := _userCouponQuery.userCouponQueryDo.TableName()
	_userCouponQuery.ALL = field.NewAsterisk(tableName)
	_userCouponQuery.ID = field.NewInt64(tableName, "id")
	_userCouponQuery.UserID = field.NewInt64(tableName, "user_id")
	_userCouponQuery.CouponID = field.NewInt64(tableName, "coupon_id")
	_userCouponQuery.OrderID = field.NewInt64(tableName, "order_id")
	_userCouponQuery.Status = field.NewField(tableName, "status")
	_userCouponQuery.UsedAt = field.NewTime(tableName, "used_at")
	_userCouponQuery.CreatedAt = field.NewTime(tableName, "created_at")

	_userCouponQuery.fillFieldMap()

	return _userCouponQuery
}

type userCouponQuery struct {
	userCouponQueryDo userCouponQueryDo

	ALL       field.Asterisk
	ID        field.Int64
	UserID    field.Int64
	CouponID  field.Int64
	OrderID   field.Int64
	Status    field.Field
	UsedAt    field.Time
	CreatedAt field.Time

	fieldMap map[string]field.Expr
}

func (u userCouponQuery) Table(newTableName string) *userCouponQuery {
	u.userCouponQueryDo.UseTable(newTableName)
	return u.updateTableName(newTableName)
}

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

func (u *userCouponQuery) updateTableName(table string) *userCouponQuery {
	u.ALL = field.NewAsterisk(table)
	u.ID = field.NewInt64(table, "id")
	u.UserID = field.NewInt64(table, "user_id")
	u.CouponID = field.NewInt64(table, "coupon_id")
	u.OrderID = field.NewInt64(table, "order_id")
	u.Status = field.NewField(table, "status")
	u.UsedAt = field.NewTime(table, "used_at")
	u.CreatedAt = field.NewTime(table, "created_at")

	u.fillFieldMap()

	return u
}

func (u *userCouponQuery) QueryContext(ctx context.Context) (*userCouponQuery, *userCouponQueryDo) {
	return u, u.userCouponQueryDo.WithContext(ctx)
}

func (u *userCouponQuery) WithContext(ctx context.Context) *userCouponQueryDo {
	return u.userCouponQueryDo.WithContext(ctx)
}

func (u userCouponQuery) TableName() string { return u.userCouponQueryDo.TableName() }

func (u userCouponQuery) Alias() string { return u.userCouponQueryDo.Alias() }

func (u userCouponQuery) Columns(cols ...field.Expr) gen.Columns {
	return u.userCouponQueryDo.Columns(cols...)
}

func (u *userCouponQuery) 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 *userCouponQuery) fillFieldMap() {
	u.fieldMap = make(map[string]field.Expr, 7)
	u.fieldMap["id"] = u.ID
	u.fieldMap["user_id"] = u.UserID
	u.fieldMap["coupon_id"] = u.CouponID
	u.fieldMap["order_id"] = u.OrderID
	u.fieldMap["status"] = u.Status
	u.fieldMap["used_at"] = u.UsedAt
	u.fieldMap["created_at"] = u.CreatedAt
}

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

func (u userCouponQuery) replaceDB(db *gorm.DB) userCouponQuery {
	u.userCouponQueryDo.ReplaceDB(db)
	return u
}

type userCouponQueryDo struct{ gen.DO }

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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