From b72d1675e416d4faed1930ceacbb9efc97499d04 Mon Sep 17 00:00:00 2001 From: laodaming <11058467+laudamine@user.noreply.gitee.com> Date: Fri, 24 Nov 2023 16:51:46 +0800 Subject: [PATCH] fix --- model/gmodel/fs_product_model3d_logic.go | 51 +++++++++++++++++++ .../internal/logic/getproductdetaillogic.go | 9 ++-- 2 files changed, 56 insertions(+), 4 deletions(-) diff --git a/model/gmodel/fs_product_model3d_logic.go b/model/gmodel/fs_product_model3d_logic.go index 756f3594..70d49e4a 100755 --- a/model/gmodel/fs_product_model3d_logic.go +++ b/model/gmodel/fs_product_model3d_logic.go @@ -176,6 +176,57 @@ func (d *FsProductModel3dModel) GetAllByProductIdsTags(ctx context.Context, prod return resp, err } +// 获取尺寸最低价格 +func (d *FsProductModel3dModel) GetProductSizeMinPrice(modelList []FsProductModel3d, mapProductSizeMinPrice map[string]int64) error { + mapModelMinPrice := make(map[int64]int64) + //每个模型/配件存储最小价格 + for _, modelInfo := range modelList { + switch *modelInfo.Tag { + case constants.TAG_MODEL: //模型 + if modelInfo.StepPrice == nil || len(*modelInfo.StepPrice) == 0 { + mapModelMinPrice[modelInfo.Id] = 0 + continue + } + var stepPrice StepPriceJsonStruct + if err := json.Unmarshal(*modelInfo.StepPrice, &stepPrice); err != nil { + return fmt.Errorf("failed to parse model step price:%d", modelInfo.Id) + } + lenRange := len(stepPrice.PriceRange) + if lenRange == 0 { + mapModelMinPrice[modelInfo.Id] = 0 + continue + } + sort.SliceStable(stepPrice.PriceRange, func(i, j int) bool { + return stepPrice.PriceRange[i].Price > stepPrice.PriceRange[j].Price + }) + mapModelMinPrice[modelInfo.Id] = stepPrice.PriceRange[lenRange-1].Price + case constants.TAG_PARTS: //配件 + mapModelMinPrice[modelInfo.Id] = *modelInfo.Price + } + } + //给产品存储最小价格 + for _, v := range modelList { + if *v.Tag != constants.TAG_MODEL { + continue + } + itemPrice := mapModelMinPrice[v.Id] + if *v.PartId > 0 { + if fittingPrice, ok := mapModelMinPrice[*v.PartId]; ok { + itemPrice += fittingPrice + } + } + key := fmt.Sprintf("%d_%d", *v.ProductId, *v.SizeId) + if minPrice, ok := mapProductSizeMinPrice[key]; ok { + if itemPrice < minPrice { + mapProductSizeMinPrice[key] = itemPrice + } + continue + } + mapProductSizeMinPrice[key] = itemPrice + } + return nil +} + // 获取每个产品最低价格 func (d *FsProductModel3dModel) GetProductMinPrice(modelList []FsProductModel3d, mapProductMinPrice map[int64]int64) error { mapModelMinPrice := make(map[int64]int64) diff --git a/server/product/internal/logic/getproductdetaillogic.go b/server/product/internal/logic/getproductdetaillogic.go index 2069352a..a0955f6b 100644 --- a/server/product/internal/logic/getproductdetaillogic.go +++ b/server/product/internal/logic/getproductdetaillogic.go @@ -3,6 +3,7 @@ package logic import ( "encoding/json" "errors" + "fmt" "fusenapi/constants" "fusenapi/model/gmodel" "fusenapi/utils/auth" @@ -162,9 +163,9 @@ func (l *GetProductDetailLogic) GetProductDetail(req *types.GetProductDetailReq, for k, v := range templateList { mapModelIdKeyTemplate[*v.ModelId] = k } - //记录产品最低价 - mapProductMinPrice := make(map[int64]int64) - if err = l.svcCtx.AllModels.FsProductModel3d.GetProductMinPrice(modelList, mapProductMinPrice); err != nil { + //记录产品尺寸最低价 + mapProductSizeMinPrice := make(map[string]int64) + if err = l.svcCtx.AllModels.FsProductModel3d.GetProductSizeMinPrice(modelList, mapProductSizeMinPrice); err != nil { logx.Error(err) return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to get product min price") } @@ -184,7 +185,7 @@ func (l *GetProductDetailLogic) GetProductDetail(req *types.GetProductDetailReq, } //尺寸下最低价 minPrice := "" - if price, ok := mapProductMinPrice[*sizeInfo.ProductId]; ok { + if price, ok := mapProductSizeMinPrice[fmt.Sprintf("%d_%d", *sizeInfo.ProductId, sizeInfo.Id)]; ok { minPrice = format.CentitoDollar(price, 3) } var modelInfoRsp types.ModelInfo