diff --git a/biz/adaptor/server.go b/biz/adaptor/server.go index 17551d7..1d3ba93 100644 --- a/biz/adaptor/server.go +++ b/biz/adaptor/server.go @@ -33,9 +33,6 @@ func (s *TradeServerImpl) GetStocks(ctx context.Context, req *trade.GetStocksReq return s.StockService.GetStocks(ctx, req) } -func (s *TradeServerImpl) CreateStock(ctx context.Context, req *trade.CreateStockReq) (resp *trade.CreateStockResp, err error) { - return s.StockService.CreateStock(ctx, req) -} -func (s *TradeServerImpl) UpdateStock(ctx context.Context, req *trade.UpdateStockReq) (resp *trade.UpdateStockResp, err error) { - return s.StockService.UpdateStock(ctx, req) +func (s *TradeServerImpl) AddStock(ctx context.Context, req *trade.AddStockReq) (resp *trade.AddStockResp, err error) { + return s.StockService.AddStock(ctx, req) } diff --git a/biz/application/service/stock.go b/biz/application/service/stock.go index 9e2a082..42d5a43 100644 --- a/biz/application/service/stock.go +++ b/biz/application/service/stock.go @@ -2,20 +2,19 @@ package service import ( "context" + "fmt" "github.com/CloudStriver/cloudmind-trade/biz/infrastructure/config" - stockmapper "github.com/CloudStriver/cloudmind-trade/biz/infrastructure/mapper/stock" 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" + "strconv" ) type StockService interface { GetStocks(ctx context.Context, req *gentrade.GetStocksReq) (resp *gentrade.GetStocksResp, err error) GetStock(ctx context.Context, req *gentrade.GetStockReq) (resp *gentrade.GetStockResp, err error) - UpdateStock(ctx context.Context, req *gentrade.UpdateStockReq) (resp *gentrade.UpdateStockResp, err error) - CreateStock(ctx context.Context, req *gentrade.CreateStockReq) (resp *gentrade.CreateStockResp, err error) + AddStock(ctx context.Context, req *gentrade.AddStockReq) (resp *gentrade.AddStockResp, err error) } var StockSet = wire.NewSet( @@ -24,59 +23,50 @@ var StockSet = wire.NewSet( ) type StockServiceImpl struct { - Config *config.Config - Redis *redis.Redis - StockMongoMapper stockmapper.IStockMongoMapper -} - -func (s *StockServiceImpl) CreateStock(ctx context.Context, req *gentrade.CreateStockReq) (resp *gentrade.CreateStockResp, err error) { - resp = new(gentrade.CreateStockResp) - oid, _ := primitive.ObjectIDFromHex(req.ProductId) - if _, err = s.StockMongoMapper.Insert(ctx, &stockmapper.Stock{ - ID: oid, - Amount: lo.ToPtr(req.Stock), - }); err != nil { - return resp, err - } - return resp, nil + Config *config.Config + Redis *redis.Redis } func (s *StockServiceImpl) GetStocks(ctx context.Context, req *gentrade.GetStocksReq) (resp *gentrade.GetStocksResp, err error) { resp = new(gentrade.GetStocksResp) - stocks, err := s.StockMongoMapper.FindMany(ctx, req.ProductIds) + + req.ProductIds = lo.Map(req.ProductIds, func(item string, _ int) string { + return fmt.Sprintf("stock:%s", item) + }) + + stocks, err := s.Redis.MgetCtx(ctx, req.ProductIds...) if err != nil { return resp, err } - index := make(map[string]int, len(req.ProductIds)) - lo.ForEach[string](req.ProductIds, func(v string, i int) { - index[v] = i - }) - - resp.Stocks = make([]int64, len(req.ProductIds), len(req.ProductIds)) - lo.ForEach[*stockmapper.Stock](stocks, func(v *stockmapper.Stock, _ int) { - resp.Stocks[index[v.ID.Hex()]] = *v.Amount + resp.Stocks = lo.Map[string, int64](stocks, func(item string, _ int) int64 { + if item == "" { + return 0 + } else { + stock, _ := strconv.ParseInt(item, 10, 64) + return stock + } }) return resp, nil } func (s *StockServiceImpl) GetStock(ctx context.Context, req *gentrade.GetStockReq) (resp *gentrade.GetStockResp, err error) { resp = new(gentrade.GetStockResp) - stock, err := s.StockMongoMapper.FindOne(ctx, req.ProductId) + val, err := s.Redis.GetCtx(ctx, fmt.Sprintf("stock:%s", req.ProductId)) if err != nil { return resp, err } - resp.Stock = *stock.Amount + if val == "" { + resp.Stock = 0 + return resp, nil + } + resp.Stock, _ = strconv.ParseInt(val, 10, 64) return resp, nil } -func (s *StockServiceImpl) UpdateStock(ctx context.Context, req *gentrade.UpdateStockReq) (resp *gentrade.UpdateStockResp, err error) { - resp = new(gentrade.UpdateStockResp) - oid, _ := primitive.ObjectIDFromHex(req.ProductId) - if _, err = s.StockMongoMapper.Update(ctx, &stockmapper.Stock{ - ID: oid, - Amount: lo.ToPtr(req.Amount), - }, lo.ToPtr(req.OldAmount)); err != nil { +func (s *StockServiceImpl) AddStock(ctx context.Context, req *gentrade.AddStockReq) (resp *gentrade.AddStockResp, err error) { + resp = new(gentrade.AddStockResp) + if _, err := s.Redis.IncrbyCtx(ctx, fmt.Sprintf("stock:%s", req.ProductId), req.Amount); err != nil { return resp, err } return resp, nil diff --git a/go.mod b/go.mod index a2be46e..539391e 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-20240119095418-bddcb5ccb1c3 + github.com/CloudStriver/service-idl-gen-go v0.0.0-20240130020155-fb47d6574085 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 cbbc91e..9335d12 100644 --- a/go.sum +++ b/go.sum @@ -6,8 +6,9 @@ github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03 github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo= github.com/CloudStriver/go-pkg v0.0.0-20240115102515-f1d7bfa047af h1:tl3MgdfL4fOkZxFYqqLXYr6dEk5TvT/hGe06uoB+spY= github.com/CloudStriver/go-pkg v0.0.0-20240115102515-f1d7bfa047af/go.mod h1:RMjN80WnoDiqHZIsv27u9BxJ9axldr+elFRHgSjhXnY= -github.com/CloudStriver/service-idl-gen-go v0.0.0-20240119095418-bddcb5ccb1c3 h1:W1BXEGDRI6Jh8VDK0+iT4IwdK0bEMpclCAx6aJdCWLI= 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/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= diff --git a/provider/provider.go b/provider/provider.go index 9a11508..9163e5f 100644 --- a/provider/provider.go +++ b/provider/provider.go @@ -4,7 +4,6 @@ import ( "github.com/CloudStriver/cloudmind-trade/biz/application/service" "github.com/CloudStriver/cloudmind-trade/biz/infrastructure/config" "github.com/CloudStriver/cloudmind-trade/biz/infrastructure/mapper/balance" - "github.com/CloudStriver/cloudmind-trade/biz/infrastructure/mapper/stock" "github.com/CloudStriver/cloudmind-trade/biz/infrastructure/stores/redis" "github.com/google/wire" ) @@ -26,6 +25,5 @@ var InfrastructureSet = wire.NewSet( ) var MapperSet = wire.NewSet( - stock.NewMongoMapper, balance.NewMongoMapper, ) diff --git a/provider/wire_gen.go b/provider/wire_gen.go index 450a42d..5744fe6 100644 --- a/provider/wire_gen.go +++ b/provider/wire_gen.go @@ -11,7 +11,6 @@ import ( "github.com/CloudStriver/cloudmind-trade/biz/application/service" "github.com/CloudStriver/cloudmind-trade/biz/infrastructure/config" "github.com/CloudStriver/cloudmind-trade/biz/infrastructure/mapper/balance" - "github.com/CloudStriver/cloudmind-trade/biz/infrastructure/mapper/stock" "github.com/CloudStriver/cloudmind-trade/biz/infrastructure/stores/redis" ) @@ -29,11 +28,9 @@ func NewTradeServerImpl() (*adaptor.TradeServerImpl, error) { Redis: redisRedis, BalanceMongoMapper: iBalanceMongoMapper, } - iStockMongoMapper := stock.NewMongoMapper(configConfig) stockServiceImpl := &service.StockServiceImpl{ - Config: configConfig, - Redis: redisRedis, - StockMongoMapper: iStockMongoMapper, + Config: configConfig, + Redis: redisRedis, } tradeServerImpl := &adaptor.TradeServerImpl{ Config: configConfig,