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

// @Tags 国家势力
// @Summary 家族榜单
// @Param token header string true "token"
// @Param nonce header string true "随机数字"
// @Param period path string true "榜单周期 day:日 week:周 month:月"
29
// @Success 200 {object} []group_power_cv.CvGroupPowerRank
hujiebin's avatar
hujiebin committed
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
// @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)
51
	response, err := group_power_s.GetGroupPowerRankResp(model, beginDate, endDate, userId, 0, 0)
hujiebin's avatar
hujiebin committed
52 53 54
	if err != nil {
		return myContext, err
	}
chenweijian's avatar
chenweijian committed
55 56 57 58 59 60 61 62 63 64 65
	resp.ResponseOk(c, response)
	return myContext, nil
}

// @Tags 国家势力
// @Summary 家族榜单-活动
// @Success 200 {object} group_power_cv.CvGroupPowerRankMontAct
// @Router /v1/h5/groupPower/rank/month [get]
func GroupPowerMonthRankAct(c *gin.Context) (*mycontext.MyContext, error) {
	myContext := mycontext.CreateMyContext(c.Keys)
	userId, err := req.GetUserId(c)
hujiebin's avatar
hujiebin committed
66 67 68
	if err != nil {
		return myContext, err
	}
chenweijian's avatar
chenweijian committed
69
	var model = domain.CreateModelContext(myContext)
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
	// 国家区域
	var area int
	// 我的家族
	myGroupPower, err := groupPower_m.GetGroupPowerUserOrNil(model, userId)
	if err != nil {
		return myContext, err
	}
	if myGroupPower != nil && myGroupPower.GroupPowerId > 0 {
		//获取势力主
		mgrUserId, err := groupPower_m.GetGroupPowerMgr(model, myGroupPower.GroupPowerId)
		if err != nil {
			return myContext, err
		}
		_, area, err = user_m.GetUserCountryArea(model, mgrUserId)
		if err != nil {
			model.Log.Errorf("GroupPowerMonthRankAct 获取国家资源错误 userId:%d, err:%v", userId, err)
			return myContext, err
		}
	}
	if area == 0 {
		_, area, err = user_m.GetUserCountryArea(model, userId)
		if err != nil {
			model.Log.Errorf("GroupPowerMonthRankAct 获取国家资源错误 userId:%d, err:%v", userId, err)
			return myContext, err
		}
	}
chenweijian's avatar
chenweijian committed
96 97
	// 这个月
	beginDate, endDate := now.BeginningOfMonth().Format(utils.DATE_FORMAT), now.EndOfMonth().Format(utils.DATE_FORMAT)
98
	response, err := GroupPowerMonthRankActByMonth(model, userId, beginDate, endDate, time.Now().Format(utils.COMPACT_MONTH_FORMAT), area)
hujiebin's avatar
hujiebin committed
99 100 101
	if err != nil {
		return myContext, err
	}
chenweijian's avatar
chenweijian committed
102 103 104 105
	// 上月
	lastMonthStart := utils.GetLastMonthStart(time.Now())
	lastTime := now.New(lastMonthStart)
	beginDateLast, endDateLast := lastTime.BeginningOfMonth().Format(utils.DATE_FORMAT), lastTime.EndOfMonth().Format(utils.DATE_FORMAT)
106
	responseLast, err := GroupPowerMonthRankActByMonth(model, userId, beginDateLast, endDateLast, lastMonthStart.Format(utils.COMPACT_MONTH_FORMAT), area)
hujiebin's avatar
hujiebin committed
107 108 109
	if err != nil {
		return myContext, err
	}
chenweijian's avatar
chenweijian committed
110 111 112 113 114 115 116 117 118 119 120 121

	res := &group_power_cv.CvGroupPowerRankMontAct{
		MyGroupPower:     response.MyGroupPower,
		Items:            response.Items,
		MyGroupPowerLast: responseLast.MyGroupPower,
		ItemsLast:        responseLast.Items,
		LeftSecond:       now.EndOfMonth().Unix() - time.Now().Unix(),
	}
	resp.ResponseOk(c, res)
	return myContext, nil
}

122 123
func GroupPowerMonthRankActByMonth(model *domain.Model, userId uint64, beginDate, endDate, month string, area int) (response group_power_cv.CvGroupPowerRank, err error) {
	response, err = group_power_s.GetGroupPowerRankResp(model, beginDate, endDate, userId, groupPower_e.GroupPowerUserHas, area)
chenweijian's avatar
chenweijian committed
124 125
	if err != nil {
		return response, err
126
	}
chenweijian's avatar
chenweijian committed
127 128 129 130 131 132 133 134 135 136
	if response.MyGroupPower != nil {
		response.MyGroupPower.StarList, err = group_power_s.GetGroupPowerStar(model, response.MyGroupPower.Id, groupPower_e.GroupPowerStarTypeFamous, 0, 10, month)
	}
	if err != nil {
		return response, err
	}
	for i, v := range response.Items {
		response.Items[i].StarList, err = group_power_s.GetGroupPowerStar(model, v.Id, groupPower_e.GroupPowerStarTypeFamous, 0, 10, month)
		if err != nil {
			return response, err
hujiebin's avatar
hujiebin committed
137 138
		}
	}
chenweijian's avatar
chenweijian committed
139
	return response, nil
hujiebin's avatar
hujiebin committed
140 141
}

