roomData.go 4.19 KB
Newer Older
hujiebin's avatar
hujiebin committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
package group_m

import (
	"git.hilo.cn/hilo-common/domain"
	"git.hilo.cn/hilo-common/mycontext"
	"git.hilo.cn/hilo-common/resource/mysql"
	"git.hilo.cn/hilo-common/rpc"
	"hilo-group/domain/model/noble_m"
	"hilo-group/domain/model/user_m"
	"sort"
	"time"
)

type RoomOnlineUser struct {
	Total int                 `json:"total"`
	Users []RoomOnlineUserMsg `json:"users"`
	//客户端对比,只要
	ServiceTime int64 `json:"serviceTime"`
}

type RoomOnlineUserMsg struct {
	Id           uint64 `json:"id,omitempty"`
	ExternalId   string `json:"externalId"`
	Avatar       string `json:"avatar"`
	Nick         string `json:"nick"`
	Sex          uint8  `json:"sex"`
	Code         string `json:"code"`
	Country      string `json:"country"`
	CountryIcon  string `json:"countryIcon"`
	IsPrettyCode bool   `json:"isPrettyCode"` // 是否靓号
	IsLogout     bool   `json:"isLogout"`     //是否注销 true:已经注销, false:没有注销
	//是否VIP用户
	IsVip bool `json:"isVip"`
	//贵族
	Noble RoomUserNoble `json:"noble"` // 当前的
	//VIP用户过期时间(只有自己查询自己,才返回)
	VipExpireTime *int64     `json:"vipExpireTime"`
	SvipLevel     int        `json:"svipLevel"`
	Svip          rpc.CvSvip `json:"svip"`
}

type RoomUserNoble struct {
	Level   uint16 `json:"level"`
	EndTime int64  `json:"endTime"`
}

func GetRoomOnlineUser(cxt *mycontext.MyContext, groupId string) (RoomOnlineUser, error) {
	userIds, total, err := getGroupInUserIds(cxt, groupId)
	if err != nil {
		return RoomOnlineUser{}, err
	}
	var model = domain.CreateModelContext(cxt)
	userMap, err := user_m.GetUserMapByIds(model, userIds)
	if err != nil {
		return RoomOnlineUser{}, err
	}

	vipMap, err := user_m.BatchGetVips(userIds)
	if err != nil {
		return RoomOnlineUser{}, err
	}

	nobleMap, err := noble_m.BatchGetNobleLevel(mysql.Db, userIds)
	if err != nil {
		return RoomOnlineUser{}, err
	}
	svip, _ := rpc.MGetUserSvip(model, userIds)

	users := make([]RoomOnlineUserMsg, 0, len(userIds))
	for i := range userIds {
		user, _ := userMap[userIds[i]]
		users = append(users, RoomOnlineUserMsg{
			Id:           user.ID,
			ExternalId:   user.ExternalId,
			Avatar:       user.Avatar,
			Nick:         user.Nick,
			Sex:          user.Sex,
			Code:         user.Code,
			Country:      user.Country,
			CountryIcon:  user.CountryIcon,
			IsPrettyCode: user.Code != user.OriginCode,
			IsLogout:     false,
			IsVip:        vipMap[userIds[i]] != nil,
			Noble: RoomUserNoble{
				Level:   nobleMap[userIds[i]],
				EndTime: 0,
			},
			VipExpireTime: vipMap[userIds[i]],
			SvipLevel:     svip[userIds[i]].SvipLevel,
			Svip:          rpc.CopySimpleSvip(svip[userIds[i]]),
		})
	}
	return RoomOnlineUser{
		Total:       total,
		Users:       users,
		ServiceTime: time.Now().UnixNano(),
	}, nil
}

var groupOnlineUserIdMaxNum int = 15

//显示的最大数量是15个。
func getGroupInUserIds(cxt *mycontext.MyContext, groupId string) ([]uint64, int, error) {
	userIds, err := RoomLivingExistsUserId(groupId)
	if err != nil {
		return []uint64{}, 0, err
	}
	if len(userIds) == 0 {
		return []uint64{}, 0, nil
	}
	var model = domain.CreateModelContext(cxt)
	svips, err := rpc.MGetUserSvip(model, userIds)
	if err != nil {
		model.Log.Errorf("MGetUserSvip fail :%v", err)
	}
	//贵族
	nobleLevelMap, err := noble_m.BatchGetNobleLevel(mysql.Db, userIds)
	if err != nil {
		model.Log.Errorf("BatchGetNobleLevel fail :%v", err)
	}
	//获取vip
	vipMap, err := user_m.BatchGetVips(userIds)
	if err != nil {
		model.Log.Errorf("MGetUserSvip fail :%v", err)
	}
	// svip > noble > vip
	sort.Slice(userIds, func(i, j int) bool {
		if svips[userIds[i]].SvipLevel > svips[userIds[j]].SvipLevel {
			return true
		} else if svips[userIds[i]].SvipLevel == svips[userIds[j]].SvipLevel {
			if nobleLevelMap[userIds[i]] > nobleLevelMap[userIds[j]] {
				return true
			} else if nobleLevelMap[userIds[i]] == nobleLevelMap[userIds[j]] {
				var vi, vj int64
				if ts, ok := vipMap[userIds[i]]; ok && ts != nil {
					vi = *ts
				}
				if ts, ok := vipMap[userIds[j]]; ok && ts != nil {
					vj = *ts
				}
				return vi > vj
			}
		}
		return false
	})
	total := len(userIds)
	if total > groupOnlineUserIdMaxNum {
		return userIds[0:groupOnlineUserIdMaxNum], total, nil
	}
	return userIds, total, nil
}