package logic import ( "context" "errors" "fusenapi/constants" "fusenapi/model" "fusenapi/product/internal/svc" "fusenapi/product/internal/types" "fusenapi/utils/auth" "fusenapi/utils/image" "github.com/zeromicro/go-zero/core/logx" "github.com/zeromicro/go-zero/core/stores/sqlx" ) 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, loginInfo auth.UserInfo) (resp *types.Response) { resp = &types.Response{} //校验前台登录情况 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 } if req.Num == 0 || req.Num > 500 { req.Num = 8 } if req.Size > 0 { req.Size = image.GetCurrentSize(req.Size) } //随机取8个产品 productModel := model.NewFsProductModel(l.svcCtx.MysqlConn) productList, err := productModel.GetRandomProductList(l.ctx, int(req.Num)) 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 } 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, //??????? } //千人千面处理 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, } image.ThousandFaceImageFormat(&thousandFaceImageFormatReq) data.Cover = thousandFaceImageFormatReq.Cover data.CoverImg = thousandFaceImageFormatReq.CoverImg data.CoverDefault = thousandFaceImageFormatReq.CoverDefault list = append(list, data) } resp.SetWithData(constants.CODE_OK, "success", list) return }