-
Notifications
You must be signed in to change notification settings - Fork 0
/
userlistlogic.go
82 lines (67 loc) · 1.99 KB
/
userlistlogic.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package logic
import (
"github.com/bhmy-shm/gofks/core/gormx"
"github.com/bhmy-shm/gofks/core/logx"
"github.com/bhmy-shm/gofks/example/model/account"
"github.com/bhmy-shm/gofks/example/rpc/account-rpc/internal/svc"
"github.com/bhmy-shm/gofks/example/rpc/account-rpc/internal/types"
"github.com/bhmy-shm/gofks/example/rpc/account-rpc/protoc/user"
"golang.org/x/net/context"
)
type UserListLogic struct {
ctx context.Context
svcCtx *svc.ServiceContext
logx.LoggerInter
}
func NewUserListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserListLogic {
return &UserListLogic{
ctx: ctx,
svcCtx: svcCtx,
LoggerInter: logx.WithContext(ctx),
}
}
func (l *UserListLogic) UserList(in *user.UserListReq) (*user.UserListResp, error) {
var (
err error
res = new(user.UserListResp)
cacheKey string
userTotal int64
userList []account.User
)
cacheKey = "test-shm-gofk-userListKey-id"
err = l.svcCtx.UserModel().QueryTakeScan(l.ctx, cacheKey, userList,
func(ctx context.Context, session gormx.SqlSession) error {
raw := `SELECT * FROM shm_test_user`
rawArgs := []string{}
return session.QueryScanContext(ctx, &userList, raw, rawArgs...)
})
if err != nil {
logx.Error("QueryCount is failed:", err)
}
cacheKey = "test-shm-gofk-userListKey-total-id"
err = l.svcCtx.UserModel().QueryTakeTotal(l.ctx, cacheKey, &userTotal,
func(ctx context.Context, session gormx.SqlSession) error {
raw := `SELECT COUNT(*) FROM shm_test_user`
rawArgs := []string{}
return session.QueryCountContext(ctx, &userTotal, raw, rawArgs...)
})
res.Total = userTotal
res.UserList = parse(userList)
res.Result = types.SuccessStatus()
return res, err
}
func parse(list []account.User) []*user.User {
result := make([]*user.User, len(list))
for i, v := range list {
result[i] = &user.User{
Id: uint64(v.ID),
Account: v.Account,
Name: v.Name,
Pass: v.Pass,
Gender: v.Gender,
Mobile: v.Mobile,
Phone: v.Phone,
}
}
return result
}