-
Notifications
You must be signed in to change notification settings - Fork 3
/
type.go
130 lines (111 loc) · 2.54 KB
/
type.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
package main
import (
"net/http"
"strings"
"github.com/gin-gonic/gin"
)
func fetchTypes(c *gin.Context) {
pid := c.Query(`permission_id`) // 有可能是查找某个权限包含的所有类型
if len(pid) > 0 {
ts, e := org.TypeByPermissionID(pid)
if e != nil {
sendError(c, e)
return
}
c.JSON(http.StatusOK, map[string]interface{}{
`total`: len(ts),
`data`: ts,
`page`: 1,
})
return
}
idStr := c.Query(`ids`) // 通过一组ID 获取type
if len(idStr) > 0 {
ts, e := org.TypeByIDs(strings.Split(idStr, `,`))
if e != nil {
sendError(c, e)
return
}
c.JSON(http.StatusOK, map[string]interface{}{
`total`: len(ts),
`data`: ts,
`page`: 1,
})
return
}
isUnit := c.Query(`isUnit`) == `true` // 需要判断是查找部门类型,还是员工类型
_, pageSize, cookie := findPageControl(c)
r, e := org.Types(isUnit, pageSize, cookie)
if e != nil {
sendError(c, e)
return
}
sendResult(c, r)
}
func createType(c *gin.Context) {
var body map[string]string
err := c.BindJSON(&body) // 会发送错误信息
if err != nil {
return
}
category := body[`category`]
isUnit := category == department
id, err := org.AddType(body[`cn`], body[`description`], isUnit)
if err != nil {
c.JSON(http.StatusInternalServerError, map[string]string{
`err`: err.Error(),
})
return
}
c.JSON(http.StatusOK, map[string]string{`id`: id}) // 将id 返回
}
func typeByID(c *gin.Context) {
t, e := org.TypeByID(c.Param(`id`))
if e != nil {
sendError(c, e)
return
}
// 做点小处理 对前端友好
if t[`isUnit`].(bool) {
t[`category`] = department
} else {
t[`category`] = member
}
// delete(t, `isUnit`)
c.JSON(http.StatusOK, t)
}
func editType(c *gin.Context) {
id := c.Param(`id`)
var body map[string]interface{}
e := c.BindJSON(&body)
if e != nil {
return
}
e = org.ModifyType(id, body[`cn`].(string), body[`description`].(string), body[`category`].(string) == department)
if e != nil {
c.JSON(http.StatusInternalServerError, map[string]string{
`err`: e.Error(),
})
} else {
c.JSON(http.StatusOK, map[string]string{
`id`: id,
})
}
}
// Type 删除逻辑
// 如果有权限包含该Type 则提示,不能删除成功
// 如果有员工属于该Type则提示,不能删除成功
func delType(c *gin.Context) {
id := c.Param(`id`)
t, e := org.TypeByID(id)
if e != nil {
sendError(c, e)
return
}
e = org.DelType(t[`id`].(string), t[`isUnit`].(bool))
if e != nil {
sendError(c, e)
} else {
c.JSON(http.StatusOK, map[string]interface{}{`data`: t})
}
}