45 lines
1.7 KiB
Go
45 lines
1.7 KiB
Go
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, 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(*productPrice.EachBoxNum)))
|
|
//根据数量获取阶梯价格中对应的价格
|
|
itemPrice := step_price.GetCentStepPrice(boxQuantity, stepNum, stepPrice)
|
|
//如果有配件,单价也要加入配件价格
|
|
itemPrice += fittingPrice
|
|
//单个购物车总价
|
|
totalPrice = itemPrice * reqPurchaseQuantity
|
|
return itemPrice, totalPrice, stepNum, stepPrice, nil
|
|
}
|