fusenapi/server/product/internal/logic/getsuccessrecommandlogic.go
2023-09-26 13:23:49 +08:00

89 lines
2.4 KiB
Go

package logic
import (
"context"
"errors"
"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"
"gorm.io/gorm"
)
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 sign in first")
}
//获取用户信息
userModel := gmodel.NewFsUserModel(l.svcCtx.MysqlConn)
user, err := userModel.FindUserById(l.ctx, userInfo.UserId)
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeServiceErr, "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, //???????
}
//千人千面处理
r := image.ThousandFaceImageFormatReq{
Size: int(req.Size),
IsThousandFace: 0,
Cover: *v.Cover,
CoverImg: *v.CoverImg,
CoverDefault: *v.CoverImg,
ProductId: v.Id,
UserId: user.Id,
}
if user.Id > 0 {
r.IsThousandFace = int(1)
}
image.ThousandFaceImageFormat(&r)
data.Cover = r.Cover
data.CoverImg = r.CoverImg
data.CoverDefault = r.CoverDefault
list = append(list, data)
}
return resp.SetStatusWithMessage(basic.CodeOK, "success", list)
}