首頁 > Java > java教程 > 主體

java怎麼實現評論和回應功能

WBOY
發布: 2023-05-25 14:46:22
轉載
3320 人瀏覽過

效果展示

java怎麼實現評論和回應功能

總共是兩層回覆(回覆留言、回覆留言下的回覆)

資料庫設計

#評論表(TFW_Comments)和回覆內容表(TFW_UserResponse)以及評論回覆關係表(TFW_MsgRelation)

java怎麼實現評論和回應功能

java怎麼實現評論和回應功能

資料庫設計想法:

註:各位讀者自動忽略評論表的服務機構ID字段,這個字段相當於這條評論是在哪個帖子(文章下面)

1、根據文章ID或者是帖子ID查詢評論表取得評論(本文的服務機構ID)。第一層(評論)

取得第二層回覆(commentsId),需要根據評論ID且回覆類型等於1,在關係表中查詢。第二層(評論下的回覆)

根據評論ID、回覆類型為2、以及回覆ID在關係表中取得第三層回覆資訊。第三層(評論下回覆中的回覆)註:回覆ID是它的上級

實作類別原始碼

@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;
    }
登入後複製

其它的程式碼可以忽略。主要語句有:

取得貼文下的評論

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

上圖根據FWJLID取得評論。 (這裡可以當成帖子的ID,獲取帖子下的評論)一級展示

對應SQL語句(OPT是我的用戶表)

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
登入後複製

查詢結果:

java怎麼實現評論和回應功能

 取得評論下的回應

List<Map<String, Object>> userResponse = userResponseDao.findUserResponse(commentsId, null, "","",null);
登入後複製

上圖根據commentsid取得評論下的回應。 (根據評論ID取得回應)二級展示

對應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 = 47
登入後複製

查詢結果

java怎麼實現評論和回應功能

## 取得二級回應

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

上圖是根據評論ID(msgRelationId)和回覆ID(userResponseId)去取得二級回應。回覆ID也就是父類別。就是回覆那一條回覆的ID。第三層展示

對應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
登入後複製
查詢結果

java怎麼實現評論和回應功能

#回傳頁面展示與回傳體展示

java怎麼實現評論和回應功能

java怎麼實現評論和回應功能

#####

以上是java怎麼實現評論和回應功能的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:yisu.com
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板