Commit 698d4f79 authored by chenweijian's avatar chenweijian

房间列表按热度排序

parent be119516
...@@ -481,12 +481,16 @@ func BuildPopularGroupInfo(model *domain.Model, myUserId uint64, groupInfo []*gr ...@@ -481,12 +481,16 @@ func BuildPopularGroupInfo(model *domain.Model, myUserId uint64, groupInfo []*gr
model.Log.Infof("BuildPopularGroupInfo, roomEnterTime: %v", roomEnterTime) model.Log.Infof("BuildPopularGroupInfo, roomEnterTime: %v", roomEnterTime)
// 排序优先级V8.0版本 // 排序优先级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 { sort.Slice(groupIds, func(i, j int) bool {
gi := groupIds[i]
gj := groupIds[j] gj := groupIds[j]
return roomEnterTime[gi] > roomEnterTime[gj] || return visitCount[gj] > visitCount[gj]
roomEnterTime[gi] == roomEnterTime[gj] && visitCount[gj] > visitCount[gj] ||
roomEnterTime[gi] == roomEnterTime[gj] && visitCount[gj] == visitCount[gj] && i >= j
}) })
result := make([]*PopularGroupInfo, 0) result := make([]*PopularGroupInfo, 0)
......
...@@ -8,10 +8,10 @@ import ( ...@@ -8,10 +8,10 @@ import (
"github.com/bluele/gcache" "github.com/bluele/gcache"
"gorm.io/gorm" "gorm.io/gorm"
"hilo-group/_const/enum/group_e" "hilo-group/_const/enum/group_e"
"hilo-group/common"
"hilo-group/myerr" "hilo-group/myerr"
"hilo-group/myerr/bizerr" "hilo-group/myerr/bizerr"
"math/rand" "math/rand"
"sort"
"time" "time"
) )
...@@ -45,6 +45,7 @@ type GroupInfo struct { ...@@ -45,6 +45,7 @@ type GroupInfo struct {
MemberFee uint64 // 加入会员需要黄钻数 MemberFee uint64 // 加入会员需要黄钻数
CreatedTime time.Time `gorm:"->"` CreatedTime time.Time `gorm:"->"`
UpdatedTime time.Time `gorm:"->"` UpdatedTime time.Time `gorm:"->"`
VisitCount int64 `gorm:"-"` // 热度
} }
func GenerateGroupCode(n uint16) string { func GenerateGroupCode(n uint16) string {
...@@ -516,18 +517,48 @@ func GetLatestGroupInfos(model *domain.Model, limit, lastId int, groupIds []stri ...@@ -516,18 +517,48 @@ func GetLatestGroupInfos(model *domain.Model, limit, lastId int, groupIds []stri
return return
} }
func GetFamilyRooms(model *domain.Model, familyId uint64, pageSize, pageIndex int) ([]*GroupInfo, int, bool, error) { func GetFamilyRooms(model *domain.Model, familyId uint64, pageSize, pageIndex int) (res []*GroupInfo, nextIdx int, hasNext bool, err error) {
rows := make([]*GroupInfo, 0) allImGroupIds := make([]string, 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)
var count int64 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 { if err != nil {
return nil, 0, false, err return nil, 0, false, err
} }
err = db.Limit(pageSize).Offset(pageIndex).Find(&rows).Error // 房间热度排序
visitCountMap, visitCountList, err := BatchGetRoomVisitCountList(model.Log, allImGroupIds)
if err != nil { if err != nil {
return nil, 0, false, err return nil, 0, false, err
} }
nextIdx, hasNext := common.PageNext(count, pageIndex, pageSize) roomNum := len(visitCountList)
return rows, nextIdx, hasNext, nil 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
} }
...@@ -451,6 +451,47 @@ func BatchGetRoomVisitCount(logE *logrus.Entry, groupIds []string) (map[string]i ...@@ -451,6 +451,47 @@ func BatchGetRoomVisitCount(logE *logrus.Entry, groupIds []string) (map[string]i
return visitCount, nil 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 { type UserEnterRoom struct {
UserId uint64 UserId uint64
GroupId string GroupId string
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment