优化代码,改bug

master
Zhaoxiaoan 5 years ago
parent c0e76ae527
commit c4760f7a59

@ -37,7 +37,7 @@ public class IDoCheerMomentController extends BaseController {
@GetMapping("/list")
public TableDataInfo list(){
startPage();
List<Map<String, Object>> mapList = cheerService.momentListReSponse();
List<Map<String, Object>> mapList = cheerService.momentListResponse();
return getDataTable(mapList);
}
/**

@ -3,6 +3,7 @@ package com.hpjpw.idoapp.service;
import com.hpjpw.ido.domain.CheerComment;
import com.hpjpw.ido.domain.CheerMoment;
import com.hpjpw.ido.domain.CheerThumb;
import com.hpjpw.ido.domain.Topic;
import com.hpjpw.ido.service.ICheerCommentService;
import com.hpjpw.ido.service.ICheerMomentService;
import com.hpjpw.ido.service.ICheerThumbService;
@ -12,7 +13,10 @@ import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service
public class IDoCheerService {
@ -27,73 +31,70 @@ public class IDoCheerService {
@Autowired
ITopicService topicService;
/* 将所有返回动态相关信息存到map中 */
public Map<String, Object> momentMap(CheerMoment item){
Map<String, Object> map = new HashMap<>();
/* 返回动态id动态发布者id/姓名/头像,动态主题内容,动态内容及图片,动态发布时间 */
map.put("id", item.getId());
map.put("reportId", item.getReportId());
map.put("reportName", sysUserService.selectUserById(item.getReportId()).getNickName());
map.put("reportAvatar", sysUserService.selectUserById(item.getReportId()).getAvatar());
/* 没有主题则传null值 */
Long topicId = item.getTopicId();
if (StringUtils.isNotNull(topicId)) {
Topic topic = topicService.selectTopicById(topicId);
if (StringUtils.isNotNull(topic)) {
map.put("topic", topic.getContent());
} else {
map.put("topic", null);
}
} else {
map.put("topic", null);
}
map.put("content", item.getContent());
map.put("picture1", item.getPicture1());
map.put("picture2", item.getPicture2());
map.put("picture3", item.getPicture3());
map.put("time", item.getTime());
/* 返回所有点赞id点赞者id和姓名以及点赞总数量 */
List<Map<String, String>> thumbList = new ArrayList<>();
CheerThumb cheerThumb = new CheerThumb();
cheerThumb.setMomentId(item.getId());
List<CheerThumb> cheerThumbList = cheerThumbService.selectCheerThumbList(cheerThumb);
for (CheerThumb thumb : cheerThumbList) {
Map<String, String> mapOfThumb = new HashMap<>();
mapOfThumb.put("thumbId", thumb.getId());
mapOfThumb.put("thumbedId", thumb.getThumbedId().toString());
mapOfThumb.put("thumbedName", thumb.getThumbedName());
thumbList.add(mapOfThumb);
}
map.put("thumbNum", cheerThumbList.size());
map.put("thumbs", thumbList);
/* 返回所有评论具体评论id、评论者id、名称、头像、评论内容以及总评论数 */
List<Map<String, String>> commentList = new ArrayList<>();
CheerComment cheerComment = new CheerComment();
cheerComment.setMomentId(item.getId());
List<CheerComment> cheerCommentList = cheerCommentService.selectCheerCommentList(cheerComment);
for (int i = 0; i < cheerCommentList.size(); i++) {
IDoPersonalMomentService.getCheerComment(commentList, cheerCommentList, i, sysUserService);
}
map.put("commentNum", cheerCommentList.size());
map.put("comments", commentList);
return map;
}
/* 针对前端的加油站动态请求服务,一次性返回相关数据 */
public List<Map<String,Object>> momentListReSponse(){
public List<Map<String, Object>> momentListResponse() {
/* 实际返回的列表 */
List<Map<String,Object>> listReturn = new ArrayList<>();
List<Map<String, Object>> listReturn = new ArrayList<>();
/* 获取所有动态 */
List<CheerMoment> cheerMomentList = cheerMomentService.selectCheerMomentList(new CheerMoment());
System.out.println("cheerMomentList=>"+cheerMomentList);
CheerMoment cheerMoment = new CheerMoment();
cheerMoment.setStatus("1");
List<CheerMoment> cheerMomentList = cheerMomentService.selectCheerMomentList(cheerMoment);
System.out.println("cheerMomentList=>" + cheerMomentList);
/* 对每一条加油站动态插入map数据 */
for (CheerMoment item :cheerMomentList) {
/* 审核通过 */
if (item.getStatus().equals("1")){
Map<String,Object> map = new HashMap<>();
/* 返回动态id动态发布者id/姓名/头像,动态主题内容,动态内容及图片,动态发布时间 */
map.put("id",item.getId());
map.put("reportId",item.getReportId());
map.put("reportName",sysUserService.selectUserById(item.getReportId()).getNickName());
map.put("reportAvatar",sysUserService.selectUserById(item.getReportId()).getAvatar());
/* 没有主题则传null值 */
if (StringUtils.isNotNull(item.getTopicId())){
map.put("topic",topicService.selectTopicById(item.getTopicId()).getContent());
}else {
map.put("topic",null);
}
map.put("content",item.getContent());
map.put("picture1",item.getPicture1());
map.put("picture2",item.getPicture2());
map.put("picture3",item.getPicture3());
map.put("time",item.getTime());
/* 返回所有点赞id点赞者id和姓名以及点赞总数量 */
List<Map<String,String>> thumbList = new ArrayList<>();
CheerThumb cheerThumb = new CheerThumb();
cheerThumb.setMomentId(item.getId());
List<CheerThumb> cheerThumbList = cheerThumbService.selectCheerThumbList(cheerThumb);
for (CheerThumb thumb :cheerThumbList) {
Map<String,String> mapOfThumb = new HashMap<>();
mapOfThumb.put("thumbId",thumb.getId());
mapOfThumb.put("thumbedId",thumb.getThumbedId().toString());
mapOfThumb.put("thumbedName", thumb.getThumbedName());
thumbList.add(mapOfThumb);
}
map.put("thumbNum",cheerThumbList.size());
map.put("thumbs",thumbList);
/* 默认返回最多3条评论返回具体评论id、评论者id、名称、头像、评论内容以及总评论数 */
/* 修改>>返回所有评论数据 */
List<Map<String,String>> commentList = new ArrayList<>();
CheerComment cheerComment = new CheerComment();
cheerComment.setMomentId(item.getId());
List<CheerComment> cheerCommentList = cheerCommentService.selectCheerCommentList(cheerComment);
int defaultComment = cheerCommentList.size();
for (int i = 0; i < defaultComment; i++) {
/* 如果当前评论为空则直接退出 */
/*if (i>=cheerCommentList.size()){
break;
}*/
CheerComment comment = cheerCommentList.get(i);
Map<String,String> mapOfComment = new HashMap<>();
mapOfComment.put("commentId",comment.getId().toString());
mapOfComment.put("commentatorId",comment.getFromId().toString());
mapOfComment.put("commentatorName",sysUserService.selectUserById(comment.getFromId()).getNickName());
mapOfComment.put("commentatorAvatar",sysUserService.selectUserById(comment.getFromId()).getAvatar());
mapOfComment.put("content", comment.getContent());
commentList.add(mapOfComment);
}
map.put("commentNum",cheerCommentList.size());
map.put("comments",commentList);
listReturn.add(map);
}
for (CheerMoment item : cheerMomentList) {
listReturn.add(momentMap(item));
}
/* 对结果做一次反转,即按照最近的时间排序,并将此结果返回 */
/*Collections.reverse(listReturn);*/
@ -101,78 +102,32 @@ public class IDoCheerService {
}
/* 针对前端的撕逼墙具体动态的评论请求服务 */
public List<Map<String,String>> commentListResponse(Long momentId){
public List<Map<String, String>> commentListResponse(Long momentId) {
CheerComment cheerComment = new CheerComment();
cheerComment.setMomentId(momentId);
List<CheerComment> cheerCommentList = cheerCommentService.selectCheerCommentList(cheerComment);
List<Map<String,String>> listReturn = new ArrayList<>();
List<Map<String, String>> listReturn = new ArrayList<>();
/* 返回每一条评论的动态id评论id评论内容评论者id评论者姓名评论者头像 */
for (CheerComment item :cheerCommentList) {
Map<String,String> map = new HashMap<>();
map.put("momentId",item.getMomentId().toString());
map.put("commentId",item.getId().toString());
map.put("content",item.getContent());
map.put("commentatorId",item.getFromId().toString());
map.put("commentatorName",sysUserService.selectUserById(item.getFromId()).getNickName());
map.put("commentatorAvatar",sysUserService.selectUserById(item.getFromId()).getAvatar());
for (CheerComment item : cheerCommentList) {
Map<String, String> map = new HashMap<>();
map.put("momentId", item.getMomentId().toString());
map.put("commentId", item.getId().toString());
map.put("content", item.getContent());
map.put("commentatorId", item.getFromId().toString());
map.put("commentatorName", sysUserService.selectUserById(item.getFromId()).getNickName());
map.put("commentatorAvatar", sysUserService.selectUserById(item.getFromId()).getAvatar());
listReturn.add(map);
}
return listReturn;
}
/* 针对某一动态id(此处确定存在此动态controller中添加安全校验),返回该动态所有内容 */
public Map<String,Object> momentResponse(Long momentId){
public Map<String, Object> momentResponse(Long momentId) {
Map<String, Object> map = new HashMap<>();
CheerMoment item = cheerMomentService.selectCheerMomentById(momentId);
/* 审核通过 */
if (item.getStatus().equals("1")) {
/* 返回动态id动态发布者id/姓名/头像,动态主题内容,动态内容及图片,动态发布时间 */
map.put("id", item.getId());
map.put("reportId", item.getReportId());
map.put("reportName", sysUserService.selectUserById(item.getReportId()).getNickName());
map.put("reportAvatar", sysUserService.selectUserById(item.getReportId()).getAvatar());
/* 没有主题则传null值 */
if (StringUtils.isNotNull(item.getTopicId())) {
map.put("topic", topicService.selectTopicById(item.getTopicId()).getContent());
} else {
map.put("topic", null);
}
map.put("content", item.getContent());
map.put("picture1", item.getPicture1());
map.put("picture2", item.getPicture2());
map.put("picture3", item.getPicture3());
map.put("time", item.getTime());
/* 返回所有点赞id点赞者id和姓名以及点赞总数量 */
List<Map<String, String>> thumbList = new ArrayList<>();
CheerThumb cheerThumb = new CheerThumb();
cheerThumb.setMomentId(item.getId());
List<CheerThumb> cheerThumbList = cheerThumbService.selectCheerThumbList(cheerThumb);
for (CheerThumb thumb : cheerThumbList) {
Map<String, String> mapOfThumb = new HashMap<>();
mapOfThumb.put("thumbId", thumb.getId());
mapOfThumb.put("thumbedId", thumb.getThumbedId().toString());
mapOfThumb.put("thumbedName", thumb.getThumbedName());
thumbList.add(mapOfThumb);
}
map.put("thumbNum", cheerThumbList.size());
map.put("thumbs", thumbList);
/* 返回所有评论具体评论id、评论者id、名称、头像、评论内容以及总评论数 */
List<Map<String, String>> commentList = new ArrayList<>();
CheerComment cheerComment = new CheerComment();
cheerComment.setMomentId(item.getId());
List<CheerComment> cheerCommentList = cheerCommentService.selectCheerCommentList(cheerComment);
for (int i = 0; i < cheerCommentList.size(); i++) {
CheerComment comment = cheerCommentList.get(i);
Map<String, String> mapOfComment = new HashMap<>();
mapOfComment.put("commentId", comment.getId().toString());
mapOfComment.put("commentatorId", comment.getFromId().toString());
mapOfComment.put("commentatorName", sysUserService.selectUserById(comment.getFromId()).getNickName());
mapOfComment.put("commentatorAvatar", sysUserService.selectUserById(comment.getFromId()).getAvatar());
mapOfComment.put("content", comment.getContent());
commentList.add(mapOfComment);
}
map.put("commentNum", cheerCommentList.size());
map.put("comments", commentList);
map = momentMap(item);
}
return map;
}

@ -57,6 +57,10 @@ public class IDoPersonalMomentService {
}
}
/* 3.最终按发布时间重排动态列表 */
return getMaps(listReturn);
}
private List<Map<String, Object>> getMaps(List<Map<String, Object>> listReturn) {
listReturn.sort(new Comparator<Map<String, Object>>() {
@Override
public int compare(Map<String, Object> o1, Map<String, Object> o2) {
@ -67,8 +71,6 @@ public class IDoPersonalMomentService {
return date1.before(date2) ? 1 : -1;
}
});
return listReturn;
}
@ -96,18 +98,7 @@ public class IDoPersonalMomentService {
}
}
/* 3.对所有动态排序 */
listReturn.sort(new Comparator<Map<String, Object>>() {
@Override
public int compare(Map<String, Object> o1, Map<String, Object> o2) {
Date date1 = new Date(o1.get("time").toString());
Date date2 = new Date(o2.get("time").toString());
/*Date date2 = (Date) o2.get("time");*/
/* >0则升序排列需要按时间降序排列 */
return date1.before(date2) ? 1 : -1;
}
});
return listReturn;
return getMaps(listReturn);
}
/* 获取一个撕逼墙动态并返回一组Map<String,Object> */
@ -149,8 +140,7 @@ public class IDoPersonalMomentService {
anonymousComment.setMomentId(item.getId());
List<AnonymousComment> anonymousCommentList = anonymousCommentService.selectAnonymousCommentList(anonymousComment);
/* 返回所有评论 */
int defaultComment = anonymousCommentList.size();
for (int i = 0; i < defaultComment; i++) {
for (int i = 0; i < anonymousCommentList.size(); i++) {
AnonymousComment comment = anonymousCommentList.get(i);
Map<String, String> mapOfComment = new HashMap<>();
mapOfComment.put("commentId", null);
@ -208,17 +198,21 @@ public class IDoPersonalMomentService {
List<CheerComment> cheerCommentList = cheerCommentService.selectCheerCommentList(cheerComment);
int defaultComment = cheerCommentList.size();
for (int i = 0; i < defaultComment; i++) {
CheerComment comment = cheerCommentList.get(i);
Map<String, String> mapOfComment = new HashMap<>();
mapOfComment.put("commentId", comment.getId().toString());
mapOfComment.put("commentatorId", comment.getFromId().toString());
mapOfComment.put("commentatorName", sysUserService.selectUserById(comment.getFromId()).getNickName());
mapOfComment.put("commentatorAvatar", sysUserService.selectUserById(comment.getFromId()).getAvatar());
mapOfComment.put("content", comment.getContent());
commentList.add(mapOfComment);
getCheerComment(commentList, cheerCommentList, i, sysUserService);
}
map.put("comments", commentList);
map.put("commentNum", cheerCommentList.size());
return map;
}
static void getCheerComment(List<Map<String, String>> commentList, List<CheerComment> cheerCommentList, int i, ISysUserService sysUserService) {
CheerComment comment = cheerCommentList.get(i);
Map<String, String> mapOfComment = new HashMap<>();
mapOfComment.put("commentId", comment.getId().toString());
mapOfComment.put("commentatorId", comment.getFromId().toString());
mapOfComment.put("commentatorName", sysUserService.selectUserById(comment.getFromId()).getNickName());
mapOfComment.put("commentatorAvatar", sysUserService.selectUserById(comment.getFromId()).getAvatar());
mapOfComment.put("content", comment.getContent());
commentList.add(mapOfComment);
}
}
Loading…
Cancel
Save