rank.go 5.86 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
	"hilo-user/_const/enum/cp_e"
hujiebin's avatar
hujiebin committed
10 11
	"hilo-user/cv/cp_cv"
	"hilo-user/cv/user_cv"
hujiebin's avatar
hujiebin committed
12
	"hilo-user/domain/model/cp_m"
hujiebin's avatar
hujiebin committed
13
	"hilo-user/domain/model/user_m"
hujiebin's avatar
hujiebin committed
14 15 16 17 18 19 20 21 22 23 24 25 26
	"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
27
// @Success 200 {object} []cp_cv.CvCp
hujiebin's avatar
hujiebin committed
28
// @Router /v2/cp/rank/:queryType [get]
hujiebin's avatar
hujiebin committed
29 30
func CpRank(c *gin.Context) (*mycontext.MyContext, error) {
	myCtx := mycontext.CreateMyContext(c.Keys)
hujiebin's avatar
hujiebin committed
31 32 33 34
	userId, err := req.GetUserId(c)
	if err != nil {
		return myCtx, err
	}
hujiebin's avatar
hujiebin committed
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
	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
60 61 62 63 64 65 66 67 68 69
	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
	}
hujiebin's avatar
hujiebin committed
70
	for _, rank := range ranks {
hujiebin's avatar
hujiebin committed
71
		response = append(response, cp_cv.CvCp{
hujiebin's avatar
hujiebin committed
72 73 74 75 76
			CpId:  rank.CpId,
			User1: userBase[rank.UserId1],
			User2: userBase[rank.UserId2],
			Score: rank.Score,
			//Ranking: i + 1 + offset,
hujiebin's avatar
hujiebin committed
77 78
		})
	}
hujiebin's avatar
hujiebin committed
79
	resp.ResponsePageBaseOk(c, response, pageReq.PageIndex+1, len(ranks) >= pageReq.PageSize)
hujiebin's avatar
hujiebin committed
80 81
	return myCtx, nil
}
hujiebin's avatar
hujiebin committed
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147

// @Tags CP v2
// @Summary 我的cp
// @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"
// @Success 200 {object} cp_cv.CvCp
// @Router /v2/cp/my/:queryType [get]
func CpMy(c *gin.Context) (*mycontext.MyContext, error) {
	myCtx := mycontext.CreateMyContext(c.Keys)
	userId, err := req.GetUserId(c)
	if err != nil {
		return myCtx, err
	}
	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")
	}
	model := domain.CreateModelContext(myCtx)
	relation, exits := cp_m.GetCpRelation(model, userId)
	var userIds []mysql.ID
	var scores uint32
	if exits {
		// 保证一下自己是userId1
		userIds = append(userIds, userId)
		if relation.UserId1 == userId {
			userIds = append(userIds, relation.UserId2)
		} else {
			userIds = append(userIds, relation.UserId1)
		}
		rank := cp_m.GetCpDayRank(model, beginDate, endDate, relation.ID)
		scores = rank.Score
	} else {
		userIds = append(userIds, userId)
		relation.UserId1 = userId
	}
	userBases, err := user_cv.GetUserBaseMap(userIds, userId)
	if err != nil {
		return myCtx, err
	}
	response := cp_cv.CvCp{
		CpId:  relation.ID,
		Score: scores,
	}
	if relation.UserId1 > 0 {
		response.User1 = userBases[relation.UserId1]
	}
	if relation.UserId2 > 0 {
		response.User2 = userBases[relation.UserId2]
	}
	resp.ResponseOk(c, response)
	return myCtx, nil
}
hujiebin's avatar
hujiebin committed
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192

// @Tags CP v2
// @Summary 成就页
// @Param token header string true "token"
// @Param nonce header string true "随机数字"
// @Success 200 {object} []cp_cv.CvCpAchievement
// @Router /v2/cp/achievement [get]
func CpAchievement(c *gin.Context) (*mycontext.MyContext, error) {
	myCtx := mycontext.CreateMyContext(c.Keys)
	userId, err := req.GetUserId(c)
	if err != nil {
		return myCtx, err
	}
	model := domain.CreateModelContext(myCtx)
	relation, exits := cp_m.GetCpRelation(model, userId)
	var response = make([]cp_cv.CvCpAchievement, 0)
	TypeDescMap := map[cp_e.CpAchievement]string{ // todo 翻译
		cp_e.CpAchievementLevel:     "等级分值最高",
		cp_e.CpAchievementVisitors:  "空间访问量最高",
		cp_e.CpAchievementMonthRank: "月榜最高",
		cp_e.CpAchievementWeekRank:  "周榜最高",
		cp_e.CpAchievementDayRank:   "天榜最高",
	}
	if exits {
		users, err := user_m.GetUserMapByIds(model, []mysql.ID{relation.UserId1, relation.UserId2})
		if err != nil {
			return myCtx, err
		}
		achievements := cp_m.GetCpAchievements(model, relation.ID)
		for _, a := range achievements {
			response = append(response, cp_cv.CvCpAchievement{
				CpId:     a.CpId,
				User1:    user_cv.UserToTiny(users[a.UserId1]),
				User2:    user_cv.UserToTiny(users[a.UserId2]),
				Type:     a.Type,
				TypeDesc: TypeDescMap[a.Type],
				Score:    a.Score,
				TimeUnix: a.UpdatedTime.Unix(),
			})
		}
	} else {
	}
	resp.ResponseOk(c, response)
	return nil, nil
}