From a6c36dee6ada16f5ccf257b58e522dbcf2f64c09 Mon Sep 17 00:00:00 2001 From: hujiebin Date: Wed, 8 Mar 2023 18:39:19 +0800 Subject: [PATCH] =?UTF-8?q?feat:=E5=AE=B6=E6=97=8F=E6=A6=9C=E5=8D=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cv/group_power_cv/group_rank.go | 22 ++++++ domain/model/groupPower_m/group_grade.go | 14 ++++ domain/model/groupPower_m/group_rank.go | 37 +++++++++ route/group_power_r/group_rank.go | 95 ++++++++++++++++++++++++ route/router.go | 1 + 5 files changed, 169 insertions(+) create mode 100644 cv/group_power_cv/group_rank.go create mode 100644 domain/model/groupPower_m/group_rank.go create mode 100644 route/group_power_r/group_rank.go diff --git a/cv/group_power_cv/group_rank.go b/cv/group_power_cv/group_rank.go new file mode 100644 index 0000000..a9c5e32 --- /dev/null +++ b/cv/group_power_cv/group_rank.go @@ -0,0 +1,22 @@ +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"` +} diff --git a/domain/model/groupPower_m/group_grade.go b/domain/model/groupPower_m/group_grade.go index a921b39..7558f5b 100644 --- a/domain/model/groupPower_m/group_grade.go +++ b/domain/model/groupPower_m/group_grade.go @@ -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 +} diff --git a/domain/model/groupPower_m/group_rank.go b/domain/model/groupPower_m/group_rank.go new file mode 100644 index 0000000..c7d2be7 --- /dev/null +++ b/domain/model/groupPower_m/group_rank.go @@ -0,0 +1,37 @@ +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 +} diff --git a/route/group_power_r/group_rank.go b/route/group_power_r/group_rank.go new file mode 100644 index 0000000..ee704aa --- /dev/null +++ b/route/group_power_r/group_rank.go @@ -0,0 +1,95 @@ +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 +} diff --git a/route/router.go b/route/router.go index ab74633..89bcc8c 100644 --- a/route/router.go +++ b/route/router.go @@ -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 } -- 2.22.0