67 lines
2.0 KiB
Go
67 lines
2.0 KiB
Go
package step_price
|
|
|
|
import (
|
|
"errors"
|
|
"fusenapi/model/gmodel"
|
|
)
|
|
|
|
// 旧的返回厘(即将废弃)
|
|
func GetCentStepPrice(minBuyNum int, stepNum []int, stepPrice []int) int64 {
|
|
if minBuyNum > stepNum[len(stepNum)-1] {
|
|
return int64(stepPrice[len(stepPrice)-1])
|
|
}
|
|
for k, v := range stepNum {
|
|
if minBuyNum <= v {
|
|
if k <= (len(stepPrice) - 1) {
|
|
return int64(stepPrice[k])
|
|
}
|
|
return int64(stepPrice[len(stepPrice)-1])
|
|
}
|
|
}
|
|
return int64(stepPrice[len(stepPrice)-1])
|
|
}
|
|
|
|
// 新的阶梯价格(返回美元)
|
|
func GetNewStepPrice(purchaseQuantity int64, stepPrice gmodel.StepPriceJsonStruct, fittingPrice int64) (totalPrice, itemPrice float64, err error) {
|
|
l := len(stepPrice.PriceRange)
|
|
if l == 0 {
|
|
return 0, 0, errors.New("price range is not set")
|
|
}
|
|
//遍历查询合适的价格
|
|
for k, v := range stepPrice.PriceRange {
|
|
//购买数量>起点
|
|
if purchaseQuantity > v.StartQuantity {
|
|
//最后一个 || 小于等于终点
|
|
if k == l-1 || purchaseQuantity <= v.EndQuantity {
|
|
itemPrice = float64(v.Price+fittingPrice) / 1000
|
|
return itemPrice * float64(purchaseQuantity), itemPrice / 1000, nil
|
|
}
|
|
}
|
|
}
|
|
//遍历里面没有则返回第一个
|
|
itemPrice = float64(stepPrice.PriceRange[0].Price+fittingPrice) / 1000
|
|
return itemPrice * float64(purchaseQuantity), itemPrice, nil
|
|
}
|
|
|
|
// 新的阶梯价格(返回厘)
|
|
func GetNewCentStepPrice(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")
|
|
}
|
|
//遍历查询合适的价格
|
|
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
|
|
}
|
|
}
|
|
}
|
|
//遍历里面没有则返回第一个
|
|
itemPrice = stepPrice.PriceRange[0].Price + fittingPrice
|
|
return itemPrice * purchaseQuantity, itemPrice, nil
|
|
}
|