package groupPower_c import ( "fmt" "git.hilo.cn/hilo-common/domain" "git.hilo.cn/hilo-common/resource/mysql" "github.com/go-redis/redis/v8" "github.com/spf13/cast" "hilo-group/_const/enum/groupPower_e" "hilo-group/_const/redis_key/groupPower_k" "time" ) // 家族之星-排名 type GroupPowerStarRank struct { Period string GroupPowerId mysql.ID UserId mysql.ID Type groupPower_e.GroupPowerStarType Score mysql.Num } // 增加家族之星分数 func IncrGroupPowerDayStarScore(model *domain.Model, groupPowerId, userId mysql.ID, _type groupPower_e.GroupPowerStarType, score mysql.Num) error { ttl := map[string]time.Duration{ "day": time.Hour * 24 * 7, "week": time.Hour * 24 * 7 * 30, "month": time.Hour * 24 * 7 * 30 * 2, } for _, period := range []string{"day", "week", "month"} { key := groupPower_k.GetGroupPowerStarRankKey(_type, period, groupPowerId) model.RedisCluster.ZIncrBy(model, key, float64(score), fmt.Sprintf("%d", userId)) model.RedisCluster.Expire(model, key, ttl[period]) } return nil } // 获取家族之星排行 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) rows, err := model.RedisCluster.ZRevRangeByScoreWithScores(model, key, &redis.ZRangeBy{ Min: "-inf", Max: "+inf", Offset: int64(offset), Count: int64(limit), }).Result() if err != nil { model.Log.Errorf("GetGroupPowerStarRankPeriod fail:%v", err) return res, err } for _, v := range rows { res = append(res, &GroupPowerStarRank{ Period: period, GroupPowerId: groupPowerId, UserId: cast.ToUint64(v.Member), Type: _type, Score: mysql.Num(v.Score), }) } return res, nil } // 获取家族之星三个排行榜的各自第一名 func GetGroupPowerMonthStartTop1(model *domain.Model, groupPowerId mysql.ID) ([]*GroupPowerStarRank, error) { var res []*GroupPowerStarRank r1, err := GetGroupPowerStarRankPeriod(model, "month", groupPowerId, groupPower_e.GroupPowerStarTypeFamous, 0, 1) if err != nil { return res, err } r2, err := GetGroupPowerStarRankPeriod(model, "month", groupPowerId, groupPower_e.GroupPowerStarTypeActive, 0, 1) if err != nil { return res, err } r3, err := GetGroupPowerStarRankPeriod(model, "month", groupPowerId, groupPower_e.GroupPowerStarTypeCharm, 0, 1) if err != nil { return res, err } res = append(res, r1...) res = append(res, r2...) res = append(res, r3...) return res, nil }