From f09220cf2d3629d874d22eeac168094ac7db1f31 Mon Sep 17 00:00:00 2001 From: laodaming <11058467+laudamine@user.noreply.gitee.com> Date: Wed, 19 Jul 2023 14:01:53 +0800 Subject: [PATCH 1/5] fix --- model/gmodel/fs_product_logic.go | 2 +- .../internal/logic/gettagproductlistlogic.go | 190 +++++++++++------- server/product/internal/types/types.go | 24 ++- server_api/product.api | 24 ++- 4 files changed, 148 insertions(+), 92 deletions(-) diff --git a/model/gmodel/fs_product_logic.go b/model/gmodel/fs_product_logic.go index 16081b58..009181db 100755 --- a/model/gmodel/fs_product_logic.go +++ b/model/gmodel/fs_product_logic.go @@ -73,7 +73,7 @@ func (p *FsProductModel) GetRandomProductList(ctx context.Context, limit int) (r return resp, err } -func (p *FsProductModel) FindAllOnlyByIds(ctx context.Context, ids []int64) (resp []*FsProduct, err error) { +func (p *FsProductModel) FindAllOnlyByIds(ctx context.Context, ids []int64) (resp []FsProduct, err error) { err = p.db.WithContext(ctx).Model(&FsProduct{}).Where("`id` IN (?)", ids).Find(&resp).Error return resp, err } diff --git a/server/product/internal/logic/gettagproductlistlogic.go b/server/product/internal/logic/gettagproductlistlogic.go index fd89b6eb..3cb7ba9f 100644 --- a/server/product/internal/logic/gettagproductlistlogic.go +++ b/server/product/internal/logic/gettagproductlistlogic.go @@ -2,6 +2,7 @@ package logic import ( "errors" + "fmt" "fusenapi/model/gmodel" "fusenapi/utils/auth" "fusenapi/utils/basic" @@ -74,67 +75,107 @@ func (l *GetTagProductListLogic) GetTagProductList(req *types.GetTagProductListR for _, v := range tagList { typeIds = append(typeIds, v.Id) } - //查询符合的产品列表 - 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") - } - //提取产品ids - productIds := make([]int64, 0, len(productList)) - for _, v := range productList { - productIds = append(productIds, v.Id) - } - productPriceList, err := l.svcCtx.AllModels.FsProductPrice.GetSimplePriceListByProductIds(l.ctx, productIds) - if err != nil { - logx.Error(err) - return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to get product min price list") - } - //存储产品最小价格 - mapProductMinPrice := make(map[int64]int64) - for _, v := range productPriceList { - priceStrSlic := strings.Split(v.Price, ",") - priceSlice, err := format.StrSlicToIntSlice(priceStrSlic) + var ( + productList []gmodel.FsProduct //产品列表(select 字段需要看查询的地方) + recommendProductList []gmodel.FsProduct //tag推荐产品列表(select 字段需要看查询的地方) + mapProduct = make(map[int64]int) //产品map + productPriceList []gmodel.GetPriceListByProductIdsRsp //产品价格列表(select 字段需要看查询的地方) + mapProductMinPrice = make(map[int64]int64) //产品最小价格map + productTemplatesV2 []gmodel.FsProductTemplateV2 //产品模板列表(select 字段需要看查询的地方) + productSizeCountList []gmodel.CountProductSizeByStatusRsp //产品尺寸数量列表(select 字段需要看查询的地方) + 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, err.Error()) + return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to get product list") } - if len(priceSlice) == 0 { - continue + //提取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, ",")) + if err != nil { + logx.Error(err) + return resp.SetStatusWithMessage(basic.CodeServiceErr, fmt.Sprintf("failed to parse recommend product ids,id=%d", v.Id)) + } + allTagRecommendProductIds = append(allTagRecommendProductIds, sl...) + } + //获取推荐的产品列表 + recommendProductList, err = l.svcCtx.AllModels.FsProduct.FindAllOnlyByIds(l.ctx, allTagRecommendProductIds) + if err != nil { + logx.Error(err) + return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get tag recommend products") + } + //提取产品ids + productIds := make([]int64, 0, len(productList)) + for k, v := range productList { + productIds = append(productIds, v.Id) + mapProduct[v.Id] = k + } + //合并产品列表 + for _, v := range recommendProductList { + //存在则不并入 + if _, ok := mapProduct[v.Id]; ok { + continue + } + productList = append(productList, v) + mapProduct[v.Id] = len(productList) - 1 + } + productPriceList, err = l.svcCtx.AllModels.FsProductPrice.GetSimplePriceListByProductIds(l.ctx, productIds) + if err != nil { + logx.Error(err) + return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to get product min price list") + } + //存储产品最小价格 + for _, v := range productPriceList { + priceStrSlic := strings.Split(v.Price, ",") + priceSlice, err := format.StrSlicToIntSlice(priceStrSlic) + if err != nil { + logx.Error(err) + return resp.SetStatusWithMessage(basic.CodeServiceErr, err.Error()) + } + if len(priceSlice) == 0 { + continue + } + sort.Ints(priceSlice) + mapProductMinPrice[v.ProductId] = int64(priceSlice[0]) + } + //获取模板(只是获取产品product_id) + productTemplatesV2, err = l.svcCtx.AllModels.FsProductTemplateV2.FindAllByProductIds(l.ctx, productIds, "product_id") + if err != nil { + logx.Error(err) + return resp.SetStatusWithMessage(basic.CodeServiceErr, "get product template_v2 err") + } + for _, v := range productTemplatesV2 { + mapProductTemplate[*v.ProductId] = struct{}{} + } + //获取产品尺寸数量 + productSizeCountList, err = l.svcCtx.AllModels.FsProductSize.GetGroupProductSizeByStatus(l.ctx, productIds, 1) + if err != nil { + logx.Error(err) + return resp.SetStatusWithMessage(basic.CodeServiceErr, "get product size count err") + } + for _, v := range productSizeCountList { + mapProductSizeCount[v.ProductId] = v.Num } - sort.Ints(priceSlice) - mapProductMinPrice[v.ProductId] = int64(priceSlice[0]) - } - //获取模板(只是获取产品product_id) - productTemplatesV2, err := l.svcCtx.AllModels.FsProductTemplateV2.FindAllByProductIds(l.ctx, productIds, "product_id") - if err != nil { - logx.Error(err) - return resp.SetStatusWithMessage(basic.CodeServiceErr, "get product template_v2 err") - } - mapProductTemplate := make(map[int64]struct{}) - for _, v := range productTemplatesV2 { - mapProductTemplate[*v.ProductId] = struct{}{} - } - //获取产品尺寸数量 - productSizeCountList, err := l.svcCtx.AllModels.FsProductSize.GetGroupProductSizeByStatus(l.ctx, productIds, 1) - if err != nil { - logx.Error(err) - return resp.SetStatusWithMessage(basic.CodeServiceErr, "get product size count err") - } - mapProductSizeCount := make(map[int64]int64) - for _, v := range productSizeCountList { - mapProductSizeCount[v.ProductId] = v.Num } mapTagLevel := make(map[string]*types.TagItem) minLevel := int64(0) //记录最小等级数字 @@ -145,6 +186,24 @@ func (l *GetTagProductListLogic) GetTagProductList(req *types.GetTagProductListR if minLevel > *tagInfo.Level { minLevel = *tagInfo.Level } + tagTem := types.TagItem{ + TagProductList: nil, + TagRecommendProductList: nil, + TypeName: *tagInfo.Title, + TypeId: tagInfo.Id, + Level: *tagInfo.Level, + LevelPrefix: *tagInfo.LevelPrefix, + Icon: *tagInfo.Icon, + Sort: *tagInfo.Sort, + Description: *tagInfo.Description, + ChildTagList: make([]*types.TagItem, 0, 100), + } + //不携带产品 + if !req.WithProduct { + //加入分类 + mapTagLevel[*tagInfo.LevelPrefix] = &tagTem + continue + } //获取分类产品列表 productListRsp := l.getTagProducts(getTagProductsReq{ TagId: tagInfo.Id, @@ -155,19 +214,12 @@ func (l *GetTagProductListLogic) GetTagProductList(req *types.GetTagProductListR Size: req.Size, User: user, }) - //加入分类 - tagTem := types.TagItem{ - TagProductList: productListRsp, - TypeName: *tagInfo.Title, - TypeId: tagInfo.Id, - Level: *tagInfo.Level, - LevelPrefix: *tagInfo.LevelPrefix, - Icon: *tagInfo.Icon, - Sort: *tagInfo.Sort, - Description: *tagInfo.Description, - ChildTagList: make([]*types.TagItem, 0, 100), + tagTem.TagProductList = productListRsp + //获取推荐产品列表 + if tagInfo.RecommendProduct != nil && *tagInfo.RecommendProduct != "" { + } - //当前tag保存入map + //加入分类 mapTagLevel[*tagInfo.LevelPrefix] = &tagTem } //组装等级从属关系 diff --git a/server/product/internal/types/types.go b/server/product/internal/types/types.go index 926bc0e5..096a3626 100644 --- a/server/product/internal/types/types.go +++ b/server/product/internal/types/types.go @@ -245,8 +245,9 @@ type GetRecommandProductListRsp struct { } type GetTagProductListReq struct { - Cid int64 `form:"cid,optional"` //分类id - Size uint32 `form:"size,optional"` //尺寸 + Cid int64 `form:"cid,optional"` //分类id + Size uint32 `form:"size,optional"` //尺寸 + WithProduct bool `form:"with_product,optional"` //不携带产品只返回菜单 } type GetTagProductListRsp struct { @@ -255,15 +256,16 @@ type GetTagProductListRsp struct { } type TagItem struct { - TypeName string `json:"type_name"` - TypeId int64 `json:"type_id"` - Description string `json:"description"` - Level int64 `json:"level"` - LevelPrefix string `json:"level_prefix"` - Icon string `json:"icon"` - Sort int64 `json:"sort"` - TagProductList []TagProduct `json:"tag_product_list"` - ChildTagList []*TagItem `json:"child_tag_list"` + TypeName string `json:"type_name"` + TypeId int64 `json:"type_id"` + Description string `json:"description"` + Level int64 `json:"level"` + LevelPrefix string `json:"level_prefix"` + Icon string `json:"icon"` + Sort int64 `json:"sort"` + TagProductList []TagProduct `json:"tag_product_list"` //分类下的产品 + TagRecommendProductList [][]TagProduct `json:"tag_recommend_product_list"` //分类推荐产品 + ChildTagList []*TagItem `json:"child_tag_list"` } type TagProduct struct { diff --git a/server_api/product.api b/server_api/product.api index 96b2b5d7..12037107 100644 --- a/server_api/product.api +++ b/server_api/product.api @@ -291,23 +291,25 @@ type GetRecommandProductListRsp { } //获取分类产品列表 type GetTagProductListReq { - Cid int64 `form:"cid,optional"` //分类id - Size uint32 `form:"size,optional"` //尺寸 + Cid int64 `form:"cid,optional"` //分类id + Size uint32 `form:"size,optional"` //尺寸 + WithProduct bool `form:"with_product,optional"` //不携带产品只返回菜单 } type GetTagProductListRsp { TotalCategory int `json:"total_category"` TagList []TagItem `json:"tag_list"` } type TagItem { - TypeName string `json:"type_name"` - TypeId int64 `json:"type_id"` - Description string `json:"description"` - Level int64 `json:"level"` - LevelPrefix string `json:"level_prefix"` - Icon string `json:"icon"` - Sort int64 `json:"sort"` - TagProductList []TagProduct `json:"tag_product_list"` - ChildTagList []*TagItem `json:"child_tag_list"` + TypeName string `json:"type_name"` + TypeId int64 `json:"type_id"` + Description string `json:"description"` + Level int64 `json:"level"` + LevelPrefix string `json:"level_prefix"` + Icon string `json:"icon"` + Sort int64 `json:"sort"` + TagProductList []TagProduct `json:"tag_product_list"` //分类下的产品 + TagRecommendProductList [][]TagProduct `json:"tag_recommend_product_list"` //分类推荐产品 + ChildTagList []*TagItem `json:"child_tag_list"` } type TagProduct { ProductId int64 `json:"product_id"` From bd127a7884336eb45e2ae4d45d990d82436c240a Mon Sep 17 00:00:00 2001 From: laodaming <11058467+laudamine@user.noreply.gitee.com> Date: Wed, 19 Jul 2023 14:58:05 +0800 Subject: [PATCH 2/5] fix --- go.mod | 2 +- .../internal/logic/gettagproductlistlogic.go | 111 +++++++++++++++++- server/product/internal/types/types.go | 20 ++-- server_api/product.api | 20 ++-- 4 files changed, 129 insertions(+), 24 deletions(-) diff --git a/go.mod b/go.mod index 862c6ebd..fd58d3af 100644 --- a/go.mod +++ b/go.mod @@ -74,7 +74,7 @@ require ( go.opentelemetry.io/otel/trace v1.14.0 // indirect go.opentelemetry.io/proto/otlp v0.19.0 // indirect go.uber.org/automaxprocs v1.5.2 // indirect - golang.org/x/net v0.12.0 // indirect + golang.org/x/net v0.12.0 golang.org/x/sys v0.10.0 // indirect golang.org/x/text v0.11.0 google.golang.org/genproto v0.0.0-20230530153820-e85fd2cbaebc // indirect diff --git a/server/product/internal/logic/gettagproductlistlogic.go b/server/product/internal/logic/gettagproductlistlogic.go index 3cb7ba9f..22ad4b2d 100644 --- a/server/product/internal/logic/gettagproductlistlogic.go +++ b/server/product/internal/logic/gettagproductlistlogic.go @@ -177,6 +177,7 @@ func (l *GetTagProductListLogic) GetTagProductList(req *types.GetTagProductListR mapProductSizeCount[v.ProductId] = v.Num } } + //map tag菜单 mapTagLevel := make(map[string]*types.TagItem) minLevel := int64(0) //记录最小等级数字 for _, tagInfo := range tagList { @@ -214,10 +215,35 @@ func (l *GetTagProductListLogic) GetTagProductList(req *types.GetTagProductListR Size: req.Size, User: user, }) + //赋值 tagTem.TagProductList = productListRsp //获取推荐产品列表 if tagInfo.RecommendProduct != nil && *tagInfo.RecommendProduct != "" { - + //上面解析过,这里就无需判断错误 + recommendProductIds, _ := format.StrSlicToInt64Slice(strings.Split(*tagInfo.RecommendProduct, ",")) + //推荐产品的排序 + recommendProductIdsSort, err := format.StrSlicToInt64Slice(strings.Split(*tagInfo.RecommendProductSort, ",")) + if err != nil{ + logx.Error(err) + 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)) + } + recommendProductListRsp := l.getTagRecommendProducts(getTagRecommendProductsReq{ + TagInfo: tagInfo, + ProductList: productList, + MapProduct: mapProduct, + MapProductMinPrice: mapProductMinPrice, + MapProductTemplate: mapProductTemplate, + MapProductSizeCount: mapProductSizeCount, + RecommendProductIds: recommendProductIds, + RecommendProductIdsSort: recommendProductIdsSort, + Size: req.Size, + User: user, + }) + //赋值 + tagTem.TagRecommendProductList = recommendProductListRsp } //加入分类 mapTagLevel[*tagInfo.LevelPrefix] = &tagTem @@ -235,7 +261,7 @@ func (l *GetTagProductListLogic) GetTagProductList(req *types.GetTagProductListR if parent, ok := mapTagLevel[parentPrefix]; ok { parent.ChildTagList = append(parent.ChildTagList, tagItem) //排序 - sort.Slice(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 }) mapTagLevel[parentPrefix] = parent @@ -250,7 +276,7 @@ func (l *GetTagProductListLogic) GetTagProductList(req *types.GetTagProductListR tagListRsp = append(tagListRsp, *v) } //排序 - sort.Slice(tagListRsp, func(i, j int) bool { + sort.SliceStable(tagListRsp, func(i, j int) bool { return tagListRsp[i].Sort < tagListRsp[j].Sort }) return resp.SetStatusWithMessage(basic.CodeOK, "success", types.GetTagProductListRsp{ @@ -258,7 +284,86 @@ func (l *GetTagProductListLogic) GetTagProductList(req *types.GetTagProductListR TagList: tagListRsp, }) } +//排序推荐产品结构体 +type sortRecommendProduct struct { + ProductId int64 + Sort int64 +} +//获取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 + RecommendProductIdsSort []int64 + Size uint32 + User gmodel.FsUser +} +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{ + ProductId: pid, + 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{ + continue + } + productInfo := req.ProductList[productIndex] + minPrice, ok := req.MapProductMinPrice[productInfo.Id] + _, tmpOk := req.MapProductTemplate[productInfo.Id] + //无最小价格则不显示 || 没有模板也不显示 + if !ok || !tmpOk { + continue + } + sizeNum := int64(0) + if mapSizeNum, ok := req.MapProductSizeCount[productInfo.Id]; ok { + sizeNum = mapSizeNum + } + item := types.TagProduct{ + ProductId: productInfo.Id, + Sn: *productInfo.Sn, + Title: *productInfo.Title, + Intro: *productInfo.Intro, + IsEnv: *productInfo.IsProtection, + IsMicro: *productInfo.IsMicrowave, + SizeNum: uint32(sizeNum), + MiniPrice: minPrice, + } + //千人千面处理 + r := image.ThousandFaceImageFormatReq{ + Size: int(req.Size), + IsThousandFace: 0, + Cover: *productInfo.Cover, + CoverImg: *productInfo.CoverImg, + CoverDefault: *productInfo.CoverImg, + ProductId: productInfo.Id, + UserId: req.User.Id, + } + if req.User.Id != 0 { + r.IsThousandFace = int(*req.User.IsThousandFace) + } + image.ThousandFaceImageFormat(&r) + item.Cover = r.Cover + item.CoverImg = r.CoverImg + item.CoverDefault = r.CoverDefault + //加入切片 + productListRsp = append(productListRsp,item) + } + return productListRsp +} // 获取对应tag的产品列表 type getTagProductsReq struct { TagId int64 diff --git a/server/product/internal/types/types.go b/server/product/internal/types/types.go index 096a3626..2e1ede83 100644 --- a/server/product/internal/types/types.go +++ b/server/product/internal/types/types.go @@ -256,16 +256,16 @@ type GetTagProductListRsp struct { } type TagItem struct { - TypeName string `json:"type_name"` - TypeId int64 `json:"type_id"` - Description string `json:"description"` - Level int64 `json:"level"` - LevelPrefix string `json:"level_prefix"` - Icon string `json:"icon"` - Sort int64 `json:"sort"` - TagProductList []TagProduct `json:"tag_product_list"` //分类下的产品 - TagRecommendProductList [][]TagProduct `json:"tag_recommend_product_list"` //分类推荐产品 - ChildTagList []*TagItem `json:"child_tag_list"` + TypeName string `json:"type_name"` + TypeId int64 `json:"type_id"` + Description string `json:"description"` + Level int64 `json:"level"` + LevelPrefix string `json:"level_prefix"` + Icon string `json:"icon"` + Sort int64 `json:"sort"` + TagProductList []TagProduct `json:"tag_product_list"` //分类下的产品 + TagRecommendProductList []TagProduct `json:"tag_recommend_product_list"` //分类推荐产品 + ChildTagList []*TagItem `json:"child_tag_list"` } type TagProduct struct { diff --git a/server_api/product.api b/server_api/product.api index 12037107..7dad4ba3 100644 --- a/server_api/product.api +++ b/server_api/product.api @@ -300,16 +300,16 @@ type GetTagProductListRsp { TagList []TagItem `json:"tag_list"` } type TagItem { - TypeName string `json:"type_name"` - TypeId int64 `json:"type_id"` - Description string `json:"description"` - Level int64 `json:"level"` - LevelPrefix string `json:"level_prefix"` - Icon string `json:"icon"` - Sort int64 `json:"sort"` - TagProductList []TagProduct `json:"tag_product_list"` //分类下的产品 - TagRecommendProductList [][]TagProduct `json:"tag_recommend_product_list"` //分类推荐产品 - ChildTagList []*TagItem `json:"child_tag_list"` + TypeName string `json:"type_name"` + TypeId int64 `json:"type_id"` + Description string `json:"description"` + Level int64 `json:"level"` + LevelPrefix string `json:"level_prefix"` + Icon string `json:"icon"` + Sort int64 `json:"sort"` + TagProductList []TagProduct `json:"tag_product_list"` //分类下的产品 + TagRecommendProductList []TagProduct `json:"tag_recommend_product_list"` //分类推荐产品 + ChildTagList []*TagItem `json:"child_tag_list"` } type TagProduct { ProductId int64 `json:"product_id"` From db15103c9342d0586f186087bc45f8a4e49b611a Mon Sep 17 00:00:00 2001 From: laodaming <11058467+laudamine@user.noreply.gitee.com> Date: Wed, 19 Jul 2023 16:13:06 +0800 Subject: [PATCH 3/5] fix --- .../internal/logic/gettagproductlistlogic.go | 116 +++++++++--------- server/product/internal/types/types.go | 7 +- server_api/product.api | 7 +- 3 files changed, 69 insertions(+), 61 deletions(-) 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"` From 12bc736d49f0ca9bb2219f431cad44c62d5cf7f5 Mon Sep 17 00:00:00 2001 From: laodaming <11058467+laudamine@user.noreply.gitee.com> Date: Wed, 19 Jul 2023 16:14:07 +0800 Subject: [PATCH 4/5] fix --- server/product/internal/logic/gettagproductlistlogic.go | 1 + 1 file changed, 1 insertion(+) diff --git a/server/product/internal/logic/gettagproductlistlogic.go b/server/product/internal/logic/gettagproductlistlogic.go index 4d2e3bf0..a5adab88 100644 --- a/server/product/internal/logic/gettagproductlistlogic.go +++ b/server/product/internal/logic/gettagproductlistlogic.go @@ -145,6 +145,7 @@ func (l *GetTagProductListLogic) GetTagProductList(req *types.GetTagProductListR productList = append(productList, v) mapProduct[v.Id] = len(productList) - 1 } + //获取产品价格列表 productPriceList, err = l.svcCtx.AllModels.FsProductPrice.GetSimplePriceListByProductIds(l.ctx, productIds) if err != nil { logx.Error(err) From 9647fe060a23c9a4cb6a713ff1f7e8b626393325 Mon Sep 17 00:00:00 2001 From: laodaming <11058467+laudamine@user.noreply.gitee.com> Date: Wed, 19 Jul 2023 16:19:45 +0800 Subject: [PATCH 5/5] fix --- .../product/internal/logic/gettagproductlistlogic.go | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) diff --git a/server/product/internal/logic/gettagproductlistlogic.go b/server/product/internal/logic/gettagproductlistlogic.go index a5adab88..3043ec68 100644 --- a/server/product/internal/logic/gettagproductlistlogic.go +++ b/server/product/internal/logic/gettagproductlistlogic.go @@ -186,14 +186,7 @@ func (l *GetTagProductListLogic) GetTagProductList(req *types.GetTagProductListR } //map tag菜单 mapTagLevel := make(map[string]*types.TagItem) - minLevel := int64(0) //记录最小等级数字 for _, tagInfo := range tagList { - if minLevel == 0 && *tagInfo.Level > 0 { - minLevel = *tagInfo.Level - } - if minLevel > *tagInfo.Level { - minLevel = *tagInfo.Level - } tagTem := types.TagItem{ TagProductList: nil, TagRecommendProductList: nil, @@ -252,11 +245,9 @@ 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), + TagList: l.organizationLevelRelation(mapTagLevel),//组装等级从属关系 }) } //排序推荐产品结构体