From 081e6e4653d96fc9b8d133cafeb5d8f94d7fb492 Mon Sep 17 00:00:00 2001 From: lizhen Date: Tue, 31 Oct 2023 14:21:26 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=B3=A8=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- dao/id_sequence.go | 11 ++++++++--- logic/idsequence/sequence.go | 13 +++++++++---- 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/dao/id_sequence.go b/dao/id_sequence.go index b671c02..2291484 100644 --- a/dao/id_sequence.go +++ b/dao/id_sequence.go @@ -23,9 +23,14 @@ func (d *idSequenceDao) SetModel(model *model.IdSequence) *idSequenceDao { return tx } -func (d *idSequenceDao) UpdateByCond(ctx context.Context, version int, update map[string]interface{}) (int64, error) { - db := d.Db.WithContext(ctx).Table(d.TableName).Where("version = (?)", version). - Updates(update) +func (d *idSequenceDao) UpdateByCond(ctx context.Context, cond map[string]interface{}, update map[string]interface{}) (int64, error) { + db := d.Db.WithContext(ctx).Table(d.TableName) + + for k, v := range cond { + db = db.Where(k+" = ?", v) + } + + db = db.Updates(update) if db.Error != nil { return 0, db.Error } diff --git a/logic/idsequence/sequence.go b/logic/idsequence/sequence.go index a15395e..61cd971 100644 --- a/logic/idsequence/sequence.go +++ b/logic/idsequence/sequence.go @@ -147,7 +147,10 @@ func (is *IdSequence) getNewIdListSync(ctx context.Context, curMaxIdCh, newMaxId newMaxId := record.Value + is.idListLength version := record.Version + 1 - rowsEffect, err := idSequenceDao.UpdateByCond(ctx, record.Version, map[string]interface{}{ + rowsEffect, err := idSequenceDao.UpdateByCond(ctx, map[string]interface{}{ + "version": record.Version, + "biz": is.biz, + }, map[string]interface{}{ "value": newMaxId, "version": version, }) @@ -166,12 +169,14 @@ func (is *IdSequence) getNewIdListSync(ctx context.Context, curMaxIdCh, newMaxId errCh <- nil } +// saveLastId - save the last id when the process is being stopped func (is *IdSequence) saveLastId(ctx context.Context) { lastId, ok := <-is.ids if ok && lastId != 0 { - dao.NewIdSequenceDao().UpdateByCond(ctx, 0, map[string]interface{}{ - "value": lastId, - "version": 0, + dao.NewIdSequenceDao().UpdateByCond(ctx, map[string]interface{}{ + "biz": is.biz, + }, map[string]interface{}{ + "value": lastId, }) } } -- Gitee