public Map<String, Object> getByContentId(Integer contentId, Integer currentPage) {
if (currentPage == null || currentPage < 1)
currentPage = 1;
Map<String, Object> result = new HashMap<String, Object>();
Content content = contentDao.getById(contentId);
if (content.getUserid() != null) {
User u = userDao.getUser(content.getUserid());
result.put("User", u);
}
List<FileManagement> f = fileManagementDao.getByContentId(contentId);
if (f != null && !f.isEmpty()) {
result.put("fileManagement", f);
}
//
List<List<CommentUserVo>> resultAllList = new ArrayList<List<CommentUserVo>>();
LinkedList<CommentUserVo> secondList = new LinkedList<CommentUserVo>();
// 评论父级id从0开始
Integer parentId = 0;
// 从第几楼开始(分页整理)
// 起始下标
int pageNum = (currentPage - 1) * PageNoUtil.pageSize_15 == 0 ? 0 : (currentPage - 1) * PageNoUtil.pageSize_15 - 1;
// 获取所有的评论树
// cycleGetComment(resultAllList, secondList, contentId, parentId, index);
// 先取出所有的评论。然后在排楼。
List<Comment> allComment = commentDao.getByContentId(contentId);
cycleGetComment(resultAllList, secondList, allComment, parentId);
List<List<CommentUserVo>> resultList = new ArrayList<List<CommentUserVo>>();
// 按照每根树的最后一个人的创建时间倒序排序
for (int i = 0; i < resultAllList.size() - 1; i++) {
for (int j = 0; j < resultAllList.size() - 1 - i; j++) {
CommentUserVo cv1 = resultAllList.get(j).get(resultAllList.get(j).size() - 1);
CommentUserVo cv2 = resultAllList.get(j + 1).get(resultAllList.get(j + 1).size() - 1);
if (cv1.getComment().getCreatetime().getTime() < cv2.getComment().getCreatetime().getTime()) {
List<CommentUserVo> list = new LinkedList<CommentUserVo>();
list = resultAllList.get(j);
resultAllList.set(j, resultAllList.get(j + 1));
resultAllList.set(j + 1, list);
}
}
}
// 给结果发进行分页
for (int i = pageNum; i < resultAllList.size(); i++) {
if (i == (pageNum + PageNoUtil.pageSize_15))
break;
resultList.add(resultAllList.get(i));
}
// 需求,给评论加上对应的用户对象
List<Integer> ids = new ArrayList<Integer>();
for (List<CommentUserVo> cvList : resultList) {
for (CommentUserVo cv : cvList) {
ids.add(cv.getComment().getUserid());
}
}
List<User> users = userDao.findByIds(ids);
for (List<CommentUserVo> cvList : resultList) {
for (CommentUserVo cv : cvList) {
for (User u : users) {
if (cv.getComment().getUserid() == u.getId())
cv.setUser(u);
break;
}
}
}
result.put("content", content);
result.put("comment", PageNoUtil.getListMap(resultList, PageNoUtil.pageSize_15, (long) resultAllList.size()));
return result;
}
public void cycleGetComment(List<List<CommentUserVo>> resultList, LinkedList<CommentUserVo> secondList,
List<Comment> allComment, Integer parentId) {
List<Comment> commentList = new ArrayList<Comment>();
for (int i = 0; i < allComment.size(); i++) {
if (allComment.get(i).getParentid() == parentId) {
commentList.add(allComment.get(i));
allComment.remove(i);
}
}
if (commentList != null && commentList.size() > 0) {
for (Comment c : commentList) {
CommentUserVo cv = new CommentUserVo();
cv.setComment(c);
secondList.add(cv);
cycleGetComment(resultList, secondList, allComment, c.getId());
secondList.pollLast();
}
} else {
if (secondList.size() > 0) {
List<CommentUserVo> list = new LinkedList<CommentUserVo>();
list.addAll(secondList);
resultList.add(list);
}
}
}
if (currentPage == null || currentPage < 1)
currentPage = 1;
Map<String, Object> result = new HashMap<String, Object>();
Content content = contentDao.getById(contentId);
if (content.getUserid() != null) {
User u = userDao.getUser(content.getUserid());
result.put("User", u);
}
List<FileManagement> f = fileManagementDao.getByContentId(contentId);
if (f != null && !f.isEmpty()) {
result.put("fileManagement", f);
}
//
List<List<CommentUserVo>> resultAllList = new ArrayList<List<CommentUserVo>>();
LinkedList<CommentUserVo> secondList = new LinkedList<CommentUserVo>();
// 评论父级id从0开始
Integer parentId = 0;
// 从第几楼开始(分页整理)
// 起始下标
int pageNum = (currentPage - 1) * PageNoUtil.pageSize_15 == 0 ? 0 : (currentPage - 1) * PageNoUtil.pageSize_15 - 1;
// 获取所有的评论树
// cycleGetComment(resultAllList, secondList, contentId, parentId, index);
// 先取出所有的评论。然后在排楼。
List<Comment> allComment = commentDao.getByContentId(contentId);
cycleGetComment(resultAllList, secondList, allComment, parentId);
List<List<CommentUserVo>> resultList = new ArrayList<List<CommentUserVo>>();
// 按照每根树的最后一个人的创建时间倒序排序
for (int i = 0; i < resultAllList.size() - 1; i++) {
for (int j = 0; j < resultAllList.size() - 1 - i; j++) {
CommentUserVo cv1 = resultAllList.get(j).get(resultAllList.get(j).size() - 1);
CommentUserVo cv2 = resultAllList.get(j + 1).get(resultAllList.get(j + 1).size() - 1);
if (cv1.getComment().getCreatetime().getTime() < cv2.getComment().getCreatetime().getTime()) {
List<CommentUserVo> list = new LinkedList<CommentUserVo>();
list = resultAllList.get(j);
resultAllList.set(j, resultAllList.get(j + 1));
resultAllList.set(j + 1, list);
}
}
}
// 给结果发进行分页
for (int i = pageNum; i < resultAllList.size(); i++) {
if (i == (pageNum + PageNoUtil.pageSize_15))
break;
resultList.add(resultAllList.get(i));
}
// 需求,给评论加上对应的用户对象
List<Integer> ids = new ArrayList<Integer>();
for (List<CommentUserVo> cvList : resultList) {
for (CommentUserVo cv : cvList) {
ids.add(cv.getComment().getUserid());
}
}
List<User> users = userDao.findByIds(ids);
for (List<CommentUserVo> cvList : resultList) {
for (CommentUserVo cv : cvList) {
for (User u : users) {
if (cv.getComment().getUserid() == u.getId())
cv.setUser(u);
break;
}
}
}
result.put("content", content);
result.put("comment", PageNoUtil.getListMap(resultList, PageNoUtil.pageSize_15, (long) resultAllList.size()));
return result;
}
public void cycleGetComment(List<List<CommentUserVo>> resultList, LinkedList<CommentUserVo> secondList,
List<Comment> allComment, Integer parentId) {
List<Comment> commentList = new ArrayList<Comment>();
for (int i = 0; i < allComment.size(); i++) {
if (allComment.get(i).getParentid() == parentId) {
commentList.add(allComment.get(i));
allComment.remove(i);
}
}
if (commentList != null && commentList.size() > 0) {
for (Comment c : commentList) {
CommentUserVo cv = new CommentUserVo();
cv.setComment(c);
secondList.add(cv);
cycleGetComment(resultList, secondList, allComment, c.getId());
secondList.pollLast();
}
} else {
if (secondList.size() > 0) {
List<CommentUserVo> list = new LinkedList<CommentUserVo>();
list.addAll(secondList);
resultList.add(list);
}
}
}