小调整
This commit is contained in:
parent
01e10f69dd
commit
8b7edea000
|
@ -9,6 +9,19 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type count[T Model] struct {
|
||||||
|
t T
|
||||||
|
N int `json:"n,omitempty" db:"n" gorm:"n"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c count[T]) PrimaryKey() string {
|
||||||
|
return c.t.PrimaryKey()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c count[T]) Table() string {
|
||||||
|
return c.t.Table()
|
||||||
|
}
|
||||||
|
|
||||||
func pagination[T Model](db dbQuery, ctx context.Context, q QueryCondition, page, pageSize int) (r []T, total int, err error) {
|
func pagination[T Model](db dbQuery, ctx context.Context, q QueryCondition, page, pageSize int) (r []T, total int, err error) {
|
||||||
if page < 1 || pageSize < 1 {
|
if page < 1 || pageSize < 1 {
|
||||||
return
|
return
|
||||||
|
@ -21,6 +34,7 @@ func pagination[T Model](db dbQuery, ctx context.Context, q QueryCondition, page
|
||||||
In: q.In,
|
In: q.In,
|
||||||
Group: q.Group,
|
Group: q.Group,
|
||||||
From: q.From,
|
From: q.From,
|
||||||
|
Fields: "count(*) n",
|
||||||
}
|
}
|
||||||
if q.Group != "" {
|
if q.Group != "" {
|
||||||
qx.Fields = q.Fields
|
qx.Fields = q.Fields
|
||||||
|
@ -32,13 +46,13 @@ func pagination[T Model](db dbQuery, ctx context.Context, q QueryCondition, page
|
||||||
}
|
}
|
||||||
qx.From = str.Join("( ", sq, " ) ", "table", number.ToString(rand.Int()))
|
qx.From = str.Join("( ", sq, " ) ", "table", number.ToString(rand.Int()))
|
||||||
qx = QueryCondition{
|
qx = QueryCondition{
|
||||||
From: qx.From,
|
From: qx.From,
|
||||||
In: qx.In,
|
In: qx.In,
|
||||||
|
Fields: "count(*) n",
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
n, err := gets[count[T]](db, ctx, qx)
|
||||||
n, err := GetField[T](ctx, "count(*)", qx)
|
total = n.N
|
||||||
total = str.ToInt[int](n)
|
|
||||||
if err != nil || total < 1 {
|
if err != nil || total < 1 {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user