group_power_star.go 3.18 KB
Newer Older
1 2 3 4 5 6 7
package groupPower_c

import (
	"fmt"
	"git.hilo.cn/hilo-common/domain"
	"git.hilo.cn/hilo-common/resource/mysql"
	"github.com/go-redis/redis/v8"
JiebinHu's avatar
JiebinHu committed
8 9
	"github.com/jinzhu/now"
	"github.com/pkg/errors"
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
	"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"} {
JiebinHu's avatar
JiebinHu committed
33 34 35 36 37 38 39 40 41 42
		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)
43 44 45 46 47 48 49 50 51
		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
JiebinHu's avatar
JiebinHu committed
52 53 54 55 56 57 58 59 60 61 62 63 64
	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)
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
	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
}