From 4ea3487043b6b9ca91bbafa96863b4817951b318 Mon Sep 17 00:00:00 2001 From: laodaming <11058467+laudamine@user.noreply.gitee.com> Date: Wed, 27 Sep 2023 11:48:15 +0800 Subject: [PATCH] fix --- model/gmodel/fs_product_model3d_logic.go | 57 +++++++++++++++++++ .../logic/getrecommandproductlistlogic.go | 34 +---------- .../internal/logic/gettagproductlistlogic.go | 50 +--------------- .../homepagerecommendproductlistlogic.go | 30 +--------- 4 files changed, 65 insertions(+), 106 deletions(-) diff --git a/model/gmodel/fs_product_model3d_logic.go b/model/gmodel/fs_product_model3d_logic.go index 5de5f05a..e7449a84 100755 --- a/model/gmodel/fs_product_model3d_logic.go +++ b/model/gmodel/fs_product_model3d_logic.go @@ -2,6 +2,11 @@ package gmodel import ( "context" + "encoding/json" + "errors" + "fmt" + "fusenapi/constants" + "github.com/zeromicro/go-zero/core/logx" ) // 阶梯价结构 @@ -166,3 +171,55 @@ func (d *FsProductModel3dModel) GetAllByProductIdsTags(ctx context.Context, prod err = db.Find(&resp).Error return resp, err } + +// 获取每个产品最低价格 +func (d *FsProductModel3dModel) GetProductMinPrice(ctx context.Context, productIds []int64) (productMinPrice map[int64]int64, err error) { + //获取产品模型价格列表 + modelList, err := d.GetAllByProductIdsTags(ctx, productIds, []int{constants.TAG_MODEL, constants.TAG_PARTS}, "id,product_id,price,tag,part_id,step_price") + if err != nil { + return + } + mapModelMinPrice := make(map[int64]int64) + //每个模型/配件存储最小价格 + for _, modelInfo := range modelList { + switch *modelInfo.Tag { + case constants.TAG_MODEL: //模型 + if modelInfo.StepPrice == nil || len(*modelInfo.StepPrice) == 0 { + return nil, errors.New(fmt.Sprintf("model step price is not set:%d", modelInfo.Id)) + } + var stepPrice StepPriceJsonStruct + if err = json.Unmarshal(*modelInfo.StepPrice, &stepPrice); err != nil { + logx.Error(err) + return nil, errors.New(fmt.Sprintf("failed to parse model step price:%d", modelInfo.Id)) + } + lenRange := len(stepPrice.PriceRange) + if lenRange == 0 { + return nil, errors.New(fmt.Sprintf("the count of step price is 0:%d", modelInfo.Id)) + } + mapModelMinPrice[modelInfo.Id] = stepPrice.PriceRange[lenRange-1].Price + case constants.TAG_PARTS: //配件 + mapModelMinPrice[modelInfo.Id] = *modelInfo.Price + } + } + productMinPrice = make(map[int64]int64) + //给产品存储最小价格 + 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 + } + } + if minPrice, ok := productMinPrice[*v.ProductId]; ok { + if itemPrice < minPrice { + productMinPrice[*v.ProductId] = itemPrice + } + continue + } + productMinPrice[*v.ProductId] = itemPrice + } + return productMinPrice, nil +} diff --git a/server/product/internal/logic/getrecommandproductlistlogic.go b/server/product/internal/logic/getrecommandproductlistlogic.go index c708f8f9..3f0545d0 100644 --- a/server/product/internal/logic/getrecommandproductlistlogic.go +++ b/server/product/internal/logic/getrecommandproductlistlogic.go @@ -10,7 +10,6 @@ import ( "fusenapi/utils/image" "fusenapi/utils/s3url_to_s3id" "gorm.io/gorm" - "sort" "strings" "context" @@ -92,39 +91,10 @@ func (l *GetRecommandProductListLogic) GetRecommandProductList(req *types.GetRec recommendProductList = append(recommendProductList, v) } } - - //查询产品价格 - priceList, err := l.svcCtx.AllModels.FsProductPrice.GetPriceListByProductIds(l.ctx, productIds) + mapProductMinPrice, err := l.svcCtx.AllModels.FsProductModel3d.GetProductMinPrice(l.ctx, productIds) if err != nil { - logx.Error(err) - return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get product price list") + return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get product min price") } - mapProductMinPrice := make(map[int64]int64) - for _, v := range priceList { - if v.StepPrice == nil || *v.StepPrice == "" { - continue - } - stepPriceSlice, err := format.StrSlicToIntSlice(strings.Split(*v.StepPrice, ",")) - if err != nil { - logx.Error(err) - return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to parse step price") - } - //正序排序 - sort.Ints(stepPriceSlice) - if min, ok := mapProductMinPrice[*v.ProductId]; ok { - if min > int64(stepPriceSlice[0]) { - mapProductMinPrice[*v.ProductId] = int64(stepPriceSlice[0]) - } - } else { - mapProductMinPrice[*v.ProductId] = int64(stepPriceSlice[0]) - } - } - //获取用户信息(不用判断存在) - /*user, err := l.svcCtx.AllModels.FsUser.FindUserById(l.ctx, userinfo.UserId) - if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) { - logx.Error(err) - return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get user") - }*/ //获取产品标签相关属性 productTagPropList, err := l.svcCtx.AllModels.FsProductTagProp.GetTagPropByProductIdsWithProductTag(l.ctx, productIds) if err != nil { diff --git a/server/product/internal/logic/gettagproductlistlogic.go b/server/product/internal/logic/gettagproductlistlogic.go index acd47689..5107c10e 100644 --- a/server/product/internal/logic/gettagproductlistlogic.go +++ b/server/product/internal/logic/gettagproductlistlogic.go @@ -3,8 +3,6 @@ package logic import ( "encoding/json" "errors" - "fmt" - "fusenapi/constants" "fusenapi/model/gmodel" "fusenapi/utils/auth" "fusenapi/utils/basic" @@ -229,52 +227,10 @@ func (l *GetTagProductListLogic) getProductRelationInfo(req getProductRelationIn CoverMetadata: req.MapResourceMetadata[*v.Cover], }) } - //获取产品模型价格列表 - modelList, err := l.svcCtx.AllModels.FsProductModel3d.GetAllByProductIdsTags(l.ctx, productIds, []int{constants.TAG_MODEL, constants.TAG_PARTS}, "id,product_id,price,tag,part_id,step_price") + //获取产品最低价格 + req.MapProductMinPrice, err = l.svcCtx.AllModels.FsProductModel3d.GetProductMinPrice(l.ctx, productIds) if err != nil { - logx.Error(err) - return nil, errors.New("failed to get model list") - } - mapModelMinPrice := make(map[int64]int64) - //每个模型/配件存储最小价格 - for _, modelInfo := range modelList { - switch *modelInfo.Tag { - case constants.TAG_MODEL: //模型 - if modelInfo.StepPrice == nil || len(*modelInfo.StepPrice) == 0 { - return nil, errors.New(fmt.Sprintf("model step price is not set:%d", modelInfo.Id)) - } - var stepPrice gmodel.StepPriceJsonStruct - if err = json.Unmarshal(*modelInfo.StepPrice, &stepPrice); err != nil { - logx.Error(err) - return nil, errors.New(fmt.Sprintf("failed to parse model step price:%d", modelInfo.Id)) - } - lenRange := len(stepPrice.PriceRange) - if lenRange == 0 { - return nil, errors.New(fmt.Sprintf("the count of step price is 0:%d", modelInfo.Id)) - } - 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 - } - } - if minPrice, ok := req.MapProductMinPrice[*v.ProductId]; ok { - if itemPrice < minPrice { - req.MapProductMinPrice[*v.ProductId] = itemPrice - } - continue - } - req.MapProductMinPrice[*v.ProductId] = itemPrice + return nil, err } //获取模板 productTemplatesV2List, err = l.svcCtx.AllModels.FsProductTemplateV2.FindAllByProductIds(l.ctx, productIds, "sort ASC", "product_id,id,model_id,template_tag") diff --git a/server/product/internal/logic/homepagerecommendproductlistlogic.go b/server/product/internal/logic/homepagerecommendproductlistlogic.go index 75d8963f..c5be671a 100644 --- a/server/product/internal/logic/homepagerecommendproductlistlogic.go +++ b/server/product/internal/logic/homepagerecommendproductlistlogic.go @@ -9,7 +9,6 @@ import ( "fusenapi/utils/format" "fusenapi/utils/s3url_to_s3id" "gorm.io/gorm" - "sort" "strings" "context" @@ -46,8 +45,6 @@ func (l *HomePageRecommendProductListLogic) HomePageRecommendProductList(req *ty recommendProductList []gmodel.FsProduct //产品列表(select 字段需要看查询的地方) productOptionalPartList []gmodel.GetGroupPartListByProductIdsRsp //产品配件列表 mapProductHaveOptionFitting = make(map[int64]struct{}) //是否有配件map - productPriceList []gmodel.GetPriceListByProductIdsRsp //产品价格列表(select 字段需要看查询的地方) - mapProductMinPrice = make(map[int64]int64) //产品最小价格map productTemplatesV2 []gmodel.FsProductTemplateV2 //产品模板列表(select 字段需要看查询的地方) productSizeCountList []gmodel.CountProductSizeByStatusRsp //产品尺寸数量列表(select 字段需要看查询的地方) mapProductSizeCount = make(map[int64]int64) //产品尺寸数量map @@ -114,31 +111,10 @@ func (l *HomePageRecommendProductListLogic) HomePageRecommendProductList(req *ty } mapProductHaveOptionFitting[partList.ProductId] = struct{}{} } - //获取产品价格列表 - productPriceList, err = l.svcCtx.AllModels.FsProductPrice.GetSimplePriceListByProductIds(l.ctx, productIds) + //获取产品最低价格 + mapProductMinPrice, err := l.svcCtx.AllModels.FsProductModel3d.GetProductMinPrice(l.ctx, productIds) if err != nil { - logx.Error(err) - return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to get product min price list") - } - //存储产品最小价格 - for _, v := range productPriceList { - priceStrSlic := strings.Split(v.Price, ",") - priceSlice, err := format.StrSlicToIntSlice(priceStrSlic) - if err != nil { - logx.Error(err) - return resp.SetStatusWithMessage(basic.CodeServiceErr, err.Error()) - } - if len(priceSlice) == 0 { - continue - } - sort.Ints(priceSlice) - if min, ok := mapProductMinPrice[v.ProductId]; ok { - if min > int64(priceSlice[0]) { - mapProductMinPrice[v.ProductId] = int64(priceSlice[0]) - } - } else { - mapProductMinPrice[v.ProductId] = int64(priceSlice[0]) - } + return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to get product min price") } //获取模板(只是获取产品product_id) productTemplatesV2, err = l.svcCtx.AllModels.FsProductTemplateV2.FindAllByProductIds(l.ctx, productIds, "", "product_id")