This commit is contained in:
laodaming 2023-08-30 16:42:49 +08:00
parent b2c68a9dc1
commit 2e790cd255

View File

@ -85,117 +85,26 @@ func (l *GetTagProductListLogic) GetTagProductList(req *types.GetTagProductListR
var (
productList []gmodel.FsProduct //产品列表select 字段需要看查询的地方)
mapTagProp = make(map[int64][]types.CoverDefaultItem)
productOptionalPartList []gmodel.GetGroupPartListByProductIdsRsp //产品配件列表
mapProductHaveOptionFitting = make(map[int64]struct{})
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]int64) //产品模板map
)
//携带产品
if req.WithProduct {
//查询符合的产品列表
pIsDel := int64(0)
pStatus := int64(1)
pIsShelf := int64(1)
//获取产品列表
productList, err = l.svcCtx.AllModels.FsProduct.GetProductListByParams(l.ctx,
gmodel.GetProductListByParamsReq{
Type: typeIds,
IsDel: &pIsDel,
IsShelf: &pIsShelf,
Status: &pStatus,
OrderBy: "`is_recommend` DESC,`sort` ASC",
productList, err = l.getProductRelationInfo(getProductRelationInfoReq{
Ctx: l.ctx,
TemplateTag: req.TemplateTag,
TypeIds: typeIds,
MapTagProp: mapTagProp,
MapProductHaveOptionFitting: mapProductHaveOptionFitting,
MapProductMinPrice: mapProductMinPrice,
MapProductSizeCount: mapProductSizeCount,
MapProductTemplate: mapProductTemplate,
})
if err != nil {
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to get product list")
}
productIds := make([]int64, 0, len(productList))
for _, product := range productList {
productIds = append(productIds, product.Id)
}
//获取商品可选配件
productOptionalPartList, err = l.svcCtx.AllModels.FsProductModel3d.GetGroupPartListByProductIds(l.ctx, productIds)
if err != nil {
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get product part list")
}
//存储有配件的map
for _, partList := range productOptionalPartList {
partList.PartList = strings.Trim(partList.PartList, " ")
partList.PartList = strings.Trim(partList.PartList, ",")
if partList.PartList == "" {
continue
}
mapProductHaveOptionFitting[partList.ProductId] = struct{}{}
}
//获取产品标签相关属性
productTagPropList, err := l.svcCtx.AllModels.FsProductTagProp.GetTagPropByProductIdsWithProductTag(l.ctx, productIds)
if err != nil {
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get product tag property")
}
for _, v := range productTagPropList {
mapTagProp[*v.ProductId] = append(mapTagProp[*v.ProductId], types.CoverDefaultItem{
Tag: v.TemplateTag,
Cover: *v.Cover,
})
}
//获取产品价格列表
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)
if min, ok := mapProductMinPrice[v.ProductId]; ok {
if min > int64(priceSlice[0]) {
mapProductMinPrice[v.ProductId] = int64(priceSlice[0])
}
} else {
mapProductMinPrice[v.ProductId] = int64(priceSlice[0])
}
}
//获取模板(只是获取产品product_id,id)
if req.TemplateTag != "" { //指定模板tag
productTemplatesV2, err = l.svcCtx.AllModels.FsProductTemplateV2.FindAllByProductIdsTemplateTag(l.ctx, productIds, req.TemplateTag, "sort ASC", "product_id,id")
} else { //没指定模板tag
productTemplatesV2, err = l.svcCtx.AllModels.FsProductTemplateV2.FindAllByProductIds(l.ctx, productIds, "sort ASC", "product_id,id")
}
if err != nil {
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to get product templates")
}
//只存第一个
for _, v := range productTemplatesV2 {
if _, ok := mapProductTemplate[*v.ProductId]; ok {
continue
}
mapProductTemplate[*v.ProductId] = v.Id
}
//获取产品尺寸数量
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
return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to get product series info")
}
}
//map tag菜单
@ -227,6 +136,128 @@ func (l *GetTagProductListLogic) GetTagProductList(req *types.GetTagProductListR
})
}
// 如果携带产品,就查询产品相关信息
type getProductRelationInfoReq struct {
Ctx context.Context
TemplateTag string
TypeIds []int64
MapTagProp map[int64][]types.CoverDefaultItem
MapProductHaveOptionFitting map[int64]struct{}
MapProductMinPrice map[int64]int64
MapProductSizeCount map[int64]int64
MapProductTemplate map[int64]int64
}
func (l *GetTagProductListLogic) getProductRelationInfo(req getProductRelationInfoReq) (productList []gmodel.FsProduct, err error) {
var (
productTemplatesV2List []gmodel.FsProductTemplateV2
productSizeCountList []gmodel.CountProductSizeByStatusRsp
productOptionalPartList []gmodel.GetGroupPartListByProductIdsRsp
)
//查询符合的产品列表
pIsDel := int64(0)
pStatus := int64(1)
pIsShelf := int64(1)
//获取产品列表
productList, err = l.svcCtx.AllModels.FsProduct.GetProductListByParams(l.ctx,
gmodel.GetProductListByParamsReq{
Type: req.TypeIds,
IsDel: &pIsDel,
IsShelf: &pIsShelf,
Status: &pStatus,
OrderBy: "`is_recommend` DESC,`sort` ASC",
})
if err != nil {
logx.Error(err)
return nil, errors.New("failed to get product list")
}
productIds := make([]int64, 0, len(productList))
for _, product := range productList {
productIds = append(productIds, product.Id)
}
//获取商品可选配件
productOptionalPartList, err = l.svcCtx.AllModels.FsProductModel3d.GetGroupPartListByProductIds(l.ctx, productIds)
if err != nil {
logx.Error(err)
return nil, errors.New("failed to get product part list")
}
//存储有配件的map
for _, partList := range productOptionalPartList {
partList.PartList = strings.Trim(partList.PartList, " ")
partList.PartList = strings.Trim(partList.PartList, ",")
if partList.PartList == "" {
continue
}
req.MapProductHaveOptionFitting[partList.ProductId] = struct{}{}
}
//获取产品标签相关属性
productTagPropList, err := l.svcCtx.AllModels.FsProductTagProp.GetTagPropByProductIdsWithProductTag(l.ctx, productIds)
if err != nil {
logx.Error(err)
return nil, errors.New("failed to get product tag property")
}
for _, v := range productTagPropList {
req.MapTagProp[*v.ProductId] = append(req.MapTagProp[*v.ProductId], types.CoverDefaultItem{
Tag: v.TemplateTag,
Cover: *v.Cover,
})
}
//获取产品价格列表
productPriceList, err := l.svcCtx.AllModels.FsProductPrice.GetSimplePriceListByProductIds(l.ctx, productIds)
if err != nil {
logx.Error(err)
return nil, errors.New("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 nil, errors.New("parse price err")
}
if len(priceSlice) == 0 {
continue
}
//正序排序价格(注意排序后的阶梯价格不能用作阶梯数量价格计算)
sort.Ints(priceSlice)
if min, ok := req.MapProductMinPrice[v.ProductId]; ok {
if min > int64(priceSlice[0]) {
req.MapProductMinPrice[v.ProductId] = int64(priceSlice[0])
}
} else {
req.MapProductMinPrice[v.ProductId] = int64(priceSlice[0])
}
}
//获取模板(只是获取产品product_id,id)
if req.TemplateTag != "" { //指定模板tag
productTemplatesV2List, err = l.svcCtx.AllModels.FsProductTemplateV2.FindAllByProductIdsTemplateTag(l.ctx, productIds, req.TemplateTag, "sort ASC", "product_id,id")
} else { //没指定模板tag
productTemplatesV2List, err = l.svcCtx.AllModels.FsProductTemplateV2.FindAllByProductIds(l.ctx, productIds, "sort ASC", "product_id,id")
}
if err != nil {
logx.Error(err)
return nil, errors.New("failed to get product templates")
}
//只存第一个
for _, v := range productTemplatesV2List {
if _, ok := req.MapProductTemplate[*v.ProductId]; ok {
continue
}
req.MapProductTemplate[*v.ProductId] = v.Id
}
//获取产品尺寸数量
productSizeCountList, err = l.svcCtx.AllModels.FsProductSize.GetGroupProductSizeByStatus(l.ctx, productIds, 1)
if err != nil {
logx.Error(err)
return nil, errors.New("get product size count err")
}
for _, v := range productSizeCountList {
req.MapProductSizeCount[v.ProductId] = v.Num
}
return productList, nil
}
// 处理tag菜单数据
type dealWithTagMenuDataReq struct {
TagList []gmodel.FsTags