diff --git a/server/product/internal/logic/gettagproductlistlogic.go b/server/product/internal/logic/gettagproductlistlogic.go index 22ad4b2d..4d2e3bf0 100644 --- a/server/product/internal/logic/gettagproductlistlogic.go +++ b/server/product/internal/logic/gettagproductlistlogic.go @@ -86,25 +86,8 @@ func (l *GetTagProductListLogic) GetTagProductList(req *types.GetTagProductListR mapProductSizeCount = make(map[int64]int64) //产品尺寸数量map mapProductTemplate = make(map[int64]struct{}) //产品模板map ) - //携带产品 - if req.WithProduct { - //查询符合的产品列表 - pIsDel := int64(0) - pStatus := int64(1) - pIsShelf := int64(1) - pReq := gmodel.GetProductListByParamsReq{ - Type: typeIds, - IsDel: &pIsDel, - IsShelf: &pIsShelf, - Status: &pStatus, - OrderBy: "`sort` DESC", - } - //获取产品列表 - productList, err = l.svcCtx.AllModels.FsProduct.GetProductListByParams(l.ctx, pReq) - if err != nil { - logx.Error(err) - return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to get product list") - } + //携带推荐产品 + if req.WithRecommendProduct{ //提取tag推荐产品 allTagRecommendProductIds := make([]int64, 0, len(tagList)*5) for _, v := range tagList { @@ -124,6 +107,29 @@ func (l *GetTagProductListLogic) GetTagProductList(req *types.GetTagProductListR logx.Error(err) return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get tag recommend products") } + } + //携带产品 + if req.WithProduct { + //查询符合的产品列表 + pIsDel := int64(0) + pStatus := int64(1) + pIsShelf := int64(1) + pReq := gmodel.GetProductListByParamsReq{ + Type: typeIds, + IsDel: &pIsDel, + IsShelf: &pIsShelf, + Status: &pStatus, + OrderBy: "`sort` DESC", + } + //获取产品列表 + productList, err = l.svcCtx.AllModels.FsProduct.GetProductListByParams(l.ctx, pReq) + 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)) for k, v := range productList { @@ -199,26 +205,23 @@ func (l *GetTagProductListLogic) GetTagProductList(req *types.GetTagProductListR Description: *tagInfo.Description, ChildTagList: make([]*types.TagItem, 0, 100), } - //不携带产品 - if !req.WithProduct { - //加入分类 - mapTagLevel[*tagInfo.LevelPrefix] = &tagTem - continue + //携带产品 + if req.WithProduct { + //获取分类产品列表 + productListRsp := l.getTagProducts(getTagProductsReq{ + TagId: tagInfo.Id, + ProductList: productList, + MapProductMinPrice: mapProductMinPrice, + MapProductTemplate: mapProductTemplate, + MapProductSizeCount: mapProductSizeCount, + Size: req.Size, + User: user, + }) + //赋值 + tagTem.TagProductList = productListRsp } - //获取分类产品列表 - productListRsp := l.getTagProducts(getTagProductsReq{ - TagId: tagInfo.Id, - ProductList: productList, - MapProductMinPrice: mapProductMinPrice, - MapProductTemplate: mapProductTemplate, - MapProductSizeCount: mapProductSizeCount, - Size: req.Size, - User: user, - }) - //赋值 - tagTem.TagProductList = productListRsp //获取推荐产品列表 - if tagInfo.RecommendProduct != nil && *tagInfo.RecommendProduct != "" { + if req.WithRecommendProduct && tagInfo.RecommendProduct != nil && *tagInfo.RecommendProduct != "" { //上面解析过,这里就无需判断错误 recommendProductIds, _ := format.StrSlicToInt64Slice(strings.Split(*tagInfo.RecommendProduct, ",")) //推荐产品的排序 @@ -249,10 +252,24 @@ func (l *GetTagProductListLogic) GetTagProductList(req *types.GetTagProductListR 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 +} +//组织等级从属关系 +func (l *GetTagProductListLogic)organizationLevelRelation(mapTagLevel map[string]*types.TagItem)[]types.TagItem{ + mapTop := make(map[string]struct{}) for prefix, tagItem := range mapTagLevel { - prefix = strings.Trim(prefix, " ") //最上级没有父级 if !strings.Contains(prefix, "/") { + mapTop[prefix] = struct{}{} continue } prefixSlice := strings.Split(prefix, "/") @@ -268,26 +285,15 @@ func (l *GetTagProductListLogic) GetTagProductList(req *types.GetTagProductListR } } //最终值提取最高级别那一层出来 - tagListRsp := make([]types.TagItem, 0, len(mapTagLevel)) - for _, v := range mapTagLevel { - if v.Level != minLevel { - continue - } - tagListRsp = append(tagListRsp, *v) + rspList := make([]types.TagItem, 0, len(mapTagLevel)) + for prefix, _ := range mapTop { + rspList = append(rspList,*mapTagLevel[prefix]) } //排序 - sort.SliceStable(tagListRsp, func(i, j int) bool { - return tagListRsp[i].Sort < tagListRsp[j].Sort + sort.SliceStable(rspList, func(i, j int) bool { + return rspList[i].Sort < rspList[j].Sort }) - return resp.SetStatusWithMessage(basic.CodeOK, "success", types.GetTagProductListRsp{ - TotalCategory: len(mapTagLevel), - TagList: tagListRsp, - }) -} -//排序推荐产品结构体 -type sortRecommendProduct struct { - ProductId int64 - Sort int64 + return rspList } //获取tag推荐产品列表 type getTagRecommendProductsReq struct { diff --git a/server/product/internal/types/types.go b/server/product/internal/types/types.go index 2e1ede83..5f5e3768 100644 --- a/server/product/internal/types/types.go +++ b/server/product/internal/types/types.go @@ -245,9 +245,10 @@ type GetRecommandProductListRsp struct { } type GetTagProductListReq struct { - Cid int64 `form:"cid,optional"` //分类id - Size uint32 `form:"size,optional"` //尺寸 - WithProduct bool `form:"with_product,optional"` //不携带产品只返回菜单 + Cid int64 `form:"cid,optional"` //分类id + Size uint32 `form:"size,optional"` //尺寸 + WithProduct bool `form:"with_product,optional"` //是否携带分类下的产品 + WithRecommendProduct bool `form:"with_recommend_product"` //是否携带分类推荐产品 } type GetTagProductListRsp struct { diff --git a/server_api/product.api b/server_api/product.api index 7dad4ba3..b1e80c00 100644 --- a/server_api/product.api +++ b/server_api/product.api @@ -291,9 +291,10 @@ type GetRecommandProductListRsp { } //获取分类产品列表 type GetTagProductListReq { - Cid int64 `form:"cid,optional"` //分类id - Size uint32 `form:"size,optional"` //尺寸 - WithProduct bool `form:"with_product,optional"` //不携带产品只返回菜单 + Cid int64 `form:"cid,optional"` //分类id + Size uint32 `form:"size,optional"` //尺寸 + WithProduct bool `form:"with_product,optional"` //是否携带分类下的产品 + WithRecommendProduct bool `form:"with_recommend_product"` //是否携带分类推荐产品 } type GetTagProductListRsp { TotalCategory int `json:"total_category"`