group_rank.go 9.66 KB
Newer Older
hujiebin's avatar
hujiebin committed
1 2 3 4 5 6 7 8 9 10 11
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"
hujiebin's avatar
hujiebin committed
12
	"hilo-group/cv/user_cv"
hujiebin's avatar
hujiebin committed
13
	"hilo-group/domain/model/groupPower_m"
hujiebin's avatar
hujiebin committed
14
	"hilo-group/domain/model/user_m"
hujiebin's avatar
hujiebin committed
15 16 17
	"hilo-group/myerr/bizerr"
	"hilo-group/req"
	"hilo-group/resp"
hujiebin's avatar
hujiebin committed
18
	"strconv"
hujiebin's avatar
hujiebin committed
19 20 21 22 23 24 25 26
	"time"
)

// @Tags 国家势力
// @Summary 家族榜单
// @Param token header string true "token"
// @Param nonce header string true "随机数字"
// @Param period path string true "榜单周期 day:日 week:周 month:月"
hujiebin's avatar
hujiebin committed
27
// @Success 200 {object} []group_power_cv.CvGroupPowerRankTop3
hujiebin's avatar
hujiebin committed
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
// @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
	}
	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
	}
hujiebin's avatar
hujiebin committed
69 70 71 72
	groupPowers, err := groupPower_m.MGetGroupPowerInfoMap(model, ids)
	if err != nil {
		return myContext, err
	}
hujiebin's avatar
hujiebin committed
73
	for _, v := range rank {
hujiebin's avatar
hujiebin committed
74 75 76
		if myGroupPower != nil && v.GroupPowerId == myGroupPower.GroupPowerId && myGroupPower.Role == groupPower_e.GroupPowerUserRoleMgr {
			response.MyGroupPower = &group_power_cv.CvGroupPowerRankData{
				CvGroupPowerBase: group_power_cv.CvGroupPowerBase{
77 78 79 80
					Id:        myGroupPower.GroupPowerId,
					Icon:      groupPowers[myGroupPower.GroupPowerId].Icon,
					Name:      groupPowers[myGroupPower.GroupPowerId].Name,
					Nameplate: groupPowers[myGroupPower.GroupPowerId].Nameplate,
hujiebin's avatar
hujiebin committed
81 82 83 84 85 86 87 88
				},
				CvGroupPowerGrade: group_power_cv.CvGroupPowerGrade{
					Grade: grades[myGroupPower.GroupPowerId].Grade,
					Exp:   v.Exp,
				},
				Rank: v.Rank,
			}
		}
hujiebin's avatar
hujiebin committed
89
		response.Items = append(response.Items, group_power_cv.CvGroupPowerRankData{
hujiebin's avatar
hujiebin committed
90
			CvGroupPowerBase: group_power_cv.CvGroupPowerBase{
91 92 93 94
				Id:        v.GroupPowerId,
				Icon:      groupPowers[v.GroupPowerId].Icon,
				Name:      groupPowers[v.GroupPowerId].Name,
				Nameplate: groupPowers[v.GroupPowerId].Nameplate,
hujiebin's avatar
hujiebin committed
95 96 97 98 99 100
			},
			CvGroupPowerGrade: group_power_cv.CvGroupPowerGrade{
				Grade: grades[v.GroupPowerId].Grade,
				Exp:   v.Exp,
			},
			Rank: v.Rank,
hujiebin's avatar
hujiebin committed
101 102
		})
	}
hujiebin's avatar
hujiebin committed
103 104 105
	if response.MyGroupPower != nil && response.MyGroupPower.Rank == 0 {
		response.MyGroupPower.Rank = 31 // 客户端统一显示30+
	}
hujiebin's avatar
hujiebin committed
106 107 108
	resp.ResponseOk(c, response)
	return myContext, nil
}
hujiebin's avatar
hujiebin committed
109

