From 698d4f7912a24204b47b9fd40cd5b327a0c4a5a4 Mon Sep 17 00:00:00 2001 From: chenweijian <820961417@qq.com> Date: Wed, 22 Mar 2023 14:18:40 +0800 Subject: [PATCH] =?UTF-8?q?=E6=88=BF=E9=97=B4=E5=88=97=E8=A1=A8=E6=8C=89?= =?UTF-8?q?=E7=83=AD=E5=BA=A6=E6=8E=92=E5=BA=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cv/group_cv/group.go | 12 +++++--- domain/model/group_m/groupInfo.go | 47 +++++++++++++++++++++++++------ domain/model/group_m/room.go | 41 +++++++++++++++++++++++++++ 3 files changed, 88 insertions(+), 12 deletions(-) diff --git a/cv/group_cv/group.go b/cv/group_cv/group.go index 354320b..4661d4b 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 0c91c58..976ce8e 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 e0898ed..a17f326 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 -- 2.22.0