From e36a06f27584ebad0f39def61371db4b41dfdfaa Mon Sep 17 00:00:00 2001 From: chenweijian <820961417@qq.com> Date: Wed, 23 Aug 2023 14:59:10 +0800 Subject: [PATCH] group support --- cron/group_cron/group_support.go | 78 ++++++++++++++++++++++++++++ domain/model/gift_m/gift.go | 11 ++++ domain/service/event_s/event_init.go | 2 +- 3 files changed, 90 insertions(+), 1 deletion(-) diff --git a/cron/group_cron/group_support.go b/cron/group_cron/group_support.go index 17f2aab..27f954d 100644 --- a/cron/group_cron/group_support.go +++ b/cron/group_cron/group_support.go @@ -1,13 +1,18 @@ package group_cron import ( + "context" "encoding/json" + "git.hilo.cn/hilo-common/_const/rediskey" "git.hilo.cn/hilo-common/domain" "git.hilo.cn/hilo-common/resource/config" + "git.hilo.cn/hilo-common/resource/redisCli" "git.hilo.cn/hilo-common/sdk/tencentyun" "git.hilo.cn/hilo-common/utils" "github.com/robfig/cron" + "hilo-group/_const/enum/gift_e" "hilo-group/_const/enum/group_e" + "hilo-group/domain/model/gift_m" "hilo-group/domain/model/group_m" "hilo-group/domain/service/group_s" "time" @@ -85,3 +90,76 @@ func sendGroupSupportH5(model *domain.Model) error { } return nil } + +// 群组扶持计算-本周旧数据 +func CalcGroupSupport_OldData() { + if !config.IsMaster() { + return + } + c := cron.New() + spec := "0 0 15 23 8 ?" + _ = c.AddFunc(spec, func() { + defer utils.CheckGoPanic() + var model = domain.CreateModelNil() + //开始 + model.Log.Infof("CalcGroupSupport_OldData start") + + beginTime, _, period := group_m.GetSupportLevelTime(time.Now()) + endTime := time.Unix(1692964800, 0) // cwj---- + + g := gift_m.GiftOperate{SceneType: gift_e.GroupSceneType, Model: model} + records, err := g.BatchGetConsumeByRange(beginTime, endTime) + if err != nil { + model.Log.Errorf("CalcGroupSupport_OldData beginTime:%v, endTime:%v, err:%v", beginTime, endTime, err) + return + } + // 流水写入redis + keyDiamond := rediskey.GetGroupSupportConsumeSummary(period) + for _, r := range records { + if r.SceneUid == "" || r.Consume <= 0 { + continue + } + _, err = model.RedisCluster.ZIncrBy(context.Background(), keyDiamond, float64(r.Consume), r.SceneUid).Result() + if err != nil { + model.Log.Errorf("CalcGroupSupport_OldData groupSupport key:%s, val:%d, member:%s, err:%v", + keyDiamond, r.Consume, r.SceneUid, err) + } + } + err = redisCli.SetExpire(model.RedisCluster, keyDiamond, time.Hour*24*14) // 保留两周 + if err != nil { + model.Log.Errorf("CalcGroupSupport_OldData groupSupport key:%s, err:%v", keyDiamond, err) + return + } + // 支持者写入redis + for _, r := range records { + if r.SceneUid == "" || r.C <= 0 { + continue + } + // 支持者列表 + support := gift_m.GiftOperate{SceneType: gift_e.GroupSceneType, SceneUid: r.SceneUid, Model: model} + uids, err := support.BatchGetSupportList(beginTime, endTime) + if err != nil { + model.Log.Errorf("CalcGroupSupport_OldData beginTime:%v, endTime:%v, imGroupId:%v, err:%v", beginTime, endTime, r.SceneUid, err) + continue + } + if len(uids) <= 0 { + continue + } + // 支持者数量 + keySupportNum := rediskey.GetGroupSupportCountSupporter(period, r.SceneUid) + for _, uid := range uids { + err = model.RedisCluster.SAdd(context.Background(), keySupportNum, uid).Err() + if err != nil { + model.Log.Errorf("CalcGroupSupport_OldData groupSupport key:%s, UserId:%d, err:%v", keySupportNum, uid, err) + } + } + err = redisCli.SetExpire(model.RedisCluster, keySupportNum, time.Hour*24*14) // 保留两周 + if err != nil { + model.Log.Errorf("AddSendGiftEventAsync groupSupport key:%s, err:%v", keySupportNum, err) + } + } + + }) + + c.Start() +} diff --git a/domain/model/gift_m/gift.go b/domain/model/gift_m/gift.go index 51ebce1..6c71403 100644 --- a/domain/model/gift_m/gift.go +++ b/domain/model/gift_m/gift.go @@ -182,6 +182,17 @@ func (g *GiftOperate) BatchGetConsumeByRange(beginTime, endTime time.Time) ([]Sc return rows, nil } +func (g *GiftOperate) BatchGetSupportList(beginTime, endTime time.Time) ([]uint64, error) { + userIds := make([]uint64, 0) + err := g.DB().Model(g). + Select("DISTINCT(send_user_id) AS user_id"). + Where(g).Where("created_time BETWEEN ? AND ?", beginTime, endTime).Pluck("user_id", &userIds).Error + if err != nil { + return nil, err + } + return userIds, nil +} + func SumSendGift(model *domain.Model, sendUserId mysql.ID, giftId mysql.ID) (uint32, error) { type Result struct { N uint32 diff --git a/domain/service/event_s/event_init.go b/domain/service/event_s/event_init.go index 2ad245d..80446a8 100644 --- a/domain/service/event_s/event_init.go +++ b/domain/service/event_s/event_init.go @@ -475,7 +475,7 @@ func FlushHiloInfo(extId string, isVip bool, isPrettyCode bool, medals []uint32, func SendGift() { // 送礼事件-群组扶持数据累加 gift_ev.AddSendGiftEventAsync(func(model *domain.Model, event interface{}) error { - if time.Now().Unix() < 1692964800 { // cwj---- + if time.Now().Unix() <= 1692964800 { // cwj---- return nil } sendGiftEvent, ok := event.(*gift_ev.SendGiftEvent) -- 2.22.0