fusenapi/server/product/internal/logic/getsuccessrecommandlogic.go
laodaming 68c431bda2 fix
2023-06-16 14:52:45 +08:00

86 lines
2.6 KiB
Go

package logic
import (
"context"
"fusenapi/model/gmodel"
"fusenapi/server/product/internal/svc"
"fusenapi/server/product/internal/types"
"fusenapi/utils/auth"
"fusenapi/utils/basic"
"fusenapi/utils/image"
"github.com/zeromicro/go-zero/core/logx"
)
type GetSuccessRecommandLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewGetSuccessRecommandLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetSuccessRecommandLogic {
return &GetSuccessRecommandLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
// 获取推荐的产品列表
func (l *GetSuccessRecommandLogic) GetSuccessRecommand(req *types.GetSuccessRecommandReq, userInfo *auth.UserInfo) (resp *basic.Response) {
if userInfo.GetIdType() != auth.IDTYPE_User {
return resp.SetStatusWithMessage(basic.CodeUnAuth, "please login first")
}
//获取用户信息
userModel := gmodel.NewFsUserModel(l.svcCtx.MysqlConn)
user, err := userModel.FindUserById(l.ctx, userInfo.UserId)
if err != nil {
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to get user info")
}
if user.Id == 0 {
return resp.SetStatusWithMessage(basic.CodeUnAuth, "failed to get user info")
}
if req.Num == 0 || req.Num > 500 {
req.Num = 8
}
if req.Size > 0 {
req.Size = image.GetCurrentSize(req.Size)
}
//随机取8个产品
productModel := gmodel.NewFsProductModel(l.svcCtx.MysqlConn)
productList, err := productModel.GetRandomProductList(l.ctx, int(req.Num))
if err != nil {
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to get product list")
}
//没有推荐产品就返回
if len(productList) == 0 {
return resp.SetStatusWithMessage(basic.CodeOK, "success")
}
list := make([]types.GetSuccessRecommandRsp, 0, len(productList))
for _, v := range productList {
data := types.GetSuccessRecommandRsp{
Title: *v.Title,
Sn: *v.Sn,
Id: v.Id,
SkuId: 0, //???????
}
//千人千面处理
thousandFaceImageFormatReq := image.ThousandFaceImageFormatReq{
Size: int(req.Size),
IsThousandFace: int(*user.IsThousandFace),
Cover: *v.Cover,
CoverImg: *v.CoverImg,
CoverDefault: *v.CoverImg,
ProductId: v.Id,
UserInfo: user,
}
image.ThousandFaceImageFormat(&thousandFaceImageFormatReq)
data.Cover = thousandFaceImageFormatReq.Cover
data.CoverImg = thousandFaceImageFormatReq.CoverImg
data.CoverDefault = thousandFaceImageFormatReq.CoverDefault
list = append(list, data)
}
return resp.SetStatusWithMessage(basic.CodeOK, "success", list)
}