From b4ec010a3bbaadc999e2988fd553ede934dea20f Mon Sep 17 00:00:00 2001 From: hujiebin Date: Tue, 20 Jun 2023 11:39:50 +0800 Subject: [PATCH 1/2] =?UTF-8?q?feat:=E5=88=86=E6=95=B0=E7=94=A8=E6=96=B0?= =?UTF-8?q?=E6=97=A7=E7=9B=B8=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- domain/model/cp_m/old_cp.go | 57 +++++++++++++++++++++++++++++++++++++ domain/model/cp_m/rank.go | 9 ++++++ route/user_r/inner.go | 6 +++- test/cp_test.go | 8 ++++++ 4 files changed, 79 insertions(+), 1 deletion(-) create mode 100644 domain/model/cp_m/old_cp.go diff --git a/domain/model/cp_m/old_cp.go b/domain/model/cp_m/old_cp.go new file mode 100644 index 0000000..260c2e1 --- /dev/null +++ b/domain/model/cp_m/old_cp.go @@ -0,0 +1,57 @@ +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 +} diff --git a/domain/model/cp_m/rank.go b/domain/model/cp_m/rank.go index ab6eb2d..915587c 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 de55aad..25ab659 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.GetOldCpOrNil(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 fb41739..b7ebf80 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.GetOldCpOrNil(model, 76421, 4549) + t.Logf("%v-%v", old, err) + n := cp_m.SumCpPoints(model, 1) + t.Logf("%v", n) +} -- 2.22.0 From dec050515a2779e60c930cb7efe6f0854a85bcc8 Mon Sep 17 00:00:00 2001 From: hujiebin Date: Tue, 20 Jun 2023 11:56:16 +0800 Subject: [PATCH 2/2] feat:oldCp --- domain/model/cp_m/old_cp.go | 8 +++++--- route/user_r/inner.go | 2 +- test/cp_test.go | 2 +- 3 files changed, 7 insertions(+), 5 deletions(-) diff --git a/domain/model/cp_m/old_cp.go b/domain/model/cp_m/old_cp.go index 260c2e1..e1f93c5 100644 --- a/domain/model/cp_m/old_cp.go +++ b/domain/model/cp_m/old_cp.go @@ -35,17 +35,19 @@ func (OldCp) TableName() string { return "cp" } -func GetOldCpOrNil(model *domain.Model, tmpUserId1 mysql.ID, tmpUserId2 mysql.ID) (*Cp, error) { +// 获取旧的绑定中的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 := Cp{} - if err := model.Db.Where(&Cp{ + 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 diff --git a/route/user_r/inner.go b/route/user_r/inner.go index 25ab659..d08d5d4 100644 --- a/route/user_r/inner.go +++ b/route/user_r/inner.go @@ -158,7 +158,7 @@ func GetUserCp(c *gin.Context) (*mycontext.MyContext, error) { title = cp_cv.GetTranslate(msgId, req.Language) } var oldScore uint32 - if oldCp, _ := cp_m.GetOldCpOrNil(model, cpRelation.UserId1, cpRelation.UserId2); oldCp != nil { + if oldCp, _ := cp_m.GetOldConnectCp(model, cpRelation.UserId1, cpRelation.UserId2); oldCp != nil { oldScore = uint32(oldCp.Score) } response = cp_cv.CvCp{ diff --git a/test/cp_test.go b/test/cp_test.go index b7ebf80..19e623f 100644 --- a/test/cp_test.go +++ b/test/cp_test.go @@ -33,7 +33,7 @@ func TestCalLoc(t *testing.T) { func TestSumCpScore(t *testing.T) { model := domain.CreateModelNil() - old, err := cp_m.GetOldCpOrNil(model, 76421, 4549) + old, err := cp_m.GetOldConnectCp(model, 7642, 4549) t.Logf("%v-%v", old, err) n := cp_m.SumCpPoints(model, 1) t.Logf("%v", n) -- 2.22.0