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

import (
hujiebin's avatar
hujiebin committed
4
	"fmt"
hujiebin's avatar
hujiebin committed
5 6
	"git.hilo.cn/hilo-common/domain"
	"git.hilo.cn/hilo-common/mycontext"
hujiebin's avatar
hujiebin committed
7
	"git.hilo.cn/hilo-common/resource/mysql"
hujiebin's avatar
hujiebin committed
8 9
	"github.com/gin-gonic/gin"
	"github.com/jinzhu/now"
hujiebin's avatar
hujiebin committed
10
	"hilo-user/_const/enum/cp_e"
hujiebin's avatar
hujiebin committed
11 12
	"hilo-user/cv/cp_cv"
	"hilo-user/cv/user_cv"
hujiebin's avatar
hujiebin committed
13
	"hilo-user/domain/model/cp_m"
hujiebin's avatar
hujiebin committed
14
	"hilo-user/domain/model/user_m"
hujiebin's avatar
hujiebin committed
15 16 17 18 19 20 21 22 23 24 25 26 27
	"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
28
// @Success 200 {object} []cp_cv.CvCpRank
hujiebin's avatar
hujiebin committed
29
// @Router /v2/cp/rank/{queryType} [get]
hujiebin's avatar
hujiebin committed
30 31
func CpRank(c *gin.Context) (*mycontext.MyContext, error) {
	myCtx := mycontext.CreateMyContext(c.Keys)
hujiebin's avatar
hujiebin committed
32 33 34 35
	userId, err := req.GetUserId(c)
	if err != nil {
		return myCtx, err
	}
hujiebin's avatar
hujiebin committed
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
	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
61
	var response []cp_cv.CvCpRank
hujiebin's avatar
hujiebin committed
62
	var userIds []mysql.ID
63
	var cpIds []mysql.ID
hujiebin's avatar
hujiebin committed
64 65 66
	for _, rank := range ranks {
		userIds = append(userIds, rank.UserId1)
		userIds = append(userIds, rank.UserId2)
67
		cpIds = append(cpIds, rank.CpId)
hujiebin's avatar
hujiebin committed
68 69 70 71 72
	}
	userBase, err := user_cv.GetUserBaseMap(userIds, userId)
	if err != nil {
		return myCtx, err
	}
73
	cpLevels := cp_m.MGetCpLevel(model, cpIds)
hujiebin's avatar
hujiebin committed
74
	for i, rank := range ranks {
hujiebin's avatar
hujiebin committed
75
		response = append(response, cp_cv.CvCpRank{
hujiebin's avatar
hujiebin committed
76 77 78 79 80
			CpId:    rank.CpId,
			User1:   userBase[rank.UserId1],
			User2:   userBase[rank.UserId2],
			Score:   rank.Score,
			Ranking: fmt.Sprintf("%d", i+1+offset),
81 82 83
			CpLevel: cp_cv.CvCpLevel{
				Level: cpLevels[rank.CpId].Level,
				//Points:       cp_e.CpLevelPoints[cpLevel] + curPoints,
hujiebin's avatar
hujiebin committed
84
				//EndPoints:   nextPoints,
85 86 87
				//RemainPoints: remainPoints,
				//ExpireAtUnix: expireAtUnix,
			},
hujiebin's avatar
hujiebin committed
88 89
		})
	}
hujiebin's avatar
hujiebin committed
90
	resp.ResponsePageBaseOk(c, response, pageReq.PageIndex+1, len(ranks) >= pageReq.PageSize)
hujiebin's avatar
hujiebin committed
91 92
	return myCtx, nil
}
hujiebin's avatar
hujiebin committed
93

hujiebin's avatar
hujiebin committed
94 95 96 97 98 99 100 101 102 103 104 105 106
// @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{
hujiebin's avatar
hujiebin committed
107 108
		Day:  make([]cp_cv.CvCpRank, 0),
		Week: make([]cp_cv.CvCpRank, 0),
hujiebin's avatar
hujiebin committed
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
	}
	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" {
