package rbac import ( "context" "cp-organization-management/errors" "cp-organization-management/model" pb_v1 "cp-organization-management/pb/v1" "cp-organization-management/utils" "encoding/json" "fmt" "github.com/jaryhe/gopkgs/database" "github.com/jaryhe/gopkgs/logger" "github.com/jinzhu/gorm" "go.uber.org/zap" "google.golang.org/grpc/status" ) func getChild(pid int64, m map[int64][]*pb_v1.RbacNodeItem) (ret []*pb_v1.RbacNodeItem) { if _, ok := m[pid]; !ok { return ret } ret = append(ret, m[pid]...) for k, v := range ret { ret[k].Childs = getChild(v.Id, m) } return ret } func NodeTree(array []model.RbacNode) (ret []*pb_v1.RbacNodeItem) { m := map[int64][]*pb_v1.RbacNodeItem{} for _, v := range array { item := pb_v1.RbacNodeItem{ Id:v.Id, Pid:v.Pid, Method:v.Method, Resource:v.Resource, Object:v.Object, Router:v.Router, Name:v.Name, } m[v.Pid] = append(m[v.Pid], &item) } return getChild(0, m) } func NodeTreeSelect(array []model.RbacNode) (ret []*pb_v1.RbacNodeItem) { m := map[int64][]*pb_v1.RbacNodeItem{} for _, v := range array { item := pb_v1.RbacNodeItem{ Id:v.Id, Pid:v.Pid, Method:v.Method, Resource:v.Resource, Object:v.Object, Router:v.Router, Name:v.Name, Select:true, } m[v.Pid] = append(m[v.Pid], &item) } return getChild(0, m) } func RbacNodeList(ctx context.Context, req *pb_v1.RbacNodeListRequest) (reply *pb_v1.RbacNodeListReply, err error) { reply = &pb_v1.RbacNodeListReply{} // 捕获各个task中的异常并返回给调用者 defer func() { if r := recover(); r != nil { err = fmt.Errorf("%+v", r) e := &status.Status{} if er := json.Unmarshal([]byte(err.Error()), e); er != nil { logger.Error("err", zap.String("system_err", err.Error()), zap.Stack("stacktrace")) } } }() if req.OrganizationCode == "" { return nil, errors.ParamsError } dbname := utils.GetDbName(req.OrganizationCode) p := model.NewRbacNode(dbname) list := []model.RbacNode{} where := map[string]interface{}{ "is_super":false, } if req.IsAll { where = nil } err = p.QueryAll(database.DB(), where, &list) if err != nil { if err == gorm.ErrRecordNotFound { return reply, nil } return nil, errors.DataBaseError } reply.List = NodeTree(list) return reply, nil }