Skip to content

Commit

Permalink
fix: 修改balance参数 (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
Lansongxx authored Feb 12, 2024
1 parent 2b776c3 commit 15af1d8
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 55 deletions.
34 changes: 21 additions & 13 deletions biz/application/service/balance.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -31,31 +31,39 @@ 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 {
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
}

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
Expand Down
5 changes: 5 additions & 0 deletions biz/infrastructure/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
20 changes: 0 additions & 20 deletions biz/infrastructure/convertor/convertor.go
Original file line number Diff line number Diff line change
@@ -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,
}
}
39 changes: 18 additions & 21 deletions biz/infrastructure/mapper/balance/mongo.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}

Expand Down
4 changes: 3 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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-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
Expand All @@ -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
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ 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/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=
Expand Down

0 comments on commit 15af1d8

Please sign in to comment.