|
@@ -1,11 +1,13 @@
|
|
|
package com.fs.course.service.impl;
|
|
package com.fs.course.service.impl;
|
|
|
|
|
|
|
|
import java.util.List;
|
|
import java.util.List;
|
|
|
|
|
+import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
|
|
import com.fs.common.core.domain.R;
|
|
import com.fs.common.core.domain.R;
|
|
|
import com.fs.common.utils.DateUtils;
|
|
import com.fs.common.utils.DateUtils;
|
|
|
import lombok.Synchronized;
|
|
import lombok.Synchronized;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
|
+import org.springframework.data.redis.core.RedisTemplate;
|
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.stereotype.Service;
|
|
|
import com.fs.course.mapper.FsUserVideoFavoriteMapper;
|
|
import com.fs.course.mapper.FsUserVideoFavoriteMapper;
|
|
|
import com.fs.course.domain.FsUserVideoFavorite;
|
|
import com.fs.course.domain.FsUserVideoFavorite;
|
|
@@ -23,6 +25,8 @@ public class FsUserVideoFavoriteServiceImpl implements IFsUserVideoFavoriteServi
|
|
|
{
|
|
{
|
|
|
@Autowired
|
|
@Autowired
|
|
|
private FsUserVideoFavoriteMapper fsUserVideoFavoriteMapper;
|
|
private FsUserVideoFavoriteMapper fsUserVideoFavoriteMapper;
|
|
|
|
|
+ @Autowired(required = false)
|
|
|
|
|
+ private RedisTemplate<String, Boolean> redisTemplate;
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* 查询课堂视频收藏
|
|
* 查询课堂视频收藏
|
|
@@ -97,30 +101,39 @@ public class FsUserVideoFavoriteServiceImpl implements IFsUserVideoFavoriteServi
|
|
|
{
|
|
{
|
|
|
return fsUserVideoFavoriteMapper.deleteFsUserVideoFavoriteByFavoriteId(favoriteId);
|
|
return fsUserVideoFavoriteMapper.deleteFsUserVideoFavoriteByFavoriteId(favoriteId);
|
|
|
}
|
|
}
|
|
|
|
|
+ private static final String FAVORITE_KEY_PREFIX = "favorite:video:";
|
|
|
|
|
+ private static final String NO_FAVORITE_KEY_PREFIX = "nofavorite:video:";
|
|
|
|
|
+
|
|
|
@Override
|
|
@Override
|
|
|
public R checkFavorite(Long videoId, long userId) {
|
|
public R checkFavorite(Long videoId, long userId) {
|
|
|
- return fsUserVideoFavoriteMapper.checkFavorite(videoId, userId) > 0
|
|
|
|
|
- ? R.ok().put("isFavorite", 1)
|
|
|
|
|
- : R.error().put("isFavorite", 0);
|
|
|
|
|
|
|
+ String key = FAVORITE_KEY_PREFIX + videoId + ":user:" + userId;
|
|
|
|
|
+ Boolean hasFavorite = redisTemplate.opsForValue().get(key);
|
|
|
|
|
+ if (hasFavorite != null && hasFavorite) {
|
|
|
|
|
+ return R.ok().put("isFavorite", 1);
|
|
|
|
|
+ } else {
|
|
|
|
|
+ return fsUserVideoFavoriteMapper.checkFavorite(videoId, userId) > 0
|
|
|
|
|
+ ? R.ok().put("isFavorite", 1)
|
|
|
|
|
+ : R.error().put("isFavorite", 0);
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
@Override
|
|
|
@Transactional
|
|
@Transactional
|
|
|
@Synchronized
|
|
@Synchronized
|
|
|
public void favoriteVideo(Long videoId, long userId) {
|
|
public void favoriteVideo(Long videoId, long userId) {
|
|
|
- if (fsUserVideoFavoriteMapper.checkFavorite(videoId, userId) == 0) {
|
|
|
|
|
- FsUserVideoFavorite favorite = new FsUserVideoFavorite();
|
|
|
|
|
- favorite.setVideoId(videoId);
|
|
|
|
|
- favorite.setUserId(userId);
|
|
|
|
|
- favorite.setCreateTime(DateUtils.getNowDate());
|
|
|
|
|
- fsUserVideoFavoriteMapper.insertFsUserVideoFavorite(favorite);
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ String unlikeKey = NO_FAVORITE_KEY_PREFIX + videoId + ":user:" + userId;
|
|
|
|
|
+ redisTemplate.delete(unlikeKey);
|
|
|
|
|
+ String key = FAVORITE_KEY_PREFIX + videoId + ":user:" + userId;
|
|
|
|
|
+ redisTemplate.opsForValue().set(key, true, 1, TimeUnit.DAYS);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
@Override
|
|
|
@Transactional
|
|
@Transactional
|
|
|
@Synchronized
|
|
@Synchronized
|
|
|
public void deleteFavorite(Long videoId, long userId) {
|
|
public void deleteFavorite(Long videoId, long userId) {
|
|
|
- fsUserVideoFavoriteMapper.deleteFavorite(videoId, userId);
|
|
|
|
|
|
|
+ String key = FAVORITE_KEY_PREFIX + videoId + ":user:" + userId;
|
|
|
|
|
+ redisTemplate.delete(key);
|
|
|
|
|
+ String unlikeKey = NO_FAVORITE_KEY_PREFIX + videoId + ":user:" + userId;
|
|
|
|
|
+ redisTemplate.opsForValue().set(unlikeKey, true, 1, TimeUnit.DAYS);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|