rank.go 2.38 KB
Newer Older
hujiebin's avatar
hujiebin committed
1 2 3 4 5
package cp_r

import (
	"git.hilo.cn/hilo-common/domain"
	"git.hilo.cn/hilo-common/mycontext"
hujiebin's avatar
hujiebin committed
6
	"git.hilo.cn/hilo-common/resource/mysql"
hujiebin's avatar
hujiebin committed
7 8
	"github.com/gin-gonic/gin"
	"github.com/jinzhu/now"
hujiebin's avatar
hujiebin committed
9 10
	"hilo-user/cv/cp_cv"
	"hilo-user/cv/user_cv"
hujiebin's avatar
hujiebin committed
11 12 13 14 15 16 17 18 19 20 21 22 23 24
	"hilo-user/domain/model/cp_m"
	"hilo-user/myerr/bizerr"
	"hilo-user/req"
	"hilo-user/resp"
	"time"
)

// @Tags CP v2
// @Summary 排行榜
// @Param token header string true "token"
// @Param nonce header string true "随机数字"
// @Param pageIndex query int true "偏移值 默认:1" default(1)
// @Param pageSize query int true "请求数量 默认:10" default(10)
// @Param queryType path string true "类型:day/week/month"
hujiebin's avatar
hujiebin committed
25
// @Success 200 {object} []cp_cv.CvCp
hujiebin's avatar
hujiebin committed
26 27 28
// @Router /v2/cp/rank/{queryType} [get]
func CpRank(c *gin.Context) (*mycontext.MyContext, error) {
	myCtx := mycontext.CreateMyContext(c.Keys)
hujiebin's avatar
hujiebin committed
29 30 31 32
	userId, err := req.GetUserId(c)
	if err != nil {
		return myCtx, err
	}
hujiebin's avatar
hujiebin committed
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
	pageReq := new(req.PageReqBase)
	if err := c.ShouldBindQuery(pageReq); err != nil {
		return myCtx, err
	}
	if pageReq.PageIndex == 0 {
		pageReq.PageIndex = 1
	}
	queryType := c.Param("queryType")
	if queryType != "day" && queryType != "week" && queryType != "month" {
		return myCtx, bizerr.InvalidParameter
	}
	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")
	case "month":
		beginDate = now.BeginningOfMonth().Format("2006-01-02")
		endDate = now.EndOfMonth().Format("2006-01-02")
	}
	offset, limit := (pageReq.PageIndex-1)*pageReq.PageSize, pageReq.PageSize
	model := domain.CreateModelContext(myCtx)
	ranks := cp_m.PageCpDayRank(model, beginDate, endDate, offset, limit)
hujiebin's avatar
hujiebin committed
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
	var response []cp_cv.CvCp
	var userIds []mysql.ID
	for _, rank := range ranks {
		userIds = append(userIds, rank.UserId1)
		userIds = append(userIds, rank.UserId2)
	}
	userBase, err := user_cv.GetUserBaseMap(userIds, userId)
	if err != nil {
		return myCtx, err
	}
	for i, rank := range ranks {
		response = append(response, cp_cv.CvCp{
			CpId:    rank.CpId,
			User1:   userBase[rank.UserId1],
			User2:   userBase[rank.UserId2],
			Score:   rank.Score,
			Ranking: i + 1 + offset,
		})
	}
	resp.ResponsePageBaseOk(c, response, pageReq.PageIndex+1, len(ranks) < pageReq.PageSize)
hujiebin's avatar
hujiebin committed
78 79
	return myCtx, nil
}