diff --git a/server/shopping-cart/internal/logic/calculatecartpricelogic.go b/server/shopping-cart/internal/logic/calculatecartpricelogic.go index d3ed6a6b..6e2344de 100644 --- a/server/shopping-cart/internal/logic/calculatecartpricelogic.go +++ b/server/shopping-cart/internal/logic/calculatecartpricelogic.go @@ -6,15 +6,13 @@ import ( "fmt" "fusenapi/constants" "fusenapi/model/gmodel" + "fusenapi/server/shopping-cart/internal/svc" + "fusenapi/server/shopping-cart/internal/types" "fusenapi/utils/auth" "fusenapi/utils/basic" "fusenapi/utils/format" "fusenapi/utils/shopping_cart" "gorm.io/gorm" - "strings" - - "fusenapi/server/shopping-cart/internal/svc" - "fusenapi/server/shopping-cart/internal/types" "github.com/zeromicro/go-zero/core/logx" ) @@ -109,23 +107,6 @@ func (l *CalculateCartPriceLogic) CalculateCartPrice(req *types.CalculateCartPri if !ok { return errors.New(fmt.Sprintf("there carts contain some one which have no price info:%d_%d", *cart.ProductId, *cart.SizeId)) } - //阶梯数量切片 - stepNum, err := format.StrSlicToIntSlice(strings.Split(*sizePrice.StepNum, ",")) - if err != nil { - logx.Error(err) - return errors.New(fmt.Sprintf("failed to parse step number:%d_%d", *cart.ProductId, *cart.SizeId)) - } - lenStepNum := len(stepNum) - //阶梯价格切片 - stepPrice, err := format.StrSlicToIntSlice(strings.Split(*sizePrice.StepPrice, ",")) - if err != nil { - logx.Error(err) - return errors.New(fmt.Sprintf("failed to parse step price:%d_%d", *cart.ProductId, *cart.SizeId)) - } - lenStepPrice := len(stepPrice) - if lenStepPrice == 0 || lenStepNum == 0 { - return errors.New(fmt.Sprintf("step price or step number is not set:%d_%d", *cart.ProductId, *cart.SizeId)) - } //请求的数量 reqPurchaseQuantity := mapCalculateQuantity[cart.Id].PurchaseQuantity isSelected := int64(0) @@ -142,7 +123,11 @@ func (l *CalculateCartPriceLogic) CalculateCartPrice(req *types.CalculateCartPri } } //计算价格 - itemPrice, totalPrice := shopping_cart.CaculateCartPrice(reqPurchaseQuantity, stepPrice, stepNum, fittingPrice, *sizePrice.EachBoxNum) + itemPrice, totalPrice, _, _, err := shopping_cart.CaculateCartPrice(reqPurchaseQuantity, &sizePrice, fittingPrice) + if err != nil { + logx.Error(err) + return err + } calculateResultList = append(calculateResultList, types.CalculateResultItem{ CartId: cart.Id, ItemPrice: fmt.Sprintf("%.3f", format.CentitoDollar(itemPrice)), diff --git a/server/shopping-cart/internal/logic/getcartslogic.go b/server/shopping-cart/internal/logic/getcartslogic.go index 6941b93e..a90254bf 100644 --- a/server/shopping-cart/internal/logic/getcartslogic.go +++ b/server/shopping-cart/internal/logic/getcartslogic.go @@ -7,16 +7,14 @@ import ( "fmt" "fusenapi/constants" "fusenapi/model/gmodel" + "fusenapi/server/shopping-cart/internal/svc" + "fusenapi/server/shopping-cart/internal/types" "fusenapi/utils/auth" "fusenapi/utils/basic" "fusenapi/utils/format" "fusenapi/utils/s3url_to_s3id" "fusenapi/utils/shopping_cart" "math" - "strings" - - "fusenapi/server/shopping-cart/internal/svc" - "fusenapi/server/shopping-cart/internal/types" "github.com/zeromicro/go-zero/core/logx" ) @@ -115,23 +113,6 @@ func (l *GetCartsLogic) GetCarts(req *types.GetCartsReq, userinfo *auth.UserInfo if !ok { return resp.SetStatusWithMessage(basic.CodeServiceErr, fmt.Sprintf("the size`s price info is not exists:%d_%d", *cart.ProductId, *cart.SizeId)) } - //阶梯数量切片 - stepNum, err := format.StrSlicToIntSlice(strings.Split(*sizePrice.StepNum, ",")) - if err != nil { - logx.Error(err) - return resp.SetStatusWithMessage(basic.CodeServiceErr, fmt.Sprintf("failed to parse step number:%d_%d", *cart.ProductId, *cart.SizeId)) - } - lenStepNum := len(stepNum) - //阶梯价格切片 - stepPrice, err := format.StrSlicToIntSlice(strings.Split(*sizePrice.StepPrice, ",")) - if err != nil { - logx.Error(err) - return resp.SetStatusWithMessage(basic.CodeServiceErr, fmt.Sprintf("failed to parse step price:%d_%d", *cart.ProductId, *cart.SizeId)) - } - lenStepPrice := len(stepPrice) - if lenStepPrice == 0 || lenStepNum == 0 { - return resp.SetStatusWithMessage(basic.CodeServiceErr, fmt.Sprintf("step price or step number is not set:%d_%d ", *cart.ProductId, *cart.SizeId)) - } //如果有配件,单价也要加入配件价格 fittingPrice := int64(0) if *cart.FittingId > 0 { @@ -142,11 +123,15 @@ func (l *GetCartsLogic) GetCarts(req *types.GetCartsReq, userinfo *auth.UserInfo } } //计算价格 - itemPrice, totalPrice := shopping_cart.CaculateCartPrice(*cart.PurchaseQuantity, stepPrice, stepNum, fittingPrice, *sizePrice.EachBoxNum) + itemPrice, totalPrice, stepNum, _, err := shopping_cart.CaculateCartPrice(*cart.PurchaseQuantity, &sizePrice, fittingPrice) + if err != nil { + logx.Error(err) + return resp.SetStatusWithMessage(basic.CodeServiceErr, err.Error()) + } //获取阶梯数量 stepQuantityList := make([]int64, 0, 20) tmpMinBuyNum := *sizePrice.MinBuyNum - for tmpMinBuyNum < (int64(stepNum[lenStepNum-1]) + 5) { + for tmpMinBuyNum < (int64(stepNum[len(stepNum)-1]) + 5) { //阶梯数 tmpQuantity := tmpMinBuyNum * (*sizePrice.EachBoxNum) stepQuantityList = append(stepQuantityList, tmpQuantity) diff --git a/utils/shopping_cart/caculate_cart_price.go b/utils/shopping_cart/caculate_cart_price.go index 70ff4c04..2acd7977 100644 --- a/utils/shopping_cart/caculate_cart_price.go +++ b/utils/shopping_cart/caculate_cart_price.go @@ -1,21 +1,44 @@ package shopping_cart import ( + "errors" + "fmt" + "fusenapi/model/gmodel" + "fusenapi/utils/format" "fusenapi/utils/step_price" + "github.com/zeromicro/go-zero/core/logx" "math" + "strings" ) // 计算价格 -func CaculateCartPrice(purchaseQuantity int64, stepPrice, stepNum []int, fittingPrice, eachBoxNum int64) (ItemPrice, totalPrice int64) { +func CaculateCartPrice(purchaseQuantity int64, productPrice *gmodel.FsProductPrice, fittingPrice int64) (ItemPrice, totalPrice int64, stepNum, stepPrice []int, err error) { + //阶梯数量切片 + stepNum, err = format.StrSlicToIntSlice(strings.Split(*productPrice.StepNum, ",")) + if err != nil { + logx.Error(err) + return 0, 0, nil, nil, errors.New(fmt.Sprintf("failed to parse step number:%d_%d", *productPrice.ProductId, *productPrice.SizeId)) + } + lenStepNum := len(stepNum) + //阶梯价格切片 + stepPrice, err = format.StrSlicToIntSlice(strings.Split(*productPrice.StepPrice, ",")) + if err != nil { + logx.Error(err) + return 0, 0, nil, nil, errors.New(fmt.Sprintf("failed to parse step price:%d_%d", *productPrice.ProductId, *productPrice.SizeId)) + } + lenStepPrice := len(stepPrice) + if lenStepPrice == 0 || lenStepNum == 0 { + return 0, 0, nil, nil, errors.New(fmt.Sprintf("step price or step number is not set:%d_%d", *productPrice.ProductId, *productPrice.SizeId)) + } //请求的数量 reqPurchaseQuantity := purchaseQuantity //购买箱数 - boxQuantity := int(math.Ceil(float64(reqPurchaseQuantity) / float64(eachBoxNum))) + boxQuantity := int(math.Ceil(float64(reqPurchaseQuantity) / float64(*productPrice.EachBoxNum))) //根据数量获取阶梯价格中对应的价格 itemPrice := step_price.GetCentStepPrice(boxQuantity, stepNum, stepPrice) //如果有配件,单价也要加入配件价格 itemPrice += fittingPrice //单个购物车总价 totalPrice = itemPrice * reqPurchaseQuantity - return itemPrice, totalPrice + return itemPrice, totalPrice, stepNum, stepPrice, nil }