feat(content): 添加内容查看记录和收藏功能
- 实现通过主键查询内容时支持用户ID参数 - 添加用户查看记录功能,防止5分钟内的重复记录 - 实现内容收藏和取消收藏功能 - 添加用户是否已收藏内容的检查方法 - 集成查看记录和收藏记录的数据持久化操作 - 在收藏操作时同步更新内容的点赞数量统计
This commit is contained in:
parent
18787b68a0
commit
063bfbde12
|
|
@ -37,6 +37,15 @@ public interface CmsContentService extends BaseService {
|
|||
*/
|
||||
CmsContent queryById(Long contentId);
|
||||
|
||||
/**
|
||||
* 通过主键查询单条数据,带用户ID
|
||||
*
|
||||
* @param contentId 主键
|
||||
* @param userId 用户ID
|
||||
* @return 实例对象
|
||||
*/
|
||||
CmsContent queryById(Long contentId, Long userId);
|
||||
|
||||
/**
|
||||
* 新增数据
|
||||
*
|
||||
|
|
@ -101,4 +110,32 @@ public interface CmsContentService extends BaseService {
|
|||
* @return 影响行数
|
||||
*/
|
||||
int deleteById(Long contentId);
|
||||
|
||||
/**
|
||||
* 添加收藏
|
||||
*
|
||||
* @param contentId 内容ID
|
||||
* @param userId 用户ID
|
||||
* @param userName 用户名
|
||||
* @return 影响行数
|
||||
*/
|
||||
int addFavorite(Long contentId, Long userId, String userName);
|
||||
|
||||
/**
|
||||
* 取消收藏
|
||||
*
|
||||
* @param contentId 内容ID
|
||||
* @param userId 用户ID
|
||||
* @return 影响行数
|
||||
*/
|
||||
int removeFavorite(Long contentId, Long userId);
|
||||
|
||||
/**
|
||||
* 检查用户是否已收藏该内容
|
||||
*
|
||||
* @param contentId 内容ID
|
||||
* @param userId 用户ID
|
||||
* @return 是否已收藏
|
||||
*/
|
||||
boolean isFavorited(Long contentId, Long userId);
|
||||
}
|
||||
|
|
@ -3,8 +3,12 @@ package com.kexue.skills.service.impl;
|
|||
import com.github.pagehelper.PageHelper;
|
||||
import com.github.pagehelper.PageInfo;
|
||||
import com.kexue.skills.entity.CmsContent;
|
||||
import com.kexue.skills.entity.CmsContentView;
|
||||
import com.kexue.skills.entity.CmsContentLike;
|
||||
import com.kexue.skills.entity.dto.CmsContentDto;
|
||||
import com.kexue.skills.mapper.CmsContentMapper;
|
||||
import com.kexue.skills.mapper.CmsContentViewMapper;
|
||||
import com.kexue.skills.mapper.CmsContentLikeMapper;
|
||||
import com.kexue.skills.service.CmsContentService;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
|
@ -24,6 +28,12 @@ import java.util.List;
|
|||
public class CmsContentServiceImpl implements CmsContentService {
|
||||
@Resource
|
||||
private CmsContentMapper cmsContentMapper;
|
||||
|
||||
@Resource
|
||||
private CmsContentViewMapper cmsContentViewMapper;
|
||||
|
||||
@Resource
|
||||
private CmsContentLikeMapper cmsContentLikeMapper;
|
||||
|
||||
/**
|
||||
* 分页查询
|
||||
|
|
@ -60,6 +70,47 @@ public class CmsContentServiceImpl implements CmsContentService {
|
|||
return this.cmsContentMapper.queryById(contentId);
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过主键查询单条数据,带用户ID
|
||||
*
|
||||
* @param contentId 主键
|
||||
* @param userId 用户ID
|
||||
* @return 实例对象
|
||||
*/
|
||||
@Override
|
||||
public CmsContent queryById(Long contentId, Long userId) {
|
||||
CmsContent content = this.cmsContentMapper.queryById(contentId);
|
||||
if (content == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// 如果用户ID不为空,添加用户查看记录
|
||||
if (userId != null) {
|
||||
// 检查用户是否已经查看过该内容(5分钟内)
|
||||
CmsContentView existingView = cmsContentViewMapper.queryByUserIdAndContentId(userId, contentId);
|
||||
if (existingView == null) {
|
||||
// 新增查看记录
|
||||
CmsContentView viewRecord = new CmsContentView();
|
||||
viewRecord.setUserId(userId);
|
||||
viewRecord.setContentId(contentId);
|
||||
viewRecord.setContentTitle(content.getTitle());
|
||||
viewRecord.setViewTime(new Date());
|
||||
viewRecord.setDeleteFlag(0);
|
||||
cmsContentViewMapper.insert(viewRecord);
|
||||
} else {
|
||||
// 检查是否超过5分钟
|
||||
Date fiveMinutesAgo = new Date(System.currentTimeMillis() - 5 * 60 * 1000);
|
||||
if (existingView.getViewTime().before(fiveMinutesAgo)) {
|
||||
// 更新查看时间
|
||||
existingView.setViewTime(new Date());
|
||||
cmsContentViewMapper.update(existingView);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return content;
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过主键查询单条数据,带权限检查
|
||||
*
|
||||
|
|
@ -197,4 +248,86 @@ public class CmsContentServiceImpl implements CmsContentService {
|
|||
public int deleteById(Long contentId) {
|
||||
return this.cmsContentMapper.deleteById(contentId);
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加收藏
|
||||
*
|
||||
* @param contentId 内容ID
|
||||
* @param userId 用户ID
|
||||
* @param userName 用户名
|
||||
* @return 影响行数
|
||||
*/
|
||||
@Override
|
||||
public int addFavorite(Long contentId, Long userId, String userName) {
|
||||
// 检查内容是否存在
|
||||
CmsContent content = this.cmsContentMapper.queryById(contentId);
|
||||
if (content == null) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
// 检查用户是否已经收藏过该内容
|
||||
CmsContentLike existingLike = cmsContentLikeMapper.queryByUserIdAndContentId(userId, contentId);
|
||||
if (existingLike != null) {
|
||||
return 0; // 已经收藏过,不允许再次收藏
|
||||
}
|
||||
|
||||
// 新增收藏记录
|
||||
CmsContentLike likeRecord = new CmsContentLike();
|
||||
likeRecord.setUserId(userId);
|
||||
likeRecord.setUserName(userName);
|
||||
likeRecord.setContentId(contentId);
|
||||
likeRecord.setContentTitle(content.getTitle());
|
||||
likeRecord.setLikeTime(new Date());
|
||||
likeRecord.setDeleteFlag(0);
|
||||
|
||||
// 增加内容的点赞数
|
||||
content.setLikeCount(content.getLikeCount() + 1);
|
||||
this.cmsContentMapper.update(content);
|
||||
|
||||
return cmsContentLikeMapper.insert(likeRecord);
|
||||
}
|
||||
|
||||
/**
|
||||
* 取消收藏
|
||||
*
|
||||
* @param contentId 内容ID
|
||||
* @param userId 用户ID
|
||||
* @return 影响行数
|
||||
*/
|
||||
@Override
|
||||
public int removeFavorite(Long contentId, Long userId) {
|
||||
// 检查用户是否已经收藏过该内容
|
||||
CmsContentLike existingLike = cmsContentLikeMapper.queryByUserIdAndContentId(userId, contentId);
|
||||
if (existingLike == null) {
|
||||
return 0; // 没有收藏过,无需取消
|
||||
}
|
||||
|
||||
// 删除收藏记录
|
||||
int result = cmsContentLikeMapper.deleteById(existingLike.getLikeId());
|
||||
|
||||
// 减少内容的点赞数
|
||||
CmsContent content = this.cmsContentMapper.queryById(contentId);
|
||||
if (content != null && content.getLikeCount() > 0) {
|
||||
content.setLikeCount(content.getLikeCount() - 1);
|
||||
this.cmsContentMapper.update(content);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查用户是否已收藏该内容
|
||||
*
|
||||
* @param contentId 内容ID
|
||||
* @param userId 用户ID
|
||||
* @return 是否已收藏
|
||||
*/
|
||||
@Override
|
||||
public boolean isFavorited(Long contentId, Long userId) {
|
||||
if (userId == null) {
|
||||
return false;
|
||||
}
|
||||
CmsContentLike existingLike = cmsContentLikeMapper.queryByUserIdAndContentId(userId, contentId);
|
||||
return existingLike != null;
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue