This commit is contained in:
@@ -2,6 +2,7 @@ package user
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/perfect-panel/server/internal/model/user"
|
||||
"github.com/perfect-panel/server/internal/svc"
|
||||
@@ -28,13 +29,17 @@ func NewGetUserListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetUs
|
||||
}
|
||||
func (l *GetUserListLogic) GetUserList(req *types.GetUserListRequest) (*types.GetUserListResponse, error) {
|
||||
list, total, err := l.svcCtx.UserModel.QueryPageList(l.ctx, req.Page, req.Size, &user.UserFilterParams{
|
||||
UserId: req.UserId,
|
||||
Search: req.Search,
|
||||
Unscoped: req.Unscoped,
|
||||
SubscribeId: req.SubscribeId,
|
||||
UserSubscribeId: req.UserSubscribeId,
|
||||
ShortCode: req.ShortCode,
|
||||
Order: "DESC",
|
||||
UserId: req.UserId,
|
||||
Search: req.Search,
|
||||
Unscoped: req.Unscoped,
|
||||
SubscribeId: req.SubscribeId,
|
||||
UserSubscribeId: req.UserSubscribeId,
|
||||
ShortCode: req.ShortCode,
|
||||
FamilyJoined: req.FamilyJoined,
|
||||
FamilyStatus: req.FamilyStatus,
|
||||
FamilyOwnerUserId: req.FamilyOwnerUserId,
|
||||
FamilyId: req.FamilyId,
|
||||
Order: "DESC",
|
||||
})
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DatabaseQueryError), "GetUserListLogic failed: %v", err.Error())
|
||||
@@ -51,6 +56,64 @@ func (l *GetUserListLogic) GetUserList(req *types.GetUserListRequest) (*types.Ge
|
||||
l.Logger.Error("FindActiveSubscribesByUserIds failed", logger.Field("error", err.Error()))
|
||||
}
|
||||
|
||||
type familyRelation struct {
|
||||
UserId int64
|
||||
FamilyId int64
|
||||
Role uint8
|
||||
FamilyStatus uint8
|
||||
OwnerUserId int64
|
||||
MaxMembers int64
|
||||
}
|
||||
|
||||
relationMap := map[int64]familyRelation{}
|
||||
familyIds := make([]int64, 0)
|
||||
if len(userIds) > 0 {
|
||||
var relations []familyRelation
|
||||
relationErr := l.svcCtx.DB.WithContext(l.ctx).
|
||||
Table("user_family_member").
|
||||
Select("user_family_member.user_id, user_family_member.family_id, user_family_member.role, user_family.status as family_status, user_family.owner_user_id, user_family.max_members").
|
||||
Joins("JOIN user_family ON user_family.id = user_family_member.family_id AND user_family.deleted_at IS NULL").
|
||||
Where("user_family_member.user_id IN ? AND user_family_member.deleted_at IS NULL AND user_family_member.status = ?", userIds, user.FamilyMemberActive).
|
||||
Scan(&relations).Error
|
||||
if relationErr != nil {
|
||||
l.Logger.Error("query family relations failed", logger.Field("error", relationErr.Error()))
|
||||
}
|
||||
|
||||
familyIdSet := make(map[int64]struct{}, len(relations))
|
||||
for _, relation := range relations {
|
||||
if _, exists := relationMap[relation.UserId]; !exists {
|
||||
relationMap[relation.UserId] = relation
|
||||
}
|
||||
if _, ok := familyIdSet[relation.FamilyId]; ok {
|
||||
continue
|
||||
}
|
||||
familyIdSet[relation.FamilyId] = struct{}{}
|
||||
familyIds = append(familyIds, relation.FamilyId)
|
||||
}
|
||||
}
|
||||
|
||||
type familyCount struct {
|
||||
FamilyId int64
|
||||
Count int64
|
||||
}
|
||||
|
||||
familyCountMap := map[int64]int64{}
|
||||
if len(familyIds) > 0 {
|
||||
var counts []familyCount
|
||||
countErr := l.svcCtx.DB.WithContext(l.ctx).
|
||||
Table("user_family_member").
|
||||
Select("family_id, COUNT(1) as count").
|
||||
Where("family_id IN ? AND status = ? AND deleted_at IS NULL", familyIds, user.FamilyMemberActive).
|
||||
Group("family_id").
|
||||
Scan(&counts).Error
|
||||
if countErr != nil {
|
||||
l.Logger.Error("query family member count failed", logger.Field("error", countErr.Error()))
|
||||
}
|
||||
for _, count := range counts {
|
||||
familyCountMap[count.FamilyId] = count.Count
|
||||
}
|
||||
}
|
||||
|
||||
userRespList := make([]types.User, 0, len(list))
|
||||
|
||||
for _, item := range list {
|
||||
@@ -66,6 +129,7 @@ func (l *GetUserListLogic) GetUserList(req *types.GetUserListRequest) (*types.Ge
|
||||
if activeSubs != nil {
|
||||
if info, ok := activeSubs[item.Id]; ok {
|
||||
u.MemberStatus = info.MemberStatus
|
||||
u.PurchasedPackage = info.PurchasedPackage
|
||||
if info.LastTrafficAt != nil {
|
||||
trafficTime := info.LastTrafficAt.Unix()
|
||||
if trafficTime > u.LastLoginTime {
|
||||
@@ -85,6 +149,29 @@ func (l *GetUserListLogic) GetUserList(req *types.GetUserListRequest) (*types.Ge
|
||||
}
|
||||
u.AuthMethods = authMethods
|
||||
|
||||
if relation, ok := relationMap[item.Id]; ok {
|
||||
u.FamilyJoined = true
|
||||
u.FamilyId = relation.FamilyId
|
||||
u.FamilyRole = relation.Role
|
||||
u.FamilyOwnerUserId = relation.OwnerUserId
|
||||
u.FamilyMaxMembers = relation.MaxMembers
|
||||
u.FamilyMemberCount = familyCountMap[relation.FamilyId]
|
||||
switch relation.FamilyStatus {
|
||||
case user.FamilyStatusActive:
|
||||
u.FamilyStatus = "active"
|
||||
default:
|
||||
u.FamilyStatus = "disabled"
|
||||
}
|
||||
switch relation.Role {
|
||||
case user.FamilyRoleOwner:
|
||||
u.FamilyRoleName = "owner"
|
||||
case user.FamilyRoleMember:
|
||||
u.FamilyRoleName = "member"
|
||||
default:
|
||||
u.FamilyRoleName = fmt.Sprintf("role_%d", relation.Role)
|
||||
}
|
||||
}
|
||||
|
||||
userRespList = append(userRespList, u)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user