fusenapi/utils/step_price/price.go
laodaming 4bbe9aaaa3 fix
2023-09-25 17:20:44 +08:00

67 lines
1.8 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) (price float64, err error) {
l := len(stepPrice.PriceRange)
if l == 0 {
return 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 {
return float64(v.Price+fittingPrice) / 1000, nil
}
}
}
//遍历里面没有则返回第一个
return float64(stepPrice.PriceRange[0].Price+fittingPrice) / 1000, nil
}
// 新的阶梯价格(返回厘)
func GetNewCentStepPrice(purchaseQuantity int64, stepPrice gmodel.StepPriceJsonStruct, fittingPrice int64) (price int64, err error) {
l := len(stepPrice.PriceRange)
if l == 0 {
return 0, errors.New("price range is not set")
}
//遍历查询合适的价格
for k, v := range stepPrice.PriceRange {
//购买数量>起点
if purchaseQuantity > v.StartQuantity {
//最后一个
if k == l-1 {
return v.Price + fittingPrice, nil
}
//小于等于终点
if purchaseQuantity <= v.EndQuantity {
return v.Price + fittingPrice, nil
}
}
}
//遍历里面没有则返回第一个
return stepPrice.PriceRange[0].Price + fittingPrice, nil
}