diff --git a/domain/model/cp_m/old_cp.go b/domain/model/cp_m/old_cp.go new file mode 100644 index 0000000000000000000000000000000000000000..e1f93c59b91ae91c9bd8e7f73698180bb5f722ae --- /dev/null +++ b/domain/model/cp_m/old_cp.go @@ -0,0 +1,59 @@ +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" +} + +// 获取旧的绑定中的cp +func GetOldConnectCp(model *domain.Model, tmpUserId1 mysql.ID, tmpUserId2 mysql.ID) (*OldCp, error) { + userId1 := tmpUserId1 + userId2 := tmpUserId2 + if tmpUserId1 < tmpUserId2 { + userId1 = tmpUserId2 + userId2 = tmpUserId1 + } + cp := OldCp{} + if err := model.Db.Where(&OldCp{ + UserId1: userId1, + UserId2: userId2, + Status: Connect, + }).First(&cp).Error; err != nil { + if err == gorm.ErrRecordNotFound { + return nil, nil + } else { + return nil, myerr.WrapErr(err) + } + } + return &cp, nil +} diff --git a/domain/model/cp_m/rank.go b/domain/model/cp_m/rank.go index ab6eb2d656cef98f17b1dd796bf6997fe37f06ba..915587cb7deec21c379c1001e0520e582d9605ad 100644 --- a/domain/model/cp_m/rank.go +++ b/domain/model/cp_m/rank.go @@ -58,3 +58,12 @@ func GetCpDayRank(model *domain.Model, beginDate, endDate string, cpId mysql.ID) } 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 +} diff --git a/route/user_r/inner.go b/route/user_r/inner.go index de55aadda857996f4d4a2db3aeb85d3613cc9a9e..d08d5d45cdec228e93075beb381a87b7cecb45b0 100644 --- a/route/user_r/inner.go +++ b/route/user_r/inner.go @@ -157,11 +157,15 @@ func GetUserCp(c *gin.Context) (*mycontext.MyContext, error) { if msgId, ok := cp_e.CpLevelTitle[level.Level]; ok { title = cp_cv.GetTranslate(msgId, req.Language) } + var oldScore uint32 + if oldCp, _ := cp_m.GetOldConnectCp(model, cpRelation.UserId1, cpRelation.UserId2); oldCp != nil { + oldScore = uint32(oldCp.Score) + } response = cp_cv.CvCp{ CpUserInfo: userBases[cpUserId], CpLevel: cp_cv.CvCpLevel{ Level: cpLevel, - Points: cp_e.CpLevelPoints[cpLevel] + level.Points, + Points: oldScore + cp_m.SumCpPoints(model, cpRelation.Id), // 历史分数 Title: title, }, MyPrivilegeList: myPrivilegeList, diff --git a/test/cp_test.go b/test/cp_test.go index fb41739721f5f97b0d987891842c89c7d2f519e1..19e623f4d4f58a9c43bbad29137a133a49b605bd 100644 --- a/test/cp_test.go +++ b/test/cp_test.go @@ -30,3 +30,11 @@ func TestCalLoc(t *testing.T) { loc = timezone_e.GetFixedTimezone("GMT+5") println(time.Now().In(loc).Format("2006-01-02 15:04:05")) } + +func TestSumCpScore(t *testing.T) { + model := domain.CreateModelNil() + old, err := cp_m.GetOldConnectCp(model, 7642, 4549) + t.Logf("%v-%v", old, err) + n := cp_m.SumCpPoints(model, 1) + t.Logf("%v", n) +}