333 lines
10 KiB
Java
333 lines
10 KiB
Java
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;
|
||
|
||
import javax.annotation.Resource;
|
||
import java.util.Date;
|
||
import java.util.List;
|
||
|
||
/**
|
||
* (CmsContent)表服务实现类
|
||
*
|
||
* @author 王志维
|
||
* @since 2025-02-21 23:01:48
|
||
*/
|
||
@Service("cmsContentService")
|
||
@Transactional(rollbackFor = Exception.class)
|
||
public class CmsContentServiceImpl implements CmsContentService {
|
||
@Resource
|
||
private CmsContentMapper cmsContentMapper;
|
||
|
||
@Resource
|
||
private CmsContentViewMapper cmsContentViewMapper;
|
||
|
||
@Resource
|
||
private CmsContentLikeMapper cmsContentLikeMapper;
|
||
|
||
/**
|
||
* 分页查询
|
||
*
|
||
* @param queryDto 筛选条件
|
||
* @return 查询结果
|
||
*/
|
||
@Override
|
||
public PageInfo<CmsContent> getPageList(CmsContentDto queryDto) {
|
||
PageHelper.startPage(queryDto.getPageNum(), queryDto.getPageSize());
|
||
List<CmsContent> list = this.cmsContentMapper.getPageList(queryDto);
|
||
return new PageInfo<>(list);
|
||
}
|
||
|
||
/**
|
||
* 查询列表
|
||
*
|
||
* @param queryDto 筛选条件
|
||
* @return 查询结果
|
||
*/
|
||
@Override
|
||
public List<CmsContent> getList(CmsContentDto queryDto) {
|
||
return this.cmsContentMapper.getList(queryDto);
|
||
}
|
||
|
||
/**
|
||
* 通过主键查询单条数据
|
||
*
|
||
* @param contentId 主键
|
||
* @return 实例对象
|
||
*/
|
||
@Override
|
||
public CmsContent queryById(Long contentId) {
|
||
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;
|
||
}
|
||
|
||
/**
|
||
* 通过主键查询单条数据,带权限检查
|
||
*
|
||
* @param contentId 主键
|
||
* @param userId 用户ID
|
||
* @return 实例对象
|
||
*/
|
||
public CmsContent queryByIdWithPermission(Long contentId, Long userId) {
|
||
CmsContent content = this.cmsContentMapper.queryById(contentId);
|
||
if (content == null) {
|
||
return null;
|
||
}
|
||
|
||
// 如果是付费内容,检查用户是否购买过
|
||
if (content.getIsPaid() == 1) {
|
||
// 这里需要调用ContentPurchaseService检查权限,但为了避免循环依赖,我们返回完整内容
|
||
// 权限检查将在Controller层进行
|
||
}
|
||
|
||
return content;
|
||
}
|
||
|
||
/**
|
||
* 新增数据
|
||
*
|
||
* @param cmsContent 实例对象
|
||
* @return 实例对象
|
||
*/
|
||
@Override
|
||
public CmsContent insert(CmsContent cmsContent) {
|
||
// 设置创建时间和更新时间
|
||
Date now = new Date();
|
||
cmsContent.setCreateTime(now);
|
||
cmsContent.setUpdateTime(now);
|
||
// 设置默认值
|
||
cmsContent.setDeleteFlag(0);
|
||
if (cmsContent.getAuditStatus() == null) {
|
||
cmsContent.setAuditStatus(1); // 默认草稿状态
|
||
}
|
||
if (cmsContent.getPublishStatus() == null) {
|
||
cmsContent.setPublishStatus(1); // 默认未发布状态
|
||
}
|
||
if (cmsContent.getViewCount() == null) {
|
||
cmsContent.setViewCount(0);
|
||
}
|
||
if (cmsContent.getLikeCount() == null) {
|
||
cmsContent.setLikeCount(0);
|
||
}
|
||
if (cmsContent.getCommentCount() == null) {
|
||
cmsContent.setCommentCount(0);
|
||
}
|
||
if (cmsContent.getSort() == null) {
|
||
cmsContent.setSort(0);
|
||
}
|
||
// 保存数据
|
||
this.cmsContentMapper.insert(cmsContent);
|
||
return cmsContent;
|
||
}
|
||
|
||
/**
|
||
* 更新数据
|
||
*
|
||
* @param cmsContent 实例对象
|
||
* @return 实例对象
|
||
*/
|
||
@Override
|
||
public CmsContent update(CmsContent cmsContent) {
|
||
// 设置更新时间
|
||
cmsContent.setUpdateTime(new Date());
|
||
// 更新数据
|
||
this.cmsContentMapper.update(cmsContent);
|
||
return this.queryById(cmsContent.getContentId());
|
||
}
|
||
|
||
/**
|
||
* 更新审核状态
|
||
*
|
||
* @param contentId 内容ID
|
||
* @param auditStatus 审核状态
|
||
* @param reviewerId 审核人ID
|
||
* @param reviewerName 审核人名称
|
||
* @param auditComment 审核意见
|
||
* @param updateBy 更新人
|
||
* @return 影响行数
|
||
*/
|
||
@Override
|
||
public int updateAuditStatus(Long contentId, Integer auditStatus, Long reviewerId, String reviewerName, String auditComment, String updateBy) {
|
||
return this.cmsContentMapper.updateAuditStatus(contentId, auditStatus, reviewerId, reviewerName, auditComment, updateBy);
|
||
}
|
||
|
||
/**
|
||
* 更新发布状态
|
||
*
|
||
* @param contentId 内容ID
|
||
* @param publishStatus 发布状态
|
||
* @param publishTime 发布时间
|
||
* @param updateBy 更新人
|
||
* @return 影响行数
|
||
*/
|
||
@Override
|
||
public int updatePublishStatus(Long contentId, Integer publishStatus, String publishTime, String updateBy) {
|
||
return this.cmsContentMapper.updatePublishStatus(contentId, publishStatus, publishTime, updateBy);
|
||
}
|
||
|
||
/**
|
||
* 增加阅读量
|
||
*
|
||
* @param contentId 内容ID
|
||
* @return 影响行数
|
||
*/
|
||
@Override
|
||
public int increaseViewCount(Long contentId) {
|
||
return this.cmsContentMapper.increaseViewCount(contentId);
|
||
}
|
||
|
||
/**
|
||
* 通过主键逻辑删除
|
||
*
|
||
* @param contentId 主键
|
||
* @param updateBy 更新人
|
||
* @return 影响行数
|
||
*/
|
||
@Override
|
||
public int logicDeleteById(Long contentId, String updateBy) {
|
||
return this.cmsContentMapper.logicDeleteById(contentId, updateBy);
|
||
}
|
||
|
||
/**
|
||
* 通过主键物理删除
|
||
*
|
||
* @param contentId 主键
|
||
* @return 影响行数
|
||
*/
|
||
@Override
|
||
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;
|
||
}
|
||
} |