CommentQuery.java 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. fromBuilder.append("order by c.created desc");
  46. if (params.isEmpty()) {
  47. return DAO.paginate(pageNumber, pageSize, select, fromBuilder.toString());
  48. }
  49. return DAO.paginate(pageNumber, pageSize, select, fromBuilder.toString(), params.toArray());
  50. }
  51. public Page<Comment> paginateWithContentNotInDelete(int pageNumber, int pageSize, String module, String type,
  52. BigInteger contentId, BigInteger parentCommentId) {
  53. String select = " select c.*,content.title content_title,u.username,u.nickname,u.avatar, "
  54. + "quote_comment.text qc_content,quote_comment.author qc_author,"
  55. + "quote_user.username qc_username,quote_user.nickname qc_nickname,quote_user.avatar qc_avatar ";
  56. StringBuilder fromBuilder = new StringBuilder(" from comment c");
  57. fromBuilder.append(" left join content on c.content_id = content.id");
  58. fromBuilder.append(" left join user u on c.user_id = u.id ");
  59. fromBuilder.append(" left join comment quote_comment on c.parent_id = quote_comment.id");
  60. fromBuilder.append(" left join user quote_user on quote_comment.user_id = quote_user.id ");
  61. fromBuilder.append(" WHERE c.status <> '" + Comment.STATUS_DELETE + "' ");
  62. LinkedList<Object> params = new LinkedList<Object>();
  63. appendIfNotEmpty(fromBuilder, " c.`type`", type, params, false);
  64. appendIfNotEmpty(fromBuilder, " c.content_module", module, params, false);
  65. appendIfNotEmpty(fromBuilder, " c.parent_id", parentCommentId, params, false);
  66. appendIfNotEmpty(fromBuilder, " content.id", contentId, params, false);
  67. fromBuilder.append("order by c.created desc");
  68. if (params.isEmpty()) {
  69. return DAO.paginate(pageNumber, pageSize, select, fromBuilder.toString());
  70. }
  71. return DAO.paginate(pageNumber, pageSize, select, fromBuilder.toString(), params.toArray());
  72. }
  73. public Page<Comment> paginateWithContentAndSon(int pageNumber, int pageSize, String module, String type,
  74. BigInteger contentId, BigInteger parentCommentId, Boolean hasSon) {
  75. String select = " select c.*,content.title content_title,u.username,u.nickname,u.avatar, "
  76. + "quote_comment.text qc_content,quote_comment.author qc_author,"
  77. + "quote_user.username qc_username,quote_user.nickname qc_nickname,quote_user.avatar qc_avatar ";
  78. StringBuilder fromBuilder = new StringBuilder(" from comment c");
  79. fromBuilder.append(" left join content on c.content_id = content.id");
  80. fromBuilder.append(" left join user u on c.user_id = u.id ");
  81. fromBuilder.append(" left join comment quote_comment on c.parent_id = quote_comment.id");
  82. fromBuilder.append(" left join user quote_user on quote_comment.user_id = quote_user.id ");
  83. fromBuilder.append(" WHERE c.status <> '" + Comment.STATUS_DELETE + "' ");
  84. if (hasSon == true) {
  85. fromBuilder.append("and c.parent_id is not null ");
  86. } else {
  87. //不找作者本身的回复(需再思考 数量和实际显示)
  88. fromBuilder.append("and c.parent_id is null and c.user_id != content.user_id ");
  89. }
  90. LinkedList<Object> params = new LinkedList<Object>();
  91. appendIfNotEmpty(fromBuilder, " c.`type`", type, params, false);
  92. appendIfNotEmpty(fromBuilder, " c.content_module", module, params, false);
  93. appendIfNotEmpty(fromBuilder, " c.parent_id", parentCommentId, params, false);
  94. appendIfNotEmpty(fromBuilder, " content.id", contentId, params, false);
  95. fromBuilder.append("order by c.created desc");
  96. if (params.isEmpty()) {
  97. return DAO.paginate(pageNumber, pageSize, select, fromBuilder.toString());
  98. }
  99. return DAO.paginate(pageNumber, pageSize, select, fromBuilder.toString(), params.toArray());
  100. }
  101. public Page<Comment> paginateByContentId(int pageNumber, int pageSize, BigInteger contentId) {
  102. return paginateWithContent(pageNumber, pageSize, null, null, contentId, null, Comment.STATUS_NORMAL);
  103. }
  104. public long findCountByContentIdInNormal(BigInteger contentId) {
  105. return findCountByContentId(contentId, Comment.STATUS_NORMAL);
  106. }
  107. public long findCountByContentId(BigInteger contentId, String status) {
  108. return DAO.doFindCount(" content_id = ? and status=? ", contentId, status);
  109. }
  110. public long findCountByParentIdInNormal(BigInteger pId) {
  111. return findCountByParentId(pId, Comment.STATUS_NORMAL);
  112. }
  113. public long findCountByParentId(BigInteger pId, String status) {
  114. return DAO.doFindCount(" parent_id = ? and status=? ", pId, status);
  115. }
  116. public long findCountByUserIdInNormal(BigInteger userId) {
  117. return findCountByUserId(userId, Comment.STATUS_NORMAL);
  118. }
  119. public long findCountByUserId(BigInteger userId, String status) {
  120. return DAO.doFindCount(" user_id = ? and status=? ", userId, status);
  121. }
  122. public Comment findById(final Object idValue) {
  123. return DAO.getCache(idValue, new IDataLoader() {
  124. @Override
  125. public Object load() {
  126. return DAO.findById(idValue);
  127. }
  128. });
  129. }
  130. public long findCountByModule(String module) {
  131. return DAO.doFindCount("content_module = ?", module);
  132. }
  133. public long findCountInNormalByModule(String module) {
  134. return DAO.doFindCount("content_module = ? AND status <> ?", module, Comment.STATUS_DELETE);
  135. }
  136. public long findCountByModuleAndStatus(String module, String status) {
  137. return DAO.doFindCount("content_module = ? and status=?", module, status);
  138. }
  139. public long findCountByModuleAndSonAndContentId(BigInteger contentId, String module, boolean hasSon) {
  140. io.jpress.model.Content content = DAO.getContentById(contentId);
  141. BigInteger authorUserId = null;
  142. if (content != null) {
  143. authorUserId = content.getUserId();
  144. }
  145. Long count = new Long(0);
  146. if (hasSon == true) {
  147. count = DAO.doFindCount("content_module = ? and parent_id is not null and content_id = ?", module ,contentId);
  148. } else {
  149. if (authorUserId == null) {
  150. count = DAO.doFindCount("content_module = ? and parent_id is null and content_id = ? ", module ,contentId);
  151. } else {
  152. count = DAO.doFindCount("content_module = ? and parent_id is null and content_id = ? and user_id != ?", module ,contentId ,authorUserId);
  153. }
  154. }
  155. return count;
  156. }
  157. public long findCountByModuleAndStatusAndContentId(BigInteger contentId, String module, String status) {
  158. return DAO.doFindCount(" content_id = ? and content_module = ? and status=?",contentId, module, status);
  159. }
  160. public long findCountByModuleAndStatusAndContentId(BigInteger contentId, String module, boolean parent) {
  161. Long count = new Long(0);
  162. if (parent == true) {
  163. count = DAO.doFindCount("content_module = ? and parent_id is not null and content_id = " + contentId, module);
  164. } else {
  165. count = DAO.doFindCount("content_module = ? and parent_id is null and content_id = " + contentId, module);
  166. }
  167. return count;
  168. }
  169. }