From 46418f2338b539f70c46129aa8e25a32587f701e Mon Sep 17 00:00:00 2001 From: laodaming <11058467+laudamine@user.noreply.gitee.com> Date: Mon, 17 Jul 2023 10:54:37 +0800 Subject: [PATCH] fix --- .../internal/logic/getpricebypidlogic.go | 57 ++++++++++++------- server/product/internal/types/types.go | 12 ++++ server_api/product.api | 10 ++++ 3 files changed, 59 insertions(+), 20 deletions(-) diff --git a/server/product/internal/logic/getpricebypidlogic.go b/server/product/internal/logic/getpricebypidlogic.go index 6af193da..7f169dd5 100644 --- a/server/product/internal/logic/getpricebypidlogic.go +++ b/server/product/internal/logic/getpricebypidlogic.go @@ -6,6 +6,7 @@ import ( "fusenapi/utils/auth" "fusenapi/utils/basic" "fusenapi/utils/format" + "fusenapi/utils/step_price" "gorm.io/gorm" "strings" @@ -52,27 +53,43 @@ func (l *GetPriceByPidLogic) GetPriceByPid(req *types.GetPriceByPidReq, userinfo return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get price list") } //处理价格信息 - mapRsp := make(map[string]interface{}) - for _, v := range priceList { - mapKey := fmt.Sprintf("_%d", v.Id) - stepNum, err := format.StrSlicToIntSlice(strings.Split(*v.StepNum, ",")) + mapRsp := make(map[string]*types.GetPriceByPidRsp) + for _, price := range priceList { + stepNumSlice, err := format.StrSlicToIntSlice(strings.Split(*price.StepNum, ",")) if err != nil { - logx.Error(err) - return resp.SetStatusWithMessage(basic.CodeServiceErr, fmt.Sprintf("failed to parse step num,price_id=%d", v.Id)) + return resp.SetStatusWithMessage(basic.CodeServiceErr, fmt.Sprintf("failed to parse step num,price_id=%d", price.Id)) + } + stepPriceSlice, err := format.StrSlicToIntSlice(strings.Split(*price.StepPrice, ",")) + if err != nil { + return resp.SetStatusWithMessage(basic.CodeServiceErr, fmt.Sprintf("failed to parse step price,id = %d", price.Id)) + } + lenStepNum := len(stepNumSlice) + itemList := make([]types.PriceItem, 0, 10) + for *price.MinBuyNum < (int64(stepNumSlice[lenStepNum-1]) + 5) { + itemList = append(itemList, types.PriceItem{ + Num: *price.MinBuyNum, + TotalNum: (*price.MinBuyNum) * (*price.EachBoxNum), + Price: step_price.GetCentStepPrice(int(*price.MinBuyNum), stepNumSlice, stepPriceSlice), + }) + *price.MinBuyNum++ + } + minPrice := float64(stepPriceSlice[len(stepPriceSlice)-1]) / 100 + maxPrice := float64(stepPriceSlice[0]) / 100 + mapKey := l.getSizePriceMapKey(*price.SizeId) + if _, ok := mapRsp[mapKey]; ok { + mapRsp[mapKey].Items = append(mapRsp[mapKey].Items, itemList...) + mapRsp[mapKey].MinPrice = minPrice + mapRsp[mapKey].MaxPrice = maxPrice + } else { + mapRsp[mapKey] = &types.GetPriceByPidRsp{ + Items: itemList, + MinPrice: minPrice, + MaxPrice: maxPrice, + } } - /*$price['step_num'] = explode(',', $price['step_num']); - $price['step_price'] = explode(',', $price['step_price']); - while ($price['min_buy_num'] < end($price['step_num']) + 5) { - $outData["{$price['size_id']}"]['items'][] = [ - 'num' => intval($price['min_buy_num']), - 'total_num' => $price['min_buy_num'] * $price['each_box_num'], - 'price' => ProductPriceService::getPrice($price['min_buy_num'], $price['step_num'], $price['step_price']) - ]; - $price['min_buy_num'] += 1; - } - - $outData["{$price['size_id']}"]['min_price'] = floatval(end($price['step_price']) / 100); - $outData["{$price['size_id']}"]['max_price'] = floatval(reset($price['step_price']) / 100);*/ } - return resp.SetStatus(basic.CodeOK) + return resp.SetStatusWithMessage(basic.CodeOK, "success", mapRsp) +} +func (l *GetPriceByPidLogic) getSizePriceMapKey(sizeId int64) string { + return fmt.Sprintf("_%d", sizeId) } diff --git a/server/product/internal/types/types.go b/server/product/internal/types/types.go index 5b3bf41b..482478a4 100644 --- a/server/product/internal/types/types.go +++ b/server/product/internal/types/types.go @@ -301,6 +301,18 @@ type GetPriceByPidReq struct { Pid string `form:"pid"` } +type GetPriceByPidRsp struct { + Items []PriceItem `json:"items"` + MinPrice float64 `json:"min_price"` + MaxPrice float64 `json:"max_price"` +} + +type PriceItem struct { + Num int64 `json:"num"` + TotalNum int64 `json:"total_num"` + Price int64 `json:"price"` +} + type Request struct { } diff --git a/server_api/product.api b/server_api/product.api index 93653147..5c80cd70 100644 --- a/server_api/product.api +++ b/server_api/product.api @@ -324,4 +324,14 @@ type GetModelByPidReq { //获取产品阶梯价格 type GetPriceByPidReq { Pid string `form:"pid"` +} +type GetPriceByPidRsp { + Items []PriceItem `json:"items"` + MinPrice float64 `json:"min_price"` + MaxPrice float64 `json:"max_price"` +} +type PriceItem { + Num int64 `json:"num"` + TotalNum int64 `json:"total_num"` + Price int64 `json:"price"` } \ No newline at end of file