This commit is contained in:
laodaming 2023-07-19 17:34:07 +08:00
parent d4c77b2a13
commit 99bd3592af

View File

@ -87,14 +87,14 @@ func (l *GetTagProductListLogic) GetTagProductList(req *types.GetTagProductListR
mapProductTemplate = make(map[int64]struct{}) //产品模板map mapProductTemplate = make(map[int64]struct{}) //产品模板map
) )
//携带推荐产品 //携带推荐产品
if req.WithRecommendProduct{ if req.WithRecommendProduct {
//提取tag推荐产品 //提取tag推荐产品
allTagRecommendProductIds := make([]int64, 0, len(tagList)*5) allTagRecommendProductIds := make([]int64, 0, len(tagList)*5)
for _, v := range tagList { for _, v := range tagList {
if v.RecommendProduct == nil || *v.RecommendProduct == "" { if v.RecommendProduct == nil || *v.RecommendProduct == "" {
continue continue
} }
sl, err := format.StrSlicToInt64Slice(strings.Split(*v.RecommendProductSort, ",")) sl, err := format.StrSlicToInt64Slice(strings.Split(*v.RecommendProduct, ","))
if err != nil { if err != nil {
logx.Error(err) logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeServiceErr, fmt.Sprintf("failed to parse recommend product ids,id=%d", v.Id)) return resp.SetStatusWithMessage(basic.CodeServiceErr, fmt.Sprintf("failed to parse recommend product ids,id=%d", v.Id))
@ -117,23 +117,20 @@ func (l *GetTagProductListLogic) GetTagProductList(req *types.GetTagProductListR
//获取产品列表 //获取产品列表
productList, err = l.svcCtx.AllModels.FsProduct.GetProductListByParams(l.ctx, productList, err = l.svcCtx.AllModels.FsProduct.GetProductListByParams(l.ctx,
gmodel.GetProductListByParamsReq{ gmodel.GetProductListByParamsReq{
Type: typeIds, Type: typeIds,
IsDel: &pIsDel, IsDel: &pIsDel,
IsShelf: &pIsShelf, IsShelf: &pIsShelf,
Status: &pStatus, Status: &pStatus,
OrderBy: "`sort` DESC", OrderBy: "`sort` DESC",
}) })
if err != nil { if err != nil {
logx.Error(err) logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to get product list") return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to get product list")
} }
} }
//任意一个不为空 //任意一个不为空
if len(productList) != 0 || len(recommendProductList) != 0{ if len(productList) != 0 || len(recommendProductList) != 0 {
//提取产品ids
productIds := make([]int64, 0, len(productList))
for k, v := range productList { for k, v := range productList {
productIds = append(productIds, v.Id)
mapProduct[v.Id] = k mapProduct[v.Id] = k
} }
//合并产品列表 //合并产品列表
@ -145,6 +142,10 @@ func (l *GetTagProductListLogic) GetTagProductList(req *types.GetTagProductListR
productList = append(productList, v) productList = append(productList, v)
mapProduct[v.Id] = len(productList) - 1 mapProduct[v.Id] = len(productList) - 1
} }
productIds := make([]int64,0,len(productList))
for _,product := range productList{
productIds = append(productIds,product.Id)
}
//获取产品价格列表 //获取产品价格列表
productPriceList, err = l.svcCtx.AllModels.FsProductPrice.GetSimplePriceListByProductIds(l.ctx, productIds) productPriceList, err = l.svcCtx.AllModels.FsProductPrice.GetSimplePriceListByProductIds(l.ctx, productIds)
if err != nil { if err != nil {
@ -199,35 +200,38 @@ func (l *GetTagProductListLogic) GetTagProductList(req *types.GetTagProductListR
MapProduct: mapProduct, MapProduct: mapProduct,
Size: req.Size, Size: req.Size,
user: user, user: user,
});err != nil{ }); err != nil {
logx.Error(err) logx.Error(err)
return resp.SetStatusAddMessage(basic.CodeServiceErr,"failed to deal with tag data") return resp.SetStatusAddMessage(basic.CodeServiceErr, "failed to deal with tag data")
} }
return resp.SetStatusWithMessage(basic.CodeOK, "success", types.GetTagProductListRsp{ return resp.SetStatusWithMessage(basic.CodeOK, "success", types.GetTagProductListRsp{
TotalCategory: len(mapTagLevel), TotalCategory: len(mapTagLevel),
TagList: l.organizationLevelRelation(mapTagLevel),//组装等级从属关系 TagList: l.organizationLevelRelation(mapTagLevel), //组装等级从属关系
}) })
} }
//排序推荐产品结构体
// 排序推荐产品结构体
type sortRecommendProduct struct { type sortRecommendProduct struct {
ProductId int64 ProductId int64
Sort int64 Sort int64
} }
//处理tag菜单数据
// 处理tag菜单数据
type dealWithTagMenuDataReq struct { type dealWithTagMenuDataReq struct {
TagList []gmodel.FsTags TagList []gmodel.FsTags
WithProduct bool WithProduct bool
WithRecommendProduct bool WithRecommendProduct bool
ProductList []gmodel.FsProduct ProductList []gmodel.FsProduct
MapProductMinPrice map[int64]int64 MapProductMinPrice map[int64]int64
MapProductTemplate map[int64]struct{} MapProductTemplate map[int64]struct{}
MapProductSizeCount map[int64]int64 MapProductSizeCount map[int64]int64
MapTagLevel map[string]*types.TagItem MapTagLevel map[string]*types.TagItem
MapProduct map[int64]int MapProduct map[int64]int
Size uint32 Size uint32
user gmodel.FsUser user gmodel.FsUser
} }
func (l *GetTagProductListLogic)dealWithTagMenuData(req dealWithTagMenuDataReq)error{
func (l *GetTagProductListLogic) dealWithTagMenuData(req dealWithTagMenuDataReq) error {
for _, tagInfo := range req.TagList { for _, tagInfo := range req.TagList {
tagTem := types.TagItem{ tagTem := types.TagItem{
TagProductList: nil, TagProductList: nil,
@ -242,7 +246,7 @@ func (l *GetTagProductListLogic)dealWithTagMenuData(req dealWithTagMenuDataReq)e
ChildTagList: make([]*types.TagItem, 0, 50), ChildTagList: make([]*types.TagItem, 0, 50),
} }
//携带产品 //携带产品
if req.WithProduct { if req.WithProduct {
//获取分类产品列表 //获取分类产品列表
productListRsp := l.getTagProducts(getTagProductsReq{ productListRsp := l.getTagProducts(getTagProductsReq{
TagId: tagInfo.Id, TagId: tagInfo.Id,
@ -262,12 +266,12 @@ func (l *GetTagProductListLogic)dealWithTagMenuData(req dealWithTagMenuDataReq)e
recommendProductIds, _ := format.StrSlicToInt64Slice(strings.Split(*tagInfo.RecommendProduct, ",")) recommendProductIds, _ := format.StrSlicToInt64Slice(strings.Split(*tagInfo.RecommendProduct, ","))
//推荐产品的排序 //推荐产品的排序
recommendProductIdsSort, err := format.StrSlicToInt64Slice(strings.Split(*tagInfo.RecommendProductSort, ",")) recommendProductIdsSort, err := format.StrSlicToInt64Slice(strings.Split(*tagInfo.RecommendProductSort, ","))
if err != nil{ if err != nil {
logx.Error(err) logx.Error(err)
return nil return nil
} }
if len(recommendProductIds) != len(recommendProductIdsSort){ if len(recommendProductIds) != len(recommendProductIdsSort) {
return errors.New(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{ recommendProductListRsp := l.getTagRecommendProducts(getTagRecommendProductsReq{
TagInfo: tagInfo, TagInfo: tagInfo,
@ -289,8 +293,9 @@ func (l *GetTagProductListLogic)dealWithTagMenuData(req dealWithTagMenuDataReq)e
} }
return nil return nil
} }
//组织等级从属关系
func (l *GetTagProductListLogic)organizationLevelRelation(mapTagLevel map[string]*types.TagItem)[]types.TagItem{ // 组织等级从属关系
func (l *GetTagProductListLogic) organizationLevelRelation(mapTagLevel map[string]*types.TagItem) []types.TagItem {
mapTop := make(map[string]struct{}) mapTop := make(map[string]struct{})
for prefix, tagItem := range mapTagLevel { for prefix, tagItem := range mapTagLevel {
//最上级没有父级 //最上级没有父级
@ -302,7 +307,7 @@ func (l *GetTagProductListLogic)organizationLevelRelation(mapTagLevel map[string
//有父级 //有父级
parentPrefix := strings.Join(prefixSlice[:len(prefixSlice)-1], "/") parentPrefix := strings.Join(prefixSlice[:len(prefixSlice)-1], "/")
parent, ok := mapTagLevel[parentPrefix] parent, ok := mapTagLevel[parentPrefix]
if !ok{ if !ok {
continue continue
} }
parent.ChildTagList = append(parent.ChildTagList, tagItem) parent.ChildTagList = append(parent.ChildTagList, tagItem)
@ -315,7 +320,7 @@ func (l *GetTagProductListLogic)organizationLevelRelation(mapTagLevel map[string
//最终值提取最高级别那一层出来 //最终值提取最高级别那一层出来
rspList := make([]types.TagItem, 0, len(mapTagLevel)) rspList := make([]types.TagItem, 0, len(mapTagLevel))
for prefix, _ := range mapTop { for prefix, _ := range mapTop {
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 {
@ -323,36 +328,37 @@ func (l *GetTagProductListLogic)organizationLevelRelation(mapTagLevel map[string
}) })
return rspList return rspList
} }
//获取tag推荐产品列表
// 获取tag推荐产品列表
type getTagRecommendProductsReq struct { type getTagRecommendProductsReq struct {
TagInfo gmodel.FsTags TagInfo gmodel.FsTags
ProductList []gmodel.FsProduct ProductList []gmodel.FsProduct
MapProduct map[int64]int MapProduct map[int64]int
MapProductMinPrice map[int64]int64 MapProductMinPrice map[int64]int64
MapProductTemplate map[int64]struct{} MapProductTemplate map[int64]struct{}
MapProductSizeCount map[int64]int64 MapProductSizeCount map[int64]int64
RecommendProductIds []int64 RecommendProductIds []int64
RecommendProductIdsSort []int64 RecommendProductIdsSort []int64
Size uint32 Size uint32
User gmodel.FsUser User gmodel.FsUser
} }
func (l *GetTagProductListLogic)getTagRecommendProducts(req getTagRecommendProductsReq)(productListRsp []types.TagProduct){ func (l *GetTagProductListLogic) getTagRecommendProducts(req getTagRecommendProductsReq) (productListRsp []types.TagProduct) {
//排序 //排序
sortList := make([]sortRecommendProduct,0,len(req.RecommendProductIds)) sortList := make([]sortRecommendProduct, 0, len(req.RecommendProductIds))
for sortIndex,pid := range req.RecommendProductIds{ for sortIndex, pid := range req.RecommendProductIds {
sortList = append(sortList,sortRecommendProduct{ sortList = append(sortList, sortRecommendProduct{
ProductId: pid, ProductId: pid,
Sort: req.RecommendProductIdsSort[sortIndex], Sort: req.RecommendProductIdsSort[sortIndex],
}) })
} }
sort.SliceStable(sortList, func(i, j int) bool { sort.SliceStable(sortList, func(i, j int) bool {
return sortList[i].Sort < sortList[j].Sort return sortList[i].Sort < sortList[j].Sort
}) })
productListRsp = make([]types.TagProduct,0,len(sortList)) productListRsp = make([]types.TagProduct, 0, len(sortList))
for _,sortVal := range sortList{ for _, sortVal := range sortList {
productIndex,ok := req.MapProduct[sortVal.ProductId] productIndex, ok := req.MapProduct[sortVal.ProductId]
if !ok{ if !ok {
continue continue
} }
productInfo := req.ProductList[productIndex] productInfo := req.ProductList[productIndex]
@ -394,10 +400,11 @@ func (l *GetTagProductListLogic)getTagRecommendProducts(req getTagRecommendProdu
item.CoverImg = r.CoverImg item.CoverImg = r.CoverImg
item.CoverDefault = r.CoverDefault item.CoverDefault = r.CoverDefault
//加入切片 //加入切片
productListRsp = append(productListRsp,item) productListRsp = append(productListRsp, item)
} }
return productListRsp return productListRsp
} }
// 获取对应tag的产品列表 // 获取对应tag的产品列表
type getTagProductsReq struct { type getTagProductsReq struct {
TagId int64 TagId int64