package com.liuyanzhao.ssm.blog.controller.admin;
import cn.hutool.http.HtmlUtil;
import com.github.pagehelper.PageInfo;
import com.liuyanzhao.ssm.blog.entity.Article;
import com.liuyanzhao.ssm.blog.entity.Comment;
import com.liuyanzhao.ssm.blog.entity.User;
import com.liuyanzhao.ssm.blog.enums.ArticleStatus;
import com.liuyanzhao.ssm.blog.enums.Role;
import com.liuyanzhao.ssm.blog.enums.UserRole;
import com.liuyanzhao.ssm.blog.service.ArticleService;
import com.liuyanzhao.ssm.blog.service.CommentService;
import com.liuyanzhao.ssm.blog.util.MyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;
@Controller
@RequestMapping("/admin/comment")
public class BackConn {
//声明对象,应该是自动传值,等一下需要传值
@Autowired
private CommentService commentService;
@Autowired
private ArticleService articleService;
//评论页面
@RequestMapping(value = "")
//设置参数 传入会话,等一下好操作
public String commentList(@RequestParam(required = false,defaultValue = "1")Integer pageIndex,
@RequestParam(required = false,defaultValue = "10")Integer pageSize,
HttpSession session,
Model model){
//获取对象
User user = (User) session.getAttribute("user");
//创造评论列表
HashMapcriteria=new HashMap<>();
//判断是管理员还是普通用户
if(!UserRole.ADMIN.getValue().equals(user.getUserRole())){
criteria.put("userId",user.getUserId());
}
//获取初始评论,评论的总数,用户的角色
PageInfo commentPageInfo = commentService.listCommentByPage(pageIndex,pageSize,criteria);
//载入视图
model.addAttribute("pageInfo",commentPageInfo);
model.addAttribute("pageUrlPrefix","/admin/comment?pageIndex");
return "Admin/Comment/index";
}
//添加评论
@RequestMapping(value = "/insert",method = {RequestMethod.POST},produces = {"text/plain;charset=UTF-8"})
@ResponseBody
public void insertComment(HttpServletRequest request,Comment comment,HttpSession session){
User user = (User) session.getAttribute("user");
//获得评论的文章
Article article = articleService.getArticleByStatusAndId(null, comment.getCommentArticleId());
if(article == null){
return;
}
//添加评论进文章中 添加用户id
comment.setCommentUserId(user.getUserId());
//通过常熟类添加ip
comment.setCommentIp(MyUtils.getIpAddr(request));
//添加修改时间
comment.setCommentCreateTime(new Date());
//拿过来修改后重新插入
commentService.insertComment(comment);
//更新文章评论数
articleService.updateCommentCount(article.getArticleId());
}
//删除评论
@RequestMapping(value = "/delete/{id}")
//为什么要传入会话
public void delectComment(@PathVariable("id") Integer id,HttpSession session){
//获取该评论
Comment comment = commentService.getCommentById(id);
//判断有没有权限 所以创造一个user传入会话的user的值
User user = (User) session.getAttribute("user");
//判断
if(user.getUserRole()!=UserRole.ADMIN.getValue()&&user.getUserId()!=comment.getCommentUserId()){
return;
}
//删除评论
commentService.deleteComment(id);
//删除子评论
ListchileCommentList=commentService.listChildComment(id);
for(int i=0;i< chileCommentList.size();i++){
//逐条删除
commentService.deleteComment(chileCommentList.get(i).getCommentId());
}
//更新评论
Article article = articleService.getArticleByStatusAndId(null, comment.getCommentArticleId());
//调用更新serv
articleService.updateCommentCount(article.getArticleId());
}
//编辑评论页面显示
@RequestMapping(value = "/edit/{id}")
public String editCommentView(@PathVariable("id") Integer id,Model model,HttpSession session){
//判断是否是管理员
User user = (User) session.getAttribute("user");
if(!Objects.equals(user.getUserRole(),UserRole.ADMIN.getValue())){
return "redirect:/403";
}
//不然就获取该评论,调用删除服务
Comment comment = commentService.getCommentById(id);
model.addAttribute("comment",comment);
return "Admin/Comment/edit";
}
//编辑评论提交
@RequestMapping(value = "/editSubmit",method = RequestMethod.POST)
public String editCommentSubmit(Comment comment,HttpSession session){
User user = (User) session.getAttribute("user");
if(!Objects.equals(user.getUserRole(),UserRole.ADMIN.getValue())){
return "redirect:/403";
}
commentService.updateComment(comment);
return "redirect:/admin/comment";
}
//回复评论页面
@RequestMapping(value = "/reply/{id}")
//传入id和视图
public String replyCommentView(@PathVariable("id") Integer id,Model model){
//获取评论
Comment comment = commentService.getCommentById(id);
//增加评论
model.addAttribute("comment",comment);
return "Admin/Comment/reply";
}
//回复评论提交
@RequestMapping(value = "/replySubmit",method = RequestMethod.POST)
//传入请求(用于设置评论发布地址) 评论 会话
public String replyCommentSubmit(HttpServletRequest request,Comment comment,HttpSession session){
//获取当前文章
Article article = articleService.getArticleByStatusAndId(ArticleStatus.PUBLISH.getValue(), comment.getCommentArticleId());
if(article==null){
return "redirect:/404";
}
//获取对象,更新文章
User user = (User) session.getAttribute("user");
//HtmlUtil.escape防止有些同学恶意输入
comment.setCommentContent(HtmlUtil.escape(comment.getCommentContent()));
//获取该输入人的昵称
comment.setCommentAuthorName(user.getUserNickname());
comment.setCommentAuthorEmail(user.getUserEmail());
//获取发布位置
comment.setCommentAuthorUrl(user.getUserUrl());
//文章字数
article.setArticleCommentCount(article.getArticleCommentCount()+1);
//键入文章
articleService.updateArticle(article);
//添加评论 设置时间
comment.setCommentCreateTime(new Date());
//设置地址
comment.setCommentIp(MyUtils.getIpAddr(request));
if(Objects.equals(user.getUserId(),article.getArticleUserId())){
//如果是作者
comment.setCommentRole(Role.OWNER.getValue());
}else{
comment.setCommentRole(Role.VISITOR.getValue());
}
//编辑好后会话调用服务
commentService.insertComment(comment);
return "redirect:/admin/comment";
}
}