room.go 1.55 KB
Newer Older
hujiebin's avatar
hujiebin committed
1 2 3 4 5 6 7 8 9 10 11 12 13
package group_m

import (
	"git.hilo.cn/hilo-common/domain"
	"git.hilo.cn/hilo-common/mylogrus"
	"git.hilo.cn/hilo-common/resource/mysql"
	"hilo-user/_const/redis_key/group_k"
	"hilo-user/myerr"
	"strconv"
	"strings"
	"time"
)

hujiebin's avatar
hujiebin committed
14 15
// 3天
const expireMinute = 60 * 60 * 24 * 3
hujiebin's avatar
hujiebin committed
16 17 18 19 20 21 22 23 24 25 26 27

//获取在房间的用户 返回值:map,key:userId, value:groupUuid
func RoomLivingUserIdFilter(model *domain.Model, userIds []mysql.ID) (map[mysql.ID]string, error) {
	userIdSet := map[mysql.ID]struct{}{}
	for i, _ := range userIds {
		userIdSet[userIds[i]] = struct{}{}
	}
	key := group_k.GetPrefixGroupRoomLiving()
	if err := ClearExpired(model, key, expireMinute); err != nil {
		return nil, myerr.WrapErr(err)
	}

hujiebin's avatar
hujiebin committed
28
	groupUserIdstrs, err := model.RedisCluster.ZRange(model, key, 0, -1).Result()
hujiebin's avatar
hujiebin committed
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
	if err != nil {
		return nil, myerr.WrapErr(err)
	}

	resultUserSet := map[mysql.ID]string{}
	for i, _ := range groupUserIdstrs {
		tempGroupUid, userId := analysisMemberStr(groupUserIdstrs[i])

		if _, flag := userIdSet[userId]; flag {
			resultUserSet[userId] = tempGroupUid
		}
	}
	return resultUserSet, nil
}

func ClearExpired(model *domain.Model, key string, expireSec int64) error {
hujiebin's avatar
hujiebin committed
45
	return model.RedisCluster.ZRemRangeByScore(model, key,
hujiebin's avatar
hujiebin committed
46 47 48 49 50 51 52 53 54 55 56 57
		"0", strconv.FormatInt(time.Now().Unix()-expireSec, 10)).Err()
}

func analysisMemberStr(memberStr string) (string, uint64) {
	strs := strings.Split(memberStr, "_")
	groupUid := strs[0]
	userId, err := strconv.ParseUint(strs[1], 10, 64)
	if err != nil {
		mylogrus.MyLog.Errorf("analysisMemberStr memberStr:%v err:%+v", memberStr, err)
	}
	return groupUid, userId
}