Commit b9aeabdc authored by hujiebin's avatar hujiebin

feat:成就yefix

parent d0121190
......@@ -41,10 +41,11 @@ func UpdateCpAchievement(model *domain.Model, cpId, userId1, userId2 mysql.ID, T
return model.DB().Model(CpAchievement{}).Where("cp_id = ? AND `type` = ?", cpId, Type).Where("score < ?", score).UpdateColumn("score", score).Error
}
// 获取用户cp成就
func GetCpAchievements(model *domain.Model, cpId mysql.ID) []CpAchievement {
// 获取cp成就
func GetCpAchievements(model *domain.Model) []CpAchievement {
var achievements []CpAchievement
if err := model.DB().Model(CpAchievement{}).Where("cp_id = ?", cpId).Find(&achievements).Error; err != nil {
subQuery := model.DB().Table("cp_achievement a").Joins("JOIN cp_relation r ON a.cp_id = r.id").Select("type,MAX(score) AS max_score").Group("type")
if err := model.DB().Model(CpAchievement{}).Where("(type, score) IN (?)", subQuery).Find(&achievements).Error; err != nil {
model.Log.Errorf("GetAchievements fail:%v", err)
}
return achievements
......
......@@ -2,7 +2,6 @@ package event_s
import (
"git.hilo.cn/hilo-common/domain"
"git.hilo.cn/hilo-common/resource/mysql"
"github.com/jinzhu/now"
"hilo-user/_const/enum/cp_e"
"hilo-user/domain/event/gift_ev"
......@@ -31,7 +30,8 @@ func CpGiftEvent() {
}
// 检查最新的等级
if cpLevel := cp_m.GetCpLevel(model, cpRelation.ID); cpLevel.CpId >= 0 {
if err := cp_m.UpdateCpAchievement(model, cpLevel.CpId, cpRelation.UserId1, cpRelation.UserId2, cp_e.CpAchievementLevel, mysql.Num(cpLevel.Level)); err != nil {
points := cpLevel.Points + cp_e.CpLevelPoints[cpLevel.Level]
if err := cp_m.UpdateCpAchievement(model, cpLevel.CpId, cpRelation.UserId1, cpRelation.UserId2, cp_e.CpAchievementLevel, points); err != nil {
model.Log.Errorf("UpdateCpAchievement fail:%v", err)
}
}
......
......@@ -231,16 +231,13 @@ func CpMy(c *gin.Context) (*mycontext.MyContext, error) {
// @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)
// @Success 200 {object} []cp_cv.CvCpAchievement
// @Router /v2/cp/achievement [get]
func CpAchievement(c *gin.Context) (*mycontext.MyContext, error) {
myCtx := mycontext.CreateMyContext(c.Keys)
userId, err := req.GetUserId(c)
if err != nil {
return myCtx, err
}
model := domain.CreateModelContext(myCtx)
relation, exits := cp_m.GetCpRelation(model, userId)
var response = make([]cp_cv.CvCpAchievement, 0)
TypeDescMap := map[cp_e.CpAchievement]string{ // todo 翻译
cp_e.CpAchievementLevel: "等级分值最高",
......@@ -249,24 +246,26 @@ func CpAchievement(c *gin.Context) (*mycontext.MyContext, error) {
cp_e.CpAchievementWeekRank: "周榜最高",
cp_e.CpAchievementDayRank: "天榜最高",
}
if exits {
users, err := user_m.GetUserMapByIds(model, []mysql.ID{relation.UserId1, relation.UserId2})
if err != nil {
return myCtx, err
}
achievements := cp_m.GetCpAchievements(model, relation.ID)
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]),
Type: a.Type,
TypeDesc: TypeDescMap[a.Type],
Score: a.Score,
TimeUnix: a.UpdatedTime.Unix(),
})
}
} else {
achievements := cp_m.GetCpAchievements(model)
var userIds []uint64
for _, v := range achievements {
userIds = append(userIds, v.UserId1)
userIds = append(userIds, v.UserId2)
}
users, err := user_m.GetUserMapByIds(model, userIds)
if err != nil {
return myCtx, err
}
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]),
Type: a.Type,
TypeDesc: TypeDescMap[a.Type],
Score: a.Score,
TimeUnix: a.UpdatedTime.Unix(),
})
}
resp.ResponsePageBaseOk(c, response, 0, false)
return myCtx, nil
......
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