package playletser import ( "context" "videoweb/app/api/proto" "videoweb/pub/common/srvdef" "videoweb/pub/models/playletlikemod" "videoweb/pub/models/playletmod" "go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/mongo/options" ) type playletLikePage struct { Total []struct { Count int64 `bson:"count"` } `bson:"total"` List playletlikemod.PlayletLikeList `bson:"list"` } // LikeList 按点赞时间倒序分页查询当前用户仍上架的短剧。 func LikeList(ctx context.Context, userId int64, req *proto.PlayletLikeListReq) (*proto.PlayletLikeListResp, *srvdef.SrvErr) { if userId <= 0 { return nil, srvdef.New(srvdef.InvalidToken, "请先登录", "请先登录") } if req == nil { return nil, srvdef.New(srvdef.ErrParamError, "", "") } pageNum, pageSize := normalizePlayletPage(req.PageNum, req.PageSize) resp := &proto.PlayletLikeListResp{ PageNum: int(pageNum), PageSize: int(pageSize), List: make([]proto.PlayletLikeData, 0), } // 在分页之前过滤下架或已删除的短剧,避免总数和当前页不一致。 pipeline := []bson.M{ {"$match": bson.M{playletlikemod.FN.PlayletLike.UserId: userId}}, {"$sort": bson.D{ {Key: playletlikemod.FN.PlayletLike.CreatedAt, Value: -1}, {Key: playletlikemod.FN.PlayletLike.Id, Value: -1}, }}, {"$lookup": bson.M{ "from": playletmod.TN.Playlet, "localField": playletlikemod.FN.PlayletLike.PlayletId, "foreignField": playletmod.FN.Playlet.Id, "as": "playlet", }}, {"$unwind": "$playlet"}, {"$match": bson.M{"playlet." + playletmod.FN.Playlet.Status: playletmod.StatusUp}}, {"$facet": bson.M{ "total": bson.A{bson.M{"$count": "count"}}, "list": bson.A{ bson.M{"$skip": (pageNum - 1) * pageSize}, bson.M{"$limit": pageSize}, bson.M{"$project": bson.M{ playletlikemod.FN.PlayletLike.Id: 1, playletlikemod.FN.PlayletLike.PlayletId: 1, playletlikemod.FN.PlayletLike.CreatedAt: 1, }}, }, }}, } var pages []playletLikePage if err := playletlikemod.AggregatePlayletLike(ctx, &pages, pipeline); err != nil { return nil, err } if len(pages) == 0 { return resp, nil } page := pages[0] if len(page.Total) > 0 { resp.TotalCount = page.Total[0].Count } if len(page.List) == 0 { return resp, nil } ids := make([]int64, 0, len(page.List)) for _, like := range page.List { if like != nil { ids = append(ids, like.PlayletId) } } playlets, err := playletmod.FindManyPlaylet(ctx, homeUpPlayletFilter(bson.M{ playletmod.FN.Playlet.Id: bson.M{"$in": ids}, }), options.Find().SetProjection(playletHomeProjection)) if err != nil { return nil, err } themeNames, err := buildHomeThemeNameMap(ctx, playlets) if err != nil { return nil, err } tagNames, err := buildHomeTagNameMap(ctx, playlets) if err != nil { return nil, err } dataByID := make(map[int64]*proto.CommonData, len(playlets)) for _, data := range playletsToHomeCommonData(ctx, playlets, themeNames, tagNames) { dataByID[data.Id] = data } for _, like := range page.List { if like == nil { continue } if data := dataByID[like.PlayletId]; data != nil { resp.List = append(resp.List, proto.PlayletLikeData{ CommonData: *data, IsLike: operateStatusYes, LikedAt: like.CreatedAt, }) } } return resp, nil }