StoreSkuBind添加JxPrice

This commit is contained in:
gazebo
2019-11-13 15:16:11 +08:00
parent 2ca58bd582
commit 7f231d1f6a
8 changed files with 146 additions and 38 deletions

View File

@@ -245,10 +245,15 @@ func CaculateUnitPrice(skuPrice int, specQuality float32, specUnit string, skuNa
return unitPrice
}
func CaculateSkuVendorPrice(price, percentage, priceAdd int) (vendorPrice int) {
if percentage <= 10 || percentage >= 400 {
percentage = 100
func ConstrainPricePercentage(percentage int) int {
if percentage <= model.MinVendorPricePercentage || percentage >= model.MaxVendorPricePercentage {
percentage = model.DefVendorPricePercentage
}
return percentage
}
func CaculateSkuVendorPrice(price, percentage, priceAdd int) (vendorPrice int) {
percentage = ConstrainPricePercentage(percentage)
vendorPrice = int(math.Round(float64(price*percentage)/100)) + priceAdd
if vendorPrice < 1 {
vendorPrice = 1
@@ -257,9 +262,7 @@ func CaculateSkuVendorPrice(price, percentage, priceAdd int) (vendorPrice int) {
}
func CaculateSkuPriceFromVendor(vendorPrice, percentage, priceAdd int) (price int) {
if percentage <= 10 || percentage >= 400 {
percentage = 100
}
percentage = ConstrainPricePercentage(percentage)
price = int(math.Round(float64(vendorPrice-priceAdd) * 100 / float64(percentage)))
if price < 0 {
price = 0
@@ -269,18 +272,28 @@ func CaculateSkuPriceFromVendor(vendorPrice, percentage, priceAdd int) (price in
func GetPricePercentage(l model.PricePercentagePack, price int, defPricePercentage int) (pricePercentage, priceAdd int) {
pricePercentage = defPricePercentage
if len(l) > 0 {
var lastItem *model.PricePercentageItem
for _, v := range l {
if v.BeginPrice > price {
itemLen := len(l)
if itemLen > 0 {
low := 0
high := itemLen - 1
mid := 0
for low <= high {
mid = low + (high-low)/2
if mid == 0 || mid == itemLen-1 {
break
}
lastItem = v
}
if lastItem != nil {
pricePercentage = lastItem.PricePercentage
priceAdd = lastItem.PriceAdd
if price >= l[mid].BeginPrice {
if price < l[mid+1].BeginPrice {
break
} else {
low = mid + 1
}
} else {
high = mid - 1
}
}
pricePercentage = l[mid].PricePercentage
priceAdd = l[mid].PriceAdd
}
return pricePercentage, priceAdd
}
@@ -303,6 +316,11 @@ func GetPricePercentageByVendorPrice(l model.PricePercentagePack, vendorPrice in
return pricePercentage, priceAdd
}
func CaculatePriceByPricePack(l model.PricePercentagePack, defPricePercentage, price int) (outPrice int) {
pricePercentage, priceAdd := GetPricePercentage(l, price, defPricePercentage)
return CaculateSkuVendorPrice(price, pricePercentage, priceAdd)
}
func IsSkuSpecial(specQuality float32, specUnit string) bool {
return int(specQuality) == model.SpecialSpecQuality && (specUnit == model.SpecialSpecUnit || specUnit == model.SpecialSpecUnit2)
}

View File

@@ -164,3 +164,47 @@ func TestCaculateSkuPrice(t *testing.T) {
}
}
}
func TestGetPricePercentage(t *testing.T) {
type tTestInfo struct {
DesiredPrice int
UnitPrice int
SpecQuality float32
SpecUnit string
Unit string
}
l := []*model.PricePercentageItem{
&model.PricePercentageItem{
BeginPrice: 0,
PricePercentage: 0,
PriceAdd: 0,
},
&model.PricePercentageItem{
BeginPrice: 10,
PricePercentage: 10,
PriceAdd: 1,
},
&model.PricePercentageItem{
BeginPrice: 20,
PricePercentage: 20,
PriceAdd: 2,
},
&model.PricePercentageItem{
BeginPrice: 30,
PricePercentage: 30,
PriceAdd: 3,
},
}
for _, v := range [][]int{
[]int{0, 0, 0, 0},
[]int{30, 3, 40, 0},
[]int{20, 2, 25, 0},
[]int{10, 1, 10, 0},
} {
pricePercentage, priceAdd := GetPricePercentage(l, v[2], v[3])
if pricePercentage != v[0] || priceAdd != v[1] {
t.Errorf("price:%d, defPricePercentage:%d, expected pricePercentage:%d, priceAdd:%d, actual pricePercentage:%d, priceAdd:%d",
v[2], v[3], v[0], v[1], pricePercentage, priceAdd)
}
}
}