评论优化

This commit is contained in:
2020-09-10 18:29:42 +08:00
parent d1e946a31f
commit 8662dd44f0
6 changed files with 154 additions and 62 deletions
@@ -6,10 +6,7 @@ import org.springframework.beans.BeanUtils;
import java.io.BufferedReader;
import java.io.Reader;
import java.sql.Clob;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.*;
/**
* @author yaoyuan2.chu
@@ -21,6 +18,44 @@ import java.util.Map;
public class PostCommonUtil {
public static List<PostCustom> getNewPostsObjMap(List<Map<String, Object>> posts) {
List<PostCustom> postCustomList = new ArrayList<>();
for (Map<String, Object> mapObj : posts) {
PostCustom postCustom = new PostCustom();
postCustom.setId(Integer.parseInt(String.valueOf(mapObj.get("id"))));
postCustom.setParentId(mapObj.get("parent_id") != null ? Integer.parseInt(String.valueOf(mapObj.get("parent_id"))) : null);
if (null != mapObj.get("create_time")) {
postCustom.setCreateTime((Date) mapObj.get("create_time"));
}
String content = "";
if (mapObj.get("content") != null) {
Clob clob = (Clob) mapObj.get("content");
content = PostCommonUtil.clobToString(clob);
}
postCustom.setContent(content);
String avatar = "";
if (mapObj.get("create_avatar") != null) {
Clob clob = (Clob) mapObj.get("create_avatar");
avatar = PostCommonUtil.clobToString(clob);
}
postCustom.setAvatar(avatar);
postCustom.setUsername(mapObj.get("create_username") != null ? String.valueOf(mapObj.get("create_username")) : null);
postCustom.setRealname(mapObj.get("create_realname") != null ? String.valueOf(mapObj.get("create_realname")) : null);
String avatarReply = "";
if (mapObj.get("user_avatar") != null) {
Clob clob = (Clob) mapObj.get("user_avatar");
avatarReply = PostCommonUtil.clobToString(clob);
}
postCustom.setAvatarReply(avatarReply);
postCustom.setUsernameReply(mapObj.get("user_username") != null ? String.valueOf(mapObj.get("user_username")) : null);
postCustom.setRealnameReply(mapObj.get("user_realname") != null ? String.valueOf(mapObj.get("user_realname")) : null);
postCustomList.add(postCustom);
}
return postCustomList;
}
public static List<PostCustom> getNewPostsObjCustom(List<PostCustom> postCustomList) {
List<PostCustom> mainList = new ArrayList<>();
Map<String, Object> postMap = new LinkedHashMap<>();
@@ -80,7 +80,7 @@ public class UserPostController extends BaseController {
List<Tag> tags = tagsService.findByDId(id);
List<PostCustom> postsObj = postsService.findPostCustomById(id);
List postsObj = postsService.findPostCustomById(id);
map.put("tags", tags);
@@ -30,9 +30,29 @@ public interface PostsRepository extends JpaRepository<Post,Integer> {
, nativeQuery = true)
List<Map<String, Object>> findPostCustomById(@Param("dId")Integer dId);
@Query("from Post where parentId is null and discussionId = :dId")
List<Post> findAllByDIdAndparentIdNull(@Param("dId")Integer dId);
@Query("from Post where parentId is not null and discussionId = :dId")
List<Post> findAllByDIdAndparentIdNotNull(@Param("dId")Integer dId);
@Query(value = "select p.*,u.avatar as user_avatar ,u.username as user_username ,u.realname as user_realname ," +
"u2.avatar as create_avatar ,u2.username as create_username ,u2.realname as create_realname \n" +
"from Post p \n" +
"left join User u on p.user_id = u.id \n" +
"left join User u2 on p.create_id = u2.id \n" +
"where p.discussion_id = :dId and p.parent_id is null order by p.create_time desc"
, nativeQuery = true)
List<Map<String, Object>> findAllByDIdAndparentIdNull(@Param("dId")Integer dId);
@Query(value = "select p.*,u.avatar as user_avatar ,u.username as user_username ,u.realname as user_realname ," +
"u2.avatar as create_avatar ,u2.username as create_username ,u2.realname as create_realname \n" +
"from Post p \n" +
"left join User u on p.user_id = u.id \n" +
"left join User u2 on p.create_id = u2.id \n" +
"where p.discussion_id = :dId and p.parent_id is not null order by p.create_time desc"
, nativeQuery = true)
List<Map<String, Object>> findAllByDIdAndparentIdNotNull(@Param("dId")Integer dId);
// @Query("from Post where parentId is null and discussionId = :dId")
// List<Post> findAllByDIdAndparentIdNull(@Param("dId")Integer dId);
//
// @Query("from Post where parentId is not null and discussionId = :dId")
// List<Post> findAllByDIdAndparentIdNotNull(@Param("dId")Integer dId);
}
@@ -13,10 +13,6 @@ public interface IPostsService {
List<Post> findOneBy(Integer id);
List<Post> findAllByDIdAndparentIdNull(Integer dId);
List<Post> findAllByDIdAndparentIdNotNull(Integer dId);
List<PostCustom> findPostCustomById(Integer id);
Post findOneByid(Integer parentId);
@@ -5,6 +5,7 @@ import java.io.Reader;
import java.sql.Clob;
import java.util.*;
import com.yaoyuan.jiscuss.common.Node;
import com.yaoyuan.jiscuss.common.PostCommonUtil;
import com.yaoyuan.jiscuss.entity.Discussion;
import com.yaoyuan.jiscuss.entity.custom.DiscussionCustom;
@@ -49,60 +50,47 @@ public class PostsServiceImpl implements IPostsService {
}
@Override
public List<PostCustom> findPostCustomById(Integer id) {
public List findPostCustomById(Integer id) {
//查询id为1且parentId为null的评论
List<Map<String, Object>> firstposts = postsRepository.findAllByDIdAndparentIdNull(id);
List<Map<String, Object>> posts = postsRepository.findPostCustomById(id);
// List<Map<String, Object>> postsObjNew = this.getNewPostsObj(posts);
List<PostCustom> firstpostCustomList = PostCommonUtil.getNewPostsObjMap(firstposts);
List<PostCustom> postCustomList = new ArrayList<>();
for(Map<String, Object> mapObj : posts){
PostCustom postCustom = new PostCustom();
postCustom.setId(Integer.parseInt(String.valueOf(mapObj.get("id"))));
postCustom.setParentId(mapObj.get("parent_id") != null ? Integer.parseInt(String.valueOf(mapObj.get("parent_id"))) : null);
if(null != mapObj.get("create_time")){
postCustom.setCreateTime((Date) mapObj.get("create_time"));
}
String content = "";
if(mapObj.get("content") != null){
Clob clob = (Clob) mapObj.get("content");
content = PostCommonUtil.clobToString(clob);
}
postCustom.setContent(content);
String avatar = "";
if(mapObj.get("create_avatar") != null){
Clob clob = (Clob) mapObj.get("create_avatar");
avatar = PostCommonUtil.clobToString(clob);
}
postCustom.setAvatar(avatar);
postCustom.setUsername(mapObj.get("create_username") != null ? String.valueOf(mapObj.get("create_username")): null);
postCustom.setRealname(mapObj.get("create_realname") != null ? String.valueOf(mapObj.get("create_realname")): null);
String avatarReply = "";
if(mapObj.get("user_avatar") != null){
Clob clob = (Clob) mapObj.get("user_avatar");
avatarReply = PostCommonUtil.clobToString(clob);
}
postCustom.setAvatarReply(avatarReply);
postCustom.setUsernameReply(mapObj.get("user_username") != null ? String.valueOf(mapObj.get("user_username")): null);
postCustom.setRealnameReply(mapObj.get("user_realname") != null ? String.valueOf(mapObj.get("user_realname")): null);
// List<PostCustom> firstpostCustomListNew = PostCommonUtil.getNewPostsObjCustom(firstpostCustomList);
postCustomList.add(postCustom);
//查询id为1且parentId不为null的评论
List<Map<String, Object>> thenposts = postsRepository.findAllByDIdAndparentIdNotNull(id);
List<PostCustom> thenpostCustomList = PostCommonUtil.getNewPostsObjMap(thenposts);
// List<PostCustom> thenpostCustomListNew = PostCommonUtil.getNewPostsObjCustom(thenpostCustomList);
//新建一个Node集合。
ArrayList<Node> nodes = new ArrayList<>();
//将第一层评论都添加都Node集合中
for (PostCustom post : firstpostCustomList) {
Node node = new Node();
BeanUtils.copyProperties(post, node);
nodes.add(node);
}
//将回复添加到对应的位置
List list = Node.addAllNode(nodes, thenpostCustomList);
System.out.println();
//打印回复链表
Node.show(list);
List<PostCustom> postCustomListNew = PostCommonUtil.getNewPostsObjCustom(postCustomList);
return postCustomListNew;
}
@Override
public List<Post> findAllByDIdAndparentIdNull(Integer dId) {
List<Post> posts = postsRepository.findAllByDIdAndparentIdNull(dId);
return posts;
}
@Override
public List<Post> findAllByDIdAndparentIdNotNull(Integer dId) {
List<Post> posts = postsRepository.findAllByDIdAndparentIdNotNull(dId);
return posts;
// List<Map<String, Object>> posts = postsRepository.findPostCustomById(id);
//
// List<PostCustom> postCustomList = PostCommonUtil.getNewPostsObjMap(posts);
//
// List<PostCustom> postCustomListNew = PostCommonUtil.getNewPostsObjCustom(postCustomList);
return list;
}