From 039137ce231c8c5da08087752deb3769bf9f8eb7 Mon Sep 17 00:00:00 2001 From: laodaming <11058467+laudamine@user.noreply.gitee.com> Date: Wed, 20 Sep 2023 12:13:27 +0800 Subject: [PATCH] 11 --- .../internal/logic/calculatecartpricelogic.go | 16 +++++-------- .../internal/logic/getcartslogic.go | 24 +++++++++---------- server/shopping-cart/internal/types/types.go | 1 + server_api/shopping-cart.api | 1 + utils/shopping_cart/caculate_cart_price.go | 21 ++++++++++++++++ 5 files changed, 41 insertions(+), 22 deletions(-) create mode 100644 utils/shopping_cart/caculate_cart_price.go diff --git a/server/shopping-cart/internal/logic/calculatecartpricelogic.go b/server/shopping-cart/internal/logic/calculatecartpricelogic.go index 4eb6b7c0..d3ed6a6b 100644 --- a/server/shopping-cart/internal/logic/calculatecartpricelogic.go +++ b/server/shopping-cart/internal/logic/calculatecartpricelogic.go @@ -9,9 +9,8 @@ import ( "fusenapi/utils/auth" "fusenapi/utils/basic" "fusenapi/utils/format" - "fusenapi/utils/step_price" + "fusenapi/utils/shopping_cart" "gorm.io/gorm" - "math" "strings" "fusenapi/server/shopping-cart/internal/svc" @@ -133,20 +132,17 @@ func (l *CalculateCartPriceLogic) CalculateCartPrice(req *types.CalculateCartPri if mapCalculateQuantity[cart.Id].IsSelected { isSelected = 1 } - //购买箱数 - boxQuantity := int(math.Ceil(float64(reqPurchaseQuantity) / float64(*sizePrice.EachBoxNum))) - //根据数量获取阶梯价格中对应的价格 - itemPrice := step_price.GetCentStepPrice(boxQuantity, stepNum, stepPrice) //如果有配件,单价也要加入配件价格 + fittingPrice := int64(0) if *cart.FittingId > 0 { - if fittingPrice, ok := mapFitting[*cart.FittingId]; ok { - itemPrice += fittingPrice + if fPrice, ok := mapFitting[*cart.FittingId]; ok { + fittingPrice = fPrice } else { return errors.New(fmt.Sprintf("cart contain some one witch lose fitting:%d", *cart.FittingId)) } } - //单个购物车总价 - totalPrice := itemPrice * reqPurchaseQuantity + //计算价格 + itemPrice, totalPrice := shopping_cart.CaculateCartPrice(reqPurchaseQuantity, stepPrice, stepNum, fittingPrice, *sizePrice.EachBoxNum) 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 f79d8db3..6941b93e 100644 --- a/server/shopping-cart/internal/logic/getcartslogic.go +++ b/server/shopping-cart/internal/logic/getcartslogic.go @@ -12,7 +12,6 @@ import ( "fusenapi/utils/format" "fusenapi/utils/s3url_to_s3id" "fusenapi/utils/shopping_cart" - "fusenapi/utils/step_price" "math" "strings" @@ -133,8 +132,17 @@ func (l *GetCartsLogic) GetCarts(req *types.GetCartsReq, userinfo *auth.UserInfo 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)) } - //购买箱数 - boxQuantity := int(math.Ceil(float64(*cart.PurchaseQuantity) / float64(*sizePrice.EachBoxNum))) + //如果有配件,单价也要加入配件价格 + fittingPrice := int64(0) + if *cart.FittingId > 0 { + if curFittingInfo, ok := mapModel[*cart.FittingId]; ok { + fittingPrice = *curFittingInfo.Price + } else { + return resp.SetStatusWithMessage(basic.CodeServiceErr, fmt.Sprintf("cart contain some one witch lose fitting:%d", *cart.FittingId)) + } + } + //计算价格 + itemPrice, totalPrice := shopping_cart.CaculateCartPrice(*cart.PurchaseQuantity, stepPrice, stepNum, fittingPrice, *sizePrice.EachBoxNum) //获取阶梯数量 stepQuantityList := make([]int64, 0, 20) tmpMinBuyNum := *sizePrice.MinBuyNum @@ -148,19 +156,10 @@ func (l *GetCartsLogic) GetCarts(req *types.GetCartsReq, userinfo *auth.UserInfo if sizeInfo, ok := mapSize[*cart.SizeId]; ok { sizeCapacity = *sizeInfo.Capacity } - //根据数量获取阶梯价格中对应的价格 - itemPrice := step_price.GetCentStepPrice(boxQuantity, stepNum, stepPrice) - //如果有配件,单价也要加入配件价格 - if *cart.FittingId > 0 { - if curFittingInfo, ok := mapModel[*cart.FittingId]; ok { - itemPrice += *curFittingInfo.Price - } - } fittingName := snapShot.FittingInfo.FittingName if fittingInfo, ok := mapModel[*cart.FittingId]; ok { fittingName = *fittingInfo.Name } - totalPrice := itemPrice * (*cart.PurchaseQuantity) productCover := "" //产品封面图 productName := snapShot.ProductInfo.ProductName productSn := snapShot.ProductInfo.ProductSn @@ -174,6 +173,7 @@ func (l *GetCartsLogic) GetCarts(req *types.GetCartsReq, userinfo *auth.UserInfo } } item := types.CartItem{ + CartId: cart.Id, ProductInfo: types.ProductInfo{ ProductId: *cart.ProductId, ProductName: productName, diff --git a/server/shopping-cart/internal/types/types.go b/server/shopping-cart/internal/types/types.go index a1eb5ab2..39699f48 100644 --- a/server/shopping-cart/internal/types/types.go +++ b/server/shopping-cart/internal/types/types.go @@ -39,6 +39,7 @@ type GetCartsRsp struct { } type CartItem struct { + CartId int64 `json:"cart_id"` ProductInfo ProductInfo `json:"product_info"` //产品信息 SizeInfo SizeInfo `json:"size_info"` //尺寸信息 FittingInfo FittingInfo `json:"fitting_info"` //配件信息 diff --git a/server_api/shopping-cart.api b/server_api/shopping-cart.api index bb347743..8f778f48 100644 --- a/server_api/shopping-cart.api +++ b/server_api/shopping-cart.api @@ -56,6 +56,7 @@ type GetCartsRsp { CartList []CartItem `json:"cart_list"` } type CartItem { + CartId int64 `json:"cart_id"` ProductInfo ProductInfo `json:"product_info"` //产品信息 SizeInfo SizeInfo `json:"size_info"` //尺寸信息 FittingInfo FittingInfo `json:"fitting_info"` //配件信息 diff --git a/utils/shopping_cart/caculate_cart_price.go b/utils/shopping_cart/caculate_cart_price.go new file mode 100644 index 00000000..70ff4c04 --- /dev/null +++ b/utils/shopping_cart/caculate_cart_price.go @@ -0,0 +1,21 @@ +package shopping_cart + +import ( + "fusenapi/utils/step_price" + "math" +) + +// 计算价格 +func CaculateCartPrice(purchaseQuantity int64, stepPrice, stepNum []int, fittingPrice, eachBoxNum int64) (ItemPrice, totalPrice int64) { + //请求的数量 + reqPurchaseQuantity := purchaseQuantity + //购买箱数 + boxQuantity := int(math.Ceil(float64(reqPurchaseQuantity) / float64(eachBoxNum))) + //根据数量获取阶梯价格中对应的价格 + itemPrice := step_price.GetCentStepPrice(boxQuantity, stepNum, stepPrice) + //如果有配件,单价也要加入配件价格 + itemPrice += fittingPrice + //单个购物车总价 + totalPrice = itemPrice * reqPurchaseQuantity + return itemPrice, totalPrice +}