Files
nuttyreading-java/src/main/java/com/peanut/modules/book/controller/BookTeachLikeAndCommentController.java
2023-12-04 17:55:16 +08:00

202 lines
7.3 KiB
Java

package com.peanut.modules.book.controller;
import com.alibaba.druid.sql.visitor.functions.Instr;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.peanut.common.utils.Query;
import com.peanut.common.utils.R;
import com.peanut.common.utils.ShiroUtils;
import com.peanut.modules.book.entity.BookTeachCommentEntity;
import com.peanut.modules.book.entity.BookTeachLikeEntity;
import com.peanut.modules.book.entity.MyUserEntity;
import com.peanut.modules.book.service.BookTeachCommentService;
import com.peanut.modules.book.service.BookTeachLikeService;
import com.peanut.modules.book.service.MyUserService;
import com.peanut.modules.sys.entity.SysSensitiveWords;
import com.peanut.modules.sys.service.SysSensitiveWordsService;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@Slf4j
@RestController
@RequestMapping("book/teach")
public class BookTeachLikeAndCommentController {
@Autowired
private BookTeachLikeService likeService;
@Autowired
private BookTeachCommentService commentService;
@Autowired
private MyUserService userService;
@Autowired
private SysSensitiveWordsService sensitiveWordsService;
/**
* 获取点赞数量
*/
@RequestMapping("/getLikeCount")
public R getLikeCount(int teachId) {
LambdaQueryWrapper<BookTeachLikeEntity> wrapper = new LambdaQueryWrapper<>();
wrapper.eq(BookTeachLikeEntity::getDelFlag,0);
wrapper.eq(BookTeachLikeEntity::getTeachId,teachId);
int count = likeService.count(wrapper);
return R.ok().put("count", count);
}
/**
* 添加或取消点赞
*/
@RequestMapping("/addOrCancelLike")
public R addOrCancelLike(int teachId) {
R r = new R();
LambdaQueryWrapper<BookTeachLikeEntity> wrapper = new LambdaQueryWrapper<>();
wrapper.eq(BookTeachLikeEntity::getTeachId,teachId);
wrapper.eq(BookTeachLikeEntity::getUserId, ShiroUtils.getUId());
BookTeachLikeEntity like = likeService.getOne(wrapper);
if (like == null){
like = new BookTeachLikeEntity();
like.setTeachId(teachId);
like.setUserId(ShiroUtils.getUId());
r.put("msg","点赞成功");
}else {
if (0==like.getDelFlag()){
like.setDelFlag(1);
r.put("msg","取消成功");
}else {
like.setDelFlag(0);
r.put("msg","点赞成功");
}
}
likeService.saveOrUpdate(like);
return r;
}
/**
* 本用户当前目录是否点赞
*/
@RequestMapping("/ifLike")
public R ifLike(int teachId) {
boolean flag ;
LambdaQueryWrapper<BookTeachLikeEntity> wrapper = new LambdaQueryWrapper<>();
wrapper.eq(BookTeachLikeEntity::getDelFlag,0);
wrapper.eq(BookTeachLikeEntity::getTeachId,teachId);
wrapper.eq(BookTeachLikeEntity::getUserId,ShiroUtils.getUId());
BookTeachLikeEntity likeEntity = likeService.getOne(wrapper);
if (likeEntity != null){
flag = true;
}else {
flag = false;
}
return R.ok().put("flag",flag);
}
/**
* 获取评论列表
*/
@RequestMapping("/getCommentList")
public R getCommentList(@RequestBody Map<String, Object> params) {
MPJLambdaWrapper<BookTeachCommentEntity> wrapper = new MPJLambdaWrapper<>();
wrapper.eq(BookTeachCommentEntity::getTeachId,params.get("teachId"));
wrapper.eq(BookTeachCommentEntity::getParentId,0);
wrapper.eq(BookTeachCommentEntity::getDelFlag,0);
IPage<BookTeachCommentEntity> page = commentService.page(
new Query<BookTeachCommentEntity>().getPage(params),wrapper);
//返回体添加子评论和用户信息
if (page.getRecords().size() > 0){
for (BookTeachCommentEntity comment:page.getRecords()){
getSonComment(comment);
}
}
return R.ok().put("page", page);
}
/**
* 获取全部评论列表
*/
@RequestMapping("/getAllCommentList")
public R getAllCommentList(@RequestBody Map<String, Object> params) {
MPJLambdaWrapper<BookTeachCommentEntity> wrapper = new MPJLambdaWrapper<>();
wrapper.eq(BookTeachCommentEntity::getDelFlag,0);
IPage<BookTeachCommentEntity> page = commentService.page(
new Query<BookTeachCommentEntity>().getPage(params),wrapper);
return R.ok().put("page", page);
}
/**
* 根据内容查询评论
*/
@RequestMapping("/getCommentByContent")
public R getCommentByContent(@RequestBody Map<String, Object> params) {
MPJLambdaWrapper<BookTeachCommentEntity> wrapper = new MPJLambdaWrapper<>();
wrapper.like(BookTeachCommentEntity::getContent,params.get("content"));
IPage<BookTeachCommentEntity> page = commentService.page(
new Query<BookTeachCommentEntity>().getPage(params),wrapper);
return R.ok().put("page", page);
}
//递归查询子评论
private void getSonComment(BookTeachCommentEntity comment){
LambdaQueryWrapper<BookTeachCommentEntity> w = new LambdaQueryWrapper();
w.eq(BookTeachCommentEntity::getParentId,comment.getId());
w.eq(BookTeachCommentEntity::getDelFlag,0);
List<BookTeachCommentEntity> list = commentService.list(w);
if (list.size() > 0){
for (BookTeachCommentEntity c : list){
getSonComment(c);
}
}
comment.setComments(list);
comment.setUser(userService.getById(comment.getUserId()));
}
/**
* 添加修改评论
*/
@RequestMapping("/addComment")
public R addComment(@RequestBody BookTeachCommentEntity comment) {
R result = new R();
comment.setUserId(ShiroUtils.getUId());
LambdaQueryWrapper<SysSensitiveWords> wrapper = new LambdaQueryWrapper<>();
wrapper.last(" where instr(\""+comment.getContent()+"\",word)>0");
List<SysSensitiveWords> list = sensitiveWordsService.list(wrapper);
if (list.size() > 0){
String str = "";
for (SysSensitiveWords words:list) {
if ("".equals(str)){
str = words.getWord();
}else {
str = str + "" + words.getWord();
}
}
result.put("tip","您的评论含有敏感词:"+str+",请重新输入。");
}else {
commentService.saveOrUpdate(comment);
result.put("tip","评论成功!");
}
return result;
}
/**
* 删除评论
*/
@RequestMapping("/delComment")
public R delComment(int commentId) {
BookTeachCommentEntity comment = commentService.getById(commentId);
comment.setDelFlag(1);
commentService.updateById(comment);
return R.ok();
}
}