group_rank.go 2.46 KB
Newer Older
hujiebin's avatar
hujiebin committed
1 2 3 4 5
package groupPower_m

import (
	"git.hilo.cn/hilo-common/domain"
	"git.hilo.cn/hilo-common/resource/mysql"
6
	"gorm.io/gorm"
chenweijian's avatar
chenweijian committed
7
	"hilo-group/_const/enum/groupPower_e"
hujiebin's avatar
hujiebin committed
8 9 10 11 12 13 14 15 16 17
)

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

// 获取家族经验排行榜
// param limit: 排行榜人数
chenweijian's avatar
chenweijian committed
18
func GetGroupPowerExpRank(model *domain.Model, beginDate, endDate string, limit int, gpStatus groupPower_e.GroupPowerStatus, area int) ([]GroupPowerExpRank, error) {
hujiebin's avatar
hujiebin committed
19
	var res []GroupPowerExpRank
chenweijian's avatar
chenweijian committed
20 21 22
	db := model.DB().Table("group_power_day_exp gpd").Select("gpd.group_power_id,SUM(gpd.exp) as exp").
		Where("gpd.date BETWEEN ? AND ?", beginDate, endDate)
	if gpStatus > 0 {
chenweijian's avatar
chenweijian committed
23
		db = db.Joins("left join group_power gp on gpd.group_power_id = gp.id").Where("gp.status = ?", gpStatus)
chenweijian's avatar
chenweijian committed
24 25 26 27
		if area > 0 {
			db = db.Joins("left join group_info gi on gp.group_uid = gi.im_group_id left join res_country rs on gi.country = rs.name").
				Where("rs.area = ?", area)
		}
chenweijian's avatar
chenweijian committed
28 29
	}
	if err := db.Group("gpd.group_power_id").Order("exp DESC").Limit(limit).Find(&res).Error; err != nil {
hujiebin's avatar
hujiebin committed
30 31 32 33 34 35 36 37
		model.Log.Errorf("GetGroupPowerExpRank fail:%v", err)
		return res, err
	}
	for i := range res {
		res[i].Rank = i + 1
	}
	return res, nil
}
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

// 获取我的家族经验排行榜
func GetMyGroupPowerExpRank(model *domain.Model, beginDate, endDate string, groupPowerId uint64) (GroupPowerExpRank, 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 ? AND group_power_id = ?", beginDate, endDate, groupPowerId).Group("group_power_id").First(&res).Error; err != nil {
		if err != gorm.ErrRecordNotFound {
			model.Log.Errorf("GetMyGroupPowerExpRank fail:%v", err)
			return res, err
		}
		return res, nil
	}
	return res, nil
}

// 获取我的家族月排名是多少,超过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
}