// Copyright 2019 autocareai.com. All rights reserved. // Use of this source code is governed by autocareai.com. package user 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" "go.uber.org/zap" "google.golang.org/grpc/status" ) func ManagementSuperUserList(ctx context.Context, req *pb_v1.ManagementSuperUserListRequest) (reply *pb_v1.ManagementSuperUserListReply, err error) { reply = &pb_v1.ManagementSuperUserListReply{} // 捕获各个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 reply, status.Error(10003, "参数错误") } if req.Page == 0 { req.Page = 1 } if req.PageSize == 0 { req.PageSize = 10 } dbname := utils.GetDbName(req.OrganizationCode) p := model.NewRbacUser(dbname) where := map[string]interface{}{ "is_super_user":true, } offset := (req.Page - 1)*req.PageSize count, err := p.Count(database.DB(), where) if err != nil { return nil, errors.DataBaseError } reply.Page = req.Page reply.Total = count if count == 0 { return reply, nil } list, err := p.ListNew(database.DB(), where, nil, req.PageSize, offset) if err != nil { return nil, errors.DataBaseError } reply.List = make([]*pb_v1.ManagementSuperUserItem, len(list)) for i, v := range list { item := &pb_v1.ManagementSuperUserItem{ OrganizationCode:req.OrganizationCode, Username:v.Username, Phone:v.Phone, Email:v.Email, Id:v.Id, CreatedAt:v.CreatedAt.Format("2006-01-02 15:04:05"), } reply.List[i] = item } return reply, err }