fusenapi/utils/shopping_cart/caculate_cart_price.go

45 lines
1.7 KiB
Go
Raw Normal View History

2023-09-20 04:13:27 +00:00
package shopping_cart
import (
2023-09-20 04:27:40 +00:00
"errors"
"fmt"
"fusenapi/model/gmodel"
"fusenapi/utils/format"
2023-09-20 04:13:27 +00:00
"fusenapi/utils/step_price"
2023-09-20 04:27:40 +00:00
"github.com/zeromicro/go-zero/core/logx"
2023-09-20 04:13:27 +00:00
"math"
2023-09-20 04:27:40 +00:00
"strings"
2023-09-20 04:13:27 +00:00
)
// 计算价格
2023-09-20 04:27:40 +00:00
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))
}
2023-09-20 04:13:27 +00:00
//请求的数量
reqPurchaseQuantity := purchaseQuantity
//购买箱数
2023-09-20 04:27:40 +00:00
boxQuantity := int(math.Ceil(float64(reqPurchaseQuantity) / float64(*productPrice.EachBoxNum)))
2023-09-20 04:13:27 +00:00
//根据数量获取阶梯价格中对应的价格
itemPrice := step_price.GetCentStepPrice(boxQuantity, stepNum, stepPrice)
//如果有配件,单价也要加入配件价格
itemPrice += fittingPrice
//单个购物车总价
totalPrice = itemPrice * reqPurchaseQuantity
2023-09-20 04:27:40 +00:00
return itemPrice, totalPrice, stepNum, stepPrice, nil
2023-09-20 04:13:27 +00:00
}