Commit b4ec010a authored by hujiebin's avatar hujiebin

feat:分数用新旧相加

parent eef32ec6
package cp_m
import (
"git.hilo.cn/hilo-common/domain"
"git.hilo.cn/hilo-common/resource/mysql"
"gorm.io/gorm"
"hilo-user/myerr"
)
type StatusCp = mysql.Type
const (
Connect StatusCp = 1
Disconnect StatusCp = 2
)
type OldCp struct {
mysql.Entity
//必须保证userId1 > userId2
UserId1 mysql.ID
UserId2 mysql.ID
Score mysql.Num
DayScore mysql.Num
PeriodDay mysql.Str
WeekScore mysql.Num
PeriodWeek mysql.Str
MonthScore mysql.Num
PeriodMonth mysql.Str
Status StatusCp
//中断时长
DisconnectSecond int64
}
func (OldCp) TableName() string {
return "cp"
}
func GetOldCpOrNil(model *domain.Model, tmpUserId1 mysql.ID, tmpUserId2 mysql.ID) (*Cp, error) {
userId1 := tmpUserId1
userId2 := tmpUserId2
if tmpUserId1 < tmpUserId2 {
userId1 = tmpUserId2
userId2 = tmpUserId1
}
cp := Cp{}
if err := model.Db.Where(&Cp{
UserId1: userId1,
UserId2: userId2,
}).First(&cp).Error; err != nil {
if err == gorm.ErrRecordNotFound {
return nil, nil
} else {
return nil, myerr.WrapErr(err)
}
}
return &cp, nil
}
...@@ -58,3 +58,12 @@ func GetCpDayRank(model *domain.Model, beginDate, endDate string, cpId mysql.ID) ...@@ -58,3 +58,12 @@ func GetCpDayRank(model *domain.Model, beginDate, endDate string, cpId mysql.ID)
} }
return rank return rank
} }
// 获取Cp历史分数
func SumCpPoints(model *domain.Model, cpId mysql.ID) mysql.Num {
var score CpDayRank
if err := model.DB().Model(CpDayRank{}).Where("cp_id = ?", cpId).Select("SUM(score) score").Scan(&score).Error; err != nil {
model.Log.Errorf("SumCpPoints fail:%v", err)
}
return score.Score
}
...@@ -157,11 +157,15 @@ func GetUserCp(c *gin.Context) (*mycontext.MyContext, error) { ...@@ -157,11 +157,15 @@ func GetUserCp(c *gin.Context) (*mycontext.MyContext, error) {
if msgId, ok := cp_e.CpLevelTitle[level.Level]; ok { if msgId, ok := cp_e.CpLevelTitle[level.Level]; ok {
title = cp_cv.GetTranslate(msgId, req.Language) title = cp_cv.GetTranslate(msgId, req.Language)
} }
var oldScore uint32
if oldCp, _ := cp_m.GetOldCpOrNil(model, cpRelation.UserId1, cpRelation.UserId2); oldCp != nil {
oldScore = uint32(oldCp.Score)
}
response = cp_cv.CvCp{ response = cp_cv.CvCp{
CpUserInfo: userBases[cpUserId], CpUserInfo: userBases[cpUserId],
CpLevel: cp_cv.CvCpLevel{ CpLevel: cp_cv.CvCpLevel{
Level: cpLevel, Level: cpLevel,
Points: cp_e.CpLevelPoints[cpLevel] + level.Points, Points: oldScore + cp_m.SumCpPoints(model, cpRelation.Id), // 历史分数
Title: title, Title: title,
}, },
MyPrivilegeList: myPrivilegeList, MyPrivilegeList: myPrivilegeList,
......
...@@ -30,3 +30,11 @@ func TestCalLoc(t *testing.T) { ...@@ -30,3 +30,11 @@ func TestCalLoc(t *testing.T) {
loc = timezone_e.GetFixedTimezone("GMT+5") loc = timezone_e.GetFixedTimezone("GMT+5")
println(time.Now().In(loc).Format("2006-01-02 15:04:05")) println(time.Now().In(loc).Format("2006-01-02 15:04:05"))
} }
func TestSumCpScore(t *testing.T) {
model := domain.CreateModelNil()
old, err := cp_m.GetOldCpOrNil(model, 76421, 4549)
t.Logf("%v-%v", old, err)
n := cp_m.SumCpPoints(model, 1)
t.Logf("%v", n)
}
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