This commit is contained in:
laodaming
2023-07-20 14:56:28 +08:00
parent f2eec528ce
commit 9ff4468778
7 changed files with 126 additions and 26 deletions

View File

@@ -30,18 +30,54 @@ func NewGetRecommandProductListLogic(ctx context.Context, svcCtx *svc.ServiceCon
}
func (l *GetRecommandProductListLogic) GetRecommandProductList(req *types.GetRecommandProductListReq, userinfo *auth.UserInfo) (resp *basic.Response) {
req.Num = 8 //目前写死
req.Num = 4 //写死4个
if req.Size > 0 {
req.Size = image.GetCurrentSize(req.Size)
}
//随机取产品列表
productList, err := l.svcCtx.AllModels.FsProduct.GetRandomProductList(l.ctx, int(req.Num))
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")
}
//随机取产品列表(不包含详情产品)
recommendList, err := l.svcCtx.AllModels.FsProductRecommend.GetIgnoreRandomRecommendProductList(l.ctx, int(req.Num), []int64{productInfo.Id})
if err != nil {
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get random recommend product list")
return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get random recommend list")
}
if len(productList) == 0 {
return resp.SetStatusWithMessage(basic.CodeOK, "success")
//需要填充时需要忽略的id
ignoreProductIds := make([]int64, 0, len(recommendList)+1)
ignoreProductIds = append(ignoreProductIds, productInfo.Id)
recommendProductIds := make([]int64, 0, len(recommendList))
for _, v := range recommendList {
ignoreProductIds = append(ignoreProductIds, *v.ProductId)
recommendProductIds = append(recommendProductIds, *v.ProductId)
}
//获取推荐产品列表
recommendProductList, err := l.svcCtx.AllModels.FsProduct.GetProductListByIds(l.ctx, recommendProductIds, "")
if err != nil {
logx.Error(err)
return resp.SetStatus(basic.CodeDbSqlErr, "failed to get recommend product list")
}
//在合并之前记住推荐的产品
mapRecommend := make(map[int64]struct{})
for _, v := range recommendProductList {
mapRecommend[v.Id] = struct{}{}
}
//小于请求的数量则需要从产品表中随机填补上
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")
}
//合并列表
recommendProductList = append(recommendProductList, productList...)
}
//获取用户信息(不用判断存在)
user, err := l.svcCtx.AllModels.FsUser.FindUserById(l.ctx, userinfo.UserId)
@@ -49,8 +85,8 @@ func (l *GetRecommandProductListLogic) GetRecommandProductList(req *types.GetRec
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get user")
}
list := make([]types.GetRecommandProductListRsp, 0, len(productList))
for _, v := range productList {
list := make([]types.GetRecommandProductListRsp, 0, len(recommendProductList))
for _, v := range recommendProductList {
r := image.ThousandFaceImageFormatReq{
Size: int(req.Size),
IsThousandFace: 0,
@@ -63,7 +99,12 @@ func (l *GetRecommandProductListLogic) GetRecommandProductList(req *types.GetRec
if user.Id != 0 {
r.IsThousandFace = int(*user.IsThousandFace)
}
//千人前面处理
image.ThousandFaceImageFormat(&r)
isRecommend := int64(0)
if _, ok := mapRecommend[v.Id]; ok {
isRecommend = 1
}
list = append(list, types.GetRecommandProductListRsp{
Id: v.Id,
Sn: *v.Sn,
@@ -73,6 +114,7 @@ func (l *GetRecommandProductListLogic) GetRecommandProductList(req *types.GetRec
CoverImg: r.CoverImg,
CoverDefault: r.CoverDefault,
Intro: *v.Intro,
IsRecommend: isRecommend,
})
}
return resp.SetStatusWithMessage(basic.CodeOK, "success", list)