Home  >  Article  >  Java  >  How to implement comment and reply functions in java

How to implement comment and reply functions in java

WBOY
WBOYforward
2023-05-25 14:46:223269browse

Effect display

How to implement comment and reply functions in java

There are two levels of reply in total (reply to comments, reply to replies under comments)

Database design

Comments Table (TFW_Comments), reply content table (TFW_UserResponse) and comment reply relationship table (TFW_MsgRelation)

How to implement comment and reply functions in java

How to implement comment and reply functions in java

Database design ideas:

Note: Readers automatically ignore the service organization ID field of the comment table. This field is equivalent to which post this comment is in (below the article)

1. Query the comment table based on the article ID or post ID. Get the comment (service ID for this article). First level (comments)

To obtain the second level reply (commentsId), you need to query in the relationship table based on the comment ID and the reply type equals 1. The second level (reply under the comment)

Get the third level reply information in the relationship table based on the comment ID, reply type 2, and reply ID. The third level (reply in the reply under the comment) Note: The reply ID is its superior

Implementation class source code

@Override
    public Map<String, Object> findComments(JSONObject jsonObject) {
        data.clear();
        String userId = jsonObject.getString("userId");
 
        String role = this.role(jsonObject);
        if (role.equals("-1")){
            //没有权限
            data.put("error","-1");
            data.put("msg","当前用户没有权限");
            return data;
        }
 
        List<Map<String, Object>> info = commentsDao.findComment(jsonObject.getString("fWJLID"),null);
        //查询点赞次数
        int countTag = 0;
        MsgRelationTag msgRelationTag = new MsgRelationTag();
 
        for (Map item : info){
            item.put("inputShow",false);
            int commentsId = (int) item.get("commentsId");
            //查询点赞次数
             countTag = msgRelationDao.findCountTagByTagId(commentsId,1);
            item.put("countTag",countTag);
            //设置点赞状态
            msgRelationTag.setTagId(commentsId);
            msgRelationTag.setTagType(1);
            msgRelationTag.setTagUserId(Integer.parseInt(userId));
            MsgRelationTag msgTag = msgRelationDao.findMsgTag(msgRelationTag);
            if (msgTag != null) {
                item.put("tagStatus",msgTag.getStatus());
            }else {
                item.put("tagStatus","");
            }
            //如果有@id
            if (item.get("atId") != null){
                String content = item.get("content").toString();
                StringBuffer tmrAtId = findUserName(item.get("atId").toString());
                item.put("content",content+&#39;@&#39;+tmrAtId);
            }
            //二级回复数据
            List<Map<String, Object>> twoReply = new ArrayList<>();
            //所有数据
            List<Map<String, Object>> userResponse = userResponseDao.findUserResponse(commentsId, null, "","",null);
            for (Map userResponseInfo :userResponse){
                int userResponseIds = Integer.parseInt(userResponseInfo.get("userResponseId").toString());
                //查询点赞次数
                countTag = msgRelationDao.findCountTagByTagId(userResponseIds,2);
                //设置点赞状态
                msgRelationTag.setTagId(userResponseIds);
                msgRelationTag.setTagType(2);
                msgTag = msgRelationDao.findMsgTag(msgRelationTag);
                if (msgTag != null) {userResponseInfo.put("tagStatus",msgTag.getStatus());}else {userResponseInfo.put("tagStatus","");}
                userResponseInfo.put("countTag",countTag);
                userResponseInfo.put("inputShow",false);
                Integer responseType = (Integer) userResponseInfo.get("responseType");
                for (Map stairReplyInfo : userResponse){
                    Integer  userResponseId = (Integer) stairReplyInfo.get("userResponseId");
                    int msgRelationId = Integer.parseInt(stairReplyInfo.get("msgRelationId").toString());
                    //接受者id*/
                    twoReply = userResponseDao.findUserResponse(msgRelationId, userResponseId,"1","",null); //二级回复数据
                    for (Map twoReplyItem : twoReply){
                        int twoReplyId = Integer.parseInt(twoReplyItem.get("userResponseId").toString());
                        twoReplyItem.put("inputShow",false);
                        //查询点赞次数
                        countTag = msgRelationDao.findCountTagByTagId(twoReplyId,2);
                        twoReplyItem.put("countTag",countTag);
                        //设置点赞状态
                        msgRelationTag.setTagId(twoReplyId);
                        msgTag = msgRelationDao.findMsgTag(msgRelationTag);
                        if (msgTag != null) {twoReplyItem.put("tagStatus",msgTag.getStatus());}else {twoReplyItem.put("tagStatus","");}
                        String userRepContent = twoReplyItem.get("userRepContent").toString();
                        if (twoReplyItem.get("tmrAtId") != null){
                            StringBuffer tmrAtId = findUserName(twoReplyItem.get("tmrAtId").toString());
                            twoReplyItem.put("userRepContent",userRepContent+&#39;@&#39;+tmrAtId);
                        }
 
                    }
                    stairReplyInfo.put("twoReply",twoReply);
                }
            }
            item.put("stairReply",userResponse);
        }
        data.put("data",info);
        data.put("error",0);
        data.put("msg","查询成功");
        return data;
    }

Other codes can be ignored. The main statements are:

Get the comments under the post

 List<Map<String, Object>> info = commentsDao.findComment(jsonObject.getString("fWJLID"),null);

The above picture gets the comments based on FWJLID. (This can be used as the ID of the post to get the comments under the post) Level 1 display

corresponds to the SQL statement (OPT is my user table)

select tc.content ,tc.commentsId,convert(varchar(19),tc.startTime,120) as startTime,tc.recipientId ,tc.operatorId,zo.NAME as operatorName,tc.atId,zo.HeadImgUrl as operatorHeadImgUrl
         from TFW_Comments tc
         left join zd_opt zo on zo.AID = tc.operatorId where tc.FWJLID = 5101

Query results:

How to implement comment and reply functions in java

Get the reply under the comment

List<Map<String, Object>> userResponse = userResponseDao.findUserResponse(commentsId, null, "","",null);

The above picture gets the reply under the comment based on commentsid. (Get the reply based on the comment ID) Second-level display

Corresponding sql statement

select
 	 tur.userResponseId,tur.operatorId,tur.recipientId,convert(varchar(19),tur.startTime,120) as startTime,tur.userRepContent,tmr.atId as tmrAtId,
 	 tmr.msgRelationId ,tmr.responseType,tmr.replyId,
        zo.NAME as operatorName,
        zo1.NAME as recipientName,
        zo.HeadImgUrl as operatorHeadImgUrl,
        zo1.HeadImgUrl as recipientHeadImgUrl
        from TFW_MsgRelation tmr
        left join TFW_UserResponse tur on tur.userResponseId = tmr.userResponseId
        left join zd_opt zo on zo.AID = tur.operatorId
        left join zd_opt zo1 on zo1.AID = tur.recipientId where tmr.commentsId = 47

Query result

How to implement comment and reply functions in java

Get the second-level reply

 twoReply = userResponseDao.findUserResponse(msgRelationId, userResponseId,"1","",null); //二级回复数据

The above picture is to obtain the secondary reply based on the comment ID (msgRelationId) and reply ID (userResponseId). The reply ID is also the parent class. It is the ID of the reply. The third layer display

corresponds to sql

select
 	 tur.userResponseId,tur.operatorId,tur.recipientId,convert(varchar(19),tur.startTime,120) as startTime,tur.userRepContent,tmr.atId as tmrAtId,
 	 tmr.msgRelationId ,tmr.responseType,tmr.replyId,
        zo.NAME as operatorName,
        zo1.NAME as recipientName,
        zo.HeadImgUrl as operatorHeadImgUrl,
        zo1.HeadImgUrl as recipientHeadImgUrl
        from TFW_MsgRelation tmr
        left join TFW_UserResponse tur on tur.userResponseId = tmr.userResponseId
        left join zd_opt zo on zo.AID = tur.operatorId
        left join zd_opt zo1 on zo1.AID = tur.recipientId where tmr.commentsId = 136 and tmr.replyId = 155

query result

How to implement comment and reply functions in java

##return page display and return body display

How to implement comment and reply functions in java

How to implement comment and reply functions in java

The above is the detailed content of How to implement comment and reply functions in java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete