From 15020a0c9fc06b42c5c0321e97a02edf597ea3a4 Mon Sep 17 00:00:00 2001 From: gazebo Date: Tue, 19 Nov 2019 16:02:25 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8DGetPricePercentage=E4=B8=AD?= =?UTF-8?q?=E7=9A=84bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- business/jxutils/jxutils_cms.go | 8 +++++--- business/jxutils/jxutils_cms_test.go | 10 ++++++++++ 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/business/jxutils/jxutils_cms.go b/business/jxutils/jxutils_cms.go index 874682300..150e96066 100644 --- a/business/jxutils/jxutils_cms.go +++ b/business/jxutils/jxutils_cms.go @@ -279,7 +279,7 @@ func GetPricePercentage(l model.PricePercentagePack, price int, defPricePercenta mid := 0 for low <= high { mid = low + (high-low)/2 - if mid == 0 || mid == itemLen-1 { + if mid < 0 || mid >= itemLen-1 { break } if price >= l[mid].BeginPrice { @@ -292,8 +292,10 @@ func GetPricePercentage(l model.PricePercentagePack, price int, defPricePercenta high = mid - 1 } } - pricePercentage = l[mid].PricePercentage - priceAdd = l[mid].PriceAdd + if mid >= 0 && mid <= itemLen-1 && low <= high { + pricePercentage = l[mid].PricePercentage + priceAdd = l[mid].PriceAdd + } } return pricePercentage, priceAdd } diff --git a/business/jxutils/jxutils_cms_test.go b/business/jxutils/jxutils_cms_test.go index 90bcd437e..701e3093b 100644 --- a/business/jxutils/jxutils_cms_test.go +++ b/business/jxutils/jxutils_cms_test.go @@ -173,6 +173,7 @@ func TestGetPricePercentage(t *testing.T) { SpecUnit string Unit string } + l := []*model.PricePercentageItem{ &model.PricePercentageItem{ BeginPrice: 0, @@ -194,12 +195,21 @@ func TestGetPricePercentage(t *testing.T) { PricePercentage: 30, PriceAdd: 3, }, + &model.PricePercentageItem{ + BeginPrice: 60, + PricePercentage: 60, + PriceAdd: 6, + }, } + for _, v := range [][]int{ + []int{88, 0, -1, 88}, []int{0, 0, 0, 0}, []int{30, 3, 40, 0}, []int{20, 2, 25, 0}, []int{10, 1, 10, 0}, + []int{60, 6, 60, 0}, + []int{60, 6, 1000, 10}, } { pricePercentage, priceAdd := GetPricePercentage(l, v[2], v[3]) if pricePercentage != v[0] || priceAdd != v[1] {