group_power_star.go 2.62 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
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
}