167 lines
4.5 KiB
Go
167 lines
4.5 KiB
Go
package analysisadminlogic
|
|
|
|
import (
|
|
"context"
|
|
|
|
"lone-services/pkg/modelbase"
|
|
"lone-services/pkg/utils"
|
|
"lone-services/pkg/validate"
|
|
analysis "lone-services/rpc/analysis/pb"
|
|
userpb "lone-services/rpc/user/pb"
|
|
"lone-services/services/analysis/internal/dao"
|
|
"lone-services/services/analysis/internal/logic/common"
|
|
"lone-services/services/analysis/internal/model"
|
|
"lone-services/services/analysis/internal/svc"
|
|
"lone-services/services/analysis/validator"
|
|
|
|
"github.com/zeromicro/go-zero/core/logx"
|
|
"google.golang.org/grpc/codes"
|
|
"google.golang.org/grpc/status"
|
|
)
|
|
|
|
type UserListLogic struct {
|
|
ctx context.Context
|
|
svcCtx *svc.ServiceContext
|
|
logx.Logger
|
|
}
|
|
|
|
func NewUserListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserListLogic {
|
|
return &UserListLogic{
|
|
ctx: ctx,
|
|
svcCtx: svcCtx,
|
|
Logger: logx.WithContext(ctx),
|
|
}
|
|
}
|
|
|
|
func (l *UserListLogic) UserList(in *analysis.AdminUserListReq) (*analysis.Response, error) {
|
|
var req validator.AdminUserListValidator
|
|
if msg := validate.ValidateFromProto(in, &req); msg != utils.StringEmpty {
|
|
return outParams(msg), nil
|
|
}
|
|
if _, fail := common.RequireAdmin(l.ctx); fail != nil {
|
|
return fail, nil
|
|
}
|
|
|
|
params := modelbase.Params{
|
|
Order: "id desc",
|
|
Page: int(req.Page),
|
|
Size: int(req.Size),
|
|
}
|
|
|
|
if req.Keyword != utils.StringEmpty {
|
|
bizIds, err := l.filterBizIds(req.Keyword)
|
|
if err != nil {
|
|
l.Errorf("admin user list filter: %v", err)
|
|
return common.FailResponse(utils.Fail), nil
|
|
}
|
|
if len(bizIds) == utils.NumberZero {
|
|
return common.OkResponse(modelbase.PageResult{Items: []dao.AdminUserListItem{}}), nil
|
|
}
|
|
params.In = map[string][]string{"id": idStrings(bizIds)}
|
|
}
|
|
|
|
var users []dao.User
|
|
page, err := model.UserModel{}.Init().Page(params, &users)
|
|
if err != nil {
|
|
l.Errorf("admin user list page: %v", err)
|
|
return common.FailResponse(utils.Fail), nil
|
|
}
|
|
if len(users) == utils.NumberZero {
|
|
page.Items = []dao.AdminUserListItem{}
|
|
return common.OkResponse(page), nil
|
|
}
|
|
|
|
pageIds := make([]int64, 0, len(users))
|
|
for _, row := range users {
|
|
pageIds = append(pageIds, row.Id)
|
|
}
|
|
|
|
profiles, err := l.loadProfiles(pageIds)
|
|
if err != nil {
|
|
l.Errorf("admin user list profiles: %v", err)
|
|
return common.FailResponse(utils.Fail), nil
|
|
}
|
|
logs, err := loadImageLogs(pageIds)
|
|
if err != nil {
|
|
l.Errorf("admin user list logs: %v", err)
|
|
return common.FailResponse(utils.Fail), nil
|
|
}
|
|
|
|
items := make([]dao.AdminUserListItem, 0, len(users))
|
|
for _, row := range users {
|
|
item := dao.AdminUserListItem{
|
|
Id: row.Id,
|
|
Status: userStatus(row),
|
|
CreatedAt: row.CreatedAt,
|
|
}
|
|
if profile, ok := profiles[row.Id]; ok {
|
|
item.Name = profile.GetName()
|
|
item.Avatar = profile.GetAvatar()
|
|
mobile, _ := utils.DecryptPhone(profile.GetMobile())
|
|
item.Mobile = mobile
|
|
item.Gender = profile.GetGender()
|
|
item.GenderText = formatGender(item.Gender)
|
|
item.Birthday = profile.GetBirthday()
|
|
item.Age = utils.CalcAgeStr(item.Birthday)
|
|
} else {
|
|
item.GenderText = formatGender(utils.NumberZero)
|
|
}
|
|
if logRow, ok := logs[row.Id]; ok {
|
|
item.DetectionNumber = logRow.DetectionNumber
|
|
}
|
|
items = append(items, item)
|
|
}
|
|
|
|
page.Items = items
|
|
return common.OkResponse(page), nil
|
|
}
|
|
|
|
func (l *UserListLogic) filterBizIds(keyword string) ([]int64, error) {
|
|
if l.svcCtx.UserSvcName == utils.StringEmpty {
|
|
return nil, status.Error(codes.FailedPrecondition, "user service empty")
|
|
}
|
|
cli, err := svc.GetRpcClient(l.svcCtx.UserSvcName)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
res, err := userpb.NewUserClient(cli.Conn()).InternalUsersByBiz(l.ctx, &userpb.InternalUsersByBizReq{
|
|
ClientCode: dao.ClientCodeUserSkin,
|
|
Keyword: keyword,
|
|
})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
ids := make([]int64, 0, len(res.GetItems()))
|
|
for _, item := range res.GetItems() {
|
|
if item.GetBizId() > utils.NumberZero {
|
|
ids = append(ids, item.GetBizId())
|
|
}
|
|
}
|
|
return ids, nil
|
|
}
|
|
|
|
func (l *UserListLogic) loadProfiles(bizIds []int64) (map[int64]*userpb.BizUserItem, error) {
|
|
out := make(map[int64]*userpb.BizUserItem, len(bizIds))
|
|
if len(bizIds) == utils.NumberZero {
|
|
return out, nil
|
|
}
|
|
if l.svcCtx.UserSvcName == utils.StringEmpty {
|
|
return nil, status.Error(codes.FailedPrecondition, "user service empty")
|
|
}
|
|
cli, err := svc.GetRpcClient(l.svcCtx.UserSvcName)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
res, err := userpb.NewUserClient(cli.Conn()).InternalUsersByBiz(l.ctx, &userpb.InternalUsersByBizReq{
|
|
ClientCode: dao.ClientCodeUserSkin,
|
|
BizIds: bizIds,
|
|
})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
for _, item := range res.GetItems() {
|
|
out[item.GetBizId()] = item
|
|
}
|
|
return out, nil
|
|
}
|