diff --git a/server/product/internal/logic/gettagproductlistlogic.go b/server/product/internal/logic/gettagproductlistlogic.go index 3043ec68..29334af2 100644 --- a/server/product/internal/logic/gettagproductlistlogic.go +++ b/server/product/internal/logic/gettagproductlistlogic.go @@ -186,7 +186,46 @@ func (l *GetTagProductListLogic) GetTagProductList(req *types.GetTagProductListR } //map tag菜单 mapTagLevel := make(map[string]*types.TagItem) - for _, tagInfo := range tagList { + //处理tags数据 + l.delWithTagMenuData(delWithTagMenuDataReq{ + TagList: tagList, + WithProduct: req.WithProduct, + WithRecommendProduct: req.WithRecommendProduct, + ProductList: productList, + MapProductMinPrice: mapProductMinPrice, + MapProductTemplate: mapProductTemplate, + MapProductSizeCount: mapProductSizeCount, + MapTagLevel: mapTagLevel, + MapProduct: mapProduct, + Size: req.Size, + user: user, + }) + return resp.SetStatusWithMessage(basic.CodeOK, "success", types.GetTagProductListRsp{ + TotalCategory: len(mapTagLevel), + TagList: l.organizationLevelRelation(mapTagLevel),//组装等级从属关系 + }) +} +//排序推荐产品结构体 +type sortRecommendProduct struct { + ProductId int64 + Sort int64 +} +//处理tag菜单数据 +type delWithTagMenuDataReq struct { + 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 +} +func (l *GetTagProductListLogic)delWithTagMenuData(req delWithTagMenuDataReq)error{ + for _, tagInfo := range req.TagList { tagTem := types.TagItem{ TagProductList: nil, TagRecommendProductList: nil, @@ -197,19 +236,19 @@ func (l *GetTagProductListLogic) GetTagProductList(req *types.GetTagProductListR Icon: *tagInfo.Icon, Sort: *tagInfo.Sort, Description: *tagInfo.Description, - ChildTagList: make([]*types.TagItem, 0, 100), + ChildTagList: make([]*types.TagItem, 0, 50), } //携带产品 if req.WithProduct { //获取分类产品列表 productListRsp := l.getTagProducts(getTagProductsReq{ TagId: tagInfo.Id, - ProductList: productList, - MapProductMinPrice: mapProductMinPrice, - MapProductTemplate: mapProductTemplate, - MapProductSizeCount: mapProductSizeCount, + ProductList: req.ProductList, + MapProductMinPrice: req.MapProductMinPrice, + MapProductTemplate: req.MapProductTemplate, + MapProductSizeCount: req.MapProductSizeCount, Size: req.Size, - User: user, + User: req.user, }) //赋值 tagTem.TagProductList = productListRsp @@ -225,35 +264,27 @@ func (l *GetTagProductListLogic) GetTagProductList(req *types.GetTagProductListR return nil } if len(recommendProductIds) != len(recommendProductIdsSort){ - return resp.SetStatusWithMessage(basic.CodeServiceErr,fmt.Sprintf("length of recommend product id is neq length of recommend sort,id= %d",tagInfo.Id)) + 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, - ProductList: productList, - MapProduct: mapProduct, - MapProductMinPrice: mapProductMinPrice, - MapProductTemplate: mapProductTemplate, - MapProductSizeCount: mapProductSizeCount, + ProductList: req.ProductList, + MapProduct: req.MapProduct, + MapProductMinPrice: req.MapProductMinPrice, + MapProductTemplate: req.MapProductTemplate, + MapProductSizeCount: req.MapProductSizeCount, RecommendProductIds: recommendProductIds, RecommendProductIdsSort: recommendProductIdsSort, Size: req.Size, - User: user, + User: req.user, }) //赋值 tagTem.TagRecommendProductList = recommendProductListRsp } //加入分类 - mapTagLevel[*tagInfo.LevelPrefix] = &tagTem + req.MapTagLevel[*tagInfo.LevelPrefix] = &tagTem } - return resp.SetStatusWithMessage(basic.CodeOK, "success", types.GetTagProductListRsp{ - TotalCategory: len(mapTagLevel), - TagList: l.organizationLevelRelation(mapTagLevel),//组装等级从属关系 - }) -} -//排序推荐产品结构体 -type sortRecommendProduct struct { - ProductId int64 - Sort int64 + return nil } //组织等级从属关系 func (l *GetTagProductListLogic)organizationLevelRelation(mapTagLevel map[string]*types.TagItem)[]types.TagItem{ @@ -267,14 +298,16 @@ func (l *GetTagProductListLogic)organizationLevelRelation(mapTagLevel map[string prefixSlice := strings.Split(prefix, "/") //有父级 parentPrefix := strings.Join(prefixSlice[:len(prefixSlice)-1], "/") - if parent, ok := mapTagLevel[parentPrefix]; ok { - parent.ChildTagList = append(parent.ChildTagList, tagItem) - //排序 - sort.SliceStable(parent.ChildTagList, func(i, j int) bool { - return parent.ChildTagList[i].Sort < parent.ChildTagList[j].Sort - }) - mapTagLevel[parentPrefix] = parent + parent, ok := mapTagLevel[parentPrefix] + if !ok{ + continue } + parent.ChildTagList = append(parent.ChildTagList, tagItem) + //排序 + sort.SliceStable(parent.ChildTagList, func(i, j int) bool { + return parent.ChildTagList[i].Sort < parent.ChildTagList[j].Sort + }) + mapTagLevel[parentPrefix] = parent } //最终值提取最高级别那一层出来 rspList := make([]types.TagItem, 0, len(mapTagLevel))