group_rank.go 1.45 KB
Newer Older
hujiebin's avatar
hujiebin committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
package groupPower_m

import (
	"git.hilo.cn/hilo-common/domain"
	"git.hilo.cn/hilo-common/resource/mysql"
)

type GroupPowerExpRank struct {
	GroupPowerId mysql.ID
	Exp          mysql.Num
	Rank         int
}

// 获取家族经验排行榜
// param limit: 排行榜人数
func GetGroupPowerExpRank(model *domain.Model, beginDate, endDate string, limit int) ([]GroupPowerExpRank, error) {
	var res []GroupPowerExpRank
hujiebin's avatar
hujiebin committed
18 19
	if err := model.DB().Table("group_power_day_exp").Select("group_power_id,SUM(exp) as exp").
		Where("date BETWEEN ? AND ?", beginDate, endDate).Group("group_power_id").Order("exp DESC").Limit(limit).Find(&res).Error; err != nil {
hujiebin's avatar
hujiebin committed
20 21 22
		model.Log.Errorf("GetGroupPowerExpRank fail:%v", err)
		return res, err
	}
hujiebin's avatar
hujiebin committed
23 24
	for i := range res {
		res[i].Rank = i + 1
hujiebin's avatar
hujiebin committed
25 26 27
	}
	return res, nil
}
chenweijian's avatar
chenweijian committed
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47

// 获取我的家族月排名是多少,超过30则返回31
func GetGroupPowerExpRankById(model *domain.Model, beginDate, endDate string, limit int, groupPowerId uint64) (int, error) {
	var res []GroupPowerExpRank
	if err := model.DB().Table("group_power_day_exp").Select("group_power_id,SUM(exp) as exp").
		Where("date BETWEEN ? AND ?", beginDate, endDate).Group("group_power_id").Order("exp DESC").Limit(limit).Find(&res).Error; err != nil {
		model.Log.Errorf("GetMyGroupPowerExpRank fail:%v", err)
		return 0, err
	}
	for i := range res {
		res[i].Rank = i + 1
	}
	myRank := 31
	for _, v := range res {
		if v.GroupPowerId == groupPowerId {
			myRank = v.Rank
		}
	}
	return myRank, nil
}