From 2c5e4e5010531f4b3478d17504a67d41246369c0 Mon Sep 17 00:00:00 2001 From: hujiebin Date: Thu, 9 Mar 2023 19:57:53 +0800 Subject: [PATCH] =?UTF-8?q?feat:=E5=A2=9E=E5=8A=A0=E5=8A=BF=E5=8A=9B?= =?UTF-8?q?=E4=B8=8A=E9=BA=A6=E6=97=B6=E9=95=BF-=E5=AE=B6=E6=97=8F?= =?UTF-8?q?=E4=B9=8B=E6=98=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- domain/model/groupPower_m/group_grade.go | 17 +++++++++++++++++ domain/model/groupPower_m/group_star.go | 18 ++++++++++++++++-- domain/service/event_s/event_init.go | 10 ++++++---- domain/service/group_mic_s/group_mic.go | 13 ++++++++++--- 4 files changed, 49 insertions(+), 9 deletions(-) diff --git a/domain/model/groupPower_m/group_grade.go b/domain/model/groupPower_m/group_grade.go index 2334292..04a7ec7 100644 --- a/domain/model/groupPower_m/group_grade.go +++ b/domain/model/groupPower_m/group_grade.go @@ -223,6 +223,23 @@ func IncrGroupPowerExpOnMic(model *domain.Model, groupPowerId, userId mysql.ID, }) } +// 增加势力上麦时长-家族之星 +// 事务操作 +func IncrGroupPowerStarOnMic(model *domain.Model, groupPowerId, userId mysql.ID, joinMicTimestamp int64) error { + return model.Transaction(func(model *domain.Model) error { + star, err := GetGroupPowerMonthStar(model, groupPowerId, userId, groupPower_e.GroupPowerStarTypeActive) + curTs := joinMicTimestamp + if err != nil && err != gorm.ErrRecordNotFound { + return err + } + if star != nil && joinMicTimestamp == star.LastCalTs { + curTs = star.LastCalTs + } + score := time.Now().Unix() - curTs + return IncrGroupPowerMonthStarScore(model, groupPowerId, userId, groupPower_e.GroupPowerStarTypeActive, mysql.Num(score), curTs) + }) +} + // 清理所有家族的经验 func ClearGroupPowerExp(model *domain.Model) error { var groupPowerGrades []*GroupPowerGrade diff --git a/domain/model/groupPower_m/group_star.go b/domain/model/groupPower_m/group_star.go index 8c7e157..c58c8fd 100644 --- a/domain/model/groupPower_m/group_star.go +++ b/domain/model/groupPower_m/group_star.go @@ -16,12 +16,13 @@ type GroupPowerMonthStar struct { UserId mysql.ID Type groupPower_e.GroupPowerStarType Score mysql.Num + LastCalTs int64 CreatedTime time.Time `gorm:"->"` UpdatedTime time.Time `gorm:"->"` } // 增加家族之星分数 -func IncrGroupPowerMonthStarScore(model *domain.Model, groupPowerId, userId mysql.ID, _type groupPower_e.GroupPowerStarType, score mysql.Num) error { +func IncrGroupPowerMonthStarScore(model *domain.Model, groupPowerId, userId mysql.ID, _type groupPower_e.GroupPowerStarType, score mysql.Num, lastCalTs int64) error { month := time.Now().Format("200601") star := &GroupPowerMonthStar{ Month: month, @@ -33,7 +34,9 @@ func IncrGroupPowerMonthStarScore(model *domain.Model, groupPowerId, userId mysq if err := model.DB().Model(GroupPowerMonthStar{}).Clauses(clause.OnConflict{ Columns: []clause.Column{{Name: "month"}, {Name: "group_power_id"}, {Name: "user_id"}, {Name: "type"}}, DoUpdates: clause.Assignments(map[string]interface{}{ - "score": gorm.Expr("score + ?", star.Score)}), + "score": gorm.Expr("score + ?", star.Score), + "last_cal_ts": lastCalTs, + }), }).Create(star).Error; err != nil { model.Log.Errorf("IncrGroupPowerMonthStarScore fail:%v", err) return err @@ -41,6 +44,17 @@ func IncrGroupPowerMonthStarScore(model *domain.Model, groupPowerId, userId mysq return nil } +// 获取家族之星分数 +// 允许返回gorm.ErrRecordNotFound +func GetGroupPowerMonthStar(model *domain.Model, groupPowerId, userId mysql.ID, _type groupPower_e.GroupPowerStarType) (*GroupPowerMonthStar, error) { + res := new(GroupPowerMonthStar) + month := time.Now().Format("200601") + if err := model.DB().Where("month = ? AND group_power_id = ? AND user_id = ? AND `type` = ?", month, groupPowerId, userId, _type).First(res).Error; err != nil { + return nil, err + } + return res, nil +} + // 获取家族之星排行 func GetGroupPowerMonthStarRank(model *domain.Model, groupPowerId mysql.ID, _type groupPower_e.GroupPowerStarType, offset, limit int) ([]*GroupPowerMonthStar, error) { var res []*GroupPowerMonthStar diff --git a/domain/service/event_s/event_init.go b/domain/service/event_s/event_init.go index b39502e..5e5f137 100644 --- a/domain/service/event_s/event_init.go +++ b/domain/service/event_s/event_init.go @@ -438,7 +438,7 @@ func SendGift() { // 送礼加分 if data, ok := groupPowers[sendGiftEvent.SendUserId]; ok { diamonds := sendGiftEvent.GiftN * sendGiftEvent.ResGift.DiamondNum * mysql.Num(len(sendGiftEvent.ReceiveUserIds)) - if err := groupPower_m.IncrGroupPowerMonthStarScore(model, data.GroupPowerId, data.UserId, groupPower_e.GroupPowerStarTypeFamous, diamonds); err != nil { + if err := groupPower_m.IncrGroupPowerMonthStarScore(model, data.GroupPowerId, data.UserId, groupPower_e.GroupPowerStarTypeFamous, diamonds, 0); err != nil { model.Log.Errorf("IncrGroupPowerMonthStarScore famous fail:%v", err) } } @@ -446,7 +446,7 @@ func SendGift() { for _, userId := range sendGiftEvent.ReceiveUserIds { if data, ok := groupPowers[userId]; ok { diamonds := sendGiftEvent.GiftN * sendGiftEvent.ResGift.DiamondNum - if err := groupPower_m.IncrGroupPowerMonthStarScore(model, data.GroupPowerId, data.UserId, groupPower_e.GroupPowerStarTypeCharm, diamonds); err != nil { + if err := groupPower_m.IncrGroupPowerMonthStarScore(model, data.GroupPowerId, data.UserId, groupPower_e.GroupPowerStarTypeCharm, diamonds, 0); err != nil { model.Log.Errorf("IncrGroupPowerMonthStarScore charm fail:%v", err) } } @@ -456,17 +456,19 @@ func SendGift() { } func OnMic() { - mic_ev.AddOnMicEventSync(func(model *domain.Model, e interface{}) error { + // 上麦经验/上麦时长 + mic_ev.AddOnMicEventAsync(func(model *domain.Model, e interface{}) error { event, ok := e.(*mic_ev.OnMicEvent) if !ok { model.Log.Errorf("AddOnMicEventSync event type err") return nil } - if err := group_mic_s.NewGroupPowerService(model.MyContext).GroupPowerOnMicExp(event.GroupUuid, event.UserId, event.Timestamp); err != nil { + if err := group_mic_s.NewGroupPowerService(model.MyContext).IncrGroupPowerOnMicExpAndTime(event.GroupUuid, event.UserId, event.Timestamp); err != nil { model.Log.Errorf("cron micIn GroupPowerOnMicExp err:%v", err) } else { model.Log.Infof("cron micIn GroupPowerOnMicExp success, groupId:%v, userId:%v", event.GroupUuid, event.UserId) } return nil }) + } diff --git a/domain/service/group_mic_s/group_mic.go b/domain/service/group_mic_s/group_mic.go index 134daad..0b8f25c 100644 --- a/domain/service/group_mic_s/group_mic.go +++ b/domain/service/group_mic_s/group_mic.go @@ -289,8 +289,8 @@ func (s *GroupMicService) GroupIMMassByMgr(groupId string, userId uint64, extern }) } -// 增加势力上麦经验 -func (s *GroupMicService) GroupPowerOnMicExp(groupId string, userId uint64, joinMicTimestamp int64) error { +// 增加势力上麦经验/时长 +func (s *GroupMicService) IncrGroupPowerOnMicExpAndTime(groupId string, userId uint64, joinMicTimestamp int64) error { var model = domain.CreateModelContext(s.svc.MyContext) exists, groupPowerId, err := groupPower_m.CheckGroupPowerUser(model, userId) if err != nil { @@ -315,5 +315,12 @@ func (s *GroupMicService) GroupPowerOnMicExp(groupId string, userId uint64, join return nil } // 增加势力上麦经验 - return groupPower_m.IncrGroupPowerExpOnMic(model, groupPowerId, userId, joinMicTimestamp) + if err := groupPower_m.IncrGroupPowerExpOnMic(model, groupPowerId, userId, joinMicTimestamp); err != nil { + model.Log.Errorf("IncrGroupPowerExpOnMic fail:%v", err) + } + // 增加势力上麦时长 + if err := groupPower_m.IncrGroupPowerStarOnMic(model, groupPowerId, userId, joinMicTimestamp); err != nil { + model.Log.Errorf("IncrGroupPowerOnMicTime fail:%v", err) + } + return nil } -- 2.22.0