This commit is contained in:
laodaming
2023-09-26 15:28:17 +08:00
parent a1d78b7a0c
commit 821cbb8879
5 changed files with 63 additions and 78 deletions

View File

@@ -3,15 +3,10 @@ package repositories
import (
"encoding/json"
"errors"
"fmt"
"fusenapi/model/gmodel"
"fusenapi/utils/format"
"fusenapi/utils/hash"
"fusenapi/utils/step_price"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/zeromicro/go-zero/core/logx"
"gorm.io/gorm"
"math"
"strings"
)
@@ -33,7 +28,7 @@ type (
// 校验订单
VerifyShoppingCartSnapshotDataChange(req VerifyShoppingCartSnapshotDataChangeReq) error
//计算购物车价格
CaculateCartPrice(purchaseQuantity int64, productPrice *gmodel.FsProductPrice, fittingPrice int64) (ItemPrice, totalPrice int64, stepNum, stepPrice []int, err error)
CaculateStepPrice(purchaseQuantity int64, stepPrice gmodel.StepPriceJsonStruct, fittingPrice int64) (totalPrice, itemPrice int64, err error)
}
)
@@ -132,33 +127,23 @@ func (d *defaultShoppingCart) VerifyShoppingCartSnapshotDataChange(req VerifySho
}
// 计算价格
func (d *defaultShoppingCart) 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))
func (d *defaultShoppingCart) CaculateStepPrice(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")
}
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))
//遍历查询合适的价格
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
}
}
}
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
//遍历里面没有则返回第一个
itemPrice = stepPrice.PriceRange[0].Price + fittingPrice
return itemPrice * purchaseQuantity, itemPrice, nil
}