From 3534f3279d62bbed5912b77836a4ff17e1064146 Mon Sep 17 00:00:00 2001 From: hujiebin Date: Wed, 8 Mar 2023 11:42:43 +0800 Subject: [PATCH] =?UTF-8?q?=20=E6=B8=85=E7=90=86=E5=AE=B6=E6=97=8F?= =?UTF-8?q?=E7=BB=8F=E9=AA=8C/=E7=AD=89=E7=BA=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cron/cron.go | 8 ++-- cron/group_cron/group_power_exp.go | 30 +++++++++++++++ domain/model/groupPower_m/group_grade.go | 49 ++++++++++++++++++++++++ 3 files changed, 84 insertions(+), 3 deletions(-) create mode 100644 cron/group_cron/group_power_exp.go diff --git a/cron/cron.go b/cron/cron.go index edccdec..9fd7f65 100644 --- a/cron/cron.go +++ b/cron/cron.go @@ -3,6 +3,7 @@ package cron import ( "git.hilo.cn/hilo-common/resource/config" "hilo-group/cron/gift_cron" + "hilo-group/cron/group_cron" "hilo-group/cron/mic_cron" ) @@ -10,7 +11,8 @@ func Init() { if !config.IsMaster() { return } - gift_cron.SendGiftEventInit() // 礼物消息 - gift_cron.GiftRemark() // 礼物消息补偿 - mic_cron.OnMicCheck() // 检查上麦 + gift_cron.SendGiftEventInit() // 礼物消息 + gift_cron.GiftRemark() // 礼物消息补偿 + mic_cron.OnMicCheck() // 检查上麦 + group_cron.GroupPowerExpClear() // 清理家族经验/等级 } diff --git a/cron/group_cron/group_power_exp.go b/cron/group_cron/group_power_exp.go new file mode 100644 index 0000000..6a81fb2 --- /dev/null +++ b/cron/group_cron/group_power_exp.go @@ -0,0 +1,30 @@ +package group_cron + +import ( + "git.hilo.cn/hilo-common/domain" + "github.com/robfig/cron" + "hilo-group/domain/model/groupPower_m" +) + +// 清理家族经验和等级 +func GroupPowerExpClear() { + c := cron.New() + // 每月1号0:00经验值清零 + spec := "0 0 0 1 * ?" + _ = c.AddFunc(spec, func() { + var model = domain.CreateModelNil() + if err := groupPower_m.ClearGroupPowerExp(model); err != nil { + model.Log.Errorf("ClearGroupPowerExp fail:%v", err) + } + }) + + // 每分钟清理过期家族等级 + spec = "0 * * * * ?" + _ = c.AddFunc(spec, func() { + var model = domain.CreateModelNil() + if err := groupPower_m.ClearGroupPowerGrade(model); err != nil { + model.Log.Errorf("ClearGroupPowerGrade fail:%v", err) + } + }) + c.Start() +} diff --git a/domain/model/groupPower_m/group_grade.go b/domain/model/groupPower_m/group_grade.go index cae79d0..a921b39 100644 --- a/domain/model/groupPower_m/group_grade.go +++ b/domain/model/groupPower_m/group_grade.go @@ -222,3 +222,52 @@ func IncrGroupPowerExpOnMic(model *domain.Model, groupPowerId, userId mysql.ID, return nil }) } + +// 清理所有家族的经验 +func ClearGroupPowerExp(model *domain.Model) error { + var groupPowerGrades []*GroupPowerGrade + if err := model.DB().Model(GroupPowerGrade{}).Where("exp > 0").Find(&groupPowerGrades).Error; err != nil { + return err + } + return model.Transaction(func(model *domain.Model) error { + for _, grade := range groupPowerGrades { + if err := model.DB().Model(GroupPowerGrade{}).Where("group_power_id = ?", grade.GroupPowerId).UpdateColumn("exp", 0).Error; err != nil { + return err + } + // 记录明细 + detail := &GroupPowerExpDetail{ + GroupPowerId: grade.GroupPowerId, + UserId: 0, + Exp: 0, + AddReduce: mysql.SET, + BefNum: grade.Exp, + AftNum: 0, + Remark: "每月清零", + } + if err := model.DB().Model(GroupPowerExpDetail{}).Create(detail).Error; err != nil { + return err + } + } + return nil + }) +} + +// 清理过期家族等级 +func ClearGroupPowerGrade(model *domain.Model) error { + var groupPowerGrades []*GroupPowerGrade + if err := model.DB().Model(GroupPowerGrade{}).Where("expire_at > ?", time.Now()).Find(&groupPowerGrades).Error; err != nil { + return err + } + return model.Transaction(func(model *domain.Model) error { + for _, grade := range groupPowerGrades { + updateAttrs := map[string]interface{}{ + "grade": 0, + "expire_at": time.Time{}, + } + if err := model.DB().Model(GroupPowerGrade{}).Where("group_power_id = ?", grade.GroupPowerId).Updates(updateAttrs).Error; err != nil { + return err + } + } + return nil + }) +} -- 2.22.0