This commit is contained in:
laodaming 2023-10-11 11:26:11 +08:00
parent 501c4643f4
commit 8f8419b8c1

View File

@ -132,7 +132,7 @@ func (l *GetTagProductListLogic) GetTagProductList(req *types.GetTagProductListR
return resp.SetStatusAddMessage(basic.CodeServiceErr, "failed to deal with tag data") return resp.SetStatusAddMessage(basic.CodeServiceErr, "failed to deal with tag data")
} }
//组装等级从属关系 //组装等级从属关系
rspTagList, TotalCategoryProduct := l.organizationLevelRelation(minLevel, mapTagLevel, productList) rspTagList, TotalCategoryProduct := l.organizationLevelRelation(minLevel, mapTagLevel)
return resp.SetStatusWithMessage(basic.CodeOK, "success", types.GetTagProductListRsp{ return resp.SetStatusWithMessage(basic.CodeOK, "success", types.GetTagProductListRsp{
TotalCategoryProduct: TotalCategoryProduct, TotalCategoryProduct: TotalCategoryProduct,
TagList: rspTagList, TagList: rspTagList,
@ -308,7 +308,7 @@ func (l *GetTagProductListLogic) dealWithTagMenuData(req dealWithTagMenuDataReq)
}) })
//tag中产品 //tag中产品
for _, tmpProduct := range productListRsp { for _, tmpProduct := range productListRsp {
tagTem.TagProductList = append(tagTem.TagProductList, tmpProduct.ProductId) tagTem.TagProductList = append(tagTem.TagProductList, tmpProduct)
} }
} }
//加入分类 //加入分类
@ -318,7 +318,7 @@ func (l *GetTagProductListLogic) dealWithTagMenuData(req dealWithTagMenuDataReq)
} }
// 组织等级从属关系 // 组织等级从属关系
func (l *GetTagProductListLogic) organizationLevelRelation(minLevel int, mapTagLevel map[string]*types.TagItem, productList []gmodel.FsProduct) (rspTagList []types.TagItem, productCount int) { func (l *GetTagProductListLogic) organizationLevelRelation(minLevel int, mapTagLevel map[string]*types.TagItem) (rspTagList []types.TagItem, productCount int) {
mapTop := make(map[string]struct{}) mapTop := make(map[string]struct{})
//设置归属关系 //设置归属关系
for prefix, tagItem := range mapTagLevel { for prefix, tagItem := range mapTagLevel {
@ -335,33 +335,22 @@ func (l *GetTagProductListLogic) organizationLevelRelation(minLevel int, mapTagL
continue continue
} }
parent.ChildTagList = append(parent.ChildTagList, tagItem) parent.ChildTagList = append(parent.ChildTagList, tagItem)
//排序子菜单 //排序
sort.SliceStable(parent.ChildTagList, func(i, j int) bool { sort.SliceStable(parent.ChildTagList, func(i, j int) bool {
return parent.ChildTagList[i].Sort < parent.ChildTagList[j].Sort return parent.ChildTagList[i].Sort < parent.ChildTagList[j].Sort
}) })
mapTagLevel[parentPrefix] = parent mapTagLevel[parentPrefix] = parent
} }
//父类聚合子类所有产品 //把子类的产品列表变成产品id列表并且把产品列表都放到最顶级父级中
for _, v := range mapTagLevel { for _, v := range mapTagLevel {
if _, ok := mapTop[v.LevelPrefix]; ok { if _, ok := mapTop[v.LevelPrefix]; ok {
continue continue
} }
//顶部需要初始化 topPrefixSlic := strings.Split(v.LevelPrefix, "/")[:minLevel]
v.TagProductList = make([]interface{}, 0, 50) topPrefix := strings.Join(topPrefixSlic, "/")
mapTypeId := make(map[int64]struct{}) for k, tmpProduct := range v.TagProductList {
for _, v2 := range mapTagLevel { v.TagProductList[k] = tmpProduct.(types.TagProduct).ProductId
//包含主路径,则属于该类型 mapTagLevel[topPrefix].TagProductList = append(mapTagLevel[topPrefix].TagProductList, tmpProduct)
if strings.Contains(v2.LevelPrefix, v.LevelPrefix) {
mapTypeId[v2.TypeId] = struct{}{}
}
}
//循环产品放入
for _, p := range productList {
_, ok := mapTypeId[*p.Type]
if !ok {
continue
}
mapTagLevel[v.LevelPrefix].TagProductList = append(mapTagLevel[v.LevelPrefix].TagProductList, p)
} }
} }
//最终值提取最高级别那一层出来 //最终值提取最高级别那一层出来
@ -374,7 +363,7 @@ func (l *GetTagProductListLogic) organizationLevelRelation(minLevel int, mapTagL
productCount += len(mapTagLevel[prefix].TagProductList) productCount += len(mapTagLevel[prefix].TagProductList)
rspList = append(rspList, *mapTagLevel[prefix]) rspList = append(rspList, *mapTagLevel[prefix])
} }
//排序主菜单 //排序
sort.SliceStable(rspList, func(i, j int) bool { sort.SliceStable(rspList, func(i, j int) bool {
return rspList[i].Sort < rspList[j].Sort return rspList[i].Sort < rspList[j].Sort
}) })