Commit a6c36dee authored by hujiebin's avatar hujiebin

feat:家族榜单

parent 3534f327
package group_power_cv
import (
"git.hilo.cn/hilo-common/resource/mysql"
"hilo-group/_const/enum/groupPower_e"
)
type CvGroupPowerRankData struct {
GroupPowerId mysql.ID `json:"groupPowerId"` // 家族id
GroupPic string `json:"groupPic"` // 家族图片
Name string `json:"name"` // 家族名
Nameplate string `json:"nameplate"` // 铭牌
Grade groupPower_e.GroupPowerGrade `json:"grade"` // 等级 0:无 1:青铜 2:白银 3:黄金 4:黑金
Exp mysql.Num `json:"exp"` // 经验值
Rank int `json:"rank"` // 排名
// todo 铭牌,图片等字段, 等伟健哥哥的结构体
}
type CvGroupPowerRank struct {
MyGroupPower CvGroupPowerRankData `json:"myGroupPower"`
Items []CvGroupPowerRankData `json:"items"`
}
......@@ -271,3 +271,17 @@ func ClearGroupPowerGrade(model *domain.Model) error {
return nil
})
}
// 批量获取家族等级
func MGetGroupPowerGrade(model *domain.Model, groupPowerIds []mysql.ID) (map[mysql.ID]GroupPowerGrade, error) {
var rows []GroupPowerGrade
res := make(map[mysql.ID]GroupPowerGrade)
if err := model.DB().Model(GroupPowerGrade{}).Where("group_power_id in ?", groupPowerIds).Find(&rows).Error; err != nil {
model.Log.Errorf("MGetGroupPowerGrade fail:%v", err)
return res, err
}
for i, v := range rows {
res[v.GroupPowerId] = rows[i]
}
return res, nil
}
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
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
}
package group_power_r
import (
"git.hilo.cn/hilo-common/domain"
"git.hilo.cn/hilo-common/mycontext"
"git.hilo.cn/hilo-common/resource/mysql"
"git.hilo.cn/hilo-common/utils"
"github.com/gin-gonic/gin"
"github.com/jinzhu/now"
"hilo-group/_const/enum/groupPower_e"
"hilo-group/cv/group_power_cv"
"hilo-group/domain/model/groupPower_m"
"hilo-group/myerr/bizerr"
"hilo-group/req"
"hilo-group/resp"
"time"
)
// @Tags 国家势力
// @Summary 家族榜单
// @Param token header string true "token"
// @Param nonce header string true "随机数字"
// @Param period path string true "榜单周期 day:日 week:周 month:月"
// @Success 200 {object} group_power_cv.CvGroupPowerRank
// @Router /v1/groupPower/rank/{period} [get]
func GroupPowerRank(c *gin.Context) (*mycontext.MyContext, error) {
myContext := mycontext.CreateMyContext(c.Keys)
userId, err := req.GetUserId(c)
if err != nil {
return myContext, err
}
period := c.Param("period")
if period != "day" && period != "week" && period != "month" {
return myContext, bizerr.InvalidParameter
}
// day
var beginDate, endDate string
switch period {
case "day":
beginDate, endDate = time.Now().Format("2006-01-02"), time.Now().Format("2006-01-02")
case "week":
beginDate, endDate = utils.GetMonday(time.Now()).Format("2006-01-02"), utils.GetMonday(time.Now()).AddDate(0, 0, 6).Format("2006-01-02")
case "month":
beginDate, endDate = now.BeginningOfMonth().Format("2006-01-02"), now.EndOfMonth().Format("2006-01-02")
}
var model = domain.CreateModelContext(myContext)
rank, err := groupPower_m.GetGroupPowerExpRank(model, beginDate, endDate, 30)
if err != nil {
return myContext, err
}
var ids []mysql.ID
for _, g := range rank {
ids = append(ids, g.GroupPowerId)
}
response := group_power_cv.CvGroupPowerRank{}
myGroupPower, err := groupPower_m.GetGroupPowerUserOrNil(model, userId)
if err != nil {
return myContext, err
}
if myGroupPower != nil {
ids = append(ids, myGroupPower.GroupPowerId)
}
grades, err := groupPower_m.MGetGroupPowerGrade(model, ids)
if err != nil {
return myContext, err
}
if myGroupPower != nil && myGroupPower.Role == groupPower_e.GroupPowerUserRoleMgr {
myGroupPowerRank, err := groupPower_m.GetGroupPowerExpMyRank(model, beginDate, endDate, myGroupPower.GroupPowerId)
if err != nil {
return myContext, err
}
response.MyGroupPower = group_power_cv.CvGroupPowerRankData{
GroupPowerId: myGroupPower.GroupPowerId,
GroupPic: "todo",
Name: "todo",
Nameplate: "todo",
Grade: grades[myGroupPower.GroupPowerId].Grade,
Exp: myGroupPowerRank.Exp,
Rank: myGroupPowerRank.Rank,
}
}
for r, v := range rank {
response.Items = append(response.Items, group_power_cv.CvGroupPowerRankData{
GroupPowerId: v.GroupPowerId,
GroupPic: "todo", // todo
Name: "todo",
Nameplate: "todo",
Grade: grades[v.GroupPowerId].Grade,
Exp: v.Exp,
Rank: r + 1, // 查询已做排序
})
}
resp.ResponseOk(c, response)
return myContext, nil
}
......@@ -125,6 +125,7 @@ func InitRouter() *gin.Engine {
groupPower.GET("/team", wrapper(group_power_r.GroupPowerTeam))
groupPower.GET("/billboard/week", wrapper(group_power_r.GroupPowerBillboardWeek))
groupPower.GET("/billboard/owner/week", wrapper(group_power_r.GroupPowerBillboardOwnerWeek))
groupPower.GET("/rank/:period", wrapper(group_power_r.GroupPowerRank))
}
return r
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment