group_rank.go 3.01 KB
Newer Older
hujiebin's avatar
hujiebin committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 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 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 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
}