fusenapi/product/internal/logic/getsuccessrecommandlogic.go

90 lines
2.6 KiB
Go
Raw Normal View History

2023-06-06 07:52:41 +00:00
package logic
import (
"context"
2023-06-06 09:18:01 +00:00
"errors"
"fusenapi/model"
2023-06-06 07:52:41 +00:00
"fusenapi/product/internal/svc"
"fusenapi/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"
2023-06-06 10:17:03 +00:00
"github.com/zeromicro/go-zero/core/stores/sqlx"
2023-06-06 07:52:41 +00:00
)
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-07 03:35:04 +00:00
func (l *GetSuccessRecommandLogic) GetSuccessRecommand(req *types.GetSuccessRecommandReq) (resp *types.Response) {
2023-06-06 07:52:41 +00:00
resp = &types.Response{}
2023-06-07 03:35:04 +00:00
loginInfo := auth.GetUserInfoFormCtx(l.ctx)
2023-06-06 09:18:01 +00:00
if loginInfo.UserId == 0 {
2023-06-07 04:21:07 +00:00
return resp.SetStatus(basic.CodeUnAuth, "get login user info err")
2023-06-06 09:18:01 +00:00
}
//获取用户信息
userModel := model.NewFsUserModel(l.svcCtx.MysqlConn)
userInfo, err := userModel.FindOne(l.ctx, loginInfo.UserId)
if err != nil && errors.Is(err, sqlx.ErrNotFound) {
logx.Error(err)
2023-06-07 04:21:07 +00:00
return resp.SetStatus(basic.CodeServiceErr, "failed to get user info")
2023-06-06 09:18:01 +00:00
}
if userInfo == nil {
2023-06-07 04:21:07 +00:00
return resp.SetStatus(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-06 09:18:01 +00:00
productModel := model.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 04:21:07 +00:00
return resp.SetStatus(basic.CodeServiceErr, "failed to get product list")
2023-06-06 09:18:01 +00:00
}
//没有推荐产品就返回
if len(productList) == 0 {
2023-06-07 04:21:07 +00:00
return resp.SetStatus(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-06 11:21:07 +00:00
Title: v.Title,
Sn: v.Sn,
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),
IsThousandFace: int(userInfo.IsThousandFace),
Cover: v.Cover,
CoverImg: v.CoverImg,
CoverDefault: v.CoverImg,
ProductId: v.Id,
UserInfo: *userInfo,
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
}