From 25c00d507242297675b12d76354392dd8b68aa1d Mon Sep 17 00:00:00 2001 From: ayflying Date: Mon, 21 Jul 2025 19:20:46 +0800 Subject: [PATCH] =?UTF-8?q?gameact=E6=8C=81=E4=B9=85=E5=8C=96=E4=BF=9D?= =?UTF-8?q?=E5=AD=98=E5=8C=BA=E5=88=86=E8=B7=9F=E6=96=B0=E4=B8=8E=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/logic/gameAct/gameAct.go | 41 +++++++++++++++++++++++++------ 1 file changed, 34 insertions(+), 7 deletions(-) diff --git a/internal/logic/gameAct/gameAct.go b/internal/logic/gameAct/gameAct.go index d7c35b5..7b52c39 100644 --- a/internal/logic/gameAct/gameAct.go +++ b/internal/logic/gameAct/gameAct.go @@ -134,7 +134,8 @@ func (s *sGameAct) Save(ctx context.Context, actId int) (err error) { //循环获取缓存数据 err = tools.Redis.RedisScanV2(cacheKey, func(keys []string) (err error) { - var add []interface{} + var add = make([]*entity.GameAct, 0) + var update = make([]*entity.GameAct, 0) var delKey []string for _, cacheKey = range keys { result := strings.Split(cacheKey, ":") @@ -176,25 +177,51 @@ func (s *sGameAct) Save(ctx context.Context, actId int) (err error) { } actionData := cacheGet.String() if data == nil { - //data = - add = append(add, &do.GameAct{ + add = append(add, &entity.GameAct{ ActId: actId, Uid: uid, Action: actionData, }) } else { //覆盖数据 + data.ActId = actId + data.Uid = uid data.Action = actionData - add = append(add, data) + update = append(update, data) } //最后删除key delKey = append(delKey, cacheKey) } //批量写入数据库 - if len(add) > 0 { - dbRes, err2 := g.Model(Name).Batch(30).Data(add).Save() - add = make([]interface{}, 0) + if len(delKey) > 0 { + for _, v := range update { + v.UpdatedAt = gtime.Now() + _, err2 := g.Model(Name).Where(do.GameAct{ + Uid: v.Uid, + ActId: v.ActId, + UpdatedAt: v.UpdatedAt, + }).Data(v).Update() + if err2 != nil { + g.Log().Error(ctx, err2) + return + } + ////获取多少个数据,删除不是当前修改的数据 + //count, _ := g.Model(Name).Where(do.GameAct{ + // Uid: v.Uid, + // ActId: v.ActId, + //}).Count() + //if count > 1 { + // g.Model(Name).Where(do.GameAct{ + // Uid: v.Uid, + // ActId: v.ActId, + // }).WhereNot("updated_at", v.UpdatedAt).Delete() + //} + } + //dbRes, err2 := g.Model(Name).Batch(50).Data(add).Update() + update = make([]*entity.GameAct, 0) + dbRes, err2 := g.Model(Name).Batch(50).Data(add).Save() + add = make([]*entity.GameAct, 0) if err2 != nil { g.Log().Error(ctx, err2) return