142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
// @Tags 国家势力
// @Summary 家族日周月榜单top3
// @Param token header string true "token"
// @Param nonce header string true "随机数字"
// @Success 200 {object} map[string][]group_power_cv.CvGroupPowerRankData
// @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 = make(map[string][]group_power_cv.CvGroupPowerRankData)
	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)
163
		rank, err := groupPower_m.GetGroupPowerExpRank(model, beginDate, endDate, 3, 0, 0)
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 193 194 195 196 197 198 199
		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
		}
		for _, v := range rank {
			response[period] = append(response[period], 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,
			})
		}
	}
	resp.ResponseOk(c, response)
	return myContext, nil
}

hujiebin's avatar
hujiebin committed
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
type GroupPowerStarReq struct {
	GroupPowerId mysql.ID                        `form:"groupPowerId" binding:"required"`
	Type         groupPower_e.GroupPowerStarType `form:"type" binding:"required"`
	PageSize     int                             `form:"pageSize"`
	PageIndex    int                             `form:"pageIndex"`
}

// @Tags 国家势力
// @Summary 家族之星
// @Param token header string true "token"
// @Param nonce header string true "随机数字"
// @Param groupPowerId query int true "家族id"
// @Param type query integer true "类型 1:送礼 2:活跃 3:收礼物"
// @Param pageSize query int false "分页大小 默认:30" default(30)
// @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
	}
	if param.PageSize <= 0 {
		param.PageSize = 30
	}
	offset, limit := (param.PageIndex-1)*param.PageSize, param.PageSize
chenweijian's avatar
chenweijian committed
230 231
	var model = domain.CreateModelContext(myContext)
	response, err := group_power_s.GetGroupPowerStar(model, param.GroupPowerId, param.Type, offset, limit, time.Now().Format(utils.COMPACT_MONTH_FORMAT))
hujiebin's avatar
hujiebin committed
232 233 234
	if err != nil {
		return myContext, err
	}
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 261 262 263 264 265 266 267
	resp.ResponsePageBaseOk(c, response, 0, false)
	return myContext, nil
}

// @Tags 国家势力
// @Summary 家族之星
// @Param token header string true "token"
// @Param nonce header string true "随机数字"
// @Param period path string true "周期 day|week|month"
// @Param groupPowerId query int true "家族id"
// @Param type query integer true "类型 1:送礼 2:活跃 3:收礼物"
// @Param pageSize query int false "分页大小 默认:30" default(30)
// @Param pageIndex query int false "第几个分页,从1开始 默认:1" default(1)
// @Success 200 {object} []group_power_cv.CvGroupPowerStarData
// @Router /v1/groupPower/star/{period} [get]
func GroupPowerStarPeriod(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
	}
	if param.PageSize <= 0 {
		param.PageSize = 30
	}
	period := c.Param("period")
	if period != "day" && period != "week" && period != "month" {
		return myContext, bizerr.InvalidParameter
	}
	var model = domain.CreateModelContext(myContext)
	offset, limit := (param.PageIndex-1)*param.PageSize, param.PageSize
268
	rank, err := groupPower_c.GetGroupPowerStarRankPeriod(model, period, param.GroupPowerId, param.Type, offset, limit)
269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
	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]
		score := row.Score
		if param.Type == groupPower_e.GroupPowerStarTypeActive {
			score = score / 60
		}
		if score <= 0 {
			continue
		}
hujiebin's avatar
hujiebin committed
287 288 289 290 291 292 293 294
		response = append(response, group_power_cv.CvGroupPowerStarData{
			User: user_cv.CvUserTiny{
				Id:         user.ID,
				ExternalId: user.ExternalId,
				Code:       user.Code,
				Nick:       user.Nick,
				Avatar:     user.Avatar,
			},
295
			Score: score,
hujiebin's avatar
hujiebin committed
296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332
		})
	}
	resp.ResponsePageBaseOk(c, response, 0, false)
	return myContext, nil
}

// @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
// @Router /v1/h5/groupPower/grade/detail [get]
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]
333
	nextExp := groupPower_e.GroupPowerGradeExp[groupPower_e.GroupPowerGradeMax]
hujiebin's avatar
hujiebin committed
334 335 336 337 338
	if grade.Grade != groupPower_e.GroupPowerGradeMax {
		nextExp = groupPower_e.GroupPowerGradeExp[grade.Grade+1]
	}
	expireAt := ""
	if grade.ExpireAt.After(time.Now()) {
339 340 341 342
		expireAt = grade.ExpireAt.Add(-time.Second).Format("2006-01-02") // 减少一秒格式化天
	}
	if grade.Grade == groupPower_e.GroupPowerGrade0 {
		expireAt = utils.AddDate(now.EndOfMonth(), 0, 1).Add(-time.Second).Format("2006-01-02") // 0级别(黑铁) 没有有效期,给下个月月底
hujiebin's avatar
hujiebin committed
343 344
	}
	isMember := false
345
	if exits, id, err := groupPower_m.CheckGroupPowerUser(model, userId); err != nil {
hujiebin's avatar
hujiebin committed
346
		return myContext, err
347
	} else if exits && id == groupPowerId {
hujiebin's avatar
hujiebin committed
348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369
		isMember = true
	}
	response := group_power_cv.CvGroupPowerGradeDetail{
		GroupPowerBase: group_power_cv.CvGroupPowerBase{
			Id:        groupPower.ID,
			Icon:      groupPower.Icon,
			Name:      groupPower.Name,
			Nameplate: groupPower.Nameplate,
		},
		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],
		IsMember:      isMember,
	}
	resp.ResponseOk(c, response)
	return myContext, nil
}