566 lines
20 KiB
Java
566 lines
20 KiB
Java
package com.example.caseData.service;
|
||
|
||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||
import com.example.caseData.dao.*;
|
||
import com.example.caseData.exception.BusinessException;
|
||
import com.example.caseData.extend.app.Base;
|
||
import com.example.caseData.model.*;
|
||
import com.example.caseData.model.UserCollectClinicalVideoModel;
|
||
import com.example.caseData.request.CaseClinicalVideoRequest.addClinicalVideoComment;
|
||
import com.example.caseData.utils.Replace;
|
||
import jakarta.annotation.Resource;
|
||
import jakarta.servlet.http.HttpServletRequest;
|
||
import org.apache.commons.io.IOUtils;
|
||
import org.springframework.stereotype.Service;
|
||
import org.springframework.transaction.annotation.Transactional;
|
||
import org.springframework.transaction.interceptor.TransactionAspectSupport;
|
||
|
||
import java.lang.reflect.Field;
|
||
import java.nio.charset.StandardCharsets;
|
||
import java.util.List;
|
||
import java.util.Map;
|
||
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 CaseClinicalVideoService {
|
||
@Resource
|
||
private UserCollectClinicalVideoDao userCollectClinicalVideoDao;
|
||
|
||
@Resource
|
||
private CaseClinicalVideoDao caseClinicalVideoDao;
|
||
|
||
@Resource
|
||
private StatsCaseClinicalDao statsCaseClinicalDao;
|
||
|
||
@Resource
|
||
private UserDao userDao;
|
||
|
||
@Resource
|
||
private StatsCaseClinicalHospitalDao statsCaseClinicalHospitalDao;
|
||
|
||
@Resource
|
||
private StatsCaseClinicalDoctorDao statsCaseClinicalDoctorDao;
|
||
|
||
@Resource
|
||
private CaseClinicalVideoAuthorDao caseClinicalVideoAuthorDao;
|
||
|
||
@Resource
|
||
private CaseClinicalDoctorDao caseClinicalDoctorDao;
|
||
|
||
@Resource
|
||
private BasicSensitiveWordService basicSensitiveWordService;
|
||
|
||
@Resource
|
||
private UserCommentClinicalVideoDao userCommentClinicalVideoDao;
|
||
|
||
/**
|
||
* 新增收藏-临床病例库-视频
|
||
* @param videoId 视频id
|
||
* @param userId 用户id
|
||
* @return bool
|
||
*/
|
||
@Transactional
|
||
public boolean AddClinicalVideoCollect(String videoId,String userId){
|
||
// 检测用户是否已收藏过
|
||
UserCollectClinicalVideoModel userCollectClinicalVideo = getUserCollectClinicalVideoStatus(videoId,userId);
|
||
if (userCollectClinicalVideo != null) {
|
||
return true;
|
||
}
|
||
|
||
// 新增收藏
|
||
UserCollectClinicalVideoModel userCollectClinicalVideoData = new UserCollectClinicalVideoModel();
|
||
userCollectClinicalVideoData.setUserId(Long.valueOf(userId));
|
||
userCollectClinicalVideoData.setVideoId(Long.valueOf(videoId));
|
||
int res = userCollectClinicalVideoDao.insert(userCollectClinicalVideoData);
|
||
if (res <= 0){
|
||
return false;
|
||
}
|
||
|
||
// 新增统计字段
|
||
boolean r = IncClinicalVideoStats(videoId,2);
|
||
if (!r){
|
||
TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
|
||
return false;
|
||
}
|
||
|
||
return true;
|
||
}
|
||
|
||
/**
|
||
* 取消收藏-临床病例库-视频
|
||
* @param videoId 视频id
|
||
* @param userId 用户id
|
||
* @return bool
|
||
*/
|
||
@Transactional
|
||
public boolean DeleteClinicalVideoCollect(String videoId,String userId){
|
||
// 检测用户是否已收藏过
|
||
UserCollectClinicalVideoModel userCollectClinicalVideo = getUserCollectClinicalVideoStatus(videoId,userId);
|
||
if (userCollectClinicalVideo == null) {
|
||
return true;
|
||
}
|
||
|
||
// 删除收藏
|
||
int res = userCollectClinicalVideoDao.deleteById(userCollectClinicalVideo.getCollectId());
|
||
if (res <= 0){
|
||
TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
|
||
return false;
|
||
}
|
||
|
||
// 减少视频的统计字段
|
||
boolean r = DecClinicalVideoStats(videoId,2);
|
||
if (!r){
|
||
TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
|
||
return false;
|
||
}
|
||
|
||
return true;
|
||
}
|
||
|
||
/**
|
||
* 新增评论-临床病例库-视频
|
||
* @param videoId 视频id
|
||
* @param userId 用户id
|
||
* @return bool
|
||
*/
|
||
@Transactional
|
||
public boolean AddClinicalVideoComment(String videoId, String userId, addClinicalVideoComment request){
|
||
// 获取视频数据
|
||
CaseClinicalVideoModel video = caseClinicalVideoDao.selectById(videoId);
|
||
if (video == null) {
|
||
throw new BusinessException("非法视频");
|
||
}
|
||
|
||
if (video.getVideoStatus() != 1){
|
||
throw new BusinessException("非法视频");
|
||
}
|
||
|
||
// 处理评论内容
|
||
BasicSensitiveWordService.FilterResult result = basicSensitiveWordService.filter(request.getContent());
|
||
if (result.hasSensitive == 1){
|
||
throw new BusinessException("存在敏感词,请修改后提交");
|
||
}
|
||
|
||
// 新增评论
|
||
UserCommentClinicalVideoModel userCommentClinicalVideoData = new UserCommentClinicalVideoModel();
|
||
userCommentClinicalVideoData.setUserId(Long.valueOf(userId));
|
||
userCommentClinicalVideoData.setVideoId(Long.valueOf(videoId));
|
||
userCommentClinicalVideoData.setStatus(1);
|
||
userCommentClinicalVideoData.setIsSensitive(0);
|
||
userCommentClinicalVideoData.setContent(request.getContent());
|
||
userCommentClinicalVideoData.setCommentImage(Replace.removeOssDomain(request.getCommentImage()));
|
||
|
||
// 评论根id
|
||
if (request.getRootId() != null) {
|
||
userCommentClinicalVideoData.setRootId(Long.valueOf(request.getRootId()));
|
||
}
|
||
|
||
// 评论父级id
|
||
if (request.getParentId() != null) {
|
||
userCommentClinicalVideoData.setParentId(Long.valueOf(request.getParentId()));
|
||
}
|
||
|
||
int res = userCommentClinicalVideoDao.insert(userCommentClinicalVideoData);
|
||
if (res <= 0){
|
||
return false;
|
||
}
|
||
|
||
// 新增文章的统计字段
|
||
boolean r = IncClinicalVideoStats(videoId,3);
|
||
if (!r){
|
||
TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
|
||
return false;
|
||
}
|
||
|
||
return true;
|
||
}
|
||
|
||
/**
|
||
* 删除评论-临床病例库-视频
|
||
* @param commentId 评论id
|
||
* @param userId 用户id
|
||
* @return bool
|
||
*/
|
||
@Transactional
|
||
public boolean DeleteClinicalVideoComment(String commentId, String userId){
|
||
// 获取评论数据
|
||
UserCommentClinicalVideoModel comment = userCommentClinicalVideoDao.selectById(commentId);
|
||
if (comment == null) {
|
||
return false;
|
||
}
|
||
|
||
// 获取视频数据
|
||
CaseClinicalVideoModel video = caseClinicalVideoDao.selectById(comment.getVideoId());
|
||
if (video == null) {
|
||
return false;
|
||
}
|
||
|
||
if (video.getVideoStatus() != 1){
|
||
return false;
|
||
}
|
||
|
||
// 检测用户是否视频作者
|
||
boolean isAuthor = checkUserIsVideoAuthor(String.valueOf(video.getVideoId()),userId);
|
||
if (!isAuthor) {
|
||
return false;
|
||
}
|
||
|
||
// 删除评论
|
||
int res = userCommentClinicalVideoDao.deleteById(comment.getCommentId());
|
||
if (res <= 0){
|
||
TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
|
||
return false;
|
||
}
|
||
|
||
// 减少文章的统计字段
|
||
boolean r = DecClinicalVideoStats(String.valueOf(comment.getVideoId()),2);
|
||
if (!r){
|
||
TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
|
||
return false;
|
||
}
|
||
|
||
return true;
|
||
}
|
||
|
||
/**
|
||
* 置顶评论-临床病例库-视频
|
||
* @param commentId 评论id
|
||
* @param userId 用户id
|
||
* @return bool
|
||
*/
|
||
@Transactional
|
||
public boolean AddTopClinicalVideoComment(String commentId, String userId){
|
||
// 获取评论数据
|
||
UserCommentClinicalVideoModel comment = userCommentClinicalVideoDao.selectById(commentId);
|
||
if (comment == null) {
|
||
return false;
|
||
}
|
||
|
||
// 获取视频数据
|
||
CaseClinicalVideoModel video = caseClinicalVideoDao.selectById(comment.getVideoId());
|
||
if (video == null) {
|
||
return false;
|
||
}
|
||
|
||
if (video.getVideoStatus() != 1){
|
||
return false;
|
||
}
|
||
|
||
// 检测用户是否视频作者
|
||
boolean isAuthor = checkUserIsVideoAuthor(String.valueOf(comment.getVideoId()),userId);
|
||
if (!isAuthor) {
|
||
return false;
|
||
}
|
||
|
||
if (comment.getIsTop() == 1){
|
||
return true;
|
||
}
|
||
|
||
// 置顶评论
|
||
comment.setIsTop(1);
|
||
userCommentClinicalVideoDao.updateById(comment);
|
||
|
||
return true;
|
||
}
|
||
|
||
/**
|
||
* 取消置顶评论-临床病例库-视频
|
||
* @param commentId 评论id
|
||
* @param userId 用户id
|
||
* @return bool
|
||
*/
|
||
@Transactional
|
||
public boolean deleteTopClinicalVideoComment(String commentId, String userId){
|
||
// 获取评论数据
|
||
UserCommentClinicalVideoModel comment = userCommentClinicalVideoDao.selectById(commentId);
|
||
if (comment == null) {
|
||
return false;
|
||
}
|
||
|
||
// 获取视频数据
|
||
CaseClinicalVideoModel video = caseClinicalVideoDao.selectById(comment.getVideoId());
|
||
if (video == null) {
|
||
return false;
|
||
}
|
||
|
||
if (video.getVideoStatus() != 1){
|
||
return false;
|
||
}
|
||
|
||
// 检测用户是否视频作者
|
||
boolean isAuthor = checkUserIsVideoAuthor(String.valueOf(comment.getVideoId()),userId);
|
||
if (!isAuthor) {
|
||
return false;
|
||
}
|
||
|
||
// 取消置顶评论
|
||
if (comment.getIsTop() == 0){
|
||
return true;
|
||
}
|
||
|
||
// 置顶评论
|
||
comment.setIsTop(0);
|
||
userCommentClinicalVideoDao.updateById(comment);
|
||
|
||
return true;
|
||
}
|
||
|
||
/**
|
||
* 检测用户是否视频作者
|
||
* @param videoId 视频id
|
||
* @param userId 用户id
|
||
* @return bool 是否作者
|
||
*/
|
||
public boolean checkUserIsVideoAuthor(String videoId,String userId){
|
||
// 获取当前登录用户数据
|
||
UserModel user = userDao.selectById(Long.valueOf(userId));
|
||
if (user == null) {
|
||
return false;
|
||
}
|
||
|
||
// 当前用户是否是作者之一
|
||
boolean isAuthor = false;
|
||
|
||
// 获取视频作者数据
|
||
LambdaQueryWrapper<CaseClinicalVideoAuthorModel> authorQueryWrapper = new LambdaQueryWrapper<>();
|
||
authorQueryWrapper.eq(CaseClinicalVideoAuthorModel::getVideoId, videoId);
|
||
List<CaseClinicalVideoAuthorModel> caseClinicalVideoAuthors = caseClinicalVideoAuthorDao.selectList(authorQueryWrapper);
|
||
for (CaseClinicalVideoAuthorModel author : caseClinicalVideoAuthors) {
|
||
// 查询医生
|
||
CaseClinicalDoctorModel caseClinicalDoctor = caseClinicalDoctorDao.selectById(author.getDoctorId());
|
||
if (caseClinicalDoctor == null) {
|
||
return false;
|
||
}
|
||
|
||
if (Objects.equals(caseClinicalDoctor.getDoctorIden(), user.getUserIden())){
|
||
isAuthor = true;
|
||
}
|
||
}
|
||
|
||
return isAuthor;
|
||
}
|
||
|
||
/**
|
||
* 获取用户收藏数据
|
||
* @param videoId 视频id
|
||
* @param userId 用户id
|
||
* @return bool false:未收藏 true:已收藏
|
||
*/
|
||
public UserCollectClinicalVideoModel getUserCollectClinicalVideoStatus(String videoId, String userId){
|
||
// 检测用户是否已收藏过
|
||
LambdaQueryWrapper<UserCollectClinicalVideoModel> mapQueryWrapper = new LambdaQueryWrapper<>();
|
||
mapQueryWrapper.eq(UserCollectClinicalVideoModel::getUserId, userId);
|
||
mapQueryWrapper.eq(UserCollectClinicalVideoModel::getVideoId, videoId);
|
||
|
||
return userCollectClinicalVideoDao.selectOne(mapQueryWrapper);
|
||
}
|
||
|
||
/**
|
||
* 新增视频的统计字段
|
||
* @param videoId 视频id
|
||
* @param type 类型:1:阅读量 2:收藏量 3:评论数
|
||
*/
|
||
@Transactional
|
||
public boolean IncClinicalVideoStats(String videoId,Integer type){
|
||
try {
|
||
// 获取文章作者
|
||
LambdaQueryWrapper<CaseClinicalVideoAuthorModel> authorQueryWrapper = new LambdaQueryWrapper<>();
|
||
authorQueryWrapper.eq(CaseClinicalVideoAuthorModel::getVideoId, videoId);
|
||
List<CaseClinicalVideoAuthorModel> caseClinicalVideoAuthors = caseClinicalVideoAuthorDao.selectList(authorQueryWrapper);
|
||
|
||
// 阅读量
|
||
if (type == 1){
|
||
caseClinicalVideoDao.inc(Long.valueOf(videoId),"read_num",1);
|
||
statsCaseClinicalDao.inc(1L,"video_read_num",1);
|
||
|
||
for (CaseClinicalVideoAuthorModel author : caseClinicalVideoAuthors) {
|
||
// 查询医生
|
||
CaseClinicalDoctorModel caseClinicalDoctor = caseClinicalDoctorDao.selectById(author.getDoctorId());
|
||
|
||
// 增加医院文章收藏数
|
||
statsCaseClinicalHospitalDao.inc(caseClinicalDoctor.getHospitalId(),"video_read_num",1);
|
||
|
||
// 增加医生文章收藏数
|
||
statsCaseClinicalDoctorDao.inc(caseClinicalDoctor.getDoctorId(),"video_read_num",1);
|
||
}
|
||
}
|
||
|
||
// 收藏量
|
||
if (type == 2){
|
||
caseClinicalVideoDao.inc(Long.valueOf(videoId),"collect_num",1);
|
||
statsCaseClinicalDao.inc(1L,"video_collect_num",1);
|
||
|
||
for (CaseClinicalVideoAuthorModel author : caseClinicalVideoAuthors) {
|
||
// 查询医生
|
||
CaseClinicalDoctorModel caseClinicalDoctor = caseClinicalDoctorDao.selectById(author.getDoctorId());
|
||
|
||
// 增加医院文章收藏数
|
||
statsCaseClinicalHospitalDao.inc(caseClinicalDoctor.getHospitalId(),"video_collect_num",1);
|
||
|
||
// 增加医生文章收藏数
|
||
statsCaseClinicalDoctorDao.inc(caseClinicalDoctor.getDoctorId(),"video_collect_num",1);
|
||
}
|
||
}
|
||
|
||
// 评论数
|
||
if (type == 3){
|
||
caseClinicalVideoDao.inc(Long.valueOf(videoId),"comment_num",1);
|
||
statsCaseClinicalDao.inc(1L,"video_comment_num",1);
|
||
|
||
for (CaseClinicalVideoAuthorModel author : caseClinicalVideoAuthors) {
|
||
// 查询医生
|
||
CaseClinicalDoctorModel caseClinicalDoctor = caseClinicalDoctorDao.selectById(author.getDoctorId());
|
||
|
||
// 增加医院文章收藏数
|
||
statsCaseClinicalHospitalDao.inc(caseClinicalDoctor.getHospitalId(),"video_comment_num",1);
|
||
|
||
// 增加医生文章收藏数
|
||
statsCaseClinicalDoctorDao.inc(caseClinicalDoctor.getDoctorId(),"video_comment_num",1);
|
||
}
|
||
}
|
||
|
||
return true;
|
||
} catch (Exception e) {
|
||
return false;
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 减少视频的统计字段
|
||
* @param videoId 文章id
|
||
* @param type 类型:1:阅读量 2:收藏量 3:评论数
|
||
*/
|
||
@Transactional
|
||
public boolean DecClinicalVideoStats(String videoId,Integer type){
|
||
try {
|
||
String caseClinicalVideoField = ""; // 具体文章
|
||
String statsCaseClinicalField = ""; // 全部文章
|
||
String statsCaseClinicalHospitalField = ""; // 医院
|
||
String statsCaseClinicalDoctorField = ""; // 医生
|
||
|
||
// 阅读
|
||
if (type == 1){
|
||
caseClinicalVideoField = "read_num"; // 具体文章
|
||
statsCaseClinicalField = "video_read_num"; // 全部文章
|
||
statsCaseClinicalHospitalField = "video_read_num"; // 医院
|
||
statsCaseClinicalDoctorField = "video_read_num"; // 医生
|
||
}
|
||
|
||
// 收藏
|
||
if (type == 2){
|
||
caseClinicalVideoField = "collect_num"; // 具体文章
|
||
statsCaseClinicalField = "video_collect_num"; // 全部文章
|
||
statsCaseClinicalHospitalField = "video_collect_num"; // 医院
|
||
statsCaseClinicalDoctorField = "video_collect_num"; // 医生
|
||
}
|
||
|
||
// 评论
|
||
if (type == 3){
|
||
caseClinicalVideoField = "comment_num"; // 具体文章
|
||
statsCaseClinicalField = "video_comment_num"; // 全部文章
|
||
statsCaseClinicalHospitalField = "video_comment_num"; // 医院
|
||
statsCaseClinicalDoctorField = "video_comment_num"; // 医生
|
||
}
|
||
|
||
caseClinicalVideoDao.dec(Long.valueOf(videoId),caseClinicalVideoField,1);
|
||
statsCaseClinicalDao.dec(1L,statsCaseClinicalField,1);
|
||
|
||
// 获取文章作者
|
||
LambdaQueryWrapper<CaseClinicalVideoAuthorModel> authorQueryWrapper = new LambdaQueryWrapper<>();
|
||
authorQueryWrapper.eq(CaseClinicalVideoAuthorModel::getVideoId, videoId);
|
||
List<CaseClinicalVideoAuthorModel> caseClinicalVideoAuthors = caseClinicalVideoAuthorDao.selectList(authorQueryWrapper);
|
||
for (CaseClinicalVideoAuthorModel author : caseClinicalVideoAuthors) {
|
||
// 查询医生
|
||
CaseClinicalDoctorModel caseClinicalDoctor = caseClinicalDoctorDao.selectById(author.getDoctorId());
|
||
|
||
// 获取医院统计数据
|
||
statsCaseClinicalHospitalDao.dec(caseClinicalDoctor.getHospitalId(),statsCaseClinicalHospitalField,1);
|
||
statsCaseClinicalDoctorDao.dec(caseClinicalDoctor.getDoctorId(),statsCaseClinicalDoctorField,1);
|
||
}
|
||
|
||
return true;
|
||
} catch (Exception e) {
|
||
TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
|
||
return false;
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 检测作品状态
|
||
*/
|
||
public boolean checkClinicalVideoStatus(CaseClinicalVideoModel caseClinicalVideo){
|
||
// 删除状态
|
||
if (caseClinicalVideo.getDeleteStatus() == 1){
|
||
return false;
|
||
}
|
||
|
||
// 状态(1:正常 2:禁用)
|
||
if (caseClinicalVideo.getVideoStatus() == 2){
|
||
return false;
|
||
}
|
||
|
||
return true;
|
||
}
|
||
|
||
/**
|
||
* 临床病例库-视频-同步app视频
|
||
* @param videoId 视频id
|
||
* @param userId 用户id
|
||
* @return bool
|
||
*/
|
||
@Transactional
|
||
public boolean AddClinicalVideoApp(String videoId, String userId, addClinicalVideoComment request){
|
||
// 获取视频数据
|
||
CaseClinicalVideoModel video = caseClinicalVideoDao.selectById(videoId);
|
||
if (video == null) {
|
||
throw new BusinessException("非法视频");
|
||
}
|
||
|
||
if (video.getVideoStatus() != 1){
|
||
throw new BusinessException("非法视频");
|
||
}
|
||
|
||
// 处理评论内容
|
||
BasicSensitiveWordService.FilterResult result = basicSensitiveWordService.filter(request.getContent());
|
||
if (result.hasSensitive == 1){
|
||
throw new BusinessException("存在敏感词,请修改后提交");
|
||
}
|
||
|
||
// 新增评论
|
||
UserCommentClinicalVideoModel userCommentClinicalVideoData = new UserCommentClinicalVideoModel();
|
||
userCommentClinicalVideoData.setUserId(Long.valueOf(userId));
|
||
userCommentClinicalVideoData.setVideoId(Long.valueOf(videoId));
|
||
userCommentClinicalVideoData.setStatus(1);
|
||
userCommentClinicalVideoData.setIsSensitive(0);
|
||
userCommentClinicalVideoData.setContent(request.getContent());
|
||
userCommentClinicalVideoData.setCommentImage(Replace.removeOssDomain(request.getCommentImage()));
|
||
|
||
// 评论根id
|
||
if (request.getRootId() != null) {
|
||
userCommentClinicalVideoData.setRootId(Long.valueOf(request.getRootId()));
|
||
}
|
||
|
||
// 评论父级id
|
||
if (request.getParentId() != null) {
|
||
userCommentClinicalVideoData.setParentId(Long.valueOf(request.getParentId()));
|
||
}
|
||
|
||
int res = userCommentClinicalVideoDao.insert(userCommentClinicalVideoData);
|
||
if (res <= 0){
|
||
return false;
|
||
}
|
||
|
||
// 新增文章的统计字段
|
||
boolean r = IncClinicalVideoStats(videoId,3);
|
||
if (!r){
|
||
TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
|
||
return false;
|
||
}
|
||
|
||
return true;
|
||
}
|
||
}
|