-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
212 lines (184 loc) · 4.37 KB
/
main.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package main
import (
"fmt"
"github.com/dkzhang/RmsGo/ResourceSM/model/resGNode"
"github.com/dkzhang/RmsGo/ResourceSM/model/resGNodeTree"
"github.com/dkzhang/RmsGo/ResourceSM/model/resNode"
"github.com/gin-gonic/gin"
"net/http"
"strconv"
"time"
)
var t resGNodeTree.Tree
var nodesMap map[int64]resNode.Node
func init() {
rootGNode := resGNode.ResGNode{
ID: 1e4,
Label: "偏移云",
Children: nil,
}
lc := &resGNode.ResGNode{
ID: 11e4,
Label: "浪潮云",
Children: nil,
}
rootGNode.Children = append(rootGNode.Children, lc)
tempGroup := &resGNode.ResGNode{
ID: 111e4,
Label: "Group1-1",
Children: nil,
}
for i := int64(0); i < 256; i++ {
p := &resGNode.ResGNode{
ID: i,
Label: fmt.Sprintf("Node%d", i),
Children: nil,
}
tempGroup.Children = append(tempGroup.Children, p)
if i%32 == 31 {
lc.Children = append(lc.Children, tempGroup)
groupID := (i+1)/32 + 1
tempGroup = &resGNode.ResGNode{
ID: 110e4 + groupID*1e4,
Label: fmt.Sprintf("Group1-%d", groupID),
Children: nil,
}
}
}
t = resGNodeTree.Tree{
Root: rootGNode,
NodesNum: 0,
}
resGNodeTree.Count(&t)
///////////////////////////////////////////////////////
nodesMap = make(map[int64]resNode.Node, 256)
for i := int64(0); i < 256; i++ {
nodesMap[i] = resNode.Node{
ID: i,
ProjectID: 0,
AllocatedTime: time.Now(),
}
}
for j := int64(50); j < 100; j++ {
nodesMap[j] = resNode.Node{
ID: j,
ProjectID: 1,
AllocatedTime: time.Now(),
}
}
for j := int64(100); j < 200; j++ {
nodesMap[j] = resNode.Node{
ID: j,
ProjectID: 2,
AllocatedTime: time.Now(),
}
}
}
func main() {
r := gin.Default()
webAPIv1 := r.Group("/webapi")
{
hTree := webAPIv1.Group("/Tree")
{
hTree.GET("/All", RetrieveAll)
hTree.GET("/ProRes/:id", RetrieveProRes)
hTree.GET("/ProResD/:id", RetrieveProResD)
hTree.POST("/ProRes/:id", SubmitProRes)
}
}
r.Run(":8084")
}
func RetrieveAll(c *gin.Context) {
jsonTree, err := resGNodeTree.ToJsonForVue(t)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"msg": "tree转json失败",
})
return
}
c.JSON(http.StatusOK, gin.H{
"tree": jsonTree,
"nodesNum": resGNodeTree.CountRO(&t),
"unfolded": []int64{1e4, 11e4, 115e4},
"selected": []int64{10, 11, 12, 14, 15},
})
}
func RetrieveProRes(c *gin.Context) {
idStr := c.Param("id")
projectID, err := strconv.Atoi(idStr)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{
"msg": fmt.Sprintf("projectID格式错误%s", idStr),
})
return
}
nt, err := resGNodeTree.Filtrate(&t, nodesMap, func(node resNode.Node) bool {
return node.ProjectID == 0 || node.ProjectID == projectID
})
jsonTree, err := resGNodeTree.ToJsonForVue(*nt)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"msg": "tree转json失败",
})
return
}
c.JSON(http.StatusOK, gin.H{
"tree": jsonTree,
"nodesNum": resGNodeTree.CountRO(nt),
"unfolded": []int64{1e4, 11e4},
"selected": []int64{10, 11, 12, 14, 15},
})
return
}
func RetrieveProResD(c *gin.Context) {
idStr := c.Param("id")
projectID, err := strconv.Atoi(idStr)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{
"msg": fmt.Sprintf("projectID格式错误%s", idStr),
})
return
}
nt, err := resGNodeTree.FiltrateMark(&t, nodesMap, func(node resNode.Node) bool {
return node.ProjectID == 0 || node.ProjectID == projectID
})
jsonTree, err := resGNodeTree.ToJsonForVue(*nt)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"msg": "tree转json失败",
})
return
}
c.JSON(http.StatusOK, gin.H{
"tree": jsonTree,
"nodesNum": resGNodeTree.CountRO(nt),
"unfolded": []int64{1e4, 11e4},
"selected": []int64{10, 11, 12, 14, 15},
})
return
}
type SubmitSelect struct {
Selected []int64 `json:"selected"`
}
func SubmitProRes(c *gin.Context) {
idStr := c.Param("id")
projectID, err := strconv.Atoi(idStr)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{
"msg": fmt.Sprintf("projectID格式错误%s", idStr),
})
return
}
ss := SubmitSelect{}
err = c.BindJSON(&ss)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{
"msg": fmt.Sprintf("BindJSON error: %v", err),
})
return
}
c.JSON(http.StatusOK, gin.H{
"msg": fmt.Sprintf("receive (projectID=%d) selected array: %v", projectID, ss.Selected),
})
return
}