From d07350969feea3c568c848ea4789f54093a564d1 Mon Sep 17 00:00:00 2001 From: laodaming <11058467+laudamine@user.noreply.gitee.com> Date: Tue, 26 Sep 2023 16:36:35 +0800 Subject: [PATCH] fix --- server/product/internal/config/config.go | 18 +++++++ .../logic/calculateproductpricelogic.go | 11 +++-- server/product/internal/svc/servicecontext.go | 26 ++++++++-- .../internal/logic/calculatecartpricelogic.go | 7 ++- .../internal/logic/getcartslogic.go | 8 +-- service/repositories/order.go | 2 +- utils/step_price/price.go | 49 ------------------- 7 files changed, 56 insertions(+), 65 deletions(-) diff --git a/server/product/internal/config/config.go b/server/product/internal/config/config.go index 31e07893..acda3411 100644 --- a/server/product/internal/config/config.go +++ b/server/product/internal/config/config.go @@ -11,4 +11,22 @@ type Config struct { SourceMysql string Auth types.Auth ReplicaId uint64 + AWS struct { + S3 struct { + Credentials struct { + AccessKeyID string + Secret string + Token string + } + } + } + BLMService struct { + Url string + LogoCombine struct { + Url string + } + } + Unity struct { + Host string + } } diff --git a/server/product/internal/logic/calculateproductpricelogic.go b/server/product/internal/logic/calculateproductpricelogic.go index 0e0e3309..22bb653a 100644 --- a/server/product/internal/logic/calculateproductpricelogic.go +++ b/server/product/internal/logic/calculateproductpricelogic.go @@ -8,7 +8,6 @@ import ( "fusenapi/utils/auth" "fusenapi/utils/basic" "fusenapi/utils/format" - "fusenapi/utils/step_price" "gorm.io/gorm" "context" @@ -65,9 +64,11 @@ func (l *CalculateProductPriceLogic) CalculateProductPrice(req *types.CalculateP return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get model info") } var stepPrice gmodel.StepPriceJsonStruct - if err = json.Unmarshal(*modelInfo.StepPrice, &stepPrice); err != nil { - logx.Error(err) - return resp.SetStatusWithMessage(basic.CodeJsonErr, "failed to parse step price") + if modelInfo.StepPrice != nil && len(*modelInfo.StepPrice) != 0 { + if err = json.Unmarshal(*modelInfo.StepPrice, &stepPrice); err != nil { + logx.Error(err) + return resp.SetStatusWithMessage(basic.CodeJsonErr, "failed to parse step price") + } } //配件 fittingPrice := int64(0) @@ -82,7 +83,7 @@ func (l *CalculateProductPriceLogic) CalculateProductPrice(req *types.CalculateP } fittingPrice = *fittingInfo.Price } - totalPrice, itemPrice, err := step_price.GetNewCentStepPrice(req.PurchaseQuantity, stepPrice, fittingPrice) + totalPrice, itemPrice, err := l.svcCtx.Repositories.NewShoppingCart.CaculateStepPrice(req.PurchaseQuantity, stepPrice, fittingPrice) if err != nil { logx.Error(err) return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to calculate product price ") diff --git a/server/product/internal/svc/servicecontext.go b/server/product/internal/svc/servicecontext.go index 9f2456cf..5fa8539d 100644 --- a/server/product/internal/svc/servicecontext.go +++ b/server/product/internal/svc/servicecontext.go @@ -5,6 +5,9 @@ import ( "fmt" "fusenapi/server/product/internal/config" "fusenapi/shared" + "github.com/aws/aws-sdk-go/aws" + "github.com/aws/aws-sdk-go/aws/credentials" + "github.com/aws/aws-sdk-go/aws/session" "net/http" "fusenapi/initalize" @@ -18,15 +21,28 @@ type ServiceContext struct { Config config.Config SharedState *shared.SharedState - MysqlConn *gorm.DB - AllModels *gmodel.AllModelsGen + MysqlConn *gorm.DB + AllModels *gmodel.AllModelsGen + Repositories *initalize.Repositories + AwsSession *session.Session } func NewServiceContext(c config.Config) *ServiceContext { + conn := initalize.InitMysql(c.SourceMysql) + config := aws.Config{ + Credentials: credentials.NewStaticCredentials(c.AWS.S3.Credentials.AccessKeyID, c.AWS.S3.Credentials.Secret, c.AWS.S3.Credentials.Token), + } + return &ServiceContext{ - Config: c, - MysqlConn: initalize.InitMysql(c.SourceMysql), - AllModels: gmodel.NewAllModels(initalize.InitMysql(c.SourceMysql)), + Config: c, + MysqlConn: initalize.InitMysql(c.SourceMysql), + AllModels: gmodel.NewAllModels(initalize.InitMysql(c.SourceMysql)), + AwsSession: session.Must(session.NewSession(&config)), + Repositories: initalize.NewAllRepositories(&initalize.NewAllRepositorieData{ + GormDB: conn, + BLMServiceUrl: &c.BLMService.Url, + AwsSession: session.Must(session.NewSession(&config)), + }), } } diff --git a/server/shopping-cart/internal/logic/calculatecartpricelogic.go b/server/shopping-cart/internal/logic/calculatecartpricelogic.go index 8a9c7a8b..0f03bbb7 100644 --- a/server/shopping-cart/internal/logic/calculatecartpricelogic.go +++ b/server/shopping-cart/internal/logic/calculatecartpricelogic.go @@ -98,8 +98,11 @@ func (l *CalculateCartPriceLogic) CalculateCartPrice(req *types.CalculateCartPri return err } var stepPrice gmodel.StepPriceJsonStruct - if err = json.Unmarshal(*modelInfo.StepPrice, &stepPrice); err != nil { - return err + if modelInfo.StepPrice != nil && len(*modelInfo.StepPrice) != 0 { + if err = json.Unmarshal(*modelInfo.StepPrice, &stepPrice); err != nil { + logx.Error(err) + return err + } } //请求的数量 reqPurchaseQuantity := mapCalculateQuantity[cart.Id].PurchaseQuantity diff --git a/server/shopping-cart/internal/logic/getcartslogic.go b/server/shopping-cart/internal/logic/getcartslogic.go index f715bab3..8240ba5c 100644 --- a/server/shopping-cart/internal/logic/getcartslogic.go +++ b/server/shopping-cart/internal/logic/getcartslogic.go @@ -104,9 +104,11 @@ func (l *GetCartsLogic) GetCarts(req *types.GetCartsReq, userinfo *auth.UserInfo return resp.SetStatusWithMessage(basic.CodeServiceErr, fmt.Sprintf("the size`s model info is not exists:%d_%d", *cart.ProductId, *cart.SizeId)) } var stepPrice gmodel.StepPriceJsonStruct - if err = json.Unmarshal(*modelInfo.StepPrice, &stepPrice); err != nil { - logx.Error(err) - return resp.SetStatusWithMessage(basic.CodeJsonErr, fmt.Sprintf("failed to parse model step price:%d", *cart.ModelId)) + if modelInfo.StepPrice != nil && len(*modelInfo.StepPrice) != 0 { + if err = json.Unmarshal(*modelInfo.StepPrice, &stepPrice); err != nil { + logx.Error(err) + return resp.SetStatusWithMessage(basic.CodeJsonErr, fmt.Sprintf("failed to parse model step price:%d", *cart.ModelId)) + } } //购买数量步进量 stepPurchaseQuantity := *modelInfo.PackedUnit diff --git a/service/repositories/order.go b/service/repositories/order.go index bb03aaca..de1c373e 100644 --- a/service/repositories/order.go +++ b/service/repositories/order.go @@ -843,7 +843,7 @@ func (d *defaultOrder) Create(ctx context.Context, in *CreateReq) (res *CreateRe } var stepPriceJson gmodel.StepPriceJsonStruct - if shoppingCartProductModel3d.StepPrice != nil { + if shoppingCartProductModel3d.StepPrice != nil && len(*shoppingCartProductModel3d.StepPrice) != 0 { json.Unmarshal(*shoppingCartProductModel3d.StepPrice, &shoppingCartProductModel3d.StepPrice) } else { errorCode = *basic.CodeErrOrderCreatProductPriceAbsent diff --git a/utils/step_price/price.go b/utils/step_price/price.go index 8230f39f..79b7047d 100644 --- a/utils/step_price/price.go +++ b/utils/step_price/price.go @@ -1,10 +1,5 @@ package step_price -import ( - "errors" - "fusenapi/model/gmodel" -) - // 旧的返回厘(即将废弃) func GetCentStepPrice(minBuyNum int, stepNum []int, stepPrice []int) int64 { if minBuyNum > stepNum[len(stepNum)-1] { @@ -20,47 +15,3 @@ func GetCentStepPrice(minBuyNum int, stepNum []int, stepPrice []int) int64 { } return int64(stepPrice[len(stepPrice)-1]) } - -// 新的阶梯价格(返回美元) -func GetNewStepPrice(purchaseQuantity int64, stepPrice gmodel.StepPriceJsonStruct, fittingPrice int64) (totalPrice, itemPrice float64, err error) { - l := len(stepPrice.PriceRange) - if l == 0 { - return 0, 0, errors.New("price range is not set") - } - //遍历查询合适的价格 - for k, v := range stepPrice.PriceRange { - //购买数量>起点 - if purchaseQuantity > v.StartQuantity { - //最后一个 || 小于等于终点 - if k == l-1 || purchaseQuantity <= v.EndQuantity { - itemPrice = float64(v.Price+fittingPrice) / 1000 - return itemPrice * float64(purchaseQuantity), itemPrice / 1000, nil - } - } - } - //遍历里面没有则返回第一个 - itemPrice = float64(stepPrice.PriceRange[0].Price+fittingPrice) / 1000 - return itemPrice * float64(purchaseQuantity), itemPrice, nil -} - -// 新的阶梯价格(返回厘) -func GetNewCentStepPrice(purchaseQuantity int64, stepPrice gmodel.StepPriceJsonStruct, fittingPrice int64) (totalPrice, itemPrice int64, err error) { - l := len(stepPrice.PriceRange) - if l == 0 { - return 0, 0, errors.New("price range is not set") - } - //遍历查询合适的价格 - for k, v := range stepPrice.PriceRange { - //购买数量>起点 - if purchaseQuantity > v.StartQuantity { - //最后一个 || 小于等于终点 - if k == l-1 || purchaseQuantity <= v.EndQuantity { - itemPrice = v.Price + fittingPrice - return itemPrice * purchaseQuantity, itemPrice, nil - } - } - } - //遍历里面没有则返回第一个 - itemPrice = stepPrice.PriceRange[0].Price + fittingPrice - return itemPrice * purchaseQuantity, itemPrice, nil -}