CommentQuery.java 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /**
  2. * Copyright (c) 2015-2016, Michael Yang 杨福海 (fuhai999@gmail.com).
  3. *
  4. * Licensed under the GNU Lesser General Public License (LGPL) ,Version 3.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.gnu.org/licenses/lgpl-3.0.txt
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package io.jpress.model.query;
  17. import com.jfinal.plugin.activerecord.Page;
  18. import com.jfinal.plugin.ehcache.IDataLoader;
  19. import io.jpress.model.Comment;
  20. import java.math.BigInteger;
  21. import java.util.LinkedList;
  22. public class CommentQuery extends JBaseQuery {
  23. protected static final Comment DAO = new Comment();
  24. private static final CommentQuery QUERY = new CommentQuery();
  25. public static CommentQuery me() {
  26. return QUERY;
  27. }
  28. public Page<Comment> paginateWithContent(int pageNumber, int pageSize, String module, String type,
  29. BigInteger contentId, BigInteger parentCommentId, String status) {
  30. String select = " select c.*,content.title content_title,u.username,u.nickname,u.avatar, "
  31. + "quote_comment.text qc_content,quote_comment.author qc_author,"
  32. + "quote_user.username qc_username,quote_user.nickname qc_nickname,quote_user.avatar qc_avatar ";
  33. StringBuilder fromBuilder = new StringBuilder(" from comment c");
  34. fromBuilder.append(" left join content on c.content_id = content.id");
  35. fromBuilder.append(" left join user u on c.user_id = u.id ");
  36. fromBuilder.append(" left join comment quote_comment on c.parent_id = quote_comment.id");
  37. fromBuilder.append(" left join user quote_user on quote_comment.user_id = quote_user.id ");
  38. LinkedList<Object> params = new LinkedList<Object>();
  39. boolean needWhere = true;
  40. needWhere = appendIfNotEmpty(fromBuilder, " c.`type`", type, params, needWhere);
  41. needWhere = appendIfNotEmpty(fromBuilder, " c.content_module", module, params, needWhere);
  42. needWhere = appendIfNotEmpty(fromBuilder, " c.`status`", status, params, needWhere);
  43. needWhere = appendIfNotEmpty(fromBuilder, " c.parent_id", parentCommentId, params, needWhere);
  44. needWhere = appendIfNotEmpty(fromBuilder, " content.id", contentId, params, needWhere);
  45. if ("uuhelper".equals(module)) {
  46. //软文详情页评论排序,先按是否置顶排,再按点赞数排,最后按评论时间排。
  47. fromBuilder.append("ORDER BY c.order_number desc,c.vote_up desc, c.created desc");
  48. } else {
  49. fromBuilder.append("order by c.created desc");
  50. }
  51. if (params.isEmpty()) {
  52. return DAO.paginate(pageNumber, pageSize, select, fromBuilder.toString());
  53. }
  54. return DAO.paginate(pageNumber, pageSize, select, fromBuilder.toString(), params.toArray());
  55. }
  56. public Page<Comment> paginateWithContentNotInDelete(int pageNumber, int pageSize, String module, String type,
  57. BigInteger contentId, BigInteger parentCommentId) {
  58. String select = " select c.*,content.title content_title,u.username,u.nickname,u.avatar, "
  59. + "quote_comment.text qc_content,quote_comment.author qc_author,"
  60. + "quote_user.username qc_username,quote_user.nickname qc_nickname,quote_user.avatar qc_avatar ";
  61. StringBuilder fromBuilder = new StringBuilder(" from comment c");
  62. fromBuilder.append(" left join content on c.content_id = content.id");
  63. fromBuilder.append(" left join user u on c.user_id = u.id ");
  64. fromBuilder.append(" left join comment quote_comment on c.parent_id = quote_comment.id");
  65. fromBuilder.append(" left join user quote_user on quote_comment.user_id = quote_user.id ");
  66. fromBuilder.append(" WHERE c.status <> '" + Comment.STATUS_DELETE + "' ");
  67. LinkedList<Object> params = new LinkedList<Object>();
  68. appendIfNotEmpty(fromBuilder, " c.`type`", type, params, false);
  69. appendIfNotEmpty(fromBuilder, " c.content_module", module, params, false);
  70. appendIfNotEmpty(fromBuilder, " c.parent_id", parentCommentId, params, false);
  71. appendIfNotEmpty(fromBuilder, " content.id", contentId, params, false);
  72. fromBuilder.append("order by c.created desc");
  73. if (params.isEmpty()) {
  74. return DAO.paginate(pageNumber, pageSize, select, fromBuilder.toString());
  75. }
  76. return DAO.paginate(pageNumber, pageSize, select, fromBuilder.toString(), params.toArray());
  77. }
  78. public Page<Comment> paginateWithContentAndSon(int pageNumber, int pageSize, String module, String type,
  79. BigInteger contentId, BigInteger parentCommentId, Boolean hasSon) {
  80. String select = " select c.*,content.title content_title,u.username,u.nickname,u.avatar, "
  81. + "quote_comment.text qc_content,quote_comment.author qc_author,"
  82. + "quote_user.username qc_username,quote_user.nickname qc_nickname,quote_user.avatar qc_avatar ";
  83. StringBuilder fromBuilder = new StringBuilder(" from comment c");
  84. fromBuilder.append(" left join content on c.content_id = content.id");
  85. fromBuilder.append(" left join user u on c.user_id = u.id ");
  86. fromBuilder.append(" left join comment quote_comment on c.parent_id = quote_comment.id");
  87. fromBuilder.append(" left join user quote_user on quote_comment.user_id = quote_user.id ");
  88. fromBuilder.append(" WHERE c.status <> '" + Comment.STATUS_DELETE + "' ");
  89. if (hasSon == true) {
  90. fromBuilder.append("and c.parent_id is not null ");
  91. } else {
  92. //不找作者本身的回复(需再思考 数量和实际显示)
  93. fromBuilder.append("and c.parent_id is null and c.user_id != content.user_id ");
  94. }
  95. LinkedList<Object> params = new LinkedList<Object>();
  96. appendIfNotEmpty(fromBuilder, " c.`type`", type, params, false);
  97. appendIfNotEmpty(fromBuilder, " c.content_module", module, params, false);
  98. appendIfNotEmpty(fromBuilder, " c.parent_id", parentCommentId, params, false);
  99. appendIfNotEmpty(fromBuilder, " content.id", contentId, params, false);
  100. fromBuilder.append("order by c.created desc");
  101. if (params.isEmpty()) {
  102. return DAO.paginate(pageNumber, pageSize, select, fromBuilder.toString());
  103. }
  104. return DAO.paginate(pageNumber, pageSize, select, fromBuilder.toString(), params.toArray());
  105. }
  106. public Page<Comment> paginateByContentId(int pageNumber, int pageSize, BigInteger contentId) {
  107. return paginateWithContent(pageNumber, pageSize, null, null, contentId, null, Comment.STATUS_NORMAL);
  108. }
  109. public Page<Comment> uuHelperPaginateByContentId(int pageNumber, int pageSize, BigInteger contentId, String module) {
  110. return paginateWithContent(pageNumber, pageSize, module, null, contentId, null, Comment.STATUS_NORMAL);
  111. }
  112. public long findCountByContentIdInNormal(BigInteger contentId) {
  113. return findCountByContentId(contentId, Comment.STATUS_NORMAL);
  114. }
  115. public long findCountByContentId(BigInteger contentId, String status) {
  116. return DAO.doFindCount(" content_id = ? and status=? ", contentId, status);
  117. }
  118. public long findCountByParentIdInNormal(BigInteger pId) {
  119. return findCountByParentId(pId, Comment.STATUS_NORMAL);
  120. }
  121. public long findCountByParentId(BigInteger pId, String status) {
  122. return DAO.doFindCount(" parent_id = ? and status=? ", pId, status);
  123. }
  124. public long findCountByUserIdInNormal(BigInteger userId) {
  125. return findCountByUserId(userId, Comment.STATUS_NORMAL);
  126. }
  127. public long findCountByUserId(BigInteger userId, String status) {
  128. return DAO.doFindCount(" user_id = ? and status=? ", userId, status);
  129. }
  130. public Comment findById(final Object idValue) {
  131. return DAO.getCache(idValue, new IDataLoader() {
  132. @Override
  133. public Object load() {
  134. return DAO.findById(idValue);
  135. }
  136. });
  137. }
  138. public long findCountByModule(String module) {
  139. return DAO.doFindCount("content_module = ?", module);
  140. }
  141. public long findCountInNormalByModule(String module) {
  142. return DAO.doFindCount("content_module = ? AND status <> ?", module, Comment.STATUS_DELETE);
  143. }
  144. public long findCountByModuleAndStatus(String module, String status) {
  145. return DAO.doFindCount("content_module = ? and status=?", module, status);
  146. }
  147. public long findCountByModuleAndSonAndContentId(BigInteger contentId, String module, boolean hasSon) {
  148. io.jpress.model.Content content = DAO.getContentById(contentId);
  149. BigInteger authorUserId = null;
  150. if (content != null) {
  151. authorUserId = content.getUserId();
  152. }
  153. Long count = new Long(0);
  154. if (hasSon == true) {
  155. count = DAO.doFindCount("content_module = ? and parent_id is not null and content_id = ?", module ,contentId);
  156. } else {
  157. if (authorUserId == null) {
  158. count = DAO.doFindCount("content_module = ? and parent_id is null and content_id = ? ", module ,contentId);
  159. } else {
  160. count = DAO.doFindCount("content_module = ? and parent_id is null and content_id = ? and user_id != ?", module ,contentId ,authorUserId);
  161. }
  162. }
  163. return count;
  164. }
  165. public long findCountByModuleAndStatusAndContentId(BigInteger contentId, String module, String status) {
  166. return DAO.doFindCount(" content_id = ? and content_module = ? and status=?",contentId, module, status);
  167. }
  168. public long findCountByModuleAndStatusAndContentId(BigInteger contentId, String module, boolean parent) {
  169. Long count = new Long(0);
  170. if (parent == true) {
  171. count = DAO.doFindCount("content_module = ? and parent_id is not null and content_id = " + contentId, module);
  172. } else {
  173. count = DAO.doFindCount("content_module = ? and parent_id is null and content_id = " + contentId, module);
  174. }
  175. return count;
  176. }
  177. public long findCountByContentIdAndUuUserId(BigInteger contentId, BigInteger uuUserId) {
  178. return DAO.doFindCount(" content_id = ? and uu_user_id=? ", contentId, uuUserId);
  179. }
  180. }