代码
/*
* (non-Javadoc)
*
* @see
* com.doudou.database.service.post.IRepostService#queryWarnPostComments
* (int, int, java.lang.String)
*/
public Pager queryWarnPostComments(int pageNo, int pageSize,
String accountId) throws BusinessException {
try {
Pager pager = new Pager(pageNo, pageSize);
int thisPage = (pageNo - 1) * pageSize;
// 获取该人所有的帖子
List<Post> postList = postRepository.getAccountPost(accountId);
if (postList.size() == 0) {
throw new BusinessException("还未发表帖子!");
}
HashMap<Integer, Post> postHashMap = new HashMap<Integer, Post>();
List<Integer> postIdList = new ArrayList<Integer>();
// 将所有帖子id放入PostIdList中,并将帖子信息缓存到hashmap中
for (Post post : postList) {
postIdList.add(post.getPostId());
postHashMap.put(post.getPostId(), post);
}
// 查询帖子的所有评论贴,并按评论贴的发帖时间进行降序排序
StringBuilder listSql = new StringBuilder(
"select * from Repost repost where repost.postId in (:list)"
+ " order by repost.repostTime DESC");
Query queryList = manager
.createNativeQuery(listSql.toString(), Repost.class)
.setFirstResult(thisPage).setMaxResults(pageSize);
queryList.setParameter("list", postIdList);
@SuppressWarnings("unchecked")
List<Repost> repostList = queryList.getResultList();
if (repostList.size() == 0) {
throw new BusinessException("还没有人评论你的帖子");
}
// 将评论贴与原帖进行相关联
for (Repost repost : repostList) {
repost.setPost(postHashMap.get(repost.getPostId()));
}
// 获取所有的总数
StringBuilder countSql = new StringBuilder(
"select count(repost.postId) from Repost repost ,Post post where post.postId=repost.repostId and post.accountId=:accountId");
Query query = manager.createNativeQuery(countSql.toString());
query.setParameter("accountId", accountId);
pager.setTotalCount(((BigInteger) query.getSingleResult())
.intValue());
pager.setItems(repostList);
return pager;
} catch (BusinessException e) {
throw e;
} catch (Exception e) {
logger.error("query database error", e);
throw new BusinessException("query database error");
}
}