diff --git a/sysutils.go b/sysutils.go index 0d599fd..da427ed 100644 --- a/sysutils.go +++ b/sysutils.go @@ -112,6 +112,60 @@ func RspErr2(c *gin.Context, code int32, message string) { }) } +func DataSetStreamPageQuery(dataSet []interface{}, + pageSize int32, cursor int64, + filterCb func(interface{}) bool, + orderByCb func(interface{}, interface{}) bool, + queryCb func(*StreamPagination), + fillCb func(interface{})) { + if pageSize <= 0 { + pageSize = 1 + } + if pageSize > 1000 { + pageSize = 1000 + } + if cursor < 1 { + cursor = 1 + } + if cursor > 10000 { + cursor = 10000 + } + var dataSetCopy []*interface{} + q5.NewSlice(&dataSetCopy, 0, int32(len(dataSet))) + for _, val := range dataSet { + if filterCb(val) { + q5.AppendSlice(&dataSetCopy, &val) + } + } + q5.Sort(dataSetCopy, func(a *interface{}, b *interface{}) bool { + return orderByCb(*a, *b) + }) + var pagination StreamPagination + pagination.NextCursor = cursor + if int64(pageSize) * cursor < int64(len(dataSetCopy)) { + pagination.NextCursor = cursor + 1 + pagination.Remaining = 1 + } + pagination.PreviousCursor = cursor + pagination.Count = int32(int64(len(dataSetCopy)) - int64(pageSize) * cursor) + if pagination.Count < 0 { + pagination.Count = 0 + } else { + pagination.Count = q5.Max(pageSize, pagination.Count) + } + pagination.TotalCount = int32(len(dataSetCopy)) + queryCb(&pagination) + var count int32 + startIdx := int(int64(pageSize) * (cursor - 1)) + for i := startIdx; i < len(dataSetCopy); i++ { + fillCb(*dataSetCopy[i]) + count++ + if count >= pagination.Count { + break + } + } +} + func init() { switch os.Getenv("SERVER_ENV") { case "TEST":