fusenapi/server/product/internal/logic/getrecommandproductlistlogic.go
laodaming 98c5727c7e fix
2023-07-11 17:08:19 +08:00

80 lines
2.2 KiB
Go

package logic
import (
"errors"
"fusenapi/utils/auth"
"fusenapi/utils/basic"
"fusenapi/utils/image"
"gorm.io/gorm"
"context"
"fusenapi/server/product/internal/svc"
"fusenapi/server/product/internal/types"
"github.com/zeromicro/go-zero/core/logx"
)
type GetRecommandProductListLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewGetRecommandProductListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetRecommandProductListLogic {
return &GetRecommandProductListLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *GetRecommandProductListLogic) GetRecommandProductList(req *types.GetRecommandProductListReq, userinfo *auth.UserInfo) (resp *basic.Response) {
req.Num = 8 //目前写死
if req.Size > 0 {
req.Size = image.GetCurrentSize(req.Size)
}
//随机取产品列表
productList, err := l.svcCtx.AllModels.FsProduct.GetRandomProductList(l.ctx, int(req.Num))
if err != nil {
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get random recommend product list")
}
if len(productList) == 0 {
return resp.SetStatusWithMessage(basic.CodeOK, "success")
}
//获取用户信息(不用判断存在)
user, err := l.svcCtx.AllModels.FsUser.FindUserById(l.ctx, userinfo.UserId)
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get user")
}
list := make([]types.GetRecommandProductListRsp, 0, len(productList))
for _, v := range productList {
r := image.ThousandFaceImageFormatReq{
Size: int(req.Size),
IsThousandFace: 0,
Cover: *v.Cover,
CoverImg: *v.CoverImg,
CoverDefault: *v.Cover,
ProductId: v.Id,
UserId: userinfo.UserId,
}
if user.Id != 0 {
r.IsThousandFace = int(*user.IsThousandFace)
}
image.ThousandFaceImageFormat(&r)
list = append(list, types.GetRecommandProductListRsp{
Id: v.Id,
Sn: *v.Sn,
Title: *v.Title,
TitleCn: *v.TitleCn,
Cover: r.Cover,
CoverImg: r.CoverImg,
CoverDefault: r.CoverDefault,
Intro: *v.Intro,
})
}
return resp.SetStatusWithMessage(basic.CodeOK, "success", list)
}