Comment and reply function of community function

background

        Recently, I am writing a common small demo - the comment and reply function in the community function. In the implementation process, a comment class entity CommunityReplyEntity is defined, and then a children subset of the reply comment collection List<CommunityReplyEntity> is added to this entity. The logic of writing in this way is that when children are not empty, there will be replies, then you will encounter a problem, that is, every comment has replies, and the reply subset still has replies. This will cause the tree structure to be deep to more than three layers, which is very inconvenient for the front end to render comments-reply information. Therefore, I use two-level data such as first-level comments + first-level comments and all replies as my return data. In this way, the structural depth problem of the tree can be avoided.

Note: The following is a simple implementation method, which is only provided for beginners or reference for self-practice projects. Everyone is welcome to discuss posting comments, better table data structures in the reply function, and return data structure formats.

Comment reply form

comment entity class

 Reply result Vo

implement logic

1. Find all first-level comments below the note        

2. Find all replies under the comment

3. The encapsulation user name method called

4. Recursive method implements logic

5. Realize the effect (response data)

{
    "msg": "success",
    "code": 0,
    "data": [{
            "id": 1,
            "memberPid": 2,
            "reviewer": "PHW",
            "replier": null,
            "memberId": null,
            "context": "wow,nice pic!",
            "createAt": null,
            "deleteAt": null,
            "rowGuid": "1",
            "parentGuid": null,
            "children": []
        },
        {
            "id": 2,
            "memberPid": 6,
            "reviewer": "壹壹wo",
            "replier": null,
            "memberId": null,
            "context": "wow,what a nice pic!",
            "createAt": null,
            "deleteAt": null,
            "rowGuid": "2",
            "parentGuid": null,
            "children": [{
                    "id": 3,
                    "memberPid": 2,
                    "reviewer": "PHW",
                    "replier": "壹壹wo",
                    "memberId": 6,
                    "context": "yes,I want to get one!",
                    "createAt": null,
                    "deleteAt": null,
                    "rowGuid": "3",
                    "parentGuid": "2",
                    "children": null
                },
                {
                    "id": 4,
                    "memberPid": 6,
                    "reviewer": "壹壹wo",
                    "replier": "PHW",
                    "memberId": 2,
                    "context": "OK,I will buy one for you!",
                    "createAt": null,
                    "deleteAt": null,
                    "rowGuid": "4",
                    "parentGuid": "3",
                    "children": null
                },
                {
                    "id": 5,
                    "memberPid": 2,
                    "reviewer": "PHW",
                    "replier": "壹壹wo",
                    "memberId": 6,
                    "context": "really,Thank you very muck!",
                    "createAt": null,
                    "deleteAt": null,
                    "rowGuid": "5",
                    "parentGuid": "4",
                    "children": null
                }
            ]
        }
    ]
}

The following is the implementation code

@Override
    public List<CommunityReplyEntity> getAllReplies(Long communityId) {

        // 找出该笔记下面所有的一级评论
        List<CommunityReplyEntity> head = this.list(new QueryWrapper<CommunityReplyEntity>().
                eq("community_id", communityId).
                isNull("parent_guid"));
        // 找出评论下的所有回复
        List<CommunityReplyEntity> collect = head.stream().map(item -> {
            List<ReplyVo> replyEntities = new ArrayList<>();
            // 封装用户名称
            this.getName(item);
            // 将父级评论以及回复结果集作为形参递归查询下一条回复
            replyEntities = getReplies(item, replyEntities);
            // 封装评论的回复结果集
            item.setReplyVos(replyEntities);
            return item;
        }).collect(Collectors.toList());
        return collect;
    }

    // 获得名字
    private void getName(CommunityReplyEntity item) {
        MemberEntity memberPEntity = memberService.getById(item.getMemberPid());
        if (memberPEntity != null) {
            // 评论人
            item.setReviewer(memberPEntity.getNickname());
        }
        MemberEntity memberEntity = memberService.getById(item.getMemberId());
        if (memberEntity != null ) {
            // 被回复人
            item.setReplier(memberEntity.getNickname());
        }
    }

    private List<ReplyVo> getReplies(CommunityReplyEntity replyEntity,  List<ReplyVo> replyEntities) {
        CommunityReplyEntity parentGuid = this.getOne(new QueryWrapper<CommunityReplyEntity>().
                eq("parent_guid", replyEntity.getRowGuid()));
        if (parentGuid != null) {
            // 有回复,封装用户名称
            this.getName(parentGuid);
            // 拷贝数据到响应实体
            ReplyVo replyVo = new ReplyVo();
            BeanUtils.copyProperties(parentGuid, replyVo);
            // 将回复放入回复结果集
            replyEntities.add(replyVo);
        }else {
            // 没有回复,返回回复结果集
            return replyEntities;
        }
        // 将父级评论以及回复结果集作为形参递归查询下一条回复
        return getReplies(parentGuid, replyEntities);
    }

 Record the ways that can be optimized based on the above implementation methods

  1. SQL optimization: In the getCommentsand getRepliesmethods, there are SQL query operations, you can consider optimizing the SQL statement, such as using table connection query, etc., to reduce the number of database queries.
  2. Cache optimization: In the getCommentsand getRepliesmethods, a caching mechanism can be added to cache the query results in the cache system, and the next query can be directly obtained from the cache, reducing the number of database queries.
  3. Paging query: If there is a large amount of comment and reply data, you can consider using paging query, which only queries one page of data at a time, reduces the amount of data, and improves query efficiency.
  4. Database index optimization: Indexing the key fields of comments and replies in the database can speed up the query.
  5. Code logic optimization: In getRepliesthe method, the iterative method can be used instead of the recursive method to reduce the number of method calls and improve performance. It is necessary to select the appropriate optimization scheme according to the specific situation to achieve the best query and maintenance effect.

 

 

 

 

Guess you like

Origin blog.csdn.net/Leungiyee/article/details/129700062