From 2f84fbf4aefb1fa79efd0400709f6115b0543e71 Mon Sep 17 00:00:00 2001 From: shenjiachi Date: Mon, 23 Dec 2024 18:11:36 +0800 Subject: [PATCH] fix: update to count team assistance data --- consume/egg_energy_team_assistance_consume.go | 71 ++++++++++++------- ...egg_energy_team_assistance_consume_test.go | 3 +- go.mod | 2 +- 3 files changed, 49 insertions(+), 27 deletions(-) diff --git a/consume/egg_energy_team_assistance_consume.go b/consume/egg_energy_team_assistance_consume.go index 28f5c9d..7983dae 100644 --- a/consume/egg_energy_team_assistance_consume.go +++ b/consume/egg_energy_team_assistance_consume.go @@ -73,34 +73,44 @@ func handleEggEnergyTeamAssistanceDataConsume(msgData []byte) error { // return err //} - cacheKey := fmt.Sprintf("EggEnergyTeamAssistance:%d", msg.Uid) - if !cache.Exists(cacheKey) { - targetTime, err1 := time.Parse("2006-01-02 15:04:05", msg.SignEnd) - if err1 != nil { - fmt.Println("解析时间错误:", err1) - return err1 - } - diff := targetTime.Sub(time.Now()).Seconds() - _, err = cache.SetEx(cacheKey, nil, int(diff)) - if err != nil { - return err - } - } - - count, err1 := cache.GetBit(cacheKey, msg.AssistanceUid) - if err1 != nil { - return err1 + // 2.查询数据库数据用户是否已经助力过 + detailDb := implement.NewEggEnergyAssistanceDetailDb(db.Db) + exist, err := detailDb.AssistanceDetailExist(msg.Uid, msg.AssistanceUid, msg.SignId) + if err != nil { + return err } - if count == 0 { - _, err = cache.SetBit(cacheKey, msg.AssistanceUid, 1) - if err != nil { - return err + now := time.Now() + if !exist { + // 数据库显示没有助力过才需要二次确认 + cacheKey := fmt.Sprintf("EggEnergyTeamAssistance:%d", msg.Uid) + if cache.Exists(cacheKey) { + // 3. 判断 redis 该用户是否助力过 + count, err1 := cache.GetBit(cacheKey, msg.AssistanceUid) + if err1 != nil { + return err1 + } + if count != 0 { + // 3.1 该用户已经助力过 + exist = true + // 3.2 插入助力数据 + detail := model.EggEnergyAssistanceDetail{ + Uid: msg.Uid, + AssistedUid: msg.AssistanceUid, + SignId: msg.SignId, + CreatedAt: now.Format("2006-01-02 15:04:05"), + UpdatedAt: now.Format("2006-01-02 15:04:05"), + } + _, err = detailDb.AssistanceDetailInsert(&detail) + if err != nil { + return err + } + } } } - // 3. 变更数据 + // 4. 变更数据 assistanceDb := implement.NewEggEnergyTeamAssistanceDb(db.Db) - now := time.Now() + date := now.Format("2006-01-02") session := db.Db.Where("") teamAssistance, err := assistanceDb.EggEnergyTeamAssistanceGetOneByParamsBySession(session, map[string]interface{}{ @@ -127,9 +137,20 @@ func handleEggEnergyTeamAssistanceDataConsume(msgData []byte) error { } } else { teamAssistance.RewardScore = utils2.Float64ToStr(msg.AssistanceValue + utils2.StrToFloat64(teamAssistance.RewardScore)) - // 如果是第一条说明还没有被记录过, 助力人数 + 1 - if count == 0 { + // 如果未助力过, 助力人数 + 1 添加助力信息 + if !exist { teamAssistance.AssistedNum++ + m := model.EggEnergyAssistanceDetail{ + Uid: msg.Uid, + AssistedUid: msg.AssistanceUid, + SignId: msg.SignId, + CreatedAt: now.Format("2006-01-02 15:04:05"), + UpdatedAt: now.Format("2006-01-02 15:04:05"), + } + _, err = detailDb.AssistanceDetailInsert(&m) + if err != nil { + return err + } } teamAssistance.AssistedTimes++ _, err = assistanceDb.EggEnergyTeamAssistanceUpdate(teamAssistance.Id, teamAssistance, "assisted_num", "reward_score", "assisted_times") diff --git a/consume/egg_energy_team_assistance_consume_test.go b/consume/egg_energy_team_assistance_consume_test.go index a7822ed..3b1ef6e 100644 --- a/consume/egg_energy_team_assistance_consume_test.go +++ b/consume/egg_energy_team_assistance_consume_test.go @@ -33,10 +33,11 @@ func TestEggEnergyTeamAssistanceConsume(t *testing.T) { } defer ch.Release() ch.Publish(md2.EggEnergyExchange, md2.EggEnergyTeamAssistanceReq{ - AssistanceUid: 17, + AssistanceUid: 19, Uid: 2, AssistanceValue: 100, SignStart: "2024-12-20 08:00:00", SignEnd: "2024-12-21 08:00:00", + SignId: 13, }, md2.EggEnergyTeamAssistance) } diff --git a/go.mod b/go.mod index 355cb99..87e6fda 100644 --- a/go.mod +++ b/go.mod @@ -2,7 +2,7 @@ module applet go 1.19 -// replace code.fnuoos.com/EggPlanet/egg_models.git => E:/company/Egg/egg_models +replace code.fnuoos.com/EggPlanet/egg_models.git => E:/company/Egg/egg_models // replace code.fnuoos.com/EggPlanet/egg_system_rules.git => E:/company/Egg/egg_system_rules