From 1ccc8c2f3b7d954f4ccbd567ebe509630c80c882 Mon Sep 17 00:00:00 2001 From: JiebinHu <458249864@qq.com> Date: Sun, 27 Aug 2023 14:20:35 +0800 Subject: [PATCH] =?UTF-8?q?fix=EF=BC=9A=E6=A6=9C=E5=8D=95=E6=BC=8F?= =?UTF-8?q?=E4=BA=86=E6=97=A5=E6=9C=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../groupPower_k/group_power_star.go | 7 ++--- domain/cache/groupPower_c/group_power_star.go | 27 +++++++++++++++++-- 2 files changed, 29 insertions(+), 5 deletions(-) diff --git a/_const/redis_key/groupPower_k/group_power_star.go b/_const/redis_key/groupPower_k/group_power_star.go index f54f759..317b955 100644 --- a/_const/redis_key/groupPower_k/group_power_star.go +++ b/_const/redis_key/groupPower_k/group_power_star.go @@ -8,9 +8,10 @@ import ( // 家族之星榜单 // type: 1:送礼 2:活跃 3:收礼 -const GroupPowerStarPrefix = "groupPowerStar:${type}:${period}:${groupPowerId}" // zset member:userId score:分数 +// date:天/周/月的开始时间 +const GroupPowerStarPrefix = "groupPowerStar:${type}:${period}:${groupPowerId}:${date}" // zset member:userId score:分数 -func GetGroupPowerStarRankKey(_type groupPower_e.GroupPowerStarType, period string, groupPowerId uint64) string { +func GetGroupPowerStarRankKey(_type groupPower_e.GroupPowerStarType, period string, groupPowerId uint64, date string) string { return redis_key.ReplaceKey(GroupPowerStarPrefix, - fmt.Sprintf("%d", _type), period, fmt.Sprintf("%d", groupPowerId)) + fmt.Sprintf("%d", _type), period, fmt.Sprintf("%d", groupPowerId), date) } diff --git a/domain/cache/groupPower_c/group_power_star.go b/domain/cache/groupPower_c/group_power_star.go index 63fe071..018f7c3 100644 --- a/domain/cache/groupPower_c/group_power_star.go +++ b/domain/cache/groupPower_c/group_power_star.go @@ -5,6 +5,8 @@ import ( "git.hilo.cn/hilo-common/domain" "git.hilo.cn/hilo-common/resource/mysql" "github.com/go-redis/redis/v8" + "github.com/jinzhu/now" + "github.com/pkg/errors" "github.com/spf13/cast" "hilo-group/_const/enum/groupPower_e" "hilo-group/_const/redis_key/groupPower_k" @@ -28,7 +30,16 @@ func IncrGroupPowerDayStarScore(model *domain.Model, groupPowerId, userId mysql. "month": time.Hour * 24 * 7 * 30 * 2, } for _, period := range []string{"day", "week", "month"} { - key := groupPower_k.GetGroupPowerStarRankKey(_type, period, groupPowerId) + date := "" + switch period { + case "day": + date = time.Now().Format("2006-01-02") + case "week": + date = now.BeginningOfWeek().Format("2006-01-02") + case "month": + date = now.BeginningOfMonth().Format("2006-01-02") + } + key := groupPower_k.GetGroupPowerStarRankKey(_type, period, groupPowerId, date) model.RedisCluster.ZIncrBy(model, key, float64(score), fmt.Sprintf("%d", userId)) model.RedisCluster.Expire(model, key, ttl[period]) } @@ -38,7 +49,19 @@ func IncrGroupPowerDayStarScore(model *domain.Model, groupPowerId, userId mysql. // 获取家族之星排行 func GetGroupPowerStarRankPeriod(model *domain.Model, period string, groupPowerId mysql.ID, _type groupPower_e.GroupPowerStarType, offset, limit int) ([]*GroupPowerStarRank, error) { var res []*GroupPowerStarRank - key := groupPower_k.GetGroupPowerStarRankKey(_type, period, groupPowerId) + date := "" + switch period { + case "day": + date = time.Now().Format("2006-01-02") + case "week": + date = now.BeginningOfWeek().Format("2006-01-02") + case "month": + date = now.BeginningOfMonth().Format("2006-01-02") + } + if len(date) <= 0 { + return res, errors.New("illegal date") + } + key := groupPower_k.GetGroupPowerStarRankKey(_type, period, groupPowerId, date) rows, err := model.RedisCluster.ZRevRangeByScoreWithScores(model, key, &redis.ZRangeBy{ Min: "-inf", Max: "+inf", -- 2.22.0