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

89 lines
2.4 KiB
Go
Raw Normal View History

2023-06-06 07:52:41 +00:00
package logic
import (
"context"
2023-07-11 09:08:19 +00:00
"errors"
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-09-26 05:23:49 +00:00
2023-06-06 07:52:41 +00:00
"github.com/zeromicro/go-zero/core/logx"
2023-07-11 09:08:19 +00:00
"gorm.io/gorm"
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-13 11:11:30 +00:00
func (l *GetSuccessRecommandLogic) GetSuccessRecommand(req *types.GetSuccessRecommandReq, userInfo *auth.UserInfo) (resp *basic.Response) {
2023-06-15 04:00:32 +00:00
if userInfo.GetIdType() != auth.IDTYPE_User {
2023-09-15 02:39:08 +00:00
return resp.SetStatusWithMessage(basic.CodeUnAuth, "please sign in first")
2023-06-15 04:00:32 +00:00
}
2023-06-06 09:18:01 +00:00
//获取用户信息
2023-06-12 08:47:48 +00:00
userModel := gmodel.NewFsUserModel(l.svcCtx.MysqlConn)
2023-06-16 06:52:45 +00:00
user, err := userModel.FindUserById(l.ctx, userInfo.UserId)
2023-07-11 09:08:19 +00:00
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
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-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
//千人千面处理
2023-07-11 09:08:19 +00:00
r := image.ThousandFaceImageFormatReq{
2023-06-06 11:21:07 +00:00
Size: int(req.Size),
2023-07-11 09:08:19 +00:00
IsThousandFace: 0,
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-07-11 09:08:19 +00:00
UserId: user.Id,
}
if user.Id > 0 {
2023-09-26 05:23:49 +00:00
r.IsThousandFace = int(1)
2023-06-06 09:18:01 +00:00
}
2023-07-11 09:08:19 +00:00
image.ThousandFaceImageFormat(&r)
data.Cover = r.Cover
data.CoverImg = r.CoverImg
data.CoverDefault = r.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
}