package com.controller; import java.util.Date; import java.util.List; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import com.alibaba.fastjson.JSONObject; import com.entity.Comment; import com.entity.User; import com.github.pagehelper.PageInfo; import com.service.ICommentService; import com.service.IMovieService; import com.service.IUserService; /** * 评论管理模块 * 该控制器负责处理与评论相关的操作,包括查询、分页查询等。 * @author Wxj */ @Controller @RequestMapping("/comment") public class CommentController { // 注入评论服务 @Resource private ICommentService commentService; // 注入用户服务 @Resource private IUserService userService; // 注入电影服务(尽管在本类中暂时未用到) @Resource private IMovieService movieService; /** * 查询所有评论 * 该方法会返回所有的评论列表,并将每条评论对应的用户信息也一起查询出来。 * @return JSONObject 包含评论数据、总数和其他信息的响应体 */ @RequestMapping("findAllComments") @ResponseBody public JSONObject findAllComments() { JSONObject obj = new JSONObject(); // 获取所有评论列表 List list = commentService.findAllComments(); // 为每条评论添加用户信息 for(Comment comment: list) { comment.setComment_user(userService.findUserById(comment.getUser_id())); } // 构建返回的JSON对象,包含代码、消息、评论数量和数据 obj.put("code", 0); // 状态码,0表示成功 obj.put("msg", ""); // 返回消息,这里为空 obj.put("count", list.size()); // 评论总数 obj.put("data", list); // 评论数据 return obj; } /** * 分页查询评论 * 该方法通过分页的方式查询评论,可以根据关键词进行筛选。 * @param page 当前页码 * @param limit 每页条数 * @param keyword 可选的查询关键词(用于评论内容或其他字段的模糊查询) * @return JSONObject 包含评论数据、总数和其他信息的响应体 */ @RequestMapping("findAllCommentsPage") @ResponseBody public JSONObject findAllCommentsPage(@RequestParam(value="page",defaultValue="1")Integer page, @RequestParam(value="limit",defaultValue="10")Integer limit, String keyword) { // 使用分页服务获取评论数据 PageInfo info = commentService.findAllCommentsBySplitPage(page, limit, keyword); // 为每条评论添加用户信息 for(Comment comment : info.getList()) { comment.setComment_user(userService.findUserById(comment.getUser_id())); } // 构建返回的JSON对象,包含代码、消息、评论总数和分页数据 JSONObject obj = new JSONObject(); obj.put("code", 0); // 状态码,0表示成功 obj.put("msg", ""); // 返回消息,这里为空 obj.put("count", info.getTotal()); // 评论总数 obj.put("data", info.getList()); // 当前页的评论数据 return obj; } } @RequestMapping("addCommentByUser") @ResponseBody public JSONObject addCommentByUser(@RequestParam("movie_id")long movie_id,@RequestParam("comment_content")String comment_content,HttpServletRequest request) { User user = (User)request.getSession().getAttribute("user"); JSONObject obj = new JSONObject(); if(user == null) { obj.put("code",200); obj.put("msg", "您未登录,登录之后才可评论~"); }else { Comment comment = new Comment(); comment.setComment_content(comment_content); comment.setMovie_id(movie_id); comment.setUser_id(user.getUser_id()); comment.setComment_time(new Date()); Integer rs = commentService.addComemnt(comment); if(rs > 0) { Integer rs2 = movieService.addCommentCount(comment.getMovie_id()); if(rs2 > 0) { obj.put("code", 0); obj.put("msg", "评论成功~"); }else { obj.put("code",200); obj.put("msg", "评论失败2~"); } }else { obj.put("code",200); obj.put("msg", "评论失败~"); } } return obj; } @RequestMapping("updateComment") @ResponseBody public JSONObject updateComment(@RequestParam("comment_id")long comment_id,@RequestParam("comment_content")String comment_content) { JSONObject obj = new JSONObject(); Comment comment = this.commentService.findCommentById(comment_id); comment.setComment_time(new Date()); comment.setComment_content(comment_content); Integer rs = commentService.updateComment(comment); if(rs > 0) { obj.put("code", 0); obj.put("msg", "修改成功~"); }else { obj.put("code",200); obj.put("msg", "修改失败~"); } return obj; } @RequestMapping("deleteComemnt") @ResponseBody public JSONObject deleteComment(@RequestParam("comment_id")long comment_id) { JSONObject obj = new JSONObject(); Integer rs2 = movieService.delCommentCount(commentService.findCommentById(comment_id).getMovie_id()); Integer rs = commentService.deleteComment(comment_id); if(rs > 0) { obj.put("code", 0); obj.put("msg", "删除成功~"); }else { obj.put("code", 200); obj.put("msg", "删除失败~"); } return obj; } @RequestMapping("findCommentsByUserName") @ResponseBody public JSONObject findCommentsByUserName(@RequestParam(value="page",defaultValue="1")Integer page,@RequestParam(value="limit",defaultValue="10")Integer limit,@RequestParam("user_name")String user_name) { PageInfo info = commentService.findCommentsByUserName(page, limit, user_name); //System.out.println(info); for(Comment comment : info.getList()) { comment.setComment_user(userService.findUserById(comment.getUser_id())); } JSONObject obj = new JSONObject(); obj.put("code", 0); obj.put("msg", ""); obj.put("count", info.getTotal()); obj.put("data", info.getList()); return obj; } }