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 = 4 //写死4个 if req.Size > 0 { req.Size = image.GetCurrentSize(req.Size) } productInfo, err := l.svcCtx.AllModels.FsProduct.FindOneBySn(l.ctx, req.Sn) if err != nil { if errors.Is(err, gorm.ErrRecordNotFound) { return resp.SetStatusWithMessage(basic.CodeDbRecordNotFoundErr, "detail`s product is not found") } logx.Error(err) return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get detail product info") } //随机取产品列表(不包含详情产品) recommendList, err := l.svcCtx.AllModels.FsProductRecommend.GetIgnoreRandomRecommendProductList(l.ctx, int(req.Num), []int64{productInfo.Id}) if err != nil { logx.Error(err) return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get random recommend list") } //需要填充时需要忽略的id ignoreProductIds := make([]int64, 0, len(recommendList)+1) ignoreProductIds = append(ignoreProductIds, productInfo.Id) recommendProductIds := make([]int64, 0, len(recommendList)) for _, v := range recommendList { ignoreProductIds = append(ignoreProductIds, *v.ProductId) recommendProductIds = append(recommendProductIds, *v.ProductId) } //获取推荐产品列表 recommendProductList, err := l.svcCtx.AllModels.FsProduct.GetProductListByIds(l.ctx, recommendProductIds, "") if err != nil { logx.Error(err) return resp.SetStatus(basic.CodeDbSqlErr, "failed to get recommend product list") } //在合并之前记住推荐的产品 mapRecommend := make(map[int64]struct{}) for _, v := range recommendProductList { mapRecommend[v.Id] = struct{}{} } //小于请求的数量则需要从产品表中随机填补上 lenRecommendProduct := len(recommendProductList) if lenRecommendProduct < int(req.Num) { appendNum := int(req.Num) - lenRecommendProduct productList, err := l.svcCtx.AllModels.FsProduct.GetIgnoreRandomProductList(l.ctx, appendNum, ignoreProductIds) if err != nil { logx.Error(err) return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get product list") } //合并列表 recommendProductList = append(recommendProductList, productList...) } //获取用户信息(不用判断存在) 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(recommendProductList)) for _, v := range recommendProductList { 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) isRecommend := int64(0) if _, ok := mapRecommend[v.Id]; ok { isRecommend = 1 } 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, IsRecommend: isRecommend, }) } return resp.SetStatusWithMessage(basic.CodeOK, "success", list) }