From a163a269b183b153ead327bc662db4ae029f9e43 Mon Sep 17 00:00:00 2001 From: shenjiachi Date: Tue, 24 Dec 2024 20:29:27 +0800 Subject: [PATCH] update --- app/hdl/hdl_add_friend.go | 2 +- app/hdl/hdl_home_page.go | 123 ++++++++++++++++++++++++++++++++++++++ app/md/md_home_page.go | 23 +++++++ app/router/router.go | 1 + app/utils/cache/redis.go | 12 ++++ go.mod | 2 +- 6 files changed, 161 insertions(+), 2 deletions(-) diff --git a/app/hdl/hdl_add_friend.go b/app/hdl/hdl_add_friend.go index d34105b..60b4544 100644 --- a/app/hdl/hdl_add_friend.go +++ b/app/hdl/hdl_add_friend.go @@ -8,7 +8,7 @@ import ( "applet/app/utils" "code.fnuoos.com/EggPlanet/egg_models.git/src/implement" "code.fnuoos.com/EggPlanet/egg_models.git/src/model" - enum "code.fnuoos.com/EggPlanet/egg_system_rules.git/enum" + "code.fnuoos.com/EggPlanet/egg_system_rules.git/enum" md2 "code.fnuoos.com/EggPlanet/egg_system_rules.git/rule/egg_energy/md" "encoding/json" "fmt" diff --git a/app/hdl/hdl_home_page.go b/app/hdl/hdl_home_page.go index b81f193..a56a958 100644 --- a/app/hdl/hdl_home_page.go +++ b/app/hdl/hdl_home_page.go @@ -489,3 +489,126 @@ func IsCanGetRedPackage(c *gin.Context) { } e.OutSuc(c, resp, nil) } + +const HeroListRedisRankKey = "EggEnergy:HomePage:HeroList:Rank:%d" // 1.kind +func HeroList(c *gin.Context) { + kind := utils.StrToInt(c.DefaultQuery("kind", "1")) + page := utils.StrToInt(c.DefaultQuery("page", "1")) + limit := 10 + start := (page - 1) * limit + end := start + limit - 1 + + rankCacheKey := fmt.Sprintf(HeroListRedisRankKey, kind) + // 1. 获取指定范围的排行 + results, err := cache.ZRevRange(rankCacheKey, start, end, true) + if err != nil { + e.OutErr(c, e.ERR, err.Error()) + return + } + fmt.Println(results) + + // 2. 查询用户数据 + total := len(results) + list := make([]md.HeroListNode, total/2) + userIds := make([]string, total/2) + for i, result := range results { + if i%2 == 1 { + // 奇数数据是分数 + list[(i-1)/2].SumValue = string(result.([]uint8)) + } else { + // 偶数数据是 uid + list[i/2] = md.HeroListNode{ + AvatarUrl: "", + SumValue: "", + Uid: string(result.([]uint8)), + Nickname: "", + } + userIds = append(userIds, string(result.([]uint8))) + } + } + userDb := implement.NewUserDb(db.Db) + users, err := userDb.UserFindByParams(map[string]interface{}{ + "key": "id", + "value": userIds, + }) + if err != nil { + e.OutErr(c, e.ERR, err.Error()) + return + } + userMap := make(map[int64]model.User, len(users)) + for _, user := range users { + userMap[user.Id] = user + } + + for i, node := range list { + val, ok := userMap[utils.StrToInt64(node.Uid)] + if ok { + list[i].Nickname = val.Nickname + list[i].AvatarUrl = svc.GetOssUrl(val.Avatar) + } + } + + user := svc.GetUser(c) + rank, err1 := cache.ZRevRangeByMember(rankCacheKey, utils.Int64ToStr(user.Id)) + if err1 != nil { + if err1.Error() != "redigo: nil returned" { + e.OutErr(c, e.ERR, err1.Error()) + return + } + } + var rankNum int + var scoreNum string + var score []interface{} + var err2 error + if rank != nil { + score, err2 = cache.ZScoreByMember(rankCacheKey, utils.Int64ToStr(user.Id)) + if err2 != nil { + if err2.Error() != "redigo: nil returned" { + e.OutErr(c, e.ERR, err2.Error()) + return + } + } + rankNum = utils.StrToInt(string(rank[0].([]uint8))) + 1 + scoreNum = string(score[0].([]uint8)) + } else { + aggregationDb := implement.NewUserVirtualCoinFlowAggregationDb(db.Db) + flowAggregation, err := aggregationDb.UserVirtualCoinFlowAggregationGetOneByParams(map[string]interface{}{ + "key": "uid", + "value": user.Id, + }) + if err != nil { + e.OutErr(c, e.ERR_DB_ORM, err.Error()) + return + } + if flowAggregation != nil { + switch kind { + case 1: + scoreNum = flowAggregation.TodayData + case 2: + scoreNum = flowAggregation.ThisWeekData + case 3: + scoreNum = flowAggregation.ThisMonthData + } + } else { + scoreNum = "0" + } + + } + + selfRank := md.SelfHeroListNode{ + AvatarUrl: svc.GetOssUrl(user.Avatar), + SumValue: scoreNum, + Uid: user.Id, + Nickname: user.Nickname, + Rank: rankNum, + } + + resp := md.HeroListResp{ + Total: 100, + Page: page, + Limit: 10, + SelfRank: selfRank, + List: list, + } + e.OutSuc(c, resp, nil) +} diff --git a/app/md/md_home_page.go b/app/md/md_home_page.go index af825ac..3d76d68 100644 --- a/app/md/md_home_page.go +++ b/app/md/md_home_page.go @@ -36,3 +36,26 @@ type IsCanSignInResp struct { type IsCanGetRedPackageResp struct { IsCan bool `json:"is_can_get_red_package"` // 能否获取红包 } + +type HeroListNode struct { + AvatarUrl string `json:"avatar_url"` // 头像 + SumValue string `json:"sum_value"` // 期内获得能量值 + Uid string `json:"uid"` + Nickname string `json:"nickname"` // 昵称 +} + +type SelfHeroListNode struct { + AvatarUrl string `json:"avatar_url"` // 头像 + SumValue string `json:"sum_value"` // 期内获得能量值 + Uid int64 `json:"uid"` + Nickname string `json:"nickname"` // 昵称 + Rank int `json:"rank"` // 排行(为0时显示为未上榜) +} + +type HeroListResp struct { + Total int `json:"total"` + Page int `json:"page"` + Limit int `json:"limit"` + SelfRank SelfHeroListNode `json:"self_rank"` // 当前用户排行 + List []HeroListNode `json:"list"` +} diff --git a/app/router/router.go b/app/router/router.go index 01b12b1..a994434 100644 --- a/app/router/router.go +++ b/app/router/router.go @@ -108,6 +108,7 @@ func route(r *gin.RouterGroup) { rHomePage.GET("/realTimePrice", hdl.RealTimePrice) // 主页-实时数据 rHomePage.GET("/isCanSignIn", hdl.IsCanSignIn) // 主页-是否可以签到 rHomePage.GET("/isCanGetRedPackage", hdl.IsCanGetRedPackage) // 主页-是否可以获得红包 + rHomePage.GET("/heroList", hdl.HeroList) // 主页-英雄榜 } rAddFriend := r.Group("/addFriend") diff --git a/app/utils/cache/redis.go b/app/utils/cache/redis.go index a1a5716..b033c72 100644 --- a/app/utils/cache/redis.go +++ b/app/utils/cache/redis.go @@ -208,6 +208,18 @@ func ZRange(key string, start int, end int, withScores bool) ([]interface{}, err } return redigo.Values(Do("ZRANGE", key, start, end)) } +func ZRevRange(key string, start int, end int, withScores bool) ([]interface{}, error) { + if withScores { + return redigo.Values(Do("ZREVRANGE", key, start, end, "WITHSCORES")) + } + return redigo.Values(Do("ZREVRANGE", key, start, end)) +} +func ZRevRangeByMember(key string, member string) ([]interface{}, error) { + return redigo.Values(Do("ZRANK", key, member)) +} +func ZScoreByMember(key string, member string) ([]interface{}, error) { + return redigo.Values(Do("ZSCORE", key, member)) +} func ZRemRangeByScore(key string, start int64, end int64) ([]interface{}, error) { return redigo.Values(Do("ZREMRANGEBYSCORE", key, start, end)) } diff --git a/go.mod b/go.mod index 7b6b18e..0562b73 100644 --- a/go.mod +++ b/go.mod @@ -33,7 +33,7 @@ require ( ) require ( - code.fnuoos.com/EggPlanet/egg_models.git v0.2.1-0.20241224014721-42a6059f0dfa + code.fnuoos.com/EggPlanet/egg_models.git v0.2.1-0.20241224090637-89a57f7fbb1e code.fnuoos.com/EggPlanet/egg_system_rules.git v0.0.4-0.20241223102933-953e3394de41 code.fnuoos.com/go_rely_warehouse/zyos_go_es.git v1.0.1-0.20241118083738-0f22da9ba0be code.fnuoos.com/go_rely_warehouse/zyos_go_mq.git v0.0.5