169 lines
6.0 KiB
Go
169 lines
6.0 KiB
Go
package logic
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
"fusenapi/constants"
|
|
"fusenapi/model/gmodel"
|
|
"fusenapi/utils/auth"
|
|
"fusenapi/utils/basic"
|
|
"fusenapi/utils/format"
|
|
"fusenapi/utils/image"
|
|
"fusenapi/utils/s3url_to_s3id"
|
|
"gorm.io/gorm"
|
|
"strings"
|
|
|
|
"context"
|
|
|
|
"fusenapi/server/product/internal/svc"
|
|
"fusenapi/server/product/internal/types"
|
|
|
|
"github.com/zeromicro/go-zero/core/logx"
|
|
)
|
|
|
|
type GetRecommandProductListLogic struct {
|
|
logx.Logger
|
|
ctx context.Context
|
|
svcCtx *svc.ServiceContext
|
|
}
|
|
|
|
func NewGetRecommandProductListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetRecommandProductListLogic {
|
|
return &GetRecommandProductListLogic{
|
|
Logger: logx.WithContext(ctx),
|
|
ctx: ctx,
|
|
svcCtx: svcCtx,
|
|
}
|
|
}
|
|
|
|
func (l *GetRecommandProductListLogic) GetRecommandProductList(req *types.GetRecommandProductListReq, userinfo *auth.UserInfo) (resp *basic.Response) {
|
|
req.Num = 4 //写死4个
|
|
if req.Size > 0 {
|
|
req.Size = int32(image.GetCurrentSize(uint32(req.Size)))
|
|
}
|
|
productInfo, err := l.svcCtx.AllModels.FsProduct.FindOneBySn(l.ctx, req.Sn)
|
|
if err != nil {
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
return resp.SetStatusWithMessage(basic.CodeDbRecordNotFoundErr, "detail`s product is not found")
|
|
}
|
|
logx.Error(err)
|
|
return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get detail product info")
|
|
}
|
|
var (
|
|
recommendProductList []gmodel.FsProduct
|
|
)
|
|
if productInfo.RecommendProduct != nil && *productInfo.RecommendProduct != "" {
|
|
recommendProductIds, err := format.StrSlicToInt64Slice(strings.Split(*productInfo.RecommendProduct, ","))
|
|
if err != nil {
|
|
logx.Error(err)
|
|
return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to split recommend product ids")
|
|
}
|
|
recommendProductList, err = l.svcCtx.AllModels.FsProduct.GetProductListByIds(l.ctx, recommendProductIds, "")
|
|
if err != nil {
|
|
logx.Error(err)
|
|
return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get recommend product list")
|
|
}
|
|
}
|
|
//资源id集合
|
|
resourceIds := make([]string, 0, 50)
|
|
//需要填充时需要忽略的id
|
|
ignoreProductIds := make([]int64, 0, len(recommendProductList))
|
|
productIds := make([]int64, 0, len(recommendProductList))
|
|
//在合并之前记住推荐的产品
|
|
mapRecommend := make(map[int64]struct{})
|
|
for _, v := range recommendProductList {
|
|
ignoreProductIds = append(ignoreProductIds, v.Id)
|
|
productIds = append(productIds, v.Id)
|
|
mapRecommend[v.Id] = struct{}{}
|
|
resourceIds = append(resourceIds, s3url_to_s3id.GetS3ResourceIdFormUrl(*v.Cover))
|
|
resourceIds = append(resourceIds, s3url_to_s3id.GetS3ResourceIdFormUrl(*v.CoverImg))
|
|
}
|
|
//小于请求的数量则需要从产品表中随机填补上(不包含上面的产品)
|
|
lenRecommendProduct := len(recommendProductList)
|
|
if lenRecommendProduct < int(req.Num) {
|
|
appendNum := int(req.Num) - lenRecommendProduct
|
|
productList, err := l.svcCtx.AllModels.FsProduct.GetIgnoreRandomProductList(l.ctx, appendNum, ignoreProductIds)
|
|
if err != nil {
|
|
logx.Error(err)
|
|
return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get product list")
|
|
}
|
|
//合并列表
|
|
for _, v := range productList {
|
|
productIds = append(productIds, v.Id)
|
|
recommendProductList = append(recommendProductList, v)
|
|
}
|
|
}
|
|
//获取产品最低价
|
|
mapProductMinPrice := make(map[int64]int64)
|
|
modelList, err := l.svcCtx.AllModels.FsProductModel3d.GetAllByProductIdsTags(l.ctx, []int64{productInfo.Id}, []int{constants.TAG_MODEL, constants.TAG_PARTS}, "id,size_id,product_id,price,tag,part_id,step_price")
|
|
if err != nil {
|
|
logx.Error(err)
|
|
return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get model list")
|
|
}
|
|
if err = l.svcCtx.AllModels.FsProductModel3d.GetProductMinPrice(modelList, mapProductMinPrice); err != nil {
|
|
logx.Error(err)
|
|
return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get product min price")
|
|
}
|
|
//获取产品标签相关属性
|
|
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 {
|
|
resourceIds = append(resourceIds, s3url_to_s3id.GetS3ResourceIdFormUrl(*v.Cover))
|
|
}
|
|
//根据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(*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{
|
|
TemplateTag: v.TemplateTag,
|
|
Cover: *v.Cover,
|
|
CoverMetadata: mapResourceMetadata[*v.Cover],
|
|
})
|
|
}
|
|
list := make([]types.GetRecommandProductListRsp, 0, len(recommendProductList))
|
|
for _, v := range recommendProductList {
|
|
recommend := false
|
|
if _, ok := mapRecommend[v.Id]; ok {
|
|
recommend = true
|
|
}
|
|
minPrice := int64(0)
|
|
if minVal, ok := mapProductMinPrice[v.Id]; ok {
|
|
minPrice = minVal
|
|
}
|
|
item := types.GetRecommandProductListRsp{
|
|
Id: v.Id,
|
|
Sn: *v.Sn,
|
|
Title: *v.Title,
|
|
TitleCn: *v.TitleCn,
|
|
Cover: *v.Cover,
|
|
CoverMetadata: mapResourceMetadata[*v.Cover],
|
|
CoverImg: *v.CoverImg,
|
|
CoverImgMetadata: mapResourceMetadata[*v.CoverImg],
|
|
CoverDefault: []types.CoverDefaultItem{},
|
|
Intro: *v.Intro,
|
|
Recommend: recommend,
|
|
MinPrice: minPrice,
|
|
IsCustomization: *v.IsCustomization,
|
|
}
|
|
if _, ok := mapTagProp[productInfo.Id]; ok {
|
|
item.CoverDefault = mapTagProp[productInfo.Id]
|
|
}
|
|
list = append(list, item)
|
|
}
|
|
return resp.SetStatusWithMessage(basic.CodeOK, "success", list)
|
|
}
|