573 lines
20 KiB
Java
573 lines
20 KiB
Java
package com.example.caseData.service;
|
||
|
||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||
import com.example.caseData.common.Response;
|
||
import com.example.caseData.dao.*;
|
||
import com.example.caseData.exception.BusinessException;
|
||
import com.example.caseData.model.*;
|
||
import com.example.caseData.request.CaseClinicalArticleRequest.addClinicalArticleComment;
|
||
import com.example.caseData.utils.Replace;
|
||
import jakarta.annotation.Resource;
|
||
import org.springframework.stereotype.Service;
|
||
import org.springframework.transaction.annotation.Transactional;
|
||
import org.springframework.transaction.interceptor.TransactionAspectSupport;
|
||
|
||
|
||
import java.util.List;
|
||
import java.util.Objects;
|
||
|
||
//import static com.baomidou.mybatisplus.extension.toolkit.Db.removeById;
|
||
//import static com.baomidou.mybatisplus.extension.toolkit.Db.save;
|
||
|
||
@Service
|
||
public class CaseClinicalArticleService {
|
||
@Resource
|
||
private UserCollectClinicalArticleDao userCollectClinicalArticleDao;
|
||
|
||
@Resource
|
||
private CaseClinicalArticleDao caseClinicalArticleDao;
|
||
|
||
@Resource
|
||
private StatsCaseClinicalDao statsCaseClinicalDao;
|
||
|
||
@Resource
|
||
private StatsCaseClinicalHospitalDao statsCaseClinicalHospitalDao;
|
||
|
||
@Resource
|
||
private StatsCaseClinicalDoctorDao statsCaseClinicalDoctorDao;
|
||
|
||
@Resource
|
||
private CaseClinicalArticleAuthorDao caseClinicalArticleAuthorDao;
|
||
|
||
@Resource
|
||
private CaseClinicalDoctorDao caseClinicalDoctorDao;
|
||
|
||
@Resource
|
||
private BasicSensitiveWordService basicSensitiveWordService;
|
||
|
||
@Resource
|
||
private UserCommentClinicalArticleDao userCommentClinicalArticleDao;
|
||
|
||
@Resource
|
||
private UserDao userDao;
|
||
|
||
@Resource
|
||
private UserService userService;
|
||
|
||
@Resource
|
||
private CaseClinicalArticleLabelDao caseClinicalArticleLabelDao;
|
||
|
||
@Resource
|
||
private StatsCaseClinicalLabelDao statsCaseClinicalLabelDao;
|
||
|
||
/**
|
||
* 新增收藏-临床病例库-文章
|
||
* @param articleId 文章id
|
||
* @param userId 用户id
|
||
* @return bool
|
||
*/
|
||
@Transactional
|
||
public boolean AddClinicalArticleCollect(String articleId,String userId){
|
||
// 获取文章数据
|
||
CaseClinicalArticleModel article = caseClinicalArticleDao.selectById(articleId);
|
||
if (article == null) {
|
||
return false;
|
||
}
|
||
|
||
// 检测用户是否已收藏过
|
||
UserCollectClinicalArticleModel userCollectClinicalArticle = getUserCollectClinicalArticleStatus(articleId,userId);
|
||
if (userCollectClinicalArticle != null) {
|
||
return true;
|
||
}
|
||
|
||
// 新增收藏
|
||
UserCollectClinicalArticleModel userCollectClinicalArticleData = new UserCollectClinicalArticleModel();
|
||
userCollectClinicalArticleData.setUserId(Long.valueOf(userId));
|
||
userCollectClinicalArticleData.setArticleId(Long.valueOf(articleId));
|
||
int res = userCollectClinicalArticleDao.insert(userCollectClinicalArticleData);
|
||
if (res <= 0){
|
||
return false;
|
||
}
|
||
|
||
// 新增统计字段
|
||
boolean r = IncClinicalArticleStats(articleId,2,1);
|
||
if (!r){
|
||
TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
|
||
return false;
|
||
}
|
||
|
||
return true;
|
||
}
|
||
|
||
/**
|
||
* 取消收藏-临床病例库-文章
|
||
* @param articleId 文章id
|
||
* @param userId 用户id
|
||
* @return bool
|
||
*/
|
||
@Transactional
|
||
public boolean DeleteClinicalArticleCollect(String articleId,String userId){
|
||
// 检测用户是否已收藏过
|
||
UserCollectClinicalArticleModel userCollectClinicalArticle = getUserCollectClinicalArticleStatus(articleId,userId);
|
||
if (userCollectClinicalArticle == null) {
|
||
return true;
|
||
}
|
||
|
||
// 删除收藏
|
||
int res = userCollectClinicalArticleDao.deleteById(userCollectClinicalArticle.getCollectId());
|
||
if (res <= 0){
|
||
TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
|
||
return false;
|
||
}
|
||
|
||
// 减少文章的统计字段
|
||
boolean r = DecClinicalArticleStats(articleId,2,1);
|
||
if (!r){
|
||
TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
|
||
return false;
|
||
}
|
||
|
||
return true;
|
||
}
|
||
|
||
/**
|
||
* 新增评论-临床病例库-文章
|
||
* @param articleId 文章id
|
||
* @param userId 用户id
|
||
* @return bool
|
||
*/
|
||
@Transactional
|
||
public boolean AddClinicalArticleComment(String articleId, String userId, addClinicalArticleComment request){
|
||
// 获取文章数据
|
||
CaseClinicalArticleModel article = caseClinicalArticleDao.selectById(articleId);
|
||
if (article == null) {
|
||
throw new BusinessException("非法文章");
|
||
}
|
||
|
||
if (article.getArticleStatus() != 1){
|
||
throw new BusinessException("非法文章");
|
||
}
|
||
|
||
// 处理评论内容
|
||
BasicSensitiveWordService.FilterResult result = basicSensitiveWordService.filter(request.getContent());
|
||
if (result.hasSensitive == 1){
|
||
throw new BusinessException("存在敏感词,请修改后提交");
|
||
}
|
||
|
||
// 新增评论
|
||
UserCommentClinicalArticleModel userCommentClinicalArticleData = new UserCommentClinicalArticleModel();
|
||
userCommentClinicalArticleData.setUserId(Long.valueOf(userId));
|
||
userCommentClinicalArticleData.setArticleId(Long.valueOf(articleId));
|
||
userCommentClinicalArticleData.setStatus(1);
|
||
userCommentClinicalArticleData.setIsSensitive(0);
|
||
userCommentClinicalArticleData.setContent(request.getContent());
|
||
userCommentClinicalArticleData.setCommentImage(Replace.removeOssDomain(request.getCommentImage()));
|
||
|
||
// 评论根id
|
||
if (request.getRootId() != null) {
|
||
userCommentClinicalArticleData.setRootId(Long.valueOf(request.getRootId()));
|
||
}
|
||
|
||
// 评论父级id
|
||
if (request.getParentId() != null) {
|
||
userCommentClinicalArticleData.setParentId(Long.valueOf(request.getParentId()));
|
||
}
|
||
|
||
int res = userCommentClinicalArticleDao.insert(userCommentClinicalArticleData);
|
||
if (res <= 0){
|
||
return false;
|
||
}
|
||
|
||
// 新增文章的统计字段
|
||
boolean r = IncClinicalArticleStats(articleId,3,1);
|
||
if (!r){
|
||
TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
|
||
return false;
|
||
}
|
||
|
||
// 获取发放积分次数
|
||
Integer num = userService.GetReportUserScore(userId);
|
||
if (num < 3){
|
||
// 发放积分
|
||
userService.ReportUserScore(articleId,1,userId,5);
|
||
}
|
||
|
||
return true;
|
||
}
|
||
|
||
/**
|
||
* 删除评论-临床病例库-文章
|
||
* @param commentId 评论id
|
||
* @param userId 用户id
|
||
* @return bool
|
||
*/
|
||
@Transactional
|
||
public boolean DeleteClinicalArticleComment(String commentId, String userId){
|
||
// 获取评论数据
|
||
UserCommentClinicalArticleModel comment = userCommentClinicalArticleDao.selectById(commentId);
|
||
if (comment == null) {
|
||
return false;
|
||
}
|
||
|
||
// 获取文章数据
|
||
CaseClinicalArticleModel article = caseClinicalArticleDao.selectById(comment.getArticleId());
|
||
if (article == null) {
|
||
System.out.println(111);
|
||
return false;
|
||
}
|
||
|
||
if (article.getArticleStatus() != 1){
|
||
System.out.println(222);
|
||
return false;
|
||
}
|
||
|
||
// 检测是否本人的评论
|
||
if (!Objects.equals(comment.getUserId(), Long.valueOf(userId))){
|
||
// 检测用户是否文章作者
|
||
boolean isAuthor = checkUserIsArticleAuthor(String.valueOf(article.getArticleId()),userId);
|
||
if (!isAuthor) {
|
||
System.out.println(333);
|
||
return false;
|
||
}
|
||
}
|
||
|
||
// 获取下级评论
|
||
LambdaQueryWrapper<UserCommentClinicalArticleModel> articleQueryWrapper = new LambdaQueryWrapper<>();
|
||
articleQueryWrapper.eq(UserCommentClinicalArticleModel::getRootId, comment.getCommentId());
|
||
List<UserCommentClinicalArticleModel> userCommentClinicalArticles = userCommentClinicalArticleDao.selectList(articleQueryWrapper);
|
||
for (UserCommentClinicalArticleModel m : userCommentClinicalArticles){
|
||
// 删除评论
|
||
int res = userCommentClinicalArticleDao.deleteById(m.getCommentId());
|
||
if (res <= 0){
|
||
TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
|
||
return false;
|
||
}
|
||
|
||
// 减少文章的统计字段
|
||
boolean r = DecClinicalArticleStats(String.valueOf(comment.getArticleId()),2,1);
|
||
if (!r){
|
||
TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
|
||
return false;
|
||
}
|
||
}
|
||
|
||
// 删除评论
|
||
int res = userCommentClinicalArticleDao.deleteById(comment.getCommentId());
|
||
if (res <= 0){
|
||
System.out.println(444);
|
||
TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
|
||
return false;
|
||
}
|
||
|
||
// 减少文章的统计字段
|
||
boolean r = DecClinicalArticleStats(String.valueOf(comment.getArticleId()),2,1);
|
||
if (!r){
|
||
System.out.println(555);
|
||
TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
|
||
return false;
|
||
}
|
||
|
||
return true;
|
||
}
|
||
|
||
/**
|
||
* 置顶评论-临床病例库-文章
|
||
* @param commentId 评论id
|
||
* @param userId 用户id
|
||
* @return bool
|
||
*/
|
||
@Transactional
|
||
public boolean AddTopClinicalArticleComment(String commentId, String userId){
|
||
// 获取评论数据
|
||
UserCommentClinicalArticleModel comment = userCommentClinicalArticleDao.selectById(commentId);
|
||
if (comment == null) {
|
||
return false;
|
||
}
|
||
|
||
// 获取文章数据
|
||
CaseClinicalArticleModel article = caseClinicalArticleDao.selectById(comment.getArticleId());
|
||
if (article == null) {
|
||
return false;
|
||
}
|
||
|
||
if (article.getArticleStatus() != 1){
|
||
return false;
|
||
}
|
||
|
||
// 检测用户是否文章作者
|
||
boolean isAuthor = checkUserIsArticleAuthor(String.valueOf(comment.getArticleId()),userId);
|
||
if (!isAuthor) {
|
||
return false;
|
||
}
|
||
|
||
if (comment.getIsTop() == 1){
|
||
return true;
|
||
}
|
||
|
||
// 置顶评论
|
||
comment.setIsTop(1);
|
||
userCommentClinicalArticleDao.updateById(comment);
|
||
|
||
return true;
|
||
}
|
||
|
||
/**
|
||
* 取消置顶评论-临床病例库-文章
|
||
* @param commentId 评论id
|
||
* @param userId 用户id
|
||
* @return bool
|
||
*/
|
||
@Transactional
|
||
public boolean deleteTopClinicalArticleComment(String commentId, String userId){
|
||
// 获取评论数据
|
||
UserCommentClinicalArticleModel comment = userCommentClinicalArticleDao.selectById(commentId);
|
||
if (comment == null) {
|
||
return false;
|
||
}
|
||
|
||
// 获取文章数据
|
||
CaseClinicalArticleModel article = caseClinicalArticleDao.selectById(comment.getArticleId());
|
||
if (article == null) {
|
||
return false;
|
||
}
|
||
|
||
if (article.getArticleStatus() != 1){
|
||
return false;
|
||
}
|
||
|
||
// 检测用户是否文章作者
|
||
boolean isAuthor = checkUserIsArticleAuthor(String.valueOf(comment.getArticleId()),userId);
|
||
if (!isAuthor) {
|
||
return false;
|
||
}
|
||
|
||
// 取消置顶评论
|
||
if (comment.getIsTop() == 0){
|
||
return true;
|
||
}
|
||
|
||
// 置顶评论
|
||
comment.setIsTop(0);
|
||
userCommentClinicalArticleDao.updateById(comment);
|
||
|
||
return true;
|
||
}
|
||
|
||
/**
|
||
* 获取用户收藏数据
|
||
* @param articleId 文章id
|
||
* @param userId 用户id
|
||
* @return bool false:未收藏 true:已收藏
|
||
*/
|
||
public UserCollectClinicalArticleModel getUserCollectClinicalArticleStatus(String articleId,String userId){
|
||
// 检测用户是否已收藏过
|
||
LambdaQueryWrapper<UserCollectClinicalArticleModel> mapQueryWrapper = new LambdaQueryWrapper<>();
|
||
mapQueryWrapper.eq(UserCollectClinicalArticleModel::getUserId, userId);
|
||
mapQueryWrapper.eq(UserCollectClinicalArticleModel::getArticleId, articleId);
|
||
|
||
return userCollectClinicalArticleDao.selectOne(mapQueryWrapper);
|
||
}
|
||
|
||
/**
|
||
* 检测用户是否文章作者
|
||
* @param articleId 文章id
|
||
* @param userId 用户id
|
||
* @return bool 是否作者
|
||
*/
|
||
public boolean checkUserIsArticleAuthor(String articleId,String userId){
|
||
// 获取当前登录用户数据
|
||
UserModel user = userDao.selectById(Long.valueOf(userId));
|
||
if (user == null) {
|
||
return false;
|
||
}
|
||
|
||
// 当前用户是否是作者之一
|
||
boolean isAuthor = false;
|
||
|
||
// 获取文章作者数据
|
||
LambdaQueryWrapper<CaseClinicalArticleAuthorModel> authorQueryWrapper = new LambdaQueryWrapper<>();
|
||
authorQueryWrapper.eq(CaseClinicalArticleAuthorModel::getArticleId, articleId);
|
||
List<CaseClinicalArticleAuthorModel> caseClinicalArticleAuthors = caseClinicalArticleAuthorDao.selectList(authorQueryWrapper);
|
||
for (CaseClinicalArticleAuthorModel author : caseClinicalArticleAuthors) {
|
||
// 查询医生
|
||
CaseClinicalDoctorModel caseClinicalDoctor = caseClinicalDoctorDao.selectById(author.getDoctorId());
|
||
if (caseClinicalDoctor == null) {
|
||
return false;
|
||
}
|
||
|
||
if (Objects.equals(caseClinicalDoctor.getDoctorIden(), user.getUserIden())){
|
||
isAuthor = true;
|
||
break;
|
||
}
|
||
}
|
||
|
||
return isAuthor;
|
||
}
|
||
|
||
/**
|
||
* 新增文章的统计字段
|
||
* @param articleId 文章id
|
||
* @param type 类型:1:阅读量 2:收藏量 3:评论数 4:文章数
|
||
*/
|
||
@Transactional
|
||
public boolean IncClinicalArticleStats(String articleId,Integer type,Integer num){
|
||
try {
|
||
String caseClinicalArticleField = ""; // 具体文章
|
||
String statsCaseClinicalField = ""; // 全部文章
|
||
|
||
// 阅读
|
||
if (type == 1){
|
||
caseClinicalArticleField = "read_num"; // 具体文章
|
||
statsCaseClinicalField = "article_read_num"; // 全部文章
|
||
}
|
||
|
||
// 收藏
|
||
if (type == 2){
|
||
caseClinicalArticleField = "collect_num"; // 具体文章
|
||
statsCaseClinicalField = "article_collect_num"; // 全部文章
|
||
}
|
||
|
||
// 评论
|
||
if (type == 3){
|
||
caseClinicalArticleField = "comment_num"; // 具体文章
|
||
statsCaseClinicalField = "article_comment_num"; // 全部文章
|
||
}
|
||
|
||
// 文章数
|
||
if (type == 4){
|
||
statsCaseClinicalField = "article_num"; // 全部文章
|
||
}
|
||
|
||
if (!caseClinicalArticleField.isEmpty()){
|
||
caseClinicalArticleDao.inc(Long.valueOf(articleId),caseClinicalArticleField,num);
|
||
}
|
||
statsCaseClinicalDao.inc(1L,statsCaseClinicalField,num);
|
||
|
||
return true;
|
||
} catch (Exception e) {
|
||
TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
|
||
return false;
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 减少文章的统计字段
|
||
* @param articleId 文章id
|
||
* @param type 类型:1:阅读量 2:收藏量 3:评论数 4:文章数
|
||
*/
|
||
@Transactional
|
||
public boolean DecClinicalArticleStats(String articleId,Integer type,Integer num){
|
||
try {
|
||
String caseClinicalArticleField = ""; // 具体文章
|
||
String statsCaseClinicalField = ""; // 全部文章
|
||
|
||
// 阅读
|
||
if (type == 1){
|
||
caseClinicalArticleField = "read_num"; // 具体文章
|
||
statsCaseClinicalField = "article_read_num"; // 全部文章
|
||
}
|
||
|
||
// 收藏
|
||
if (type == 2){
|
||
caseClinicalArticleField = "collect_num"; // 具体文章
|
||
statsCaseClinicalField = "article_collect_num"; // 全部文章
|
||
}
|
||
|
||
// 评论
|
||
if (type == 3){
|
||
caseClinicalArticleField = "comment_num"; // 具体文章
|
||
statsCaseClinicalField = "article_comment_num"; // 全部文章
|
||
}
|
||
|
||
// 文章数
|
||
if (type == 4){
|
||
statsCaseClinicalField = "article_num"; // 全部文章
|
||
}
|
||
|
||
if (!caseClinicalArticleField.isEmpty()){
|
||
caseClinicalArticleDao.dec(Long.valueOf(articleId),caseClinicalArticleField,num);
|
||
}
|
||
statsCaseClinicalDao.dec(1L,statsCaseClinicalField,num);
|
||
|
||
return true;
|
||
} catch (Exception e) {
|
||
TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
|
||
return false;
|
||
}
|
||
}
|
||
|
||
// /**
|
||
// * 处理文章所属医院的数量统计
|
||
// */
|
||
// @Transactional
|
||
// public boolean handleHospitalStat(String doctorId,Long oldHospitalId,String newHospitalId){
|
||
// try {
|
||
// // 获取统计表医生数据
|
||
// LambdaQueryWrapper<StatsCaseClinicalDoctorModel> doctorQueryWrapper = new LambdaQueryWrapper<>();
|
||
// doctorQueryWrapper.eq(StatsCaseClinicalDoctorModel::getDoctorId, doctorId);
|
||
// StatsCaseClinicalDoctorModel statsCaseClinicalDoctor = statsCaseClinicalDoctorDao.selectOne(doctorQueryWrapper);
|
||
// if (statsCaseClinicalDoctor == null){
|
||
// return true;
|
||
// }
|
||
//
|
||
// // 获取旧医院数据
|
||
// StatsCaseClinicalHospitalModel statsCaseClinicalHospital = statsCaseClinicalHospitalDao.selectById(oldHospitalId);
|
||
// if (statsCaseClinicalHospital == null){
|
||
// throw new BusinessException("操作失败");
|
||
// }
|
||
//
|
||
// // 减少数-文章
|
||
// statsCaseClinicalHospitalDao.dec(oldHospitalId,"article_num",statsCaseClinicalDoctor.getArticleNum());
|
||
//
|
||
// // 减少阅读数-文章
|
||
// statsCaseClinicalHospitalDao.dec(oldHospitalId,"article_read_num",statsCaseClinicalDoctor.getArticleReadNum());
|
||
//
|
||
// // 减少收藏数-文章
|
||
// statsCaseClinicalHospitalDao.dec(oldHospitalId,"article_collect_num",statsCaseClinicalDoctor.getArticleCollectNum());
|
||
//
|
||
// // 减少评论数-文章
|
||
// statsCaseClinicalHospitalDao.dec(oldHospitalId,"article_comment_num",statsCaseClinicalDoctor.getArticleCommentNum());
|
||
//
|
||
// // 减少数-视频
|
||
// statsCaseClinicalHospitalDao.dec(oldHospitalId,"video_num",statsCaseClinicalDoctor.getVideoNum());
|
||
//
|
||
// // 减少阅读数-视频
|
||
// statsCaseClinicalHospitalDao.dec(oldHospitalId,"video_read_num",statsCaseClinicalDoctor.getVideoReadNum());
|
||
//
|
||
// // 减少收藏数-视频
|
||
// statsCaseClinicalHospitalDao.dec(oldHospitalId,"video_collect_num",statsCaseClinicalDoctor.getVideoCollectNum());
|
||
//
|
||
// // 减少评论数-视频
|
||
// statsCaseClinicalHospitalDao.dec(oldHospitalId,"video_comment_num",statsCaseClinicalDoctor.getVideoCommentNum());
|
||
//
|
||
// // 最后一篇文章发表时间
|
||
//
|
||
// } catch (Exception e) {
|
||
// TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
|
||
// return false;
|
||
// }
|
||
//
|
||
// return true;
|
||
//
|
||
// }
|
||
|
||
|
||
|
||
/**
|
||
* 检测作品状态
|
||
*/
|
||
public boolean checkClinicalArticleStatus(CaseClinicalArticleModel caseClinicalArticle){
|
||
// 删除状态
|
||
if (caseClinicalArticle.getDeleteStatus() == 1){
|
||
return false;
|
||
}
|
||
|
||
// 状态(1:正常 2:禁用)
|
||
if (caseClinicalArticle.getArticleStatus() == 2){
|
||
return false;
|
||
}
|
||
|
||
return true;
|
||
}
|
||
}
|