// Copyright 2019 getensh.com. All rights reserved. // Use of this source code is governed by getensh.com. package user import ( "context" "encoding/json" "fmt" "git.getensh.com/common/gopkgs/database" "git.getensh.com/common/gopkgs/logger" "go.uber.org/zap" "google.golang.org/grpc/status" "time" "xingjia-management-gateway/apis" "xingjia-management-gateway/consts" "xingjia-management-gateway/errors" dbmodel "xingjia-management-gateway/model" ) func checkUserListParam(req *apis.UserListRequest) error { if req.SelfId == 0 { return status.Error(10003, "未知操作者") } if req.PageSize == 0 { req.PageSize = 10 } if req.Page == 0 { req.Page = 1 } return nil } // func UserList(ctx context.Context, req *apis.UserListRequest) (reply *apis.UserListReply, err error) { reply = &apis.UserListReply{} // 捕获各个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 err = checkUserListParam(req); err != nil { return nil, err } if err = checkAdmin(req.SelfId); err != nil { return nil, err } where := map[string]interface{}{} if req.User != "" { where["user like"] = "%" + req.User + "%" } if req.RealName != "" { where["real_name"] = "%" + req.RealName + "%" } p := &dbmodel.TUser{} count, err := p.Count(database.DB(), where, nil) if err != nil { return nil, errors.DataBaseError } reply.Page = req.Page reply.Total = count if count == 0 { return reply, nil } list, err := p.List(database.DB(), where, nil, int(req.Page), int(req.PageSize)) if err != nil { return nil, errors.DataBaseError } reply.List = make([]*apis.UserItem, len(list)) for i, v := range list { item := &apis.UserItem{ Uid: v.ID, User: v.User, UserType: v.UserType, EffectiveStart: v.EffectiveStart, EffectiveEnd: v.EffectiveEnd, Status: consts.UserStatusEffective, RealName: v.RealName, } if v.UserType == consts.UserTypeTemp && item.EffectiveStart > time.Now().Unix() { item.Status = consts.UserStatusWait } if v.UserType == consts.UserTypeTemp && item.EffectiveEnd < time.Now().Unix() { item.Status = consts.UserStatusExpired } reply.List[i] = item } return reply, nil }