From eb25e5bfcc198e5ba08ae2cd71b5a6483c1f7ad6 Mon Sep 17 00:00:00 2001 From: laodaming <11058467+laudamine@user.noreply.gitee.com> Date: Fri, 21 Jul 2023 12:14:19 +0800 Subject: [PATCH] fix --- .../internal/logic/getpricebypidlogic.go | 65 +++++++++++-------- 1 file changed, 37 insertions(+), 28 deletions(-) diff --git a/server/product/internal/logic/getpricebypidlogic.go b/server/product/internal/logic/getpricebypidlogic.go index ad1f1689..5319555f 100644 --- a/server/product/internal/logic/getpricebypidlogic.go +++ b/server/product/internal/logic/getpricebypidlogic.go @@ -3,6 +3,7 @@ package logic import ( "errors" "fmt" + "fusenapi/model/gmodel" "fusenapi/utils/auth" "fusenapi/utils/basic" "fusenapi/utils/format" @@ -68,7 +69,6 @@ func (l *GetPriceByPidLogic) GetPriceByPid(req *types.GetPriceByPidReq, userinfo return resp.SetStatusWithMessage(basic.CodeServiceErr, fmt.Sprintf("failed to parse step price,id = %d", priceInfo.Id)) } lenStepNum := len(stepNumSlice) - lenStepPrice := len(stepPriceSlice) itemList := make([]types.PriceItem, 0, 10) for *priceInfo.MinBuyNum < (int64(stepNumSlice[lenStepNum-1]) + 5) { itemList = append(itemList, types.PriceItem{ @@ -79,33 +79,7 @@ func (l *GetPriceByPidLogic) GetPriceByPid(req *types.GetPriceByPidReq, userinfo *priceInfo.MinBuyNum++ } //组装阶梯数量范围价格 - stepListRsp := make([]types.StepPrice, 0, lenStepNum) - for numKey, stepNum := range stepNumSlice { - //先取最后一个 - tmpPrice := float64(stepPriceSlice[lenStepPrice-1]) / 100 - //如果同下标下面有价格 - if numKey < lenStepPrice { - tmpPrice = float64(stepPriceSlice[numKey]) / 100 - } - num := int64(stepNum) * (*priceInfo.EachBoxNum) - rangeNum := "" - if numKey < lenStepNum-1 { //前面的 - nextNum := int64(stepNumSlice[numKey+1]) * (*priceInfo.EachBoxNum) - //第一个 - if numKey == 0 { - rangeNum = fmt.Sprintf("%d-%dPCS", num, nextNum-1) - } else { - rangeNum = fmt.Sprintf("%d-%dPCS", num, nextNum-1) - } - } else { //最后一个 - rangeNum = fmt.Sprintf(">=%dPCS", num) - } - stepListRsp = append(stepListRsp, types.StepPrice{ - Range: rangeNum, - Price: tmpPrice, - }) - - } + stepListRsp := l.dealWithStepRange(stepNumSlice, stepPriceSlice, priceInfo) //排序(必须放在其他逻辑之后) sort.Ints(stepPriceSlice) minPrice := float64(stepPriceSlice[0]) / 100 @@ -120,6 +94,41 @@ func (l *GetPriceByPidLogic) GetPriceByPid(req *types.GetPriceByPidReq, userinfo } return resp.SetStatusWithMessage(basic.CodeOK, "success", mapRsp) } + +// 组装阶梯价格范围 +func (l *GetPriceByPidLogic) dealWithStepRange(stepNumSlice, stepPriceSlice []int, priceInfo gmodel.FsProductPrice) []types.StepPrice { + lenStepNum := len(stepNumSlice) + lenStepPrice := len(stepPriceSlice) + stepListRsp := make([]types.StepPrice, 0, lenStepNum) + for numKey, stepNum := range stepNumSlice { + //先取最后一个 + tmpPrice := float64(stepPriceSlice[lenStepPrice-1]) / 100 + //如果同下标下面有价格 + if numKey < lenStepPrice { + tmpPrice = float64(stepPriceSlice[numKey]) / 100 + } + num := int64(stepNum) * (*priceInfo.EachBoxNum) + rangeNum := "" + if numKey < lenStepNum-1 { //前面的 + nextNum := int64(stepNumSlice[numKey+1]) * (*priceInfo.EachBoxNum) + //第一个 + if numKey == 0 { + rangeNum = fmt.Sprintf("%d-%dPCS", num, nextNum-1) + } else { + rangeNum = fmt.Sprintf("%d-%dPCS", num, nextNum-1) + } + } else { //最后一个 + rangeNum = fmt.Sprintf(">=%dPCS", num) + } + stepListRsp = append(stepListRsp, types.StepPrice{ + Range: rangeNum, + Price: tmpPrice, + }) + } + return stepListRsp +} + +// 获取mapKey func (l *GetPriceByPidLogic) getSizePriceMapKey(sizeId int64) string { return fmt.Sprintf("_%d", sizeId) }