From 4fbbbb27df335837faea500aae51f6dcd0937e50 Mon Sep 17 00:00:00 2001 From: laodaming <11058467+laudamine@user.noreply.gitee.com> Date: Tue, 5 Sep 2023 16:35:02 +0800 Subject: [PATCH] fix --- model/gmodel/fs_resource_logic.go | 12 +++++- .../homepagerecommendproductlistlogic.go | 40 ++++++++++++++++++- server/product/internal/types/types.go | 6 ++- server_api/product.api | 6 ++- 4 files changed, 57 insertions(+), 7 deletions(-) diff --git a/model/gmodel/fs_resource_logic.go b/model/gmodel/fs_resource_logic.go index 31212da2..5b4736b6 100644 --- a/model/gmodel/fs_resource_logic.go +++ b/model/gmodel/fs_resource_logic.go @@ -104,7 +104,17 @@ func (m *FsResourceModel) Trans(ctx context.Context, fn func(ctx context.Context // return err } - +func (m *FsResourceModel) FindAllByResourceIds(ctx context.Context, resourceIds []string, fields ...string) (resp []FsResource, err error) { + if len(resourceIds) == 0 { + return + } + db := m.db.WithContext(ctx).Model(&FsResource{}).Where("resource_id in (?)", resourceIds) + if len(fields) != 0 { + db = db.Select(fields[0]) + } + err = db.Find(&resp).Error + return resp, err +} func (m *FsResourceModel) TableName() string { return m.name } diff --git a/server/product/internal/logic/homepagerecommendproductlistlogic.go b/server/product/internal/logic/homepagerecommendproductlistlogic.go index 2b5f27fa..aef9ef24 100644 --- a/server/product/internal/logic/homepagerecommendproductlistlogic.go +++ b/server/product/internal/logic/homepagerecommendproductlistlogic.go @@ -1,6 +1,7 @@ package logic import ( + "encoding/json" "errors" "fusenapi/model/gmodel" "fusenapi/utils/auth" @@ -80,6 +81,8 @@ func (l *HomePageRecommendProductListLogic) HomePageRecommendProductList(req *ty logx.Error(err) return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to parse recommend product") } + //资源id集合 + resourceIds := make([]string, 0, 100) //获取列表推荐产品 recommendProductList, err = l.svcCtx.AllModels.FsProduct.GetProductListByIds(l.ctx, recommendProductIds, "sort-desc") if err != nil { @@ -92,6 +95,16 @@ func (l *HomePageRecommendProductListLogic) HomePageRecommendProductList(req *ty productIds := make([]int64, 0, len(recommendProductList)) for _, product := range recommendProductList { productIds = append(productIds, product.Id) + coverSlice := strings.Split(*product.Cover, "/") + coverImgSlice := strings.Split(*product.CoverImg, "/") + lenCoverSlice := len(coverSlice) + lenCoverImgSlice := len(coverImgSlice) + if lenCoverSlice > 1 { + resourceIds = append(resourceIds, coverSlice[lenCoverSlice-1]) + } + if lenCoverImgSlice > 1 { + resourceIds = append(resourceIds, coverImgSlice[lenCoverImgSlice-1]) + } } //获取商品可选配件 productOptionalPartList, err = l.svcCtx.AllModels.FsProductModel3d.GetGroupPartListByProductIds(l.ctx, productIds) @@ -158,11 +171,33 @@ func (l *HomePageRecommendProductListLogic) HomePageRecommendProductList(req *ty logx.Error(err) return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get product tag property") } + for _, v := range productTagPropList { + coverSlice := strings.Split(*v.Cover, "/") + lenCoverSlice := len(coverSlice) + if lenCoverSlice > 1 { + resourceIds = append(resourceIds, coverSlice[lenCoverSlice-1]) + } + } + //根据resourceUrls找到对应的元数据 + resourceMetadataList, err := l.svcCtx.AllModels.FsResource.FindAllByResourceIds(l.ctx, resourceIds) + if err != nil { + logx.Error(err) + return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get resource list") + } + mapResourceMetadata := make(map[string]map[string]interface{}) + for _, v := range resourceMetadataList { + var metadata map[string]interface{} + if v.Metadata != nil { + _ = json.Unmarshal([]byte(*v.Metadata), &metadata) + } + mapResourceMetadata[*v.ResourceUrl] = metadata + } mapTagProp := make(map[int64][]types.CoverDefaultItem) for _, v := range productTagPropList { mapTagProp[*v.ProductId] = append(mapTagProp[*v.ProductId], types.CoverDefaultItem{ - Tag: v.TemplateTag, - Cover: *v.Cover, + Tag: v.TemplateTag, + Cover: *v.Cover, + CoverMetadata: mapResourceMetadata[*v.Cover], }) } //组装返回 @@ -188,6 +223,7 @@ func (l *HomePageRecommendProductListLogic) HomePageRecommendProductList(req *ty Sn: *productInfo.Sn, Title: *productInfo.Title, Cover: *productInfo.Cover, + CoverMetadata: mapResourceMetadata[*productInfo.Cover], CoverDefault: []types.CoverDefaultItem{}, SizeNum: uint32(sizeNum), MinPrice: minPrice, diff --git a/server/product/internal/types/types.go b/server/product/internal/types/types.go index bc9c1135..b8151045 100644 --- a/server/product/internal/types/types.go +++ b/server/product/internal/types/types.go @@ -282,8 +282,9 @@ type TagProduct struct { } type CoverDefaultItem struct { - Tag string `json:"tag"` - Cover string `json:"cover"` + Tag string `json:"tag"` + Cover string `json:"cover"` + CoverMetadata interface{} `json:"cover_metadata"` } type GetRenderDesignReq struct { @@ -401,6 +402,7 @@ type HomePageRecommendProductListRsp struct { Sn string `json:"sn"` Title string `json:"title"` Cover string `json:"cover"` + CoverMetadata interface{} `json:"cover_metadata"` SizeNum uint32 `json:"size_num"` MinPrice int64 `json:"min_price"` CoverDefault []CoverDefaultItem `json:"cover_default"` diff --git a/server_api/product.api b/server_api/product.api index b479c9ef..70661782 100644 --- a/server_api/product.api +++ b/server_api/product.api @@ -331,8 +331,9 @@ type TagProduct { Recommended bool `json:"recommended"` } type CoverDefaultItem { - Tag string `json:"tag"` - Cover string `json:"cover"` + Tag string `json:"tag"` + Cover string `json:"cover"` + CoverMetadata interface{} `json:"cover_metadata"` } //获取云渲染设计方案信息 type GetRenderDesignReq { @@ -443,6 +444,7 @@ type HomePageRecommendProductListRsp { Sn string `json:"sn"` Title string `json:"title"` Cover string `json:"cover"` + CoverMetadata interface{} `json:"cover_metadata"` SizeNum uint32 `json:"size_num"` MinPrice int64 `json:"min_price"` CoverDefault []CoverDefaultItem `json:"cover_default"`