分页修改和评论获取
This commit is contained in:
@@ -0,0 +1,115 @@
|
||||
package com.yaoyuan.jiscuss.common;
|
||||
import com.yaoyuan.jiscuss.entity.Posts;
|
||||
import lombok.Data;
|
||||
import org.springframework.beans.BeanUtils;
|
||||
|
||||
import javax.persistence.Column;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
/**
|
||||
* @author yaoyuan2.chu
|
||||
* @Title:
|
||||
* @Package com.yaoyuan.jiscuss.common
|
||||
* @Description:
|
||||
* @date 2020/8/17 14:51
|
||||
*/
|
||||
@Data
|
||||
public class Node {
|
||||
|
||||
public Node() {
|
||||
|
||||
}
|
||||
|
||||
private Integer id;
|
||||
|
||||
private Integer discussionId;
|
||||
|
||||
private Integer number;
|
||||
|
||||
private Date time;
|
||||
|
||||
private Integer userId;
|
||||
|
||||
private String type;
|
||||
|
||||
private String content;
|
||||
|
||||
private Integer parentId;
|
||||
|
||||
private Date editTime;
|
||||
|
||||
private Integer editUserId;
|
||||
|
||||
private String ipAddress;
|
||||
|
||||
private String copyright;
|
||||
|
||||
private Integer isApproved;
|
||||
|
||||
private Integer createId;
|
||||
|
||||
private Date createTime;
|
||||
//下一条回复
|
||||
private List<Node> nextNodes = new ArrayList<Node>();
|
||||
|
||||
|
||||
/**
|
||||
* 将单个node添加到链表中
|
||||
*
|
||||
* @param list
|
||||
* @param node
|
||||
* @return
|
||||
*/
|
||||
public static boolean addNode(List<Node> list, Node node) {
|
||||
for (Node node1 : list) { //循环添加
|
||||
if (node1.getId() .equals(node.getParentId()) ) { //判断留言的上一段是都是这条留言
|
||||
node1.getNextNodes().add(node); //是,添加,返回true;
|
||||
System.out.println("添加了一个");
|
||||
return true;
|
||||
} else { //否则递归继续判断
|
||||
if (node1.getNextNodes().size() != 0) {
|
||||
if (Node.addNode(node1.getNextNodes(), node)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 将查出来的lastId不为null的回复都添加到第一层Node集合中
|
||||
*
|
||||
* @param firstList
|
||||
* @param thenList
|
||||
* @return
|
||||
*/
|
||||
public static List addAllNode(List<Node> firstList, List<Posts> thenList) {
|
||||
while (thenList.size() != 0) {
|
||||
int size = thenList.size();
|
||||
for (int i = 0; i < size; i++) {
|
||||
Node node = new Node();
|
||||
BeanUtils.copyProperties(thenList.get(i), node);
|
||||
if (Node.addNode(firstList, node)) {
|
||||
thenList.remove(i);
|
||||
i--;
|
||||
size--;
|
||||
}
|
||||
}
|
||||
}
|
||||
return firstList;
|
||||
}
|
||||
|
||||
//打印
|
||||
public static void show(List<Node> list) {
|
||||
for (Node node : list) {
|
||||
System.out.println(node.getUserId() + " 用户回复了你:" + node.getContent());
|
||||
if (node.getNextNodes().size() != 0) {
|
||||
Node.show(node.getNextNodes());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -1,17 +1,20 @@
|
||||
package com.yaoyuan.jiscuss.controller;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
import com.yaoyuan.jiscuss.common.Node;
|
||||
import com.yaoyuan.jiscuss.entity.Posts;
|
||||
import com.yaoyuan.jiscuss.entity.UserInfo;
|
||||
import com.yaoyuan.jiscuss.service.IPostsService;
|
||||
import org.json.JSONObject;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.BeanUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.ModelMap;
|
||||
@@ -58,6 +61,27 @@ public class UserPostController extends BaseController {
|
||||
Discussions discussions = discussionsService.findOne(id);
|
||||
|
||||
List<Posts> posts = postsService.findOneBy(id);
|
||||
|
||||
//查询id为1且parentId为null的评论
|
||||
List<Posts> firstList = postsService.findAllByDIdAndparentIdNull(1);
|
||||
//查询id为1且parentId不为null的评论
|
||||
List<Posts> thenList = postsService.findAllByDIdAndparentIdNotNull(1);
|
||||
//新建一个Node集合。
|
||||
ArrayList<Node> nodes = new ArrayList<>();
|
||||
//将第一层评论都添加都Node集合中
|
||||
for (Posts post : firstList) {
|
||||
Node node = new Node();
|
||||
BeanUtils.copyProperties(post, node);
|
||||
nodes.add(node);
|
||||
}
|
||||
//将回复添加到对应的位置
|
||||
List list = Node.addAllNode(nodes, thenList);
|
||||
System.out.println();
|
||||
//打印回复链表
|
||||
Node.show(list);
|
||||
|
||||
|
||||
|
||||
map.put("discussions", discussions);
|
||||
map.put("posts", posts);
|
||||
UserInfo user = getUserInfo(request);
|
||||
@@ -78,10 +102,10 @@ public class UserPostController extends BaseController {
|
||||
HttpServletRequest request = servletRequestAttributes.getRequest();
|
||||
UserInfo user = getUserInfo(request);
|
||||
if(user != null){
|
||||
discussions.setLast_user_id( user.getId());
|
||||
discussions.setCreate_id( user.getId());
|
||||
discussions.setLastUserId( user.getId());
|
||||
discussions.setCreateId( user.getId());
|
||||
}
|
||||
discussions.setCreate_time(new Date());
|
||||
discussions.setCreateTime(new Date());
|
||||
|
||||
Discussions saveDiscussions = discussionsService.insert(discussions);
|
||||
JSONObject resultobj = new JSONObject();
|
||||
@@ -109,9 +133,9 @@ public class UserPostController extends BaseController {
|
||||
|
||||
UserInfo user = getUserInfo(request);
|
||||
if(user != null){
|
||||
posts.setCreate_id( user.getId());
|
||||
posts.setCreateId( user.getId());
|
||||
}
|
||||
posts.setCreate_time(new Date());
|
||||
posts.setCreateTime(new Date());
|
||||
|
||||
Posts savePost = postsService.insert(posts);
|
||||
JSONObject resultobj = new JSONObject();
|
||||
@@ -135,9 +159,9 @@ public class UserPostController extends BaseController {
|
||||
|
||||
UserInfo user = getUserInfo(request);
|
||||
if(user != null){
|
||||
tags.setCreate_id( user.getId());
|
||||
tags.setCreateId( user.getId());
|
||||
}
|
||||
tags.setCreate_time(new Date());
|
||||
tags.setCreateTime(new Date());
|
||||
|
||||
Tags saveTags = tagsService.insert(tags);
|
||||
JSONObject resultobj = new JSONObject();
|
||||
|
||||
@@ -53,13 +53,15 @@ public class UserSystemController extends BaseController {
|
||||
|
||||
int pageSiz = 5;
|
||||
int pageNumNew = pageNum - 1;
|
||||
|
||||
Discussions discussions = new Discussions();
|
||||
//分页获取主题帖子
|
||||
Page<Discussions> allDiscussionsPage = discussionsService.queryAllDiscussionsList(pageNumNew,pageSiz);
|
||||
Page<Discussions> allDiscussionsPage = discussionsService.queryAllDiscussionsList(discussions,pageNumNew,pageSiz);
|
||||
|
||||
List<Discussions> allDiscussions =allDiscussionsPage.getContent();
|
||||
|
||||
logger.info("全部主题==>:{}",allDiscussions);
|
||||
|
||||
Long total = allDiscussionsPage.getTotalElements();
|
||||
|
||||
//获取主题帖子的分页数据
|
||||
List<String> pageNumList = getPageNumList(allDiscussionsPage.getTotalPages());
|
||||
@@ -73,10 +75,13 @@ public class UserSystemController extends BaseController {
|
||||
map.put("allDiscussions", allDiscussions);
|
||||
map.put("pageDiscussions", pageNumList);
|
||||
map.put("allTags", allTags);
|
||||
|
||||
map.put("tag", tag);
|
||||
map.put("type", type);
|
||||
|
||||
map.put("pageSize",pageSiz);
|
||||
map.put("pageTotal", total);
|
||||
map.put("pageNum", pageNum);
|
||||
map.put("pageNumAll", pageNumList.size());
|
||||
map.put("pageTotalPages", allDiscussionsPage.getTotalPages());
|
||||
UserInfo user = getUserInfo(request);
|
||||
if(user != null){
|
||||
map.put("username", user.getUsername());
|
||||
@@ -85,38 +90,6 @@ public class UserSystemController extends BaseController {
|
||||
return "index";
|
||||
}
|
||||
|
||||
|
||||
// //首页main
|
||||
// @RequestMapping("/main")
|
||||
// public String homePage(HttpServletRequest request, ModelMap map) {
|
||||
// logger.info(">>> index");
|
||||
//
|
||||
// //分页获取主题帖子
|
||||
// Page<Discussions> allDiscussionsPage = discussionsService.queryAllDiscussionsList(0,20);
|
||||
//
|
||||
// List<Discussions> allDiscussions =allDiscussionsPage.getContent();
|
||||
//
|
||||
// logger.info("全部主题==>:{}",allDiscussions);
|
||||
//
|
||||
// //获取主题帖子的分页数据
|
||||
// List<String> pageNumList = getPageNumList(allDiscussionsPage.getTotalPages());
|
||||
//
|
||||
// //获取所有标签(以后尝试去缓存中取)
|
||||
// List<Tags> allTags = tagsService.getAllList();
|
||||
// logger.info("全部标签==>:{}",allTags);
|
||||
//
|
||||
// map.put("data", "Jiscuss 用户");
|
||||
// map.put("allDiscussions", allDiscussions);
|
||||
// map.put("pageDiscussions", pageNumList);
|
||||
// map.put("allTags", allTags);
|
||||
// UserInfo user = getUserInfo(request);
|
||||
// if(user != null){
|
||||
// map.put("username", user.getUsername());
|
||||
// map.put("data", "Jiscuss 用户:" + user.getUsername());
|
||||
// }
|
||||
// return "index";
|
||||
// }
|
||||
|
||||
|
||||
private List<String> getPageNumList(int size) {
|
||||
List<String> pageNumList = new ArrayList<String>();
|
||||
@@ -127,28 +100,6 @@ public class UserSystemController extends BaseController {
|
||||
}
|
||||
|
||||
|
||||
//登录
|
||||
// @PostMapping(value = "/login")
|
||||
// @ResponseBody
|
||||
// public String login(@RequestParam("username") String username, @RequestParam("password") String password,
|
||||
// HttpSession session) {
|
||||
// JSONObject resultobj = new JSONObject();
|
||||
// Users user = usersService.checkByUsernameAndPassword(username, password);
|
||||
// if (user!=null) {
|
||||
// //用户名和密码完成校验
|
||||
// session.setAttribute("username", username); //缓存session
|
||||
// session.setAttribute("userid", user.getId()); //缓存session
|
||||
// resultobj.put("username", username);
|
||||
// resultobj.put("msg", "登录成功");
|
||||
// resultobj.put("flag", true);
|
||||
// } else {
|
||||
// //用户名和密码未完成校验
|
||||
// resultobj.put("msg", "用户名或密码错误");
|
||||
// resultobj.put("flag", false);
|
||||
// }
|
||||
// return resultobj.toString();
|
||||
// }
|
||||
|
||||
//登录页
|
||||
@GetMapping("/login")
|
||||
public String login(@RequestParam(value = "error", required = false) String error,
|
||||
@@ -165,17 +116,6 @@ public class UserSystemController extends BaseController {
|
||||
return "login";
|
||||
}
|
||||
|
||||
//退出
|
||||
// @PostMapping(value = "/loginout")
|
||||
// @ResponseBody
|
||||
// public String logout(HttpServletRequest request, HttpServletResponse response){
|
||||
// JSONObject resultobj = new JSONObject();
|
||||
// HttpSession session=request.getSession();
|
||||
// session.invalidate();
|
||||
// resultobj.put("msg", "用户退出成功");
|
||||
// resultobj.put("flag", true);
|
||||
// return resultobj.toString(); //
|
||||
// }
|
||||
|
||||
//注册
|
||||
|
||||
|
||||
@@ -29,49 +29,49 @@ public class Discussions implements Serializable {
|
||||
private String content;
|
||||
|
||||
@Column(name="comments_count")
|
||||
private Integer comments_count;
|
||||
private Integer commentsCount;
|
||||
|
||||
@Column(name="participants_count")
|
||||
private Integer participants_count;
|
||||
private Integer participantsCount;
|
||||
|
||||
@Column(name="number_index")
|
||||
private Integer number_index;
|
||||
private Integer numberIndex;
|
||||
|
||||
@Column(name="start_time")
|
||||
private Date start_time;
|
||||
private Date startTime;
|
||||
|
||||
@Column(name="start_user_id")
|
||||
private Integer start_user_id;
|
||||
private Integer startUserId;
|
||||
|
||||
@Column(name="start_post_id")
|
||||
private Integer start_post_id;
|
||||
private Integer startPostId;
|
||||
|
||||
@Column(name="last_time")
|
||||
private Date last_time;
|
||||
private Date lastTime;
|
||||
|
||||
@Column(name="last_user_id")
|
||||
private Integer last_user_id;
|
||||
private Integer lastUserId;
|
||||
|
||||
@Column(name="last_post_id")
|
||||
private Integer last_post_id;
|
||||
private Integer lastPostId;
|
||||
|
||||
@Column(name="last_post_number")
|
||||
private Integer last_post_number;
|
||||
private Integer lastPostNumber;
|
||||
|
||||
@Column(name="is_approved")
|
||||
private Integer is_approved;
|
||||
private Integer isApproved;
|
||||
|
||||
@Column(name="like_count")
|
||||
private Integer like_count;
|
||||
private Integer likeCount;
|
||||
|
||||
|
||||
@Column(name="ip_address")
|
||||
private String ip_address;
|
||||
private String ipAddress;
|
||||
|
||||
@Column(name="create_id")
|
||||
private Integer create_id;
|
||||
private Integer createId;
|
||||
|
||||
@Column(name="create_time")
|
||||
private Date create_time;
|
||||
private Date createTime;
|
||||
|
||||
}
|
||||
|
||||
@@ -16,10 +16,10 @@ public class DiscussionsTags implements Serializable{
|
||||
|
||||
@Id
|
||||
@Column(name="discussion_id")
|
||||
private Integer discussion_id;
|
||||
private Integer discussionId;
|
||||
|
||||
@Column(name="tag_id")
|
||||
private Integer tag_id;
|
||||
private Integer tagId;
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -23,7 +23,7 @@ public class Posts implements Serializable{
|
||||
private Integer id;
|
||||
|
||||
@Column(name="discussion_id")
|
||||
private Integer discussion_id;
|
||||
private Integer discussionId;
|
||||
|
||||
@Column(name="number")
|
||||
private Integer number;
|
||||
@@ -32,7 +32,7 @@ public class Posts implements Serializable{
|
||||
private Date time;
|
||||
|
||||
@Column(name="user_id")
|
||||
private Integer user_id;
|
||||
private Integer userId;
|
||||
|
||||
@Column(name="type")
|
||||
private String type;
|
||||
@@ -41,27 +41,27 @@ public class Posts implements Serializable{
|
||||
private String content;
|
||||
|
||||
@Column(name="parent_id")
|
||||
private Integer parent_id;
|
||||
private Integer parentId;
|
||||
|
||||
@Column(name="edit_time")
|
||||
private Date edit_time;
|
||||
private Date editTime;
|
||||
|
||||
@Column(name="edit_user_id")
|
||||
private Integer edit_user_id;
|
||||
private Integer editUserId;
|
||||
|
||||
@Column(name="ip_address")
|
||||
private String ip_address;
|
||||
private String ipAddress;
|
||||
|
||||
@Column(name="copyright")
|
||||
private String copyright;
|
||||
|
||||
@Column(name="is_approved")
|
||||
private Integer is_approved;
|
||||
private Integer isApproved;
|
||||
|
||||
@Column(name="create_id")
|
||||
private Integer create_id;
|
||||
private Integer createId;
|
||||
|
||||
@Column(name="create_time")
|
||||
private Date create_time;
|
||||
private Date createTime;
|
||||
}
|
||||
|
||||
|
||||
@@ -35,20 +35,20 @@ public class Tags implements Serializable{
|
||||
private Integer position;
|
||||
|
||||
@Column(name="parent_id")
|
||||
private Integer parent_id;
|
||||
private Integer parentId;
|
||||
|
||||
@Column(name="discussions_count")
|
||||
private String discussions_count;
|
||||
private String discussionsCount;
|
||||
|
||||
@Column(name="last_time")
|
||||
private Date last_time;
|
||||
private Date lastTime;
|
||||
|
||||
@Column(name="last_discussion_id")
|
||||
private Integer last_discussion_id;
|
||||
private Integer lastDiscussionId;
|
||||
|
||||
@Column(name="create_id")
|
||||
private Integer create_id;
|
||||
private Integer createId;
|
||||
|
||||
@Column(name="create_time")
|
||||
private Date create_time;
|
||||
private Date createTime;
|
||||
}
|
||||
|
||||
@@ -44,7 +44,7 @@ public class Users implements Serializable {
|
||||
private String password;
|
||||
|
||||
@Column(name="join_time")
|
||||
private Date join_time;
|
||||
private Date joinTime;
|
||||
|
||||
@Column(name="age")
|
||||
private Integer age;
|
||||
@@ -56,13 +56,13 @@ public class Users implements Serializable {
|
||||
private String phone;
|
||||
|
||||
@Column(name="discussions_count")
|
||||
private Integer discussions_count;
|
||||
private Integer discussionsCount;
|
||||
|
||||
@Column(name="comments_count")
|
||||
private Integer comments_count;
|
||||
private Integer commentsCount;
|
||||
|
||||
@Column(name="last_seen_time")
|
||||
private Date last_seen_time;
|
||||
private Date lastSeenTime;
|
||||
|
||||
@Column(name="flag")
|
||||
private Integer flag;
|
||||
|
||||
@@ -17,7 +17,12 @@ public interface PostsRepository extends JpaRepository<Posts,Integer> {
|
||||
* @param dId
|
||||
* @return
|
||||
*/
|
||||
@Query("from Posts where discussion_id = :dId")
|
||||
@Query("from Posts where discussionId = :dId")
|
||||
List<Posts> findOneBy(@Param("dId")Integer dId);
|
||||
|
||||
@Query("from Posts where parentId is null and discussionId = :dId")
|
||||
List<Posts> findAllByDIdAndparentIdNull(@Param("dId")Integer dId);
|
||||
|
||||
@Query("from Posts where parentId is not null and discussionId = :dId")
|
||||
List<Posts> findAllByDIdAndparentIdNotNull(@Param("dId")Integer dId);
|
||||
}
|
||||
|
||||
@@ -27,12 +27,6 @@ public interface UsersRepository extends JpaRepository<Users,Integer> {
|
||||
*/
|
||||
Users getById(Integer id);
|
||||
|
||||
/**
|
||||
*
|
||||
* @param id
|
||||
*/
|
||||
void deleteById(Integer id);
|
||||
|
||||
/**
|
||||
*
|
||||
* @param username
|
||||
|
||||
@@ -14,6 +14,6 @@ public interface IDiscussionsService {
|
||||
|
||||
Discussions findOne(Integer id);
|
||||
|
||||
Page<Discussions> queryAllDiscussionsList(int pageNum,int pageSize);
|
||||
Page<Discussions> queryAllDiscussionsList(Discussions discussions,int pageNum,int pageSize);
|
||||
|
||||
}
|
||||
|
||||
@@ -10,4 +10,8 @@ public interface IPostsService {
|
||||
Posts insert(Posts posts);
|
||||
|
||||
List<Posts> findOneBy(Integer id);
|
||||
|
||||
List<Posts> findAllByDIdAndparentIdNull(Integer dId);
|
||||
|
||||
List<Posts> findAllByDIdAndparentIdNotNull(Integer dId);
|
||||
}
|
||||
|
||||
@@ -5,10 +5,7 @@ import java.util.List;
|
||||
import javax.transaction.Transactional;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.PageRequest;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.domain.Sort;
|
||||
import org.springframework.data.domain.*;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import com.yaoyuan.jiscuss.entity.Discussions;
|
||||
@@ -40,10 +37,13 @@ public class DiscussionsServiceImpl implements IDiscussionsService {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Page<Discussions> queryAllDiscussionsList(int pageNum, int pageSize) {
|
||||
public Page<Discussions> queryAllDiscussionsList(Discussions discussions,int pageNum, int pageSize) {
|
||||
Sort sort=new Sort(Sort.Direction.DESC,"id");
|
||||
@SuppressWarnings("deprecation")
|
||||
Pageable pageable=new PageRequest(pageNum,pageSize,sort);
|
||||
return discussionsRepository.findAll(pageable);
|
||||
//将匹配对象封装成Example对象
|
||||
Example<Discussions> example = Example.of(discussions);
|
||||
|
||||
return discussionsRepository.findAll(example,pageable);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -27,8 +27,20 @@ public class PostsServiceImpl implements IPostsService {
|
||||
@Override
|
||||
public List<Posts> findOneBy(Integer id) {
|
||||
|
||||
List<Posts> ss = postsRepository.findOneBy(id);
|
||||
return ss;
|
||||
List<Posts> posts = postsRepository.findOneBy(id);
|
||||
return posts;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Posts> findAllByDIdAndparentIdNull(Integer dId) {
|
||||
List<Posts> posts = postsRepository.findAllByDIdAndparentIdNull(dId);
|
||||
return posts;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Posts> findAllByDIdAndparentIdNotNull(Integer dId) {
|
||||
List<Posts> posts = postsRepository.findAllByDIdAndparentIdNotNull(dId);
|
||||
return posts;
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user