-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathbiz_callback_query.go
58 lines (53 loc) · 1.56 KB
/
biz_callback_query.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package kuu
import (
"fmt"
"math"
"reflect"
)
func init() {
DefaultCallback.Query().Register("kuu:biz_before_query", bizBeforeQueryCallback)
DefaultCallback.Query().Register("kuu:biz_query", bizQueryCallback)
DefaultCallback.Query().Register("kuu:biz_after_query", bizAfterQueryCallback)
}
func bizBeforeQueryCallback(scope *Scope) {
if !scope.HasError() {
scope.CallMethod("BizBeforeFind")
if scope.Meta != nil {
if err := execBizHooks(fmt.Sprintf("%s:BizBeforeFind", scope.Meta.Name), scope); err != nil {
_ = scope.Err(err)
return
}
}
}
}
func bizQueryCallback(scope *Scope) {
if !scope.HasError() {
if err := scope.DB.Find(scope.QueryResult.List).Error; err != nil {
_ = scope.Err(err)
return
}
scope.QueryResult.List = Meta(reflect.New(scope.ReflectType).Interface()).OmitPassword(scope.QueryResult.List)
scope.QueryResult.List = ProjectFields(scope.QueryResult.List, scope.QueryResult.Project)
// 处理totalrecords、totalpages
var totalRecords int
if err := scope.DB.Offset(-1).Limit(-1).Count(&totalRecords).Error; err != nil {
_ = scope.Err(err)
return
}
scope.QueryResult.TotalRecords = totalRecords
if scope.QueryResult.Range == "PAGE" {
scope.QueryResult.TotalPages = int(math.Ceil(float64(totalRecords) / float64(scope.QueryResult.Size)))
}
}
}
func bizAfterQueryCallback(scope *Scope) {
if !scope.HasError() {
scope.CallMethod("BizAfterFind")
if scope.Meta != nil {
if err := execBizHooks(fmt.Sprintf("%s:BizAfterFind", scope.Meta.Name), scope); err != nil {
_ = scope.Err(err)
return
}
}
}
}