CommentQuery.java 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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 java.math.BigInteger;
  18. import java.util.LinkedList;
  19. import com.jfinal.plugin.activerecord.Page;
  20. import com.jfinal.plugin.ehcache.IDataLoader;
  21. import io.jpress.model.Comment;
  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> paginateByContentId(int pageNumber, int pageSize, BigInteger contentId) {
  74. return paginateWithContent(pageNumber, pageSize, null, null, contentId, null, Comment.STATUS_NORMAL);
  75. }
  76. public long findCountByContentIdInNormal(BigInteger contentId) {
  77. return findCountByContentId(contentId, Comment.STATUS_NORMAL);
  78. }
  79. public long findCountByContentId(BigInteger contentId, String status) {
  80. return DAO.doFindCount(" content_id = ? and status=? ", contentId, status);
  81. }
  82. public long findCountByParentIdInNormal(BigInteger pId) {
  83. return findCountByParentId(pId, Comment.STATUS_NORMAL);
  84. }
  85. public long findCountByParentId(BigInteger pId, String status) {
  86. return DAO.doFindCount(" parent_id = ? and status=? ", pId, status);
  87. }
  88. public long findCountByUserIdInNormal(BigInteger userId) {
  89. return findCountByUserId(userId, Comment.STATUS_NORMAL);
  90. }
  91. public long findCountByUserId(BigInteger userId, String status) {
  92. return DAO.doFindCount(" user_id = ? and status=? ", userId, status);
  93. }
  94. public Comment findById(final Object idValue) {
  95. return DAO.getCache(idValue, new IDataLoader() {
  96. @Override
  97. public Object load() {
  98. return DAO.findById(idValue);
  99. }
  100. });
  101. }
  102. public long findCountByModule(String module) {
  103. return DAO.doFindCount("content_module = ?", module);
  104. }
  105. public long findCountInNormalByModule(String module) {
  106. return DAO.doFindCount("content_module = ? AND status <> ?", module, Comment.STATUS_DELETE);
  107. }
  108. public long findCountByModuleAndStatus(String module, String status) {
  109. return DAO.doFindCount("content_module = ? and status=?", module, status);
  110. }
  111. }