From 99bd3592af15e8faddb65f4cdf3b3dd4510a5c82 Mon Sep 17 00:00:00 2001 From: laodaming <11058467+laudamine@user.noreply.gitee.com> Date: Wed, 19 Jul 2023 17:34:07 +0800 Subject: [PATCH] fix --- .../internal/logic/gettagproductlistlogic.go | 121 +++++++++--------- 1 file changed, 64 insertions(+), 57 deletions(-) diff --git a/server/product/internal/logic/gettagproductlistlogic.go b/server/product/internal/logic/gettagproductlistlogic.go index 3a2bd556..b0beee0e 100644 --- a/server/product/internal/logic/gettagproductlistlogic.go +++ b/server/product/internal/logic/gettagproductlistlogic.go @@ -87,14 +87,14 @@ func (l *GetTagProductListLogic) GetTagProductList(req *types.GetTagProductListR mapProductTemplate = make(map[int64]struct{}) //产品模板map ) //携带推荐产品 - if req.WithRecommendProduct{ + if req.WithRecommendProduct { //提取tag推荐产品 allTagRecommendProductIds := make([]int64, 0, len(tagList)*5) for _, v := range tagList { if v.RecommendProduct == nil || *v.RecommendProduct == "" { continue } - sl, err := format.StrSlicToInt64Slice(strings.Split(*v.RecommendProductSort, ",")) + sl, err := format.StrSlicToInt64Slice(strings.Split(*v.RecommendProduct, ",")) if err != nil { logx.Error(err) return resp.SetStatusWithMessage(basic.CodeServiceErr, fmt.Sprintf("failed to parse recommend product ids,id=%d", v.Id)) @@ -117,23 +117,20 @@ func (l *GetTagProductListLogic) GetTagProductList(req *types.GetTagProductListR //获取产品列表 productList, err = l.svcCtx.AllModels.FsProduct.GetProductListByParams(l.ctx, gmodel.GetProductListByParamsReq{ - Type: typeIds, - IsDel: &pIsDel, - IsShelf: &pIsShelf, - Status: &pStatus, - OrderBy: "`sort` DESC", - }) + Type: typeIds, + IsDel: &pIsDel, + IsShelf: &pIsShelf, + Status: &pStatus, + OrderBy: "`sort` DESC", + }) if err != nil { logx.Error(err) return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to get product list") } } //任意一个不为空 - if len(productList) != 0 || len(recommendProductList) != 0{ - //提取产品ids - productIds := make([]int64, 0, len(productList)) + if len(productList) != 0 || len(recommendProductList) != 0 { for k, v := range productList { - productIds = append(productIds, v.Id) mapProduct[v.Id] = k } //合并产品列表 @@ -145,6 +142,10 @@ func (l *GetTagProductListLogic) GetTagProductList(req *types.GetTagProductListR productList = append(productList, v) mapProduct[v.Id] = len(productList) - 1 } + productIds := make([]int64,0,len(productList)) + for _,product := range productList{ + productIds = append(productIds,product.Id) + } //获取产品价格列表 productPriceList, err = l.svcCtx.AllModels.FsProductPrice.GetSimplePriceListByProductIds(l.ctx, productIds) if err != nil { @@ -199,35 +200,38 @@ func (l *GetTagProductListLogic) GetTagProductList(req *types.GetTagProductListR MapProduct: mapProduct, Size: req.Size, user: user, - });err != nil{ + }); err != nil { logx.Error(err) - return resp.SetStatusAddMessage(basic.CodeServiceErr,"failed to deal with tag data") + return resp.SetStatusAddMessage(basic.CodeServiceErr, "failed to deal with tag data") } return resp.SetStatusWithMessage(basic.CodeOK, "success", types.GetTagProductListRsp{ TotalCategory: len(mapTagLevel), - TagList: l.organizationLevelRelation(mapTagLevel),//组装等级从属关系 + TagList: l.organizationLevelRelation(mapTagLevel), //组装等级从属关系 }) } -//排序推荐产品结构体 + +// 排序推荐产品结构体 type sortRecommendProduct struct { ProductId int64 - Sort int64 + Sort int64 } -//处理tag菜单数据 + +// 处理tag菜单数据 type dealWithTagMenuDataReq struct { - TagList []gmodel.FsTags - WithProduct bool + TagList []gmodel.FsTags + WithProduct bool WithRecommendProduct bool - ProductList []gmodel.FsProduct - MapProductMinPrice map[int64]int64 - MapProductTemplate map[int64]struct{} - MapProductSizeCount map[int64]int64 - MapTagLevel map[string]*types.TagItem - MapProduct map[int64]int - Size uint32 - user gmodel.FsUser + ProductList []gmodel.FsProduct + MapProductMinPrice map[int64]int64 + MapProductTemplate map[int64]struct{} + MapProductSizeCount map[int64]int64 + MapTagLevel map[string]*types.TagItem + MapProduct map[int64]int + Size uint32 + user gmodel.FsUser } -func (l *GetTagProductListLogic)dealWithTagMenuData(req dealWithTagMenuDataReq)error{ + +func (l *GetTagProductListLogic) dealWithTagMenuData(req dealWithTagMenuDataReq) error { for _, tagInfo := range req.TagList { tagTem := types.TagItem{ TagProductList: nil, @@ -242,7 +246,7 @@ func (l *GetTagProductListLogic)dealWithTagMenuData(req dealWithTagMenuDataReq)e ChildTagList: make([]*types.TagItem, 0, 50), } //携带产品 - if req.WithProduct { + if req.WithProduct { //获取分类产品列表 productListRsp := l.getTagProducts(getTagProductsReq{ TagId: tagInfo.Id, @@ -262,12 +266,12 @@ func (l *GetTagProductListLogic)dealWithTagMenuData(req dealWithTagMenuDataReq)e recommendProductIds, _ := format.StrSlicToInt64Slice(strings.Split(*tagInfo.RecommendProduct, ",")) //推荐产品的排序 recommendProductIdsSort, err := format.StrSlicToInt64Slice(strings.Split(*tagInfo.RecommendProductSort, ",")) - if err != nil{ + if err != nil { logx.Error(err) return nil } - if len(recommendProductIds) != len(recommendProductIdsSort){ - return errors.New(fmt.Sprintf("length of recommend product id is neq length of recommend sort,id= %d",tagInfo.Id)) + if len(recommendProductIds) != len(recommendProductIdsSort) { + return errors.New(fmt.Sprintf("length of recommend product id is neq length of recommend sort,id= %d", tagInfo.Id)) } recommendProductListRsp := l.getTagRecommendProducts(getTagRecommendProductsReq{ TagInfo: tagInfo, @@ -289,8 +293,9 @@ func (l *GetTagProductListLogic)dealWithTagMenuData(req dealWithTagMenuDataReq)e } return nil } -//组织等级从属关系 -func (l *GetTagProductListLogic)organizationLevelRelation(mapTagLevel map[string]*types.TagItem)[]types.TagItem{ + +// 组织等级从属关系 +func (l *GetTagProductListLogic) organizationLevelRelation(mapTagLevel map[string]*types.TagItem) []types.TagItem { mapTop := make(map[string]struct{}) for prefix, tagItem := range mapTagLevel { //最上级没有父级 @@ -302,7 +307,7 @@ func (l *GetTagProductListLogic)organizationLevelRelation(mapTagLevel map[string //有父级 parentPrefix := strings.Join(prefixSlice[:len(prefixSlice)-1], "/") parent, ok := mapTagLevel[parentPrefix] - if !ok{ + if !ok { continue } parent.ChildTagList = append(parent.ChildTagList, tagItem) @@ -315,7 +320,7 @@ func (l *GetTagProductListLogic)organizationLevelRelation(mapTagLevel map[string //最终值提取最高级别那一层出来 rspList := make([]types.TagItem, 0, len(mapTagLevel)) for prefix, _ := range mapTop { - rspList = append(rspList,*mapTagLevel[prefix]) + rspList = append(rspList, *mapTagLevel[prefix]) } //排序 sort.SliceStable(rspList, func(i, j int) bool { @@ -323,36 +328,37 @@ func (l *GetTagProductListLogic)organizationLevelRelation(mapTagLevel map[string }) return rspList } -//获取tag推荐产品列表 + +// 获取tag推荐产品列表 type getTagRecommendProductsReq struct { - TagInfo gmodel.FsTags - ProductList []gmodel.FsProduct - MapProduct map[int64]int - MapProductMinPrice map[int64]int64 - MapProductTemplate map[int64]struct{} - MapProductSizeCount map[int64]int64 - RecommendProductIds []int64 + TagInfo gmodel.FsTags + ProductList []gmodel.FsProduct + MapProduct map[int64]int + MapProductMinPrice map[int64]int64 + MapProductTemplate map[int64]struct{} + MapProductSizeCount map[int64]int64 + RecommendProductIds []int64 RecommendProductIdsSort []int64 - Size uint32 - User gmodel.FsUser + Size uint32 + User gmodel.FsUser } -func (l *GetTagProductListLogic)getTagRecommendProducts(req getTagRecommendProductsReq)(productListRsp []types.TagProduct){ +func (l *GetTagProductListLogic) getTagRecommendProducts(req getTagRecommendProductsReq) (productListRsp []types.TagProduct) { //排序 - sortList := make([]sortRecommendProduct,0,len(req.RecommendProductIds)) - for sortIndex,pid := range req.RecommendProductIds{ - sortList = append(sortList,sortRecommendProduct{ + sortList := make([]sortRecommendProduct, 0, len(req.RecommendProductIds)) + for sortIndex, pid := range req.RecommendProductIds { + sortList = append(sortList, sortRecommendProduct{ ProductId: pid, - Sort: req.RecommendProductIdsSort[sortIndex], + Sort: req.RecommendProductIdsSort[sortIndex], }) } sort.SliceStable(sortList, func(i, j int) bool { return sortList[i].Sort < sortList[j].Sort }) - productListRsp = make([]types.TagProduct,0,len(sortList)) - for _,sortVal := range sortList{ - productIndex,ok := req.MapProduct[sortVal.ProductId] - if !ok{ + productListRsp = make([]types.TagProduct, 0, len(sortList)) + for _, sortVal := range sortList { + productIndex, ok := req.MapProduct[sortVal.ProductId] + if !ok { continue } productInfo := req.ProductList[productIndex] @@ -394,10 +400,11 @@ func (l *GetTagProductListLogic)getTagRecommendProducts(req getTagRecommendProdu item.CoverImg = r.CoverImg item.CoverDefault = r.CoverDefault //加入切片 - productListRsp = append(productListRsp,item) + productListRsp = append(productListRsp, item) } return productListRsp } + // 获取对应tag的产品列表 type getTagProductsReq struct { TagId int64