From a28a0a7648d40608ca30ca35cd32df6033c2f045 Mon Sep 17 00:00:00 2001 From: hujiebin Date: Tue, 7 Mar 2023 20:08:24 +0800 Subject: [PATCH] =?UTF-8?q?feat=EF=BC=9A=E4=BA=8B=E5=8A=A1=E6=93=8D?= =?UTF-8?q?=E4=BD=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- domain/model/groupPower_m/group_grade.go | 99 ++++++++++++------------ 1 file changed, 51 insertions(+), 48 deletions(-) diff --git a/domain/model/groupPower_m/group_grade.go b/domain/model/groupPower_m/group_grade.go index faf5d1f..d495a99 100644 --- a/domain/model/groupPower_m/group_grade.go +++ b/domain/model/groupPower_m/group_grade.go @@ -131,58 +131,61 @@ func GetGroupPowerOnMic(model *domain.Model, groupPowerId, userId mysql.ID) (*Gr const MaxMinuteTimes = 18 // 增加势力上麦经验 +// 事务操作 func IncrGroupPowerExpOnMic(model *domain.Model, groupPowerId, userId mysql.ID, joinMicTimestamp int64) error { - // 获取用户上麦奖励历史 - onMicDetails, err := GetGroupPowerUserOnMicDetails(model, groupPowerId, userId) - if err != nil { - return err - } - numDetails := len(onMicDetails) - if numDetails >= MaxMinuteTimes { - // 上麦经验贡献值最多1800,1分钟100 - return nil - } - onMic, err := GetGroupPowerOnMic(model, groupPowerId, userId) - if err != nil { - return err - } - onMicSeconds := time.Now().Unix() - joinMicTimestamp - if onMic.LastCalTs == joinMicTimestamp { - onMicSeconds = onMicSeconds - int64(numDetails*600) // 扣除之前加过的时间 - } - var moreDetails []*GroupPowerOnMicDetail - totalMinuteTimes := int((onMic.Seconds + onMicSeconds) % 600) // 今天实际能加经验次数 - if totalMinuteTimes >= MaxMinuteTimes { - totalMinuteTimes = MaxMinuteTimes - } - if totalMinuteTimes > numDetails { - for minute := numDetails + 1; minute <= totalMinuteTimes; minute++ { - moreDetails = append(moreDetails, &GroupPowerOnMicDetail{ - Date: time.Now().Format("2006-01-02"), - GroupPowerId: groupPowerId, - UserId: userId, - MinuteTimes: minute, - }) + return model.Transaction(func(model *domain.Model) error { + // 获取用户上麦奖励历史 + onMicDetails, err := GetGroupPowerUserOnMicDetails(model, groupPowerId, userId) + if err != nil { + return err } - } - // 有更多麦上10分钟,可以加经验 - if len(moreDetails) > 0 { - for _, detail := range moreDetails { - // 添加明细,避免重复计算 - if err := model.DB().Model(GroupPowerOnMicDetail{}).Create(detail).Error; err != nil { - return err + numDetails := len(onMicDetails) + if numDetails >= MaxMinuteTimes { + // 上麦经验贡献值最多1800,1分钟100 + return nil + } + onMic, err := GetGroupPowerOnMic(model, groupPowerId, userId) + if err != nil { + return err + } + onMicSeconds := time.Now().Unix() - joinMicTimestamp + if onMic.LastCalTs == joinMicTimestamp { + onMicSeconds = onMicSeconds - int64(numDetails*600) // 扣除之前加过的时间 + } + var moreDetails []*GroupPowerOnMicDetail + totalMinuteTimes := int((onMic.Seconds + onMicSeconds) % 600) // 今天实际能加经验次数 + if totalMinuteTimes >= MaxMinuteTimes { + totalMinuteTimes = MaxMinuteTimes + } + if totalMinuteTimes > numDetails { + for minute := numDetails + 1; minute <= totalMinuteTimes; minute++ { + moreDetails = append(moreDetails, &GroupPowerOnMicDetail{ + Date: time.Now().Format("2006-01-02"), + GroupPowerId: groupPowerId, + UserId: userId, + MinuteTimes: minute, + }) + } + } + // 有更多麦上10分钟,可以加经验 + if len(moreDetails) > 0 { + for _, detail := range moreDetails { + // 添加明细,避免重复计算 + if err := model.DB().Model(GroupPowerOnMicDetail{}).Create(detail).Error; err != nil { + return err + } + // 每10分钟增加100点经验 + if err := IncrGroupPowerExp(model, groupPowerId, 100); err != nil { + return err + } } - // 每10分钟增加100点经验 - if err := IncrGroupPowerExp(model, groupPowerId, 100); err != nil { + // 更新micExp信息 + onMic.Seconds = onMic.Seconds + onMicSeconds + onMic.LastCalTs = joinMicTimestamp + if err := model.DB().Save(onMic).Error; err != nil { return err } } - // 更新micExp信息 - onMic.Seconds = onMic.Seconds + onMicSeconds - onMic.LastCalTs = joinMicTimestamp - if err := model.DB().Save(onMic).Error; err != nil { - return err - } - } - return nil + return nil + }) } -- 2.22.0