评论优化
This commit is contained in:
@@ -6,10 +6,7 @@ import org.springframework.beans.BeanUtils;
|
|||||||
import java.io.BufferedReader;
|
import java.io.BufferedReader;
|
||||||
import java.io.Reader;
|
import java.io.Reader;
|
||||||
import java.sql.Clob;
|
import java.sql.Clob;
|
||||||
import java.util.ArrayList;
|
import java.util.*;
|
||||||
import java.util.LinkedHashMap;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author yaoyuan2.chu
|
* @author yaoyuan2.chu
|
||||||
@@ -21,6 +18,44 @@ import java.util.Map;
|
|||||||
public class PostCommonUtil {
|
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) {
|
public static List<PostCustom> getNewPostsObjCustom(List<PostCustom> postCustomList) {
|
||||||
List<PostCustom> mainList = new ArrayList<>();
|
List<PostCustom> mainList = new ArrayList<>();
|
||||||
Map<String, Object> postMap = new LinkedHashMap<>();
|
Map<String, Object> postMap = new LinkedHashMap<>();
|
||||||
|
|||||||
@@ -80,7 +80,7 @@ public class UserPostController extends BaseController {
|
|||||||
|
|
||||||
List<Tag> tags = tagsService.findByDId(id);
|
List<Tag> tags = tagsService.findByDId(id);
|
||||||
|
|
||||||
List<PostCustom> postsObj = postsService.findPostCustomById(id);
|
List postsObj = postsService.findPostCustomById(id);
|
||||||
|
|
||||||
map.put("tags", tags);
|
map.put("tags", tags);
|
||||||
|
|
||||||
|
|||||||
@@ -30,9 +30,29 @@ public interface PostsRepository extends JpaRepository<Post,Integer> {
|
|||||||
, nativeQuery = true)
|
, nativeQuery = true)
|
||||||
List<Map<String, Object>> findPostCustomById(@Param("dId")Integer dId);
|
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")
|
@Query(value = "select p.*,u.avatar as user_avatar ,u.username as user_username ,u.realname as user_realname ," +
|
||||||
List<Post> findAllByDIdAndparentIdNotNull(@Param("dId")Integer dId);
|
"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> findOneBy(Integer id);
|
||||||
|
|
||||||
List<Post> findAllByDIdAndparentIdNull(Integer dId);
|
|
||||||
|
|
||||||
List<Post> findAllByDIdAndparentIdNotNull(Integer dId);
|
|
||||||
|
|
||||||
List<PostCustom> findPostCustomById(Integer id);
|
List<PostCustom> findPostCustomById(Integer id);
|
||||||
|
|
||||||
Post findOneByid(Integer parentId);
|
Post findOneByid(Integer parentId);
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import java.io.Reader;
|
|||||||
import java.sql.Clob;
|
import java.sql.Clob;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
|
||||||
|
import com.yaoyuan.jiscuss.common.Node;
|
||||||
import com.yaoyuan.jiscuss.common.PostCommonUtil;
|
import com.yaoyuan.jiscuss.common.PostCommonUtil;
|
||||||
import com.yaoyuan.jiscuss.entity.Discussion;
|
import com.yaoyuan.jiscuss.entity.Discussion;
|
||||||
import com.yaoyuan.jiscuss.entity.custom.DiscussionCustom;
|
import com.yaoyuan.jiscuss.entity.custom.DiscussionCustom;
|
||||||
@@ -49,60 +50,47 @@ public class PostsServiceImpl implements IPostsService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@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<PostCustom> firstpostCustomList = PostCommonUtil.getNewPostsObjMap(firstposts);
|
||||||
// List<Map<String, Object>> postsObjNew = this.getNewPostsObj(posts);
|
|
||||||
|
|
||||||
List<PostCustom> postCustomList = new ArrayList<>();
|
// List<PostCustom> firstpostCustomListNew = PostCommonUtil.getNewPostsObjCustom(firstpostCustomList);
|
||||||
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);
|
//查询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
|
// List<Map<String, Object>> posts = postsRepository.findPostCustomById(id);
|
||||||
public List<Post> findAllByDIdAndparentIdNotNull(Integer dId) {
|
//
|
||||||
List<Post> posts = postsRepository.findAllByDIdAndparentIdNotNull(dId);
|
// List<PostCustom> postCustomList = PostCommonUtil.getNewPostsObjMap(posts);
|
||||||
return posts;
|
//
|
||||||
|
// List<PostCustom> postCustomListNew = PostCommonUtil.getNewPostsObjCustom(postCustomList);
|
||||||
|
|
||||||
|
return list;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -82,7 +82,8 @@
|
|||||||
<div class="ui threaded comments">
|
<div class="ui threaded comments">
|
||||||
<h3 class="ui dividing header">评论区</h3>
|
<h3 class="ui dividing header">评论区</h3>
|
||||||
<input type="hidden" name="postId" id="postId" value=""/>
|
<input type="hidden" name="postId" id="postId" value=""/>
|
||||||
<#list posts as post>
|
|
||||||
|
<#--<#list posts as post>
|
||||||
<div class="comment">
|
<div class="comment">
|
||||||
<a class="avatar">
|
<a class="avatar">
|
||||||
<img src="/static/assets/images/logo.png">
|
<img src="/static/assets/images/logo.png">
|
||||||
@@ -100,7 +101,7 @@
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<#list post.child as child>
|
<#list post.nextNodes as child>
|
||||||
<div class="comments">
|
<div class="comments">
|
||||||
<div class="comment">
|
<div class="comment">
|
||||||
<a class="avatar">
|
<a class="avatar">
|
||||||
@@ -122,7 +123,59 @@
|
|||||||
</div>
|
</div>
|
||||||
</#list>
|
</#list>
|
||||||
</div>
|
</div>
|
||||||
|
</#list>-->
|
||||||
|
|
||||||
|
<!-- 定义遍历方法 -->
|
||||||
|
<#macro bpTree posts>
|
||||||
|
<#if posts?? && posts?size gt 0> <!-- 判断是否为空,并且长度是否大于0 -->
|
||||||
|
<!-- 不为空 开始遍历 -->
|
||||||
|
<#list posts as post>
|
||||||
|
<#if post.nextNodes?? && post.nextNodes?size gt 0>
|
||||||
|
<!-- 如果是拥有子节点 -->
|
||||||
|
<div class="comment">
|
||||||
|
<a class="avatar">
|
||||||
|
<img src="/static/assets/images/logo.png">
|
||||||
|
</a>
|
||||||
|
<div class="content">
|
||||||
|
<a class="author">${post.username}</a>
|
||||||
|
<div class="metadata">
|
||||||
|
<span class="date">${post.createTime}</span>
|
||||||
|
</div>
|
||||||
|
<div class="text">
|
||||||
|
${post.content}
|
||||||
|
</div>
|
||||||
|
<div class="actions">
|
||||||
|
<a class="reply" onclick="replyThis('${post.username}', '${post.id}')">回复</a>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div class="comments">
|
||||||
|
<@bpTree posts=post.nextNodes />
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<#else>
|
||||||
|
<!-- 没有子节点! -->
|
||||||
|
<div class="comment">
|
||||||
|
<a class="avatar">
|
||||||
|
<img src="/static/assets/images/logo.png">
|
||||||
|
</a>
|
||||||
|
<div class="content">
|
||||||
|
<a class="author">${post.username}</a>
|
||||||
|
<div class="metadata">
|
||||||
|
<span class="date">${post.createTime}</span>
|
||||||
|
</div>
|
||||||
|
<div class="text">
|
||||||
|
${post.content}
|
||||||
|
</div>
|
||||||
|
<div class="actions">
|
||||||
|
<a class="reply" onclick="replyThis('${post.username}', '${post.id}')">回复</a>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</#if>
|
||||||
</#list>
|
</#list>
|
||||||
|
</#if>
|
||||||
|
</#macro>
|
||||||
|
<@bpTree posts=posts /> <!-- 调用方法 -->
|
||||||
|
|
||||||
<div class="ui">
|
<div class="ui">
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user