3939import java .util .ArrayList ;
4040import java .util .Collections ;
4141import java .util .HashMap ;
42+ import java .util .HashSet ;
4243import java .util .LinkedHashMap ;
44+ import java .util .LinkedHashSet ;
4345import java .util .List ;
4446import java .util .Map ;
4547
4648import org .slf4j .Logger ;
4749import org .slf4j .LoggerFactory ;
4850import org .springframework .data .domain .Sort ;
4951import java .util .Properties ;
52+ import java .util .Set ;
53+
5054import org .apache .kafka .common .serialization .StringSerializer ;
5155import org .apache .kafka .common .serialization .StringDeserializer ;
5256import java .time .Duration ;
@@ -192,6 +196,19 @@ public String getVideoIdFromThumbnailId(String thumbnailId) {
192196 return gridFSFile .getMetadata ().get ("videoId" ).toString ();
193197 }
194198
199+ public List <String > getThumbnailIdsByVideoId (String videoId ) {
200+ Query query = Query .query (Criteria .where ("metadata.videoId" ).is (videoId ));
201+ List <GridFSFile > gridFSFiles = template .find (query ).into (new ArrayList <>());
202+
203+ List <String > thumbnailIds = new ArrayList <>();
204+ for (GridFSFile file : gridFSFiles ) {
205+ thumbnailIds .add (file .getObjectId ().toString ());
206+ }
207+
208+ return thumbnailIds ;
209+ }
210+
211+
195212 public VideoWithStream getVideoWithStream (String id ) throws IOException {
196213 GridFSFile gridFSFile = template .findOne (new Query (Criteria .where ("_id" ).is (id )));
197214 MDC .put ("type" , "videoservice" );
@@ -203,6 +220,8 @@ public VideoWithStream getVideoWithStream(String id) throws IOException {
203220 return null ;
204221 }
205222
223+ //Hanle view
224+
206225 public void updateViews (String id ) {
207226 Query query = new Query (Criteria .where ("_id" ).is (id ));
208227 Update update = new Update ().inc ("views" , 1 );
@@ -353,7 +372,7 @@ public Map<String, Integer> getGenresByUserId(String userId) {
353372 // 1. Lấy danh sách thumbnailId từ History
354373 List <String > thumbnailIds = getHistoryByUserId (userId );
355374
356- Map <String , Integer > genreCounts = new LinkedHashMap <>();
375+ Map <String , Integer > genreCounts = new HashMap <>();
357376
358377 // 2. Duyệt qua từng thumbnailId
359378 for (String thumbnailId : thumbnailIds ) {
@@ -374,12 +393,64 @@ public Map<String, Integer> getGenresByUserId(String userId) {
374393 }
375394 }
376395
377- return genreCounts ;
396+ // 3. Sắp xếp genres theo số lượng giảm dần và giới hạn 12 genres
397+ return genreCounts .entrySet ().stream ()
398+ .sorted ((entry1 , entry2 ) -> Integer .compare (entry2 .getValue (), entry1 .getValue ())) // Sắp xếp giảm dần theo giá trị
399+ .limit (12 ) // Lấy tối đa 12 genres
400+ .collect (LinkedHashMap ::new ,
401+ (map , entry ) -> map .put (entry .getKey (), entry .getValue ()),
402+ LinkedHashMap ::putAll );
378403 }
404+
405+ //handle recommend
406+ public List <String > getUniqueVideoIdsByGenres (String userId ) {
407+ // 1. Lấy danh sách genres và số lần xuất hiện từ API /getGenresByUserId/{userId}
408+ Map <String , Integer > genreCounts = getGenresByUserId (userId );
409+
410+ Set <String > uniqueVideoIds = new LinkedHashSet <>();
411+
412+ // 2. Tìm videoId theo từng genre
413+ for (String genre : genreCounts .keySet ()) {
414+ Query query = Query .query (Criteria .where ("genres" ).is (genre ));
415+ List <DBObject > videoFiles = mongoTemplate .find (query , DBObject .class , "fs.files" );
416+
417+ for (DBObject video : videoFiles ) {
418+ String videoId = video .get ("_id" ).toString ();
419+ uniqueVideoIds .add (videoId );
420+ }
421+ }
422+
423+ // 3. Trả về danh sách videoId (duy nhất, không trùng lặp)
424+ return new ArrayList <>(uniqueVideoIds );
425+ }
426+
427+ public List <String > getThumbnailIdsByUserGenres (String userId ) {
428+ // Bước 1: Lấy genres từ userId
429+ Map <String , Integer > genreCounts = getGenresByUserId (userId );
430+
431+ // Dùng Set để đảm bảo không có `thumbnailId` trùng lặp
432+ Set <String > uniqueThumbnailIds = new LinkedHashSet <>();
379433
434+ // Bước 2: Lấy danh sách videoId từ từng genre
435+ for (String genre : genreCounts .keySet ()) {
436+ Query query = Query .query (Criteria .where ("genres" ).is (genre ));
437+ List <DBObject > videos = mongoTemplate .find (query , DBObject .class , "fs.files" );
438+ for (DBObject video : videos ) {
439+ String videoId = video .get ("_id" ).toString ();
440+
441+ // Bước 3: Lấy danh sách thumbnailId từ videoId
442+ List <String > thumbnailIds = getThumbnailIdsByVideoId (videoId );
443+ uniqueThumbnailIds .addAll (thumbnailIds );
444+ }
445+ }
446+
447+ // Trả về danh sách thumbnailId duy nhất
448+ return new ArrayList <>(uniqueThumbnailIds );
449+ }
380450
451+
381452
382- //hanlde Report
453+ //hanlde Report
383454 public void uploadReport (String videoId , String msg , String userId ) {
384455 Report report = new Report (videoId , msg , userId );
385456 mongoTemplate .save (report );
0 commit comments