Commit 059d1c27 authored by hujiebin's avatar hujiebin

feat:myCp

parent 56b527c7
...@@ -3,9 +3,9 @@ package cp_cv ...@@ -3,9 +3,9 @@ package cp_cv
import "hilo-user/cv/user_cv" import "hilo-user/cv/user_cv"
type CvCp struct { type CvCp struct {
CpId uint64 `json:"cpId"` // cpId CpId uint64 `json:"cpId"` // cpId
User1 *user_cv.CvUserBase `json:"user1"` // user1 User1 *user_cv.CvUserBase `json:"user1"` // user1
User2 *user_cv.CvUserBase `json:"user2"` // user2 User2 *user_cv.CvUserBase `json:"user2"` // user2
Score uint32 `json:"score"` // 分值 Score uint32 `json:"score"` // 分值
Ranking int `json:"ranking"` // 排名 //Ranking int `json:"ranking"` // 排名
} }
...@@ -24,7 +24,7 @@ type CvCpLevel struct { ...@@ -24,7 +24,7 @@ type CvCpLevel struct {
Points uint32 `json:"points"` // CP值 Points uint32 `json:"points"` // CP值
NextPoints uint32 `json:"nextPoints,omitempty"` // 下个等级所需CP值 NextPoints uint32 `json:"nextPoints,omitempty"` // 下个等级所需CP值
RemainPoints uint32 `json:"remainPoints,omitempty"` // 还需要多少CP值到下一个级别 RemainPoints uint32 `json:"remainPoints,omitempty"` // 还需要多少CP值到下一个级别
ExpireAt string `json:"expireAt,omitempty"` // 有效期 ExpireAtUnix int64 `json:"expireAtUnix,omitempty"` // 有效期,时间戳
} }
// 资源等级 // 资源等级
......
...@@ -104,7 +104,8 @@ func GetUserBases(userIds []mysql.ID, myUserId mysql.ID) ([]*CvUserBase, error) ...@@ -104,7 +104,8 @@ func GetUserBases(userIds []mysql.ID, myUserId mysql.ID) ([]*CvUserBase, error)
} }
svips, err := rpc.MGetUserSvip(domain.CreateModelNil(), userIds) svips, err := rpc.MGetUserSvip(domain.CreateModelNil(), userIds)
if err != nil { if err != nil {
return nil, myerr.WrapErr(err) mylogrus.MyLog.Errorf("MGetUserSvip fail:%v", err)
//return nil, myerr.WrapErr(err)
} }
headwearMap, err := headwear_cv.BatchGetCvHeadwears(userIds) headwearMap, err := headwear_cv.BatchGetCvHeadwears(userIds)
......
...@@ -44,21 +44,42 @@ type CpLevelDetail struct { ...@@ -44,21 +44,42 @@ type CpLevelDetail struct {
Remark string Remark string
} }
// 获取cp等级
func GetCpLevel(model *domain.Model, cpId mysql.ID) CpLevel {
var level CpLevel
if err := model.DB().Model(CpLevel{}).Where("cp_id = ?", cpId).First(&level).Error; err != nil {
model.Log.Errorf("GetCpLevel fail:%v", err)
}
return level
}
// 添加cp等级积分增减明细 // 添加cp等级积分增减明细
func AddCpLevelDetail(model *domain.Model, detail CpLevelDetail) error { func AddCpLevelDetail(model *domain.Model, detail CpLevelDetail) error {
return model.DB().Create(&detail).Error return model.DB().Create(&detail).Error
} }
// 获取cpRelation // 获取cpRelation pair
// 因为cp的业务逻辑唯一性,只要user_id1命中其中一个即可 func GetCpRelation(model *domain.Model, userId mysql.ID) (cpRelation CpRelationTmp, exits bool) {
func GetCpRelation(model *domain.Model, userId1, userId2 mysql.ID) (cpRelation CpRelationTmp, exits bool) { if err := model.DB().Model(CpRelationTmp{}).Where("user_id1 = ? or user_id2 = ?", userId, userId).First(&cpRelation).Error; err != nil {
if err := model.DB().Model(CpRelationTmp{}).Where("user_id1 = ?", userId1).First(&cpRelation).Error; err != nil { if err != gorm.ErrRecordNotFound {
model.Log.Errorf("GetCpRelation fail:%v", err)
return
}
} else {
exits = true
}
return
}
// 获取cpRelation pair
func GetCpRelationPair(model *domain.Model, userId1, userId2 mysql.ID) (cpRelation CpRelationTmp, exits bool) {
if err := model.DB().Model(CpRelationTmp{}).Where("user_id1 = ? AND user_id2 = ?", userId1, userId2).First(&cpRelation).Error; err != nil {
if err != gorm.ErrRecordNotFound { if err != gorm.ErrRecordNotFound {
model.Log.Errorf("GetCpRelation fail:%v", err) model.Log.Errorf("GetCpRelation fail:%v", err)
return return
} else { } else {
// gorm.ErrRecordNotFound // gorm.ErrRecordNotFound
if err := model.DB().Model(CpRelationTmp{}).Where("user_id1 = ?", userId2).First(&cpRelation).Error; err != nil { if err := model.DB().Model(CpRelationTmp{}).Where("user_id1 = ? AND user_id2 = ?", userId2, userId1).First(&cpRelation).Error; err != nil {
if err != gorm.ErrRecordNotFound { if err != gorm.ErrRecordNotFound {
model.Log.Errorf("GetCpRelation fail:%v", err) model.Log.Errorf("GetCpRelation fail:%v", err)
return return
......
...@@ -47,3 +47,13 @@ func PageCpDayRank(model *domain.Model, beginDate, endDate string, offset, limit ...@@ -47,3 +47,13 @@ func PageCpDayRank(model *domain.Model, beginDate, endDate string, offset, limit
} }
return ranks return ranks
} }
// 获取指定cp排行榜
func GetCpDayRank(model *domain.Model, beginDate, endDate string, cpId mysql.ID) CpDayRank {
var rank CpDayRank
if err := model.DB().Table("cp_day_rank").
Where("date BETWEEN ? AND ?", beginDate, endDate).Where("cp_id = ?", cpId).First(&rank).Error; err != nil {
model.Log.Errorf("GetCpDayRank fail:%v", err)
}
return rank
}
...@@ -18,7 +18,7 @@ func CpGiftEvent() { ...@@ -18,7 +18,7 @@ func CpGiftEvent() {
for _, receiverUid := range sendGiftEvent.ReceiveUserIds { for _, receiverUid := range sendGiftEvent.ReceiveUserIds {
diamonds := sendGiftEvent.GiftN * sendGiftEvent.ResGift.DiamondNum diamonds := sendGiftEvent.GiftN * sendGiftEvent.ResGift.DiamondNum
// 有cp关系 // 有cp关系
if cpRelation, exits := cp_m.GetCpRelation(model, sendGiftEvent.SendUserId, receiverUid); exits { if cpRelation, exits := cp_m.GetCpRelationPair(model, sendGiftEvent.SendUserId, receiverUid); exits {
if err := cp_m.AddCpLevelPoints(model, cpRelation, diamonds); err != nil { if err := cp_m.AddCpLevelPoints(model, cpRelation, diamonds); err != nil {
model.Log.Errorf("AddCpLevelPoints fail:%v", err) model.Log.Errorf("AddCpLevelPoints fail:%v", err)
} }
......
...@@ -23,7 +23,7 @@ import ( ...@@ -23,7 +23,7 @@ import (
// @Param pageSize query int true "请求数量 默认:10" default(10) // @Param pageSize query int true "请求数量 默认:10" default(10)
// @Param queryType path string true "类型:day/week/month" // @Param queryType path string true "类型:day/week/month"
// @Success 200 {object} []cp_cv.CvCp // @Success 200 {object} []cp_cv.CvCp
// @Router /v2/cp/rank/{queryType} [get] // @Router /v2/cp/rank/:queryType [get]
func CpRank(c *gin.Context) (*mycontext.MyContext, error) { func CpRank(c *gin.Context) (*mycontext.MyContext, error) {
myCtx := mycontext.CreateMyContext(c.Keys) myCtx := mycontext.CreateMyContext(c.Keys)
userId, err := req.GetUserId(c) userId, err := req.GetUserId(c)
...@@ -65,15 +65,81 @@ func CpRank(c *gin.Context) (*mycontext.MyContext, error) { ...@@ -65,15 +65,81 @@ func CpRank(c *gin.Context) (*mycontext.MyContext, error) {
if err != nil { if err != nil {
return myCtx, err return myCtx, err
} }
for i, rank := range ranks { for _, rank := range ranks {
response = append(response, cp_cv.CvCp{ response = append(response, cp_cv.CvCp{
CpId: rank.CpId, CpId: rank.CpId,
User1: userBase[rank.UserId1], User1: userBase[rank.UserId1],
User2: userBase[rank.UserId2], User2: userBase[rank.UserId2],
Score: rank.Score, Score: rank.Score,
Ranking: i + 1 + offset, //Ranking: i + 1 + offset,
}) })
} }
resp.ResponsePageBaseOk(c, response, pageReq.PageIndex+1, len(ranks) >= pageReq.PageSize) resp.ResponsePageBaseOk(c, response, pageReq.PageIndex+1, len(ranks) >= pageReq.PageSize)
return myCtx, nil return myCtx, nil
} }
// @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
}
...@@ -37,17 +37,21 @@ func CpSpace(c *gin.Context) (*mycontext.MyContext, error) { ...@@ -37,17 +37,21 @@ func CpSpace(c *gin.Context) (*mycontext.MyContext, error) {
if err != nil { if err != nil {
return myContext, err return myContext, err
} }
cpUserInfo, err := user_m.GetUserByExtId(model, externalId) // todo get from cp2 db cpUserInfo, err := user_m.GetUserByExtId(model, externalId)
if err != nil { if err != nil {
return myContext, err return myContext, err
} }
expireAt := "" // todo expireAtUnix, cpLevel := int64(0), cp_e.CpLevel0
cpLevel := cp_e.CpLevel5 // todo get from db nextPoints, remainPoints, curPoints := cp_e.CpLevelPoints[cp_e.CpLevel1], mysql.Num(0), mysql.Num(0)
nextPoints := cp_e.CpLevelPoints[cp_e.CpLevelMax] cpRelation, exists := cp_m.GetCpRelation(model, cpUserInfo.ID)
remainPoints := mysql.Num(0) if exists {
level := cp_m.GetCpLevel(model, cpRelation.ID)
cpLevel, curPoints = level.Level, level.Points
expireAtUnix = level.ExpireAt.Unix()
}
if cpLevel != cp_e.CpLevelMax { if cpLevel != cp_e.CpLevelMax {
nextPoints = cp_e.CpLevelPoints[cpLevel+1] nextPoints = cp_e.CpLevelPoints[cpLevel+1]
remainPoints = nextPoints - cp_e.CpLevelPoints[cpLevel] // todo get from db remainPoints = nextPoints - cp_e.CpLevelPoints[cpLevel] - curPoints
} }
privilegeList := cp_cv.CopyCpLevelPrivilegeList(cpLevel, lang) privilegeList := cp_cv.CopyCpLevelPrivilegeList(cpLevel, lang)
userPrivileges, err := cp_m.MGetUserSvipPrivilege(model, []uint64{userId}) userPrivileges, err := cp_m.MGetUserSvipPrivilege(model, []uint64{userId})
...@@ -66,10 +70,10 @@ func CpSpace(c *gin.Context) (*mycontext.MyContext, error) { ...@@ -66,10 +70,10 @@ func CpSpace(c *gin.Context) (*mycontext.MyContext, error) {
}, },
CpLevel: cp_cv.CvCpLevel{ CpLevel: cp_cv.CvCpLevel{
Level: cpLevel, Level: cpLevel,
Points: cp_e.CpLevelPoints[cpLevel], // todo get from db Points: cp_e.CpLevelPoints[cpLevel] + curPoints,
NextPoints: nextPoints, NextPoints: nextPoints,
RemainPoints: remainPoints, RemainPoints: remainPoints,
ExpireAt: expireAt, ExpireAtUnix: expireAtUnix,
}, },
ResLevelList: cp_cv.CvResLevelList, ResLevelList: cp_cv.CvResLevelList,
PrivilegeList: privilegeList, PrivilegeList: privilegeList,
......
...@@ -34,6 +34,7 @@ func InitRouter() *gin.Engine { ...@@ -34,6 +34,7 @@ func InitRouter() *gin.Engine {
cp.GET("/space", wrapper(cp_r.CpSpace)) cp.GET("/space", wrapper(cp_r.CpSpace))
cp.PUT("/privilege/openClose", wrapper(cp_r.CpPrivilegeOpenClose)) cp.PUT("/privilege/openClose", wrapper(cp_r.CpPrivilegeOpenClose))
cp.GET("/rank/:queryType", wrapper(cp_r.CpRank)) cp.GET("/rank/:queryType", wrapper(cp_r.CpRank))
cp.GET("/my/:queryType", wrapper(cp_r.CpMy))
} }
inner := r.Group("/inner") inner := r.Group("/inner")
inner.Use(ExceptionHandle, LoggerHandle) inner.Use(ExceptionHandle, LoggerHandle)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment