diff --git a/cv/group_cv/group.go b/cv/group_cv/group.go index 354320bcae0203c8a3d9c916e19e445eacf16fc5..4661d4b42227404976739b9b15783232d819b777 100644 --- a/cv/group_cv/group.go +++ b/cv/group_cv/group.go @@ -481,12 +481,16 @@ func BuildPopularGroupInfo(model *domain.Model, myUserId uint64, groupInfo []*gr model.Log.Infof("BuildPopularGroupInfo, roomEnterTime: %v", roomEnterTime) // 排序优先级V8.0版本 + //sort.Slice(groupIds, func(i, j int) bool { + // gi := groupIds[i] + // gj := groupIds[j] + // return roomEnterTime[gi] > roomEnterTime[gj] || + // roomEnterTime[gi] == roomEnterTime[gj] && visitCount[gj] > visitCount[gj] || + // roomEnterTime[gi] == roomEnterTime[gj] && visitCount[gj] == visitCount[gj] && i >= j + //}) sort.Slice(groupIds, func(i, j int) bool { - gi := groupIds[i] gj := groupIds[j] - return roomEnterTime[gi] > roomEnterTime[gj] || - roomEnterTime[gi] == roomEnterTime[gj] && visitCount[gj] > visitCount[gj] || - roomEnterTime[gi] == roomEnterTime[gj] && visitCount[gj] == visitCount[gj] && i >= j + return visitCount[gj] > visitCount[gj] }) result := make([]*PopularGroupInfo, 0) diff --git a/domain/model/group_m/groupInfo.go b/domain/model/group_m/groupInfo.go index 0c91c58ca6555a1f82e50a49d464e1e420c3dd44..976ce8e4c248f6652eb6fb6e736ee04864486d86 100644 --- a/domain/model/group_m/groupInfo.go +++ b/domain/model/group_m/groupInfo.go @@ -8,10 +8,10 @@ import ( "github.com/bluele/gcache" "gorm.io/gorm" "hilo-group/_const/enum/group_e" - "hilo-group/common" "hilo-group/myerr" "hilo-group/myerr/bizerr" "math/rand" + "sort" "time" ) @@ -45,6 +45,7 @@ type GroupInfo struct { MemberFee uint64 // 加入会员需要黄钻数 CreatedTime time.Time `gorm:"->"` UpdatedTime time.Time `gorm:"->"` + VisitCount int64 `gorm:"-"` // 热度 } func GenerateGroupCode(n uint16) string { @@ -516,18 +517,48 @@ func GetLatestGroupInfos(model *domain.Model, limit, lastId int, groupIds []stri return } -func GetFamilyRooms(model *domain.Model, familyId uint64, pageSize, pageIndex int) ([]*GroupInfo, int, bool, error) { - rows := make([]*GroupInfo, 0) - db := model.Db.Model(GroupInfo{}).Where("owner in (select user_id from group_power_user where group_power_id=? order by field(`role`, 2, 3, 1))", familyId) +func GetFamilyRooms(model *domain.Model, familyId uint64, pageSize, pageIndex int) (res []*GroupInfo, nextIdx int, hasNext bool, err error) { + allImGroupIds := make([]string, 0) var count int64 - err := db.Count(&count).Error + err = model.Db.Model(GroupInfo{}).Select("im_group_id").Where("owner in (select user_id from group_power_user where group_power_id=?)", + familyId).Pluck("im_group_id", &allImGroupIds).Count(&count).Error if err != nil { return nil, 0, false, err } - err = db.Limit(pageSize).Offset(pageIndex).Find(&rows).Error + // 房间热度排序 + visitCountMap, visitCountList, err := BatchGetRoomVisitCountList(model.Log, allImGroupIds) if err != nil { return nil, 0, false, err } - nextIdx, hasNext := common.PageNext(count, pageIndex, pageSize) - return rows, nextIdx, hasNext, nil + roomNum := len(visitCountList) + if roomNum > pageIndex { + endIndex := pageIndex + pageSize + if roomNum >= endIndex { + visitCountList = visitCountList[pageIndex : pageIndex+pageSize] + if roomNum > endIndex { + nextIdx = endIndex + hasNext = true + } + } else { + visitCountList = visitCountList[pageIndex:] + } + } else { + return nil, 0, false, err + } + + res = make([]*GroupInfo, 0) + err = model.Db.Model(GroupInfo{}).Where("im_group_id in ?", allImGroupIds).Find(&res).Error + if err != nil { + return nil, 0, false, err + } + for i, v := range res { + if c, ok := visitCountMap[v.ImGroupId]; ok { + res[i].VisitCount = c + } + } + sort.Slice(res, func(i, j int) bool { + return res[i].VisitCount > res[j].VisitCount + }) + + return res, nextIdx, hasNext, nil } diff --git a/domain/model/group_m/room.go b/domain/model/group_m/room.go index e0898ed65d5648ed65ffa6adfc70e6c602c6992b..a17f32653683ab58e1cb3bddeb58014ee18b7e3f 100644 --- a/domain/model/group_m/room.go +++ b/domain/model/group_m/room.go @@ -451,6 +451,47 @@ func BatchGetRoomVisitCount(logE *logrus.Entry, groupIds []string) (map[string]i return visitCount, nil } +type RoomVisit struct { + ImGroupId string + Count int64 +} + +// 批量查询房间最近N天的访问人数。使用二级缓存 +func BatchGetRoomVisitCountList(logE *logrus.Entry, groupIds []string) (map[string]int64, []*RoomVisit, error) { + //roomVisitCount, err := redis.GetAllRoomVisitCount() + roomVisitCount, err := room_c.MGetRoomVisitCount(groupIds) + if err != nil { + return nil, nil, err + } + logE.Infof("MGetRoomVisitCount:%v", roomVisitCount) + + res := make([]*RoomVisit, 0, len(groupIds)) + visitCount := make(map[string]int64) + for _, groupId := range groupIds { + // 先从二级缓存中找 + if c, ok := roomVisitCount[groupId]; ok { + if vc, err := strconv.ParseInt(c, 10, 64); err == nil && vc > 0 { + logE.Debugf("GetRoomVisitCount, from roomVisitCount %s - %d", groupId, vc) + + visitCount[groupId] = vc + res = append(res, &RoomVisit{ImGroupId: groupId, Count: vc}) + } + } else { + // 如果没有,就从roomVisit中取 + if vc, err := room_c.GetSetRoomVisitCount(groupId); err == nil && vc > 0 { + logE.Infof("GetRoomVisitCount, from roomVisit %s - %d", groupId, vc) + + visitCount[groupId] = vc + res = append(res, &RoomVisit{ImGroupId: groupId, Count: vc}) + } + } + } + sort.Slice(res, func(i, j int) bool { + return res[i].Count >= res[j].Count + }) + return visitCount, res, nil +} + type UserEnterRoom struct { UserId uint64 GroupId string