group_rank.go 1.59 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 18
package groupPower_m

import (
	"fmt"
	"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
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")
hujiebin's avatar
hujiebin committed
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
	if err := model.DB().Table("group_power_day_exp p").Joins(",(SELECT @curRank := 0) p").Where("date BETWEEN ? AND ?", beginDate, endDate).
		Select("group_power_id,SUM(exp) as exp,@curRank := @curRank + 1 `rank`").
		Group("group_power_id").Order("exp DESC").Limit(limit).Find(&res).Error; err != nil {
		model.Log.Errorf("GetGroupPowerExpRank fail:%v", err)
		return res, err
	}
	return res, nil
}

// 获取我的家族排行
func GetGroupPowerExpMyRank(model *domain.Model, beginDate, endDate string, groupPowerId mysql.ID) (GroupPowerExpRank, error) {
	table := fmt.Sprintf("(select group_power_id,SUM(exp) as exp,@curRank := @curRank + 1 `rank` from group_power_day_exp p,(SELECT @curRank := 0) tmp"+
		" WHERE `date` BETWEEN '%s' AND '%s' group BY group_power_id ORDER BY exp DESC ) t", beginDate, endDate)
	var res GroupPowerExpRank
	if err := model.DB().Table(table).Select("t.group_power_id,t.exp,t.`rank`").Where("t.group_power_id = ?", groupPowerId).Scan(&res).Error; err != nil {
		return res, err
	}
	return res, nil
}