hujiebin's avatar
hujiebin committed
137
				response.Day = append(response.Day, cp_cv.CvCpRank{
hujiebin's avatar
hujiebin committed
138 139 140 141 142 143 144 145 146 147
					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 {
hujiebin's avatar
hujiebin committed
148
				response.Week = append(response.Week, cp_cv.CvCpRank{
hujiebin's avatar
hujiebin committed
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
					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
}

hujiebin's avatar
hujiebin committed
165 166 167 168 169
// @Tags CP v2
// @Summary 我的cp
// @Param token header string true "token"
// @Param nonce header string true "随机数字"
// @Param queryType path string true "类型:day/week/month"
hujiebin's avatar
hujiebin committed
170
// @Success 200 {object} cp_cv.CvCpRank
hujiebin's avatar
hujiebin committed
171
// @Router /v2/cp/my/{queryType} [get]
hujiebin's avatar
hujiebin committed
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
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)
		}
hujiebin's avatar
hujiebin committed
205
		rank := cp_m.GetCpDayRank(model, beginDate, endDate, relation.Id)
hujiebin's avatar
hujiebin committed
206 207 208 209 210 211 212 213 214
		scores = rank.Score
	} else {
		userIds = append(userIds, userId)
		relation.UserId1 = userId
	}
	userBases, err := user_cv.GetUserBaseMap(userIds, userId)
	if err != nil {
		return myCtx, err
	}
hujiebin's avatar
hujiebin committed
215
	response := cp_cv.CvCpRank{
hujiebin's avatar
hujiebin committed
216
		CpId:  relation.Id,
hujiebin's avatar
hujiebin committed
217 218 219 220 221 222 223
		Score: scores,
	}
	if relation.UserId1 > 0 {
		response.User1 = userBases[relation.UserId1]
	}
	if relation.UserId2 > 0 {
		response.User2 = userBases[relation.UserId2]
hujiebin's avatar
hujiebin committed
224
		response.Ranking = "30+"
hujiebin's avatar
hujiebin committed
225 226
		ranks := cp_m.PageCpDayRank(model, beginDate, endDate, 0, 30)
		for i, rank := range ranks {
hujiebin's avatar
hujiebin committed
227
			if relation.Id == rank.CpId {
hujiebin's avatar
hujiebin committed
228 229 230 231
				response.Ranking = fmt.Sprintf("%d", i+1)
				break
			}
		}
hujiebin's avatar
hujiebin committed
232 233 234 235
	}
	resp.ResponseOk(c, response)
	return myCtx, nil
}
hujiebin's avatar
hujiebin committed
236 237 238 239 240

// @Tags CP v2
// @Summary 成就页
// @Param token header string true "token"
// @Param nonce header string true "随机数字"
hujiebin's avatar
hujiebin committed
241 242
// @Param pageIndex query int true "偏移值 默认:1" default(1)
// @Param pageSize query int true "请求数量 默认:10" default(10)
hujiebin's avatar
hujiebin committed
243 244 245 246 247
// @Success 200 {object} []cp_cv.CvCpAchievement
// @Router /v2/cp/achievement [get]
func CpAchievement(c *gin.Context) (*mycontext.MyContext, error) {
	myCtx := mycontext.CreateMyContext(c.Keys)
	model := domain.CreateModelContext(myCtx)
hujiebin's avatar
hujiebin committed
248 249 250 251
	_, lang, err := req.GetUserIdLang(c, myCtx)
	if err != nil {
		return myCtx, err
	}
hujiebin's avatar
hujiebin committed
252
	var response = make([]cp_cv.CvCpAchievement, 0)
hujiebin's avatar
hujiebin committed
253 254 255 256 257 258
	TypeDescMap := map[cp_e.CpAchievement]uint{
		cp_e.CpAchievementLevel:     280, //"等级分值最高",
		cp_e.CpAchievementVisitors:  281, //"空间访问量最高",
		cp_e.CpAchievementMonthRank: 282, //"月榜最高",
		cp_e.CpAchievementWeekRank:  283, //"周榜最高",
		cp_e.CpAchievementDayRank:   284, //"天榜最高",
hujiebin's avatar
hujiebin committed
259
	}
hujiebin's avatar
hujiebin committed
260 261
	achievements := cp_m.GetCpAchievements(model)
	var userIds []uint64
hujiebin's avatar
hujiebin committed
262
	var cpIds []uint64
hujiebin's avatar
hujiebin committed
263 264 265
	for _, v := range achievements {
		userIds = append(userIds, v.UserId1)
		userIds = append(userIds, v.UserId2)
hujiebin's avatar
hujiebin committed
266
		cpIds = append(cpIds, v.CpId)
hujiebin's avatar
hujiebin committed
267 268 269 270 271
	}
	users, err := user_m.GetUserMapByIds(model, userIds)
	if err != nil {
		return myCtx, err
	}
hujiebin's avatar
hujiebin committed
272
	cpLevels := cp_m.MGetCpLevel(model, cpIds)
hujiebin's avatar
hujiebin committed
273 274 275 276 277
	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]),
hujiebin's avatar
hujiebin committed
278
			CpLevel:  cpLevels[a.CpId].Level,
hujiebin's avatar
hujiebin committed
279
			Type:     a.Type,
hujiebin's avatar
hujiebin committed
280
			TypeDesc: cp_m.GetTranslate(TypeDescMap[a.Type], lang),
hujiebin's avatar
hujiebin committed
281 282 283
			Score:    a.Score,
			TimeUnix: a.UpdatedTime.Unix(),
		})
hujiebin's avatar
hujiebin committed
284
	}
hujiebin's avatar
hujiebin committed
285
	resp.ResponsePageBaseOk(c, response, 0, false)
hujiebin's avatar
hujiebin committed
286
	return myCtx, nil
hujiebin's avatar
hujiebin committed
287
}