1515import lombok .RequiredArgsConstructor ;
1616import org .springframework .stereotype .Service ;
1717
18- import java .time .LocalDateTime ;
1918import java .util .Collections ;
2019import java .util .LinkedHashSet ;
2120import java .util .List ;
2524@ RequiredArgsConstructor
2625public class UserEpisodeWatchServiceImpl implements UserEpisodeWatchService {
2726
28- private static final int WATCH_STATUS_UNWATCHED = 0 ;
2927 private static final int WATCH_STATUS_WATCHED = 1 ;
3028 private static final int EPISODE_TYPE_MAIN = 0 ;
3129
@@ -42,17 +40,10 @@ public void updateEpisodeWatch(String accessToken, int episodeId, UpdateEpisodeW
4240 throw new IllegalArgumentException ("剧集不存在" );
4341 }
4442
45- UserEpisodeWatchEntity existing = userEpisodeWatchMapper .selectOne (
46- new LambdaQueryWrapper <UserEpisodeWatchEntity >()
47- .eq (UserEpisodeWatchEntity ::getUserId , userId )
48- .eq (UserEpisodeWatchEntity ::getEpisodeId , episodeId )
49- .last ("LIMIT 1" ));
50-
5143 if (Boolean .TRUE .equals (dto .getWatched ())) {
52- upsertWatched (userId , episode , existing );
53- } else if (existing != null ) {
54- existing .setWatchStatus (WATCH_STATUS_UNWATCHED );
55- userEpisodeWatchMapper .updateById (existing );
44+ userEpisodeWatchMapper .upsertWatched (userId , episode .getSubjectId (), episode .getId ());
45+ } else {
46+ userEpisodeWatchMapper .markUnwatched (userId , episodeId );
5647 }
5748
5849 refreshCollectionEpisodeProgress (userId , episode .getSubjectId ());
@@ -84,25 +75,6 @@ public Set<Long> listWatchedEpisodeIds(long userId, int subjectId) {
8475 return watchedEpisodeIds ;
8576 }
8677
87- private void upsertWatched (Long userId , BangumiEpisodeEntity episode , UserEpisodeWatchEntity existing ) {
88- if (existing != null ) {
89- existing .setWatchStatus (WATCH_STATUS_WATCHED );
90- if (existing .getWatchedAt () == null ) {
91- existing .setWatchedAt (LocalDateTime .now ());
92- }
93- userEpisodeWatchMapper .updateById (existing );
94- return ;
95- }
96-
97- UserEpisodeWatchEntity row = new UserEpisodeWatchEntity ();
98- row .setUserId (userId );
99- row .setSubjectId (episode .getSubjectId ());
100- row .setEpisodeId (episode .getId ());
101- row .setWatchStatus (WATCH_STATUS_WATCHED );
102- row .setWatchedAt (LocalDateTime .now ());
103- userEpisodeWatchMapper .insert (row );
104- }
105-
10678 /**
10779 * 维护 user_bgm_collection.ep_status,表示按正片顺序连续看完的话数。
10880 */
0 commit comments