From 0b696202e7dfada4fccd646540122898738898a5 Mon Sep 17 00:00:00 2001 From: qinyening <710leo@gmail.com> Date: Sat, 28 Nov 2020 11:42:46 +0800 Subject: [PATCH] support get users by org (#426) * support get users by org --- src/models/user.go | 14 ++++---- src/models/user_service.go | 27 +++++++++++++++ src/modules/rdb/http/router.go | 2 +- src/modules/rdb/http/router_user.go | 53 +++++++++++++++++++++-------- 4 files changed, 72 insertions(+), 24 deletions(-) diff --git a/src/models/user.go b/src/models/user.go index 690f094f..a3f60ec2 100644 --- a/src/models/user.go +++ b/src/models/user.go @@ -277,7 +277,7 @@ func (u *User) Save() error { return err } -func UserTotal(ids []int64, query string) (int64, error) { +func UserTotal(ids []int64, where string, args ...interface{}) (int64, error) { session := DB["rdb"].NewSession() defer session.Close() @@ -285,23 +285,21 @@ func UserTotal(ids []int64, query string) (int64, error) { session = session.In("id", ids) } - if query != "" { - q := "%" + query + "%" - return session.Where("username like ? or dispname like ? or phone like ? or email like ?", q, q, q, q).Count(new(User)) + if where != "" { + session = session.Where(where, args...) } return session.Count(new(User)) } -func UserGets(ids []int64, query string, limit, offset int) ([]User, error) { +func UserGets(ids []int64, limit, offset int, where string, args ...interface{}) ([]User, error) { session := DB["rdb"].Limit(limit, offset).OrderBy("username") if len(ids) > 0 { session = session.In("id", ids) } - if query != "" { - q := "%" + query + "%" - session = session.Where("username like ? or dispname like ? or phone like ? or email like ?", q, q, q, q) + if where != "" { + session = session.Where(where, args...) } var users []User diff --git a/src/models/user_service.go b/src/models/user_service.go index 1e0a3f28..c2a2dac6 100644 --- a/src/models/user_service.go +++ b/src/models/user_service.go @@ -140,3 +140,30 @@ func UsernameCandoNodeOp(username, operation string, nodeId int64) (bool, error) return user.HasPermByNode(node, operation) } + +func UserAndTotalGets(query, org string, limit, offset int, ids []int64) ([]User, int64, error) { + where := "1 = 1" + param := []interface{}{} + + if query != "" { + q := "%" + query + "%" + where += " and (username like ? or dispname like ? or phone like ? or email like ?)" + param = append(param, q, q, q, q) + } + + if org != "" { + q := "%" + org + "%" + where += " and organization like ?" + param = append(param, q) + + } + + total, err := UserTotal(ids, where, param...) + if err != nil { + return []User{}, total, err + } + + list, err := UserGets(ids, limit, offset, where, param...) + + return list, total, err +} diff --git a/src/modules/rdb/http/router.go b/src/modules/rdb/http/router.go index 70fcf8f9..5bad6631 100644 --- a/src/modules/rdb/http/router.go +++ b/src/modules/rdb/http/router.go @@ -174,7 +174,7 @@ func Config(r *gin.Engine) { v1.GET("/get-teams-by-ids", v1TeamGetByIds) v1.GET("/get-user-ids-by-team-ids", v1UserIdsGetByTeamIds) - v1.GET("/users", userListGet) + v1.GET("/users", v1UserListGet) v1.POST("/login", v1Login) v1.POST("/send-login-code-by-sms", v1SendLoginCodeBySms) diff --git a/src/modules/rdb/http/router_user.go b/src/modules/rdb/http/router_user.go index 49bf55ad..0925a579 100644 --- a/src/modules/rdb/http/router_user.go +++ b/src/modules/rdb/http/router_user.go @@ -15,12 +15,10 @@ import ( func userListGet(c *gin.Context) { limit := queryInt(c, "limit", 20) query := queryStr(c, "query", "") + org := queryStr(c, "org", "") ids := str.IdsInt64(queryStr(c, "ids", "")) - total, err := models.UserTotal(ids, query) - dangerous(err) - - list, err := models.UserGets(ids, query, limit, offset(c, limit)) + list, total, err := models.UserAndTotalGets(query, org, limit, offset(c, limit), ids) dangerous(err) for i := 0; i < len(list); i++ { @@ -33,17 +31,32 @@ func userListGet(c *gin.Context) { }, nil) } +func v1UserListGet(c *gin.Context) { + limit := queryInt(c, "limit", 20) + query := queryStr(c, "query", "") + org := queryStr(c, "org", "") + ids := str.IdsInt64(queryStr(c, "ids", "")) + + list, total, err := models.UserAndTotalGets(query, org, limit, offset(c, limit), ids) + + renderData(c, gin.H{ + "list": list, + "total": total, + }, err) +} + type userProfileForm struct { - Username string `json:"username"` - Password string `json:"password"` - Dispname string `json:"dispname"` - Phone string `json:"phone"` - Email string `json:"email"` - Im string `json:"im"` - IsRoot int `json:"is_root"` - LeaderId int64 `json:"leader_id"` - Typ int `json:"typ"` - Status int `json:"status"` + Username string `json:"username"` + Password string `json:"password"` + Dispname string `json:"dispname"` + Phone string `json:"phone"` + Email string `json:"email"` + Im string `json:"im"` + IsRoot int `json:"is_root"` + LeaderId int64 `json:"leader_id"` + Typ int `json:"typ"` + Status int `json:"status"` + Organization string `json:"organization"` } func userAddPost(c *gin.Context) { @@ -141,7 +154,17 @@ func userProfilePut(c *gin.Context) { target.Status = f.Status } - err := target.Update("dispname", "phone", "email", "im", "is_root", "leader_id", "leader_name", "typ", "status") + if f.Status != target.Status { + arr = append(arr, fmt.Sprintf("typ: %s -> %s", target.Status, f.Status)) + target.Status = f.Status + } + + if f.Organization != target.Organization { + arr = append(arr, fmt.Sprintf("organization: %s -> %s", target.Organization, f.Organization)) + target.Organization = f.Organization + } + + err := target.Update("dispname", "phone", "email", "im", "is_root", "leader_id", "leader_name", "typ", "status", "organization") if err == nil && len(arr) > 0 { content := strings.Join(arr, ",") go models.OperationLogNew(root.Username, "user", target.Id, fmt.Sprintf("UserModify %s %s", target.Username, content))