From 1c32b5fc975b8f9b775b9d3b5696047a4642178b Mon Sep 17 00:00:00 2001 From: Lansong <2095621390@qq.com> Date: Mon, 12 Feb 2024 16:58:57 +0800 Subject: [PATCH 1/2] =?UTF-8?q?fix:=20=E4=BF=AE=E6=94=B9balance=E5=8F=82?= =?UTF-8?q?=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- biz/application/service/balance.go | 43 +++++++++++++++-------- biz/infrastructure/config/config.go | 5 +++ biz/infrastructure/convertor/convertor.go | 20 ----------- go.mod | 4 ++- go.sum | 2 ++ 5 files changed, 39 insertions(+), 35 deletions(-) diff --git a/biz/application/service/balance.go b/biz/application/service/balance.go index 1a32692..0fe5c07 100644 --- a/biz/application/service/balance.go +++ b/biz/application/service/balance.go @@ -3,10 +3,10 @@ package service import ( "context" "github.com/CloudStriver/cloudmind-trade/biz/infrastructure/config" - "github.com/CloudStriver/cloudmind-trade/biz/infrastructure/convertor" balancemapper "github.com/CloudStriver/cloudmind-trade/biz/infrastructure/mapper/balance" gentrade "github.com/CloudStriver/service-idl-gen-go/kitex_gen/cloudmind/trade" "github.com/google/wire" + "github.com/samber/lo" "github.com/zeromicro/go-zero/core/stores/redis" "go.mongodb.org/mongo-driver/bson/primitive" ) @@ -29,33 +29,48 @@ type BalanceServiceImpl struct { } func (s *BalanceServiceImpl) UpdateBalance(ctx context.Context, req *gentrade.UpdateBalanceReq) (resp *gentrade.UpdateBalanceResp, err error) { - resp = new(gentrade.UpdateBalanceResp) oid, _ := primitive.ObjectIDFromHex(req.UserId) - oldBalance := convertor.BalanceToBalanceMapper(req.OldBalance) - oldBalance.ID = oid - if _, err = s.BalanceMongoMapper.Update(ctx, convertor.BalanceToBalanceMapper(req.Balance), oldBalance); err != nil { + oldbalance := &balancemapper.Balance{ + ID: oid, + } + balance := &balancemapper.Balance{} + switch req.BalanceType { + case gentrade.BalanceType_FlowBalanceType: + oldbalance.Flow = lo.ToPtr(req.Oldbalance) + balance.Flow = lo.ToPtr(req.Balance) + case gentrade.BalanceType_MemoryBalanceType: + oldbalance.Memory = lo.ToPtr(req.Oldbalance) + balance.Memory = lo.ToPtr(req.Balance) + case gentrade.BalanceType_PointBalanceType: + oldbalance.Point = lo.ToPtr(req.Oldbalance) + balance.Point = lo.ToPtr(req.Balance) + } + if _, err = s.BalanceMongoMapper.Update(ctx, balance, oldbalance); err != nil { return resp, err } return resp, nil } func (s *BalanceServiceImpl) GetBalance(ctx context.Context, req *gentrade.GetBalanceReq) (resp *gentrade.GetBalanceResp, err error) { - resp = new(gentrade.GetBalanceResp) - stock, err := s.BalanceMongoMapper.FindOne(ctx, req.UserId) + balance, err := s.BalanceMongoMapper.FindOne(ctx, req.UserId) if err != nil { return resp, err } - resp.Balance = convertor.BalanceMapperToBalance(stock) - return resp, nil + return &gentrade.GetBalanceResp{ + Flow: *balance.Flow, + Memory: *balance.Memory, + Point: *balance.Point, + }, nil } func (s *BalanceServiceImpl) CreateBalance(ctx context.Context, req *gentrade.CreateBalanceReq) (resp *gentrade.CreateBalanceResp, err error) { - resp = new(gentrade.CreateBalanceResp) oid, _ := primitive.ObjectIDFromHex(req.UserId) - balance := convertor.BalanceToBalanceMapper(req.Balance) - balance.ID = oid - - if _, err = s.BalanceMongoMapper.Insert(ctx, balance); err != nil { + if _, err = s.BalanceMongoMapper.Insert(ctx, &balancemapper.Balance{ + ID: oid, + Flow: lo.ToPtr(s.Config.Balance.DefaultFlow), + Memory: lo.ToPtr(s.Config.Balance.DefaultMemory), + Point: lo.ToPtr(s.Config.Balance.DefaultPoint), + }); err != nil { return resp, err } return resp, nil diff --git a/biz/infrastructure/config/config.go b/biz/infrastructure/config/config.go index 8d33527..0de578c 100644 --- a/biz/infrastructure/config/config.go +++ b/biz/infrastructure/config/config.go @@ -17,6 +17,11 @@ type Config struct { } CacheConf cache.CacheConf Redis *redis.RedisConf + Balance struct { + DefaultFlow int64 + DefaultMemory int64 + DefaultPoint int64 + } } func NewConfig() (*Config, error) { diff --git a/biz/infrastructure/convertor/convertor.go b/biz/infrastructure/convertor/convertor.go index a6b97bb..4d61c86 100644 --- a/biz/infrastructure/convertor/convertor.go +++ b/biz/infrastructure/convertor/convertor.go @@ -1,22 +1,2 @@ package convertor -import ( - balancemapper "github.com/CloudStriver/cloudmind-trade/biz/infrastructure/mapper/balance" - gentrade "github.com/CloudStriver/service-idl-gen-go/kitex_gen/cloudmind/trade" -) - -func BalanceMapperToBalance(in *balancemapper.Balance) *gentrade.Balance { - return &gentrade.Balance{ - Flow: in.Flow, - Memory: in.Memory, - Point: in.Point, - } -} - -func BalanceToBalanceMapper(in *gentrade.Balance) *balancemapper.Balance { - return &balancemapper.Balance{ - Flow: in.Flow, - Memory: in.Memory, - Point: in.Point, - } -} diff --git a/go.mod b/go.mod index 539391e..6433ecd 100644 --- a/go.mod +++ b/go.mod @@ -4,7 +4,7 @@ go 1.20 require ( github.com/CloudStriver/go-pkg v0.0.0-20240115102515-f1d7bfa047af - github.com/CloudStriver/service-idl-gen-go v0.0.0-20240130020155-fb47d6574085 + github.com/CloudStriver/service-idl-gen-go v0.0.0-20240212085733-66265d95908a github.com/cloudwego/kitex v0.8.0 github.com/google/wire v0.5.0 github.com/kitex-contrib/obs-opentelemetry v0.2.5 @@ -14,6 +14,8 @@ require ( google.golang.org/grpc v1.60.1 ) +//replace github.com/CloudStriver/service-idl-gen-go => ../service-idl-gen-go + require ( github.com/apache/thrift v0.16.0 // indirect github.com/beorn7/perks v1.0.1 // indirect diff --git a/go.sum b/go.sum index 9335d12..69a13fd 100644 --- a/go.sum +++ b/go.sum @@ -9,6 +9,8 @@ github.com/CloudStriver/go-pkg v0.0.0-20240115102515-f1d7bfa047af/go.mod h1:RMjN github.com/CloudStriver/service-idl-gen-go v0.0.0-20240119095418-bddcb5ccb1c3/go.mod h1:chtR82RvfrjUujTGWROSCNAwF9Lh/U959k34bXIDvBI= github.com/CloudStriver/service-idl-gen-go v0.0.0-20240130020155-fb47d6574085 h1:slsYIWCHTDv8Z5N+cZ3TY6fvmrsNuJhCrliljQOBP2w= github.com/CloudStriver/service-idl-gen-go v0.0.0-20240130020155-fb47d6574085/go.mod h1:chtR82RvfrjUujTGWROSCNAwF9Lh/U959k34bXIDvBI= +github.com/CloudStriver/service-idl-gen-go v0.0.0-20240212085733-66265d95908a h1:gju1wtbOsmP6XaBKKBU6OmCKNh+KDf9xPzZq4woM4UY= +github.com/CloudStriver/service-idl-gen-go v0.0.0-20240212085733-66265d95908a/go.mod h1:chtR82RvfrjUujTGWROSCNAwF9Lh/U959k34bXIDvBI= github.com/ajstarks/deck v0.0.0-20200831202436-30c9fc6549a9/go.mod h1:JynElWSGnm/4RlzPXRlREEwqTHAN3T56Bv2ITsFT3gY= github.com/ajstarks/deck/generate v0.0.0-20210309230005-c3f852c02e19/go.mod h1:T13YZdzov6OU0A1+RfKZiZN9ca6VeKdBdyDV+BY97Tk= github.com/ajstarks/svgo v0.0.0-20180226025133-644b8db467af/go.mod h1:K08gAheRH3/J6wwsYMMT4xOr94bZjxIelGM0+d/wbFw= From 312142afab85672849086cb5322d6cde91bec5ce Mon Sep 17 00:00:00 2001 From: Lansong <2095621390@qq.com> Date: Mon, 12 Feb 2024 18:33:14 +0800 Subject: [PATCH 2/2] =?UTF-8?q?fix:=20=E4=BF=AE=E6=94=B9updateBalance?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- biz/application/service/balance.go | 25 +++++--------- biz/infrastructure/mapper/balance/mongo.go | 39 ++++++++++------------ go.mod | 2 +- go.sum | 2 ++ 4 files changed, 30 insertions(+), 38 deletions(-) diff --git a/biz/application/service/balance.go b/biz/application/service/balance.go index 0fe5c07..d298e97 100644 --- a/biz/application/service/balance.go +++ b/biz/application/service/balance.go @@ -29,25 +29,18 @@ type BalanceServiceImpl struct { } func (s *BalanceServiceImpl) UpdateBalance(ctx context.Context, req *gentrade.UpdateBalanceReq) (resp *gentrade.UpdateBalanceResp, err error) { + resp = new(gentrade.UpdateBalanceResp) oid, _ := primitive.ObjectIDFromHex(req.UserId) - oldbalance := &balancemapper.Balance{ - ID: oid, - } - balance := &balancemapper.Balance{} - switch req.BalanceType { - case gentrade.BalanceType_FlowBalanceType: - oldbalance.Flow = lo.ToPtr(req.Oldbalance) - balance.Flow = lo.ToPtr(req.Balance) - case gentrade.BalanceType_MemoryBalanceType: - oldbalance.Memory = lo.ToPtr(req.Oldbalance) - balance.Memory = lo.ToPtr(req.Balance) - case gentrade.BalanceType_PointBalanceType: - oldbalance.Point = lo.ToPtr(req.Oldbalance) - balance.Point = lo.ToPtr(req.Balance) - } - if _, err = s.BalanceMongoMapper.Update(ctx, balance, oldbalance); err != nil { + result, err := s.BalanceMongoMapper.Update(ctx, &balancemapper.Balance{ + ID: oid, + Flow: req.Flow, + Memory: req.Memory, + Point: req.Point, + }) + if err != nil { return resp, err } + resp.Ok = result.ModifiedCount != 0 return resp, nil } diff --git a/biz/infrastructure/mapper/balance/mongo.go b/biz/infrastructure/mapper/balance/mongo.go index 4f4ff6c..b5a4bcc 100644 --- a/biz/infrastructure/mapper/balance/mongo.go +++ b/biz/infrastructure/mapper/balance/mongo.go @@ -19,10 +19,10 @@ var _ IBalanceMongoMapper = (*MongoMapper)(nil) type ( IBalanceMongoMapper interface { - Insert(ctx context.Context, data *Balance) (string, error) // 插入 - FindOne(ctx context.Context, id string) (*Balance, error) // 查找 - Update(ctx context.Context, data *Balance, oldBalance *Balance) (*mongo.UpdateResult, error) // 修改 - Delete(ctx context.Context, id string) (int64, error) // 删除 + Insert(ctx context.Context, data *Balance) (string, error) // 插入 + FindOne(ctx context.Context, id string) (*Balance, error) // 查找 + Update(ctx context.Context, data *Balance) (*mongo.UpdateResult, error) // 修改 + Delete(ctx context.Context, id string) (int64, error) // 删除 } Balance struct { ID primitive.ObjectID `bson:"_id,omitempty" json:"id,omitempty"` // 用户ID @@ -74,26 +74,23 @@ func (m *MongoMapper) FindOne(ctx context.Context, id string) (*Balance, error) } } -func (b *Balance) ToBson() bson.M { - m := bson.M{} - if b.ID != primitive.NilObjectID { - m[consts.ID] = b.ID - } - if b.Flow != nil { - m[consts.Flow] = b.Flow +func (m *MongoMapper) Update(ctx context.Context, data *Balance) (*mongo.UpdateResult, error) { + key := PrefixBalanceCacheKey + data.ID.Hex() + b := bson.M{} + filter := bson.M{consts.ID: data.ID} + if data.Flow != nil { + b[consts.Flow] = data.Flow + filter[consts.Flow] = bson.M{"$gte": data.Flow} } - if b.Memory != nil { - m[consts.Memory] = b.Memory + if data.Memory != nil { + b[consts.Memory] = data.Memory + filter[consts.Memory] = bson.M{"$gte": data.Memory} } - if b.Point != nil { - m[consts.Point] = b.Point + if data.Point != nil { + b[consts.Point] = data.Point + filter[consts.Point] = bson.M{"$gte": data.Point} } - return m -} - -func (m *MongoMapper) Update(ctx context.Context, data *Balance, oldBalance *Balance) (*mongo.UpdateResult, error) { - key := PrefixBalanceCacheKey + oldBalance.ID.Hex() - res, err := m.conn.UpdateOne(ctx, key, oldBalance.ToBson(), bson.M{"$set": data}) + res, err := m.conn.UpdateOne(ctx, key, filter, bson.M{"$inc": b}) return res, err } diff --git a/go.mod b/go.mod index 6433ecd..837c00e 100644 --- a/go.mod +++ b/go.mod @@ -4,7 +4,7 @@ go 1.20 require ( github.com/CloudStriver/go-pkg v0.0.0-20240115102515-f1d7bfa047af - github.com/CloudStriver/service-idl-gen-go v0.0.0-20240212085733-66265d95908a + github.com/CloudStriver/service-idl-gen-go v0.0.0-20240212102959-196f941e2b33 github.com/cloudwego/kitex v0.8.0 github.com/google/wire v0.5.0 github.com/kitex-contrib/obs-opentelemetry v0.2.5 diff --git a/go.sum b/go.sum index 69a13fd..7b9b69d 100644 --- a/go.sum +++ b/go.sum @@ -11,6 +11,8 @@ github.com/CloudStriver/service-idl-gen-go v0.0.0-20240130020155-fb47d6574085 h1 github.com/CloudStriver/service-idl-gen-go v0.0.0-20240130020155-fb47d6574085/go.mod h1:chtR82RvfrjUujTGWROSCNAwF9Lh/U959k34bXIDvBI= github.com/CloudStriver/service-idl-gen-go v0.0.0-20240212085733-66265d95908a h1:gju1wtbOsmP6XaBKKBU6OmCKNh+KDf9xPzZq4woM4UY= github.com/CloudStriver/service-idl-gen-go v0.0.0-20240212085733-66265d95908a/go.mod h1:chtR82RvfrjUujTGWROSCNAwF9Lh/U959k34bXIDvBI= +github.com/CloudStriver/service-idl-gen-go v0.0.0-20240212102959-196f941e2b33 h1:Zx24TxfcypK2pY7c8pW8Iin2uhElvkRaoY7WtPvwuEE= +github.com/CloudStriver/service-idl-gen-go v0.0.0-20240212102959-196f941e2b33/go.mod h1:chtR82RvfrjUujTGWROSCNAwF9Lh/U959k34bXIDvBI= github.com/ajstarks/deck v0.0.0-20200831202436-30c9fc6549a9/go.mod h1:JynElWSGnm/4RlzPXRlREEwqTHAN3T56Bv2ITsFT3gY= github.com/ajstarks/deck/generate v0.0.0-20210309230005-c3f852c02e19/go.mod h1:T13YZdzov6OU0A1+RfKZiZN9ca6VeKdBdyDV+BY97Tk= github.com/ajstarks/svgo v0.0.0-20180226025133-644b8db467af/go.mod h1:K08gAheRH3/J6wwsYMMT4xOr94bZjxIelGM0+d/wbFw=