2023-06-06 07:52:41 +00:00
|
|
|
package logic
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-06-06 09:18:01 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"fusenapi/constants"
|
|
|
|
"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"
|
|
|
|
"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"
|
|
|
|
"strings"
|
|
|
|
"time"
|
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-06 07:52:41 +00:00
|
|
|
func (l *GetSuccessRecommandLogic) GetSuccessRecommand(req *types.GetSuccessRecommandReq, loginInfo auth.UserInfo) (resp *types.Response) {
|
|
|
|
resp = &types.Response{}
|
2023-06-06 10:17:03 +00:00
|
|
|
loginInfo.UserId = 83
|
2023-06-06 09:18:01 +00:00
|
|
|
//校验前台登录情况
|
|
|
|
if loginInfo.UserId == 0 {
|
|
|
|
resp.Set(constants.CODE_UNAUTH, "please sign in")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
//获取用户信息
|
|
|
|
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)
|
|
|
|
resp.Set(constants.CODE_SERVICE_ERR, "failed to get user info")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if userInfo == nil {
|
|
|
|
resp.Set(constants.CODE_UNAUTH, "failed to get user info")
|
|
|
|
return
|
|
|
|
}
|
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)
|
|
|
|
resp.Set(constants.CODE_SERVICE_ERR, "failed to get product list")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
//没有推荐产品就返回
|
|
|
|
if len(productList) == 0 {
|
|
|
|
resp.Set(constants.CODE_OK, "success")
|
|
|
|
return
|
|
|
|
}
|
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{
|
|
|
|
Title: v.Title,
|
|
|
|
Cover: v.Cover,
|
|
|
|
CoverImg: v.CoverImg,
|
|
|
|
Sn: v.Sn,
|
|
|
|
Id: v.Id,
|
|
|
|
SkuId: 0, //???????
|
|
|
|
CoverDefault: v.CoverImg,
|
|
|
|
}
|
|
|
|
if req.Size > 0 {
|
|
|
|
coverImgSlice := strings.Split(v.CoverImg, ".")
|
|
|
|
coverSlice := strings.Split(v.Cover, ".")
|
|
|
|
if req.Size > 200 {
|
|
|
|
data.Cover = fmt.Sprintf("%s_%d.%s", coverSlice[0], req.Size, coverSlice[1])
|
|
|
|
data.CoverImg = fmt.Sprintf("%s_%d.%s", coverImgSlice[0], req.Size, coverImgSlice[1])
|
|
|
|
}
|
|
|
|
//千人千面
|
|
|
|
if userInfo.IsThousandFace == 1 {
|
|
|
|
data.Cover = ""
|
|
|
|
data.CoverImg = fmt.Sprintf("%s/test/%d/%d_%d.png?%d", constants.DOMAIN_RENDER_IMG_NAME, userInfo.Id, userInfo.Id, v.Id, time.Now().Unix())
|
|
|
|
if req.Size > 200 {
|
|
|
|
data.CoverDefault = fmt.Sprintf("%s_%d.%s", coverImgSlice[0], req.Size, coverImgSlice[1])
|
|
|
|
}
|
2023-06-06 09:18:01 +00:00
|
|
|
}
|
|
|
|
}
|
2023-06-06 10:17:03 +00:00
|
|
|
list = append(list, data)
|
2023-06-06 09:18:01 +00:00
|
|
|
}
|
2023-06-06 10:17:03 +00:00
|
|
|
resp.SetWithData(constants.CODE_OK, "success", list)
|
|
|
|
return
|
2023-06-06 07:52:41 +00:00
|
|
|
}
|