From f2613d0ebac60c0f9b234ebd3a8188c62426a249 Mon Sep 17 00:00:00 2001 From: hujiebin Date: Fri, 2 Jun 2023 15:06:09 +0800 Subject: [PATCH] CP,Top3 --- cv/cp_cv/rank.go | 5 ++++ route/cp_r/rank.go | 71 ++++++++++++++++++++++++++++++++++++++++++++++ route/router.go | 1 + 3 files changed, 77 insertions(+) diff --git a/cv/cp_cv/rank.go b/cv/cp_cv/rank.go index 99de932..0d38cf0 100644 --- a/cv/cp_cv/rank.go +++ b/cv/cp_cv/rank.go @@ -30,3 +30,8 @@ type CvCpAnniversary struct { Date string `json:"date"` // 纪念日日期 IsRemind bool `json:"isRemind"` // 是否提醒 } + +type CpTops struct { + Day []CvCp `json:"day"` + Week []CvCp `json:"week"` +} diff --git a/route/cp_r/rank.go b/route/cp_r/rank.go index ec92de7..0b1911d 100644 --- a/route/cp_r/rank.go +++ b/route/cp_r/rank.go @@ -91,6 +91,77 @@ func CpRank(c *gin.Context) (*mycontext.MyContext, error) { return myCtx, nil } +// @Tags CP v2 +// @Summary CP,Top3 +// @Param token header string true "token" +// @Param nonce header string true "随机数字" +// @Success 200 {object} cp_cv.CpTops +// @Router /v2/cp/top3 [get] +func CpTop3(c *gin.Context) (*mycontext.MyContext, error) { + myCtx := mycontext.CreateMyContext(c.Keys) + userId, err := req.GetUserId(c) + if err != nil { + return myCtx, err + } + var response = cp_cv.CpTops{ + Day: make([]cp_cv.CvCp, 0), + Week: make([]cp_cv.CvCp, 0), + } + queryTypes := []string{"day", "week"} + for _, queryType := range queryTypes { + var beginDate, endDate string + switch queryType { + case "day": + beginDate, endDate = time.Now().Format("2006-01-02"), time.Now().Format("2006-01-02") + case "week": + beginDate = now.BeginningOfWeek().Format("2006-01-02") + endDate = now.EndOfWeek().Format("2006-01-02") + } + offset, limit := 0, 3 + model := domain.CreateModelContext(myCtx) + ranks := cp_m.PageCpDayRank(model, beginDate, endDate, offset, limit) + var userIds []mysql.ID + var cpIds []mysql.ID + for _, rank := range ranks { + userIds = append(userIds, rank.UserId1) + userIds = append(userIds, rank.UserId2) + cpIds = append(cpIds, rank.CpId) + } + userBase, err := user_cv.GetUserBaseMap(userIds, userId) + if err != nil { + return myCtx, err + } + cpLevels := cp_m.MGetCpLevel(model, cpIds) + for i, rank := range ranks { + if queryType == "day" { + response.Day = append(response.Day, cp_cv.CvCp{ + CpId: rank.CpId, + User1: userBase[rank.UserId1], + User2: userBase[rank.UserId2], + Score: rank.Score, + Ranking: fmt.Sprintf("%d", i+1+offset), + CpLevel: cp_cv.CvCpLevel{ + Level: cpLevels[rank.CpId].Level, + }, + }) + } else { + response.Week = append(response.Week, cp_cv.CvCp{ + CpId: rank.CpId, + User1: userBase[rank.UserId1], + User2: userBase[rank.UserId2], + Score: rank.Score, + Ranking: fmt.Sprintf("%d", i+1+offset), + CpLevel: cp_cv.CvCpLevel{ + Level: cpLevels[rank.CpId].Level, + }, + }) + } + } + } + resp.ResponseOk(c, response) + return myCtx, nil +} + // @Tags CP v2 // @Summary 我的cp // @Param token header string true "token" diff --git a/route/router.go b/route/router.go index 9659612..d37c5fd 100755 --- a/route/router.go +++ b/route/router.go @@ -36,6 +36,7 @@ func InitRouter() *gin.Engine { cp.GET("/rank/:queryType", wrapper(cp_r.CpRank)) cp.GET("/my/:queryType", wrapper(cp_r.CpMy)) cp.GET("/achievement", wrapper(cp_r.CpAchievement)) + cp.GET("/top3", wrapper(cp_r.CpTop3)) cp.POST("/anniversary", wrapper(cp_r.PostAnniversary)) cp.PUT("/anniversary/:id", wrapper(cp_r.PutAnniversary)) -- 2.22.0