hujiebin's avatar
hujiebin committed
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 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
// @Tags 国家势力
// @Summary 家族日周月榜单top3
// @Param token header string true "token"
// @Param nonce header string true "随机数字"
// @Success 200 {object} []group_power_cv.CvGroupPowerRankTop3
// @Router /v1/groupPower/rankTop [get]
func GroupPowerRankTop(c *gin.Context) (*mycontext.MyContext, error) {
	myContext := mycontext.CreateMyContext(c.Keys)
	periods := []string{"day", "week", "month"}
	var response []group_power_cv.CvGroupPowerRankTop3
	for _, period := range periods {
		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, 3)
		if err != nil {
			return myContext, err
		}
		var ids []mysql.ID
		for _, g := range rank {
			ids = append(ids, g.GroupPowerId)
		}

		grades, err := groupPower_m.MGetGroupPowerGrade(model, ids)
		if err != nil {
			return myContext, err
		}
		groupPowers, err := groupPower_m.MGetGroupPowerInfoMap(model, ids)
		if err != nil {
			return myContext, err
		}
		data := group_power_cv.CvGroupPowerRankTop3{Period: period}
		for _, v := range rank {
			data.Items = append(data.Items, group_power_cv.CvGroupPowerRankData{
				CvGroupPowerBase: group_power_cv.CvGroupPowerBase{
					Id:        v.GroupPowerId,
					Icon:      groupPowers[v.GroupPowerId].Icon,
					Name:      groupPowers[v.GroupPowerId].Name,
					Nameplate: groupPowers[v.GroupPowerId].Nameplate,
				},
				CvGroupPowerGrade: group_power_cv.CvGroupPowerGrade{
					Grade: grades[v.GroupPowerId].Grade,
					Exp:   v.Exp,
				},
				Rank: v.Rank,
			})
		}
		response = append(response, data)
	}
	resp.ResponseOk(c, response)
	return myContext, nil
}

hujiebin's avatar
hujiebin committed
170 171 172
type GroupPowerStarReq struct {
	GroupPowerId mysql.ID                        `form:"groupPowerId" binding:"required"`
	Type         groupPower_e.GroupPowerStarType `form:"type" binding:"required"`
hujiebin's avatar
hujiebin committed
173 174
	PageSize     int                             `form:"pageSize"`
	PageIndex    int                             `form:"pageIndex"`
hujiebin's avatar
hujiebin committed
175 176 177 178 179 180
}

// @Tags 国家势力
// @Summary 家族之星
// @Param token header string true "token"
// @Param nonce header string true "随机数字"
hujiebin's avatar
hujiebin committed
181
// @Param groupPowerId query int true "家族id"
hujiebin's avatar
hujiebin committed
182
// @Param type query integer true "类型 1:送礼 2:活跃 3:收礼物"
hujiebin's avatar
hujiebin committed
183
// @Param pageSize query int false "分页大小 默认:30" default(30)
hujiebin's avatar
hujiebin committed
184 185 186 187 188 189 190 191 192 193 194 195
// @Param pageIndex query int false "第几个分页,从1开始 默认:1" default(1)
// @Success 200 {object} []group_power_cv.CvGroupPowerStarData
// @Router /v1/groupPower/star [get]
func GroupPowerStar(c *gin.Context) (*mycontext.MyContext, error) {
	myContext := mycontext.CreateMyContext(c.Keys)
	param := new(GroupPowerStarReq)
	if err := c.ShouldBindQuery(param); err != nil {
		return myContext, err
	}
	if param.PageIndex <= 0 {
		param.PageIndex = 1
	}
hujiebin's avatar
hujiebin committed
196 197 198
	if param.PageSize <= 0 {
		param.PageSize = 30
	}
hujiebin's avatar
hujiebin committed
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
	var model = domain.CreateModelContext(myContext)
	offset, limit := (param.PageIndex-1)*param.PageSize, param.PageSize
	rank, err := groupPower_m.GetGroupPowerMonthStarRank(model, param.GroupPowerId, param.Type, offset, limit)
	if err != nil {
		return myContext, err
	}
	var response []group_power_cv.CvGroupPowerStarData
	var userIds []mysql.ID
	for _, row := range rank {
		userIds = append(userIds, row.UserId)
	}
	users, err := user_m.GetUserMapByIds(model, userIds)
	for _, row := range rank {
		user := users[row.UserId]
		response = append(response, group_power_cv.CvGroupPowerStarData{
hujiebin's avatar
hujiebin committed
214
			User: user_cv.CvUserTiny{
hujiebin's avatar
hujiebin committed
215 216 217 218 219
				Id:         user.ID,
				ExternalId: user.ExternalId,
				Code:       user.Code,
				Nick:       user.Nick,
				Avatar:     user.Avatar,
hujiebin's avatar
hujiebin committed
220 221 222 223
			},
			Score: row.Score,
		})
	}
hujiebin's avatar
hujiebin committed
224
	resp.ResponsePageBaseOk(c, response, 0, false)
hujiebin's avatar
hujiebin committed
225 226
	return myContext, nil
}
hujiebin's avatar
hujiebin committed
227 228 229 230 231 232 233

// @Tags 国家势力
// @Summary 家族等级页
// @Param token header string true "token"
// @Param nonce header string true "随机数字"
// @Param groupPowerId query int true "家族id"
// @Success 200 {object} group_power_cv.CvGroupPowerGradeDetail
hujiebin's avatar
hujiebin committed
234
// @Router /v1/h5/groupPower/grade/detail [get]
hujiebin's avatar
hujiebin committed
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
func GroupPowerGradeDetail(c *gin.Context) (*mycontext.MyContext, error) {
	myContext := mycontext.CreateMyContext(c.Keys)
	userId, err := req.GetUserId(c)
	if err != nil {
		return myContext, err
	}
	groupPowerId, err := strconv.ParseUint(c.Query("groupPowerId"), 10, 16)
	if err != nil || groupPowerId <= 0 {
		return myContext, bizerr.InvalidParameter
	}
	var model = domain.CreateModelContext(myContext)
	groupPowerInfo, err := groupPower_m.MGetGroupPowerInfoMap(model, []mysql.ID{groupPowerId})
	if err != nil {
		return myContext, err
	}
	gradeM, err := groupPower_m.MGetGroupPowerGrade(model, []mysql.ID{groupPowerId})
	if err != nil {
		return myContext, err
	}
	groupPower, ok := groupPowerInfo[groupPowerId]
	if !ok {
		return myContext, bizerr.GroupNotFound
	}
	grade := gradeM[groupPowerId]
	nextExp := mysql.Num(0)
	if grade.Grade != groupPower_e.GroupPowerGradeMax {
hujiebin's avatar
hujiebin committed
261
		nextExp = groupPower_e.GroupPowerGradeExp[grade.Grade+1]
hujiebin's avatar
hujiebin committed
262 263 264 265 266
	}
	expireAt := ""
	if grade.ExpireAt.After(time.Now()) {
		expireAt = grade.ExpireAt.Format("2006-01-02")
	}
hujiebin's avatar
hujiebin committed
267
	isMember := false
hujiebin's avatar
hujiebin committed
268 269 270
	if exits, _, err := groupPower_m.CheckGroupPowerUser(model, userId); err != nil {
		return myContext, err
	} else if exits {
hujiebin's avatar
hujiebin committed
271
		isMember = true
hujiebin's avatar
hujiebin committed
272 273
	}
	response := group_power_cv.CvGroupPowerGradeDetail{
hujiebin's avatar
hujiebin committed
274
		GroupPowerBase: group_power_cv.CvGroupPowerBase{
275 276 277 278
			Id:        groupPower.ID,
			Icon:      groupPower.Icon,
			Name:      groupPower.Name,
			Nameplate: groupPower.Nameplate,
hujiebin's avatar
hujiebin committed
279 280 281 282 283 284 285 286 287
		},
		GroupPowerGrade: group_power_cv.CvGroupPowerGrade{
			Grade:    grade.Grade,
			Exp:      grade.Exp,
			NextExp:  nextExp,
			ExpireAt: expireAt,
		},
		ResGradeList:  group_power_cv.CvResGradeList,
		PrivilegeList: group_power_cv.GroupPowerGradePrivilegeNum[grade.Grade],
hujiebin's avatar
hujiebin committed
288
		IsMember:      isMember,
hujiebin's avatar
hujiebin committed
289 290 291 292
	}
	resp.ResponseOk(c, response)
	return myContext, nil
}