feat: fund operator support count

fix: set where condition
This commit is contained in:
xuthus5 2023-09-17 11:03:08 +08:00
parent e63125d1d5
commit 9806f42fff
Signed by: xuthus5
GPG Key ID: A23CF9620CBB55F9
3 changed files with 51 additions and 14 deletions

View File

@ -15,7 +15,8 @@ type _delete struct {
// SetWhere 设置删除条件 // SetWhere 设置删除条件
func (d *_delete) SetWhere(query any, args ...any) *_delete { func (d *_delete) SetWhere(query any, args ...any) *_delete {
d.where = append(d.where, query, args) d.where = append(d.where, query)
d.where = append(d.where, args...)
return d return d
} }

View File

@ -7,22 +7,24 @@ import (
) )
type _find struct { type _find struct {
scope *scope scope *scope
sdbc *Driver sdbc *Driver
ctx context.Context ctx context.Context
where []any where []any
selectOpts []interface{} selectOpts []interface{}
omitOpts []string omitOpts []string
notOpts []interface{} notOpts []interface{}
orOpts []interface{} orOpts []interface{}
orderOpts string distinctOpts []interface{}
limit int orderOpts string
offset int limit int
offset int
} }
// SetWhere 设置更新条件 // SetWhere 设置更新条件
func (f *_find) SetWhere(query any, args ...any) *_find { func (f *_find) SetWhere(query any, args ...any) *_find {
f.where = append(f.where, query, args) f.where = append(f.where, query)
f.where = append(f.where, args...)
return f return f
} }
@ -68,6 +70,12 @@ func (f *_find) SetOrder(order string) *_find {
return f return f
} }
// SetDistinct 字段去重
func (f *_find) SetDistinct(distinct ...interface{}) *_find {
f.distinctOpts = distinct
return f
}
// FindById 主键检索 // FindById 主键检索
func (f *_find) FindById(id any, bind any) error { func (f *_find) FindById(id any, bind any) error {
// 不是结构体指针 返回错误 // 不是结构体指针 返回错误
@ -117,5 +125,32 @@ func (f *_find) Find(binds any) error {
if len(f.notOpts) != 0 { if len(f.notOpts) != 0 {
db = db.Not(f.notOpts[0], f.notOpts[1:]...) db = db.Not(f.notOpts[0], f.notOpts[1:]...)
} }
if len(f.distinctOpts) != 0 {
db = db.Distinct(f.distinctOpts...)
}
return db.Model(f.scope.model.ptr()).Find(binds).Error return db.Model(f.scope.model.ptr()).Find(binds).Error
} }
// Count 获取匹配的记录数
func (f *_find) Count(count *int64) error {
db := f.sdbc.client
if len(f.where) != 0 {
db = db.Where(f.where[0], f.where[1:]...)
}
if f.limit != 0 {
db = db.Limit(f.limit)
}
if f.offset != 0 {
db = db.Offset(f.offset)
}
if f.orderOpts != "" {
db = db.Order(f.orderOpts)
}
if len(f.orOpts) != 0 {
db = db.Or(f.orOpts[0], f.orOpts[1:]...)
}
if len(f.notOpts) != 0 {
db = db.Not(f.notOpts[0], f.notOpts[1:]...)
}
return db.Model(f.scope.model.ptr()).Count(count).Error
}

View File

@ -16,7 +16,8 @@ type _update struct {
// SetWhere 设置更新条件 // SetWhere 设置更新条件
func (u *_update) SetWhere(query any, args ...any) *_update { func (u *_update) SetWhere(query any, args ...any) *_update {
u.where = append(u.where, query, args) u.where = append(u.where, query)
u.where = append(u.where, args...)
return u return u
} }