From 7dcde6a448aea4c960f22571bda64e25f0927a95 Mon Sep 17 00:00:00 2001 From: Leufolium Date: Tue, 2 Jul 2024 17:15:54 +0800 Subject: [PATCH] by Robin at 20240702 --- app/mix/dao/mongo.go | 40 ++++++++++++++++++++++++++++++ app/mix/service/logic/worker_id.go | 22 ++++++++++++++++ app/mix/service/service.go | 16 ++++++++++-- dbstruct/worker_id.go | 7 ++++++ 4 files changed, 83 insertions(+), 2 deletions(-) diff --git a/app/mix/dao/mongo.go b/app/mix/dao/mongo.go index 8d1a3f41..eeb1b811 100644 --- a/app/mix/dao/mongo.go +++ b/app/mix/dao/mongo.go @@ -13,7 +13,9 @@ import ( "github.com/gin-gonic/gin" "github.com/qiniu/qmgo" + "github.com/qiniu/qmgo/options" "go.mongodb.org/mongo-driver/bson" + options2 "go.mongodb.org/mongo-driver/mongo/options" accountproto "service/api/proto/account/proto" account_cancellationproto "service/api/proto/account_cancellation/proto" @@ -5330,6 +5332,44 @@ func (m *Mongo) GetWorkerIdByMid(ctx *gin.Context, req *workeridproto.OpListByMi return workerId, err } +func (m *Mongo) UpsertWorkerIdByWorkerId(ctx *gin.Context, worker_id *dbstruct.WorkerId) error { + col := m.getColWorkerId() + + query := qmgo.M{ + "worker_id": worker_id.GetWorkerId(), + } + + set := util.EntityToM(worker_id) + set["_id"] = worker_id.GetId() + + up := qmgo.M{ + "$setOnInsert": set, + } + + opts := options2.Update().SetUpsert(true) + + err := col.UpdateOne(ctx, query, up, options.UpdateOptions{ + UpdateHook: qmgo.M{}, + UpdateOptions: opts, + }) + + return err +} + +func (m *Mongo) UpdateWorkerIdByWorkerId(ctx *gin.Context, worker_id *dbstruct.WorkerId, workerId string) error { + col := m.getColWorkerId() + set := util.EntityToM(worker_id) + set["ut"] = time.Now().Unix() + up := qmgo.M{ + "$set": set, + } + filter := qmgo.M{ + "worker_id": workerId, + } + err := col.UpdateOne(ctx, filter, up) + return err +} + // 慧用工下发打款历史表相关 func (m *Mongo) CreateSingleDistributeHis(ctx *gin.Context, single_distribute_his *dbstruct.SingleDistributeHis) error { col := m.getColSingleDistributeHis() diff --git a/app/mix/service/logic/worker_id.go b/app/mix/service/logic/worker_id.go index 4dd53019..32d89af4 100644 --- a/app/mix/service/logic/worker_id.go +++ b/app/mix/service/logic/worker_id.go @@ -63,3 +63,25 @@ func (p *WorkerId) OpListByMid(ctx *gin.Context, req *worker_idproto.OpListByMid } return workerId, nil } + +func (p *WorkerId) OpUpsert(ctx *gin.Context, req *worker_idproto.OpCreateReq) error { + req.WorkerId.Id = goproto.Int64(idgenerator.GenWorkerIdId()) + req.WorkerId.Ct = goproto.Int64(time.Now().Unix()) + req.WorkerId.Ut = goproto.Int64(time.Now().Unix()) + req.WorkerId.DelFlag = goproto.Int64(consts.Exist) + err := p.store.UpsertWorkerIdByWorkerId(ctx, req.WorkerId) + if err != nil { + logger.Error("UpsertWorkerIdByWorkerId fail, err: %v", err) + return err + } + return nil +} + +func (p *WorkerId) OpUpdateByWorkerId(ctx *gin.Context, req *worker_idproto.OpUpdateReq, workerId string) error { + err := p.store.UpdateWorkerIdByWorkerId(ctx, req.WorkerId, workerId) + if err != nil { + logger.Error("UpdateWorkerIdByWorkerId fail, err: %v", err) + return err + } + return nil +} diff --git a/app/mix/service/service.go b/app/mix/service/service.go index dcf2c741..9a560786 100644 --- a/app/mix/service/service.go +++ b/app/mix/service/service.go @@ -4168,8 +4168,8 @@ func (s *Service) SaveAgreeCallback(ctx *gin.Context, req *hvyogoproto.ExtAgreeC return } - // 存入数据 - err = _DefaultWorkerId.OpCreate(ctx, &workeridproto.OpCreateReq{ + // 预创建 + err = _DefaultWorkerId.OpUpsert(ctx, &workeridproto.OpCreateReq{ WorkerId: &dbstruct.WorkerId{ Mid: list[0].Mid, WorkerId: goproto.String(vo.WorkerId), @@ -4181,6 +4181,18 @@ func (s *Service) SaveAgreeCallback(ctx *gin.Context, req *hvyogoproto.ExtAgreeC return } + // 重写 + err = _DefaultWorkerId.OpUpdateByWorkerId(ctx, &workeridproto.OpUpdateReq{ + WorkerId: &dbstruct.WorkerId{ + Mid: list[0].Mid, + }, + }, vo.WorkerId) + if err != nil { + logger.Error("_DefaultWorkerId OpUpdateByWorkerId fail, req: %v, err: %v", util.ToJson(req), err) + ec = errcode.ErrCodeWorkerIdSrvFail + return + } + return } diff --git a/dbstruct/worker_id.go b/dbstruct/worker_id.go index 35800aa2..a1f6e732 100644 --- a/dbstruct/worker_id.go +++ b/dbstruct/worker_id.go @@ -9,6 +9,13 @@ type WorkerId struct { DelFlag *int64 `json:"del_flag" bson:"del_flag"` // 删除标记 } +func (p *WorkerId) GetId() int64 { + if p == nil || p.Id == nil { + return 0 + } + return *p.Id +} + func (p *WorkerId) GetWorkerId() string { if p == nil || p.WorkerId == nil { return ""