From bf3d9f9631cc22a3eda40512a78a8c045302b3b7 Mon Sep 17 00:00:00 2001 From: laodaming <11058467+laudamine@user.noreply.gitee.com> Date: Thu, 20 Jul 2023 15:27:17 +0800 Subject: [PATCH] fix --- .../logic/getrecommandproductlistlogic.go | 45 +++++++++++++++++-- .../internal/logic/gettagproductlistlogic.go | 8 +++- server/product/internal/types/types.go | 1 + server_api/product.api | 1 + 4 files changed, 50 insertions(+), 5 deletions(-) diff --git a/server/product/internal/logic/getrecommandproductlistlogic.go b/server/product/internal/logic/getrecommandproductlistlogic.go index 68f4cb29..465e9dee 100644 --- a/server/product/internal/logic/getrecommandproductlistlogic.go +++ b/server/product/internal/logic/getrecommandproductlistlogic.go @@ -4,8 +4,11 @@ import ( "errors" "fusenapi/utils/auth" "fusenapi/utils/basic" + "fusenapi/utils/format" "fusenapi/utils/image" "gorm.io/gorm" + "sort" + "strings" "context" @@ -51,13 +54,13 @@ func (l *GetRecommandProductListLogic) GetRecommandProductList(req *types.GetRec //需要填充时需要忽略的id ignoreProductIds := make([]int64, 0, len(recommendList)+1) ignoreProductIds = append(ignoreProductIds, productInfo.Id) - recommendProductIds := make([]int64, 0, len(recommendList)) + productIds := make([]int64, 0, len(recommendList)) for _, v := range recommendList { ignoreProductIds = append(ignoreProductIds, *v.ProductId) - recommendProductIds = append(recommendProductIds, *v.ProductId) + productIds = append(productIds, *v.ProductId) } //获取推荐产品列表 - recommendProductList, err := l.svcCtx.AllModels.FsProduct.GetProductListByIds(l.ctx, recommendProductIds, "") + recommendProductList, err := l.svcCtx.AllModels.FsProduct.GetProductListByIds(l.ctx, productIds, "") if err != nil { logx.Error(err) return resp.SetStatus(basic.CodeDbSqlErr, "failed to get recommend product list") @@ -77,7 +80,36 @@ func (l *GetRecommandProductListLogic) GetRecommandProductList(req *types.GetRec return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get product list") } //合并列表 - recommendProductList = append(recommendProductList, productList...) + for _, v := range productList { + productIds = append(productIds, v.Id) + recommendProductList = append(recommendProductList, v) + } + } + //查询产品价格 + priceList, err := l.svcCtx.AllModels.FsProductPrice.GetPriceListByProductIds(l.ctx, productIds) + if err != nil { + logx.Error(err) + return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get product price list") + } + 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) @@ -105,6 +137,10 @@ func (l *GetRecommandProductListLogic) GetRecommandProductList(req *types.GetRec if _, ok := mapRecommend[v.Id]; ok { isRecommend = 1 } + minPrice := int64(0) + if minVal, ok := mapProductMinPrice[v.Id]; ok { + minPrice = minVal + } list = append(list, types.GetRecommandProductListRsp{ Id: v.Id, Sn: *v.Sn, @@ -115,6 +151,7 @@ func (l *GetRecommandProductListLogic) GetRecommandProductList(req *types.GetRec CoverDefault: r.CoverDefault, Intro: *v.Intro, IsRecommend: isRecommend, + MinPrice: minPrice, }) } return resp.SetStatusWithMessage(basic.CodeOK, "success", list) diff --git a/server/product/internal/logic/gettagproductlistlogic.go b/server/product/internal/logic/gettagproductlistlogic.go index d1c034e1..1d6a1a18 100644 --- a/server/product/internal/logic/gettagproductlistlogic.go +++ b/server/product/internal/logic/gettagproductlistlogic.go @@ -143,7 +143,13 @@ func (l *GetTagProductListLogic) GetTagProductList(req *types.GetTagProductListR continue } sort.Ints(priceSlice) - mapProductMinPrice[v.ProductId] = int64(priceSlice[0]) + 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]) + } } //获取模板(只是获取产品product_id) productTemplatesV2, err = l.svcCtx.AllModels.FsProductTemplateV2.FindAllByProductIds(l.ctx, productIds, "product_id") diff --git a/server/product/internal/types/types.go b/server/product/internal/types/types.go index 3b611579..073f6a3e 100644 --- a/server/product/internal/types/types.go +++ b/server/product/internal/types/types.go @@ -243,6 +243,7 @@ type GetRecommandProductListRsp struct { CoverDefault string `json:"cover_default"` Intro string `json:"intro"` IsRecommend int64 `json:"is_recommend"` + MinPrice int64 `json:"min_price"` } type GetTagProductListReq struct { diff --git a/server_api/product.api b/server_api/product.api index 9c214c82..f3eb5ab8 100644 --- a/server_api/product.api +++ b/server_api/product.api @@ -292,6 +292,7 @@ type GetRecommandProductListRsp { CoverDefault string `json:"cover_default"` Intro string `json:"intro"` IsRecommend int64 `json:"is_recommend"` + MinPrice int64 `json:"min_price"` } //获取分类产品列表 type GetTagProductListReq {