fusenapi/server/product/internal/logic/getsuccessrecommandlogic.go

83 lines
2.4 KiB
Go
Raw Normal View History

2023-06-06 07:52:41 +00:00
package logic
import (
"context"
2023-06-12 08:47:48 +00:00
"fusenapi/model/gmodel"
2023-06-08 03:03:20 +00:00
"fusenapi/server/product/internal/svc"
"fusenapi/server/product/internal/types"
2023-06-06 09:18:01 +00:00
"fusenapi/utils/auth"
2023-06-07 04:21:07 +00:00
"fusenapi/utils/basic"
2023-06-06 09:18:01 +00:00
"fusenapi/utils/image"
2023-06-06 07:52:41 +00:00
"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,
}
}
2023-06-06 10:17:03 +00:00
// 获取推荐的产品列表
2023-06-13 11:11:30 +00:00
func (l *GetSuccessRecommandLogic) GetSuccessRecommand(req *types.GetSuccessRecommandReq, userInfo *auth.UserInfo) (resp *basic.Response) {
2023-06-06 09:18:01 +00:00
//获取用户信息
2023-06-12 08:47:48 +00:00
userModel := gmodel.NewFsUserModel(l.svcCtx.MysqlConn)
2023-06-13 11:11:30 +00:00
user, err := userModel.FindOne(l.ctx, userInfo.UserId)
2023-06-12 08:47:48 +00:00
if err != nil {
2023-06-06 09:18:01 +00:00
logx.Error(err)
2023-06-07 09:27:17 +00:00
return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to get user info")
2023-06-06 09:18:01 +00:00
}
2023-06-13 11:11:30 +00:00
if user.Id == 0 {
2023-06-07 09:27:17 +00:00
return resp.SetStatusWithMessage(basic.CodeUnAuth, "failed to get user info")
2023-06-06 09:18:01 +00:00
}
2023-06-06 10:17:03 +00:00
if req.Num == 0 || req.Num > 500 {
2023-06-06 09:18:01 +00:00
req.Num = 8
}
if req.Size > 0 {
req.Size = image.GetCurrentSize(req.Size)
}
2023-06-06 10:17:03 +00:00
//随机取8个产品
2023-06-12 08:47:48 +00:00
productModel := gmodel.NewFsProductModel(l.svcCtx.MysqlConn)
2023-06-06 10:17:03 +00:00
productList, err := productModel.GetRandomProductList(l.ctx, int(req.Num))
2023-06-06 09:18:01 +00:00
if err != nil {
logx.Error(err)
2023-06-07 09:27:17 +00:00
return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to get product list")
2023-06-06 09:18:01 +00:00
}
//没有推荐产品就返回
if len(productList) == 0 {
2023-06-07 09:27:17 +00:00
return resp.SetStatusWithMessage(basic.CodeOK, "success")
2023-06-06 09:18:01 +00:00
}
2023-06-06 10:17:03 +00:00
list := make([]types.GetSuccessRecommandRsp, 0, len(productList))
2023-06-06 09:18:01 +00:00
for _, v := range productList {
2023-06-06 10:17:03 +00:00
data := types.GetSuccessRecommandRsp{
2023-06-12 08:47:48 +00:00
Title: *v.Title,
Sn: *v.Sn,
2023-06-06 11:21:07 +00:00
Id: v.Id,
SkuId: 0, //???????
2023-06-06 10:17:03 +00:00
}
2023-06-06 11:21:07 +00:00
//千人千面处理
thousandFaceImageFormatReq := image.ThousandFaceImageFormatReq{
Size: int(req.Size),
2023-06-13 11:11:30 +00:00
IsThousandFace: int(user.IsThousandFace),
2023-06-12 08:47:48 +00:00
Cover: *v.Cover,
CoverImg: *v.CoverImg,
CoverDefault: *v.CoverImg,
2023-06-06 11:21:07 +00:00
ProductId: v.Id,
2023-06-13 11:11:30 +00:00
UserInfo: user,
2023-06-06 09:18:01 +00:00
}
2023-06-06 11:21:07 +00:00
image.ThousandFaceImageFormat(&thousandFaceImageFormatReq)
data.Cover = thousandFaceImageFormatReq.Cover
data.CoverImg = thousandFaceImageFormatReq.CoverImg
data.CoverDefault = thousandFaceImageFormatReq.CoverDefault
2023-06-06 10:17:03 +00:00
list = append(list, data)
2023-06-06 09:18:01 +00:00
}
2023-06-07 04:21:07 +00:00
return resp.SetStatusWithMessage(basic.CodeOK, "success", list)
2023-06-06 07:52:41 +00:00
}