ContentQuery.java 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908
  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.activerecord.Record;
  19. import com.jfinal.plugin.ehcache.IDataLoader;
  20. import io.jpress.model.Content;
  21. import io.jpress.model.Taxonomy;
  22. import io.jpress.model.core.Jdb;
  23. import io.jpress.model.vo.Archive;
  24. import io.jpress.template.TemplateManager;
  25. import io.jpress.utils.StringUtils;
  26. import java.math.BigInteger;
  27. import java.util.ArrayList;
  28. import java.util.LinkedList;
  29. import java.util.List;
  30. public class ContentQuery extends JBaseQuery {
  31. protected static final Content DAO = new Content();
  32. private static final ContentQuery QUERY = new ContentQuery();
  33. public static ContentQuery me() {
  34. return QUERY;
  35. }
  36. public boolean deleteById(BigInteger id) {
  37. Content c = findById(id);
  38. if (c != null)
  39. return c.delete();
  40. return false;
  41. }
  42. public Page<Content> paginateByModule(int page, int pagesize, String module) {
  43. return paginate(page, pagesize, module, null, null, null, null, null);
  44. }
  45. public Page<Content> paginateByModuleAndStatus(int page, int pagesize, String module, String status,
  46. String orderBy) {
  47. return paginate(page, pagesize, module, null, status, null, null, orderBy);
  48. }
  49. public Page<Content> paginateByModuleAndStatus(int page, int pagesize, String module, String status) {
  50. return paginate(page, pagesize, module, null, status, null, null, null);
  51. }
  52. public Page<Content> paginateBySearch(int page, int pagesize, String module, String keyword, String status,
  53. BigInteger[] tids, String month) {
  54. String[] modules = StringUtils.isNotBlank(module) ? new String[] { module } : null;
  55. return paginate(page, pagesize, modules, keyword, status, tids, null, month, null);
  56. }
  57. public Page<Content> uuHelperPaginateBySearch(int page, int pagesize, String module, String keyword,
  58. String pushTime, String author, String copyRight, String taxonomy,
  59. String status, BigInteger[] tids, String month) {
  60. String[] modules = StringUtils.isNotBlank(module) ? new String[] { module } : null;
  61. return uuHelperPaginate(page, pagesize, modules, keyword, pushTime, author, copyRight, taxonomy, status, tids, null, month, null);
  62. }
  63. public Page<Content> paginateByModuleInNormal(int page, int pagesize, String module) {
  64. return paginate(page, pagesize, module, null, Content.STATUS_NORMAL, null, null, null);
  65. }
  66. public Page<Content> paginateByModuleNotInDelete(int page, int pagesize, String module, String keyword,
  67. BigInteger[] taxonomyIds, String month) {
  68. StringBuilder sql = new StringBuilder(" from content c");
  69. sql.append(" left join mapping m on c.id = m.`content_id`");
  70. sql.append(" left join taxonomy t on m.`taxonomy_id` = t.id");
  71. sql.append(" where c.status <> ?");
  72. LinkedList<Object> params = new LinkedList<Object>();
  73. params.add(Content.STATUS_DELETE);
  74. appendIfNotEmpty(sql, "c.module", module, params, false);
  75. if (StringUtils.isNotBlank(keyword)) {
  76. sql.append(" AND c.title like ? ");
  77. params.add("%" + keyword + "%");
  78. }
  79. if (taxonomyIds != null && taxonomyIds.length > 0) {
  80. sql.append(" AND t.id in " + toString(taxonomyIds));
  81. }
  82. if (StringUtils.isNotBlank(month)) {
  83. sql.append(" DATE_FORMAT( c.created, \"%Y-%m\" ) = ?");
  84. params.add(month);
  85. }
  86. sql.append(" group by c.id");
  87. sql.append(" ORDER BY c.created DESC");
  88. String select = "select c.*";
  89. if (params.isEmpty()) {
  90. return DAO.paginate(page, pagesize, true, select, sql.toString());
  91. }
  92. return DAO.paginate(page, pagesize, true, select, sql.toString(), params.toArray());
  93. }
  94. public Page<Content> uuHelperPaginateByModuleNotInDelete(int page, int pagesize, String module, String keyword,
  95. String pushTime, String author, String copyRight, String taxonomy,
  96. BigInteger[] taxonomyIds, String month) {
  97. StringBuilder sql = new StringBuilder(" from content c");
  98. sql.append(" left join mapping m on c.id = m.`content_id`");
  99. sql.append(" left join taxonomy t on m.`taxonomy_id` = t.id");
  100. sql.append(" left join metadata d on c.id = d.`object_id`");
  101. sql.append(" left join metadata d1 on c.id = d1.`object_id`");
  102. sql.append(" where c.status <> ?");
  103. LinkedList<Object> params = new LinkedList<Object>();
  104. params.add(Content.STATUS_DELETE);
  105. appendIfNotEmpty(sql, "c.module", module, params, false);
  106. //uuhelper过滤筛选
  107. if (StringUtils.isNotBlank(pushTime)) {
  108. appendIfNotEmpty(sql, "d.meta_key", "push_time", params, false);
  109. appendIfNotEmpty(sql, "d.meta_value", pushTime, params, false);
  110. }
  111. if (StringUtils.isNotBlank(copyRight)) {
  112. appendIfNotEmpty(sql, "d1.meta_key", "copy_right", params, false);
  113. appendIfNotEmpty(sql, "d1.meta_value", copyRight, params, false);
  114. }
  115. if (StringUtils.isNotBlank(author)) {
  116. appendIfNotEmpty(sql, "c.author", author, params, false);
  117. }
  118. if (StringUtils.isNotBlank(taxonomy)) {
  119. appendIfNotEmpty(sql, "t.title", taxonomy, params, false);
  120. }
  121. if (StringUtils.isNotBlank(keyword)) {
  122. sql.append(" AND c.title like ? ");
  123. params.add("%" + keyword + "%");
  124. }
  125. if (taxonomyIds != null && taxonomyIds.length > 0) {
  126. sql.append(" AND t.id in " + toString(taxonomyIds));
  127. }
  128. if (StringUtils.isNotBlank(month)) {
  129. sql.append(" DATE_FORMAT( c.created, \"%Y-%m\" ) = ?");
  130. params.add(month);
  131. }
  132. sql.append(" group by c.id");
  133. sql.append(" ORDER BY c.created DESC");
  134. String select = "select c.*";
  135. if (params.isEmpty()) {
  136. return DAO.paginate(page, pagesize, true, select, sql.toString());
  137. }
  138. return DAO.paginate(page, pagesize, true, select, sql.toString(), params.toArray());
  139. }
  140. public Page<Content> paginateInNormal(int page, int pagesize, String module, BigInteger[] taxonomyIds,
  141. String orderBy) {
  142. LinkedList<Object> params = new LinkedList<Object>();
  143. String select = "select c.*";
  144. StringBuilder sql = new StringBuilder(" from content c");
  145. sql.append(" left join mapping m on c.id = m.`content_id`");
  146. sql.append(" left join taxonomy t on m.`taxonomy_id` = t.id");
  147. /*ContentPageTag源码只支持meta:date这种写法,固定desc。修改源码之后格式meta:date asc,asc可不写,默认desc*/
  148. if (orderBy != null && orderBy.startsWith("meta:")) {
  149. sql.append(
  150. " left join metadata meta on meta.`object_type`='content' and meta.`object_id`=c.id and meta.`meta_key`=? ");
  151. String[] orderByArr = orderBy.split(" ");
  152. if (orderByArr.length > 1) {
  153. params.add(orderByArr[0].substring("meta:".length()));
  154. } else {
  155. params.add(orderBy.substring("meta:".length()));
  156. }
  157. }
  158. sql.append(" WHERE c.status = 'normal' ");
  159. appendIfNotEmpty(sql, "c.module", module, params, false);
  160. if (taxonomyIds != null && taxonomyIds.length > 0) {
  161. if (taxonomyIds.length == 1) {
  162. sql.append(" AND m.taxonomy_id = ?");
  163. params.add(taxonomyIds[0]);
  164. } else {
  165. sql.append(" AND exists(select 1 from mapping m where m.`taxonomy_id` in " + toString(taxonomyIds)
  166. + " and m.`content_id`=c.id) ");
  167. }
  168. }
  169. sql.append(" group by c.id");
  170. if (orderBy != null && orderBy.startsWith("meta:")) {
  171. //将源码固定desc排序方式改为动态获取
  172. String[] orderArr = orderBy.split(" ");
  173. String orderWay = "desc";
  174. if (orderArr.length > 1) {
  175. orderWay = orderArr[orderArr.length-1];
  176. }
  177. //sql.append(" order by meta.`meta_value` + 0 desc ");
  178. sql.append(" order by meta.`meta_value` " + orderWay);
  179. } else {
  180. buildOrderBy(orderBy, sql);
  181. }
  182. if (params.isEmpty()) {
  183. return DAO.paginate(page, pagesize, true, select, sql.toString());
  184. }
  185. return DAO.paginate(page, pagesize, true, select, sql.toString(), params.toArray());
  186. }
  187. public Page<Content> paginate(int page, int pagesize, String module, String keyword, String status,
  188. BigInteger[] taxonomyIds, BigInteger userId, String orderBy) {
  189. String[] modules = StringUtils.isNotBlank(module) ? new String[] { module } : null;
  190. return paginate(page, pagesize, modules, keyword, status, taxonomyIds, userId, null, orderBy);
  191. }
  192. public Page<Content> paginate(int page, int pagesize, String[] modules, String keyword, String status,
  193. BigInteger[] taxonomyIds, BigInteger userId, String orderBy) {
  194. return paginate(page, pagesize, modules, keyword, status, taxonomyIds, userId, null, orderBy);
  195. }
  196. public Page<Content> paginate(int page, int pagesize, String[] modules, String keyword, String status,
  197. BigInteger[] taxonomyIds, BigInteger userId, String month, String orderBy) {
  198. String[] texts = null;
  199. // 关键词处理
  200. if (keyword != null) {
  201. texts = keyword.split(" ");
  202. }
  203. String select = "select c.*";
  204. StringBuilder sql = new StringBuilder(" from content c");
  205. sql.append(" left join mapping m on c.id = m.`content_id`");
  206. sql.append(" left join taxonomy t on m.`taxonomy_id` = t.id");
  207. //连接元数据表
  208. sql.append(" left join metadata d on c.id = d.`object_id`");
  209. LinkedList<Object> params = new LinkedList<Object>();
  210. boolean needWhere = true;
  211. needWhere = appendIfNotEmpty(sql, "c.module", modules, params, needWhere);
  212. needWhere = appendIfNotEmpty(sql, "c.status", status, params, needWhere);
  213. needWhere = appendIfNotEmpty(sql, "c.user_id", userId, params, needWhere);
  214. // 比对
  215. if (null != texts) {
  216. for (String key : texts) {
  217. if (StringUtils.isNotBlank(key)) {
  218. needWhere = appendWhereOrAnd(sql, needWhere);
  219. sql.append(" (c.title like ? ");
  220. params.add("%" + key + "%");
  221. sql.append(" or c.text like ? ");
  222. params.add("%" + key + "%");
  223. sql.append(" or c.summary like ? )");
  224. params.add("%" + key + "%");
  225. }
  226. }
  227. }
  228. sql.append(" and c.text is not null ");
  229. if (taxonomyIds != null && taxonomyIds.length > 0) {
  230. needWhere = appendWhereOrAnd(sql, needWhere);
  231. sql.append(" t.id in " + toString(taxonomyIds));
  232. }
  233. if (StringUtils.isNotBlank(month)) {
  234. needWhere = appendWhereOrAnd(sql, needWhere);
  235. sql.append(" DATE_FORMAT( c.created, \"%Y-%m\" ) = ?");
  236. params.add(month);
  237. }
  238. sql.append(" group by c.id");
  239. buildOrderBy(orderBy, sql);
  240. if (params.isEmpty()) {
  241. return DAO.paginate(page, pagesize, true, select, sql.toString());
  242. }
  243. return DAO.paginate(page, pagesize, true, select, sql.toString(), params.toArray());
  244. }
  245. public Page<Content> uuHelperPaginate(int page, int pagesize, String[] modules, String keyword,
  246. String pushTime, String author, String copyRight, String taxonomy,
  247. String status, BigInteger[] taxonomyIds, BigInteger userId, String month, String orderBy) {
  248. String[] texts = null;
  249. // 关键词处理
  250. if (keyword != null) {
  251. texts = keyword.split(" ");
  252. }
  253. String select = "select c.*";
  254. StringBuilder sql = new StringBuilder(" from content c");
  255. sql.append(" left join mapping m on c.id = m.`content_id`");
  256. sql.append(" left join taxonomy t on m.`taxonomy_id` = t.id");
  257. //连接元数据表
  258. sql.append(" left join metadata d on c.id = d.`object_id`");
  259. //uuhelper元数据过滤时用
  260. sql.append(" left join metadata d1 on c.id = d1.`object_id`");
  261. LinkedList<Object> params = new LinkedList<Object>();
  262. boolean needWhere = true;
  263. needWhere = appendIfNotEmpty(sql, "c.module", modules, params, needWhere);
  264. needWhere = appendIfNotEmpty(sql, "c.status", status, params, needWhere);
  265. needWhere = appendIfNotEmpty(sql, "c.user_id", userId, params, needWhere);
  266. //uuhelper过滤筛选
  267. if (StringUtils.isNotBlank(pushTime)) {
  268. needWhere = appendIfNotEmpty(sql, "d.meta_key", "push_time", params, needWhere);
  269. needWhere = appendIfNotEmpty(sql, "d.meta_value", pushTime, params, needWhere);
  270. }
  271. if (StringUtils.isNotBlank(copyRight)) {
  272. needWhere = appendIfNotEmpty(sql, "d1.meta_key", "copy_right", params, needWhere);
  273. needWhere = appendIfNotEmpty(sql, "d1.meta_value", copyRight, params, needWhere);
  274. }
  275. if (StringUtils.isNotBlank(author)) {
  276. needWhere = appendIfNotEmpty(sql, "c.author", author, params, needWhere);
  277. }
  278. if (StringUtils.isNotBlank(taxonomy)) {
  279. needWhere = appendIfNotEmpty(sql, "t.title", taxonomy, params, needWhere);
  280. }
  281. // 比对
  282. if (null != texts) {
  283. for (String key : texts) {
  284. if (StringUtils.isNotBlank(key)) {
  285. needWhere = appendWhereOrAnd(sql, needWhere);
  286. sql.append(" (c.title like ? ");
  287. params.add("%" + key + "%");
  288. sql.append(" or c.text like ? ");
  289. params.add("%" + key + "%");
  290. sql.append(" or c.summary like ? )");
  291. params.add("%" + key + "%");
  292. }
  293. }
  294. }
  295. sql.append(" and c.text is not null ");
  296. if (taxonomyIds != null && taxonomyIds.length > 0) {
  297. needWhere = appendWhereOrAnd(sql, needWhere);
  298. sql.append(" t.id in " + toString(taxonomyIds));
  299. }
  300. if (StringUtils.isNotBlank(month)) {
  301. needWhere = appendWhereOrAnd(sql, needWhere);
  302. sql.append(" DATE_FORMAT( c.created, \"%Y-%m\" ) = ?");
  303. params.add(month);
  304. }
  305. sql.append(" group by c.id");
  306. buildOrderBy(orderBy, sql);
  307. if (params.isEmpty()) {
  308. return DAO.paginate(page, pagesize, true, select, sql.toString());
  309. }
  310. return DAO.paginate(page, pagesize, true, select, sql.toString(), params.toArray());
  311. }
  312. protected String toString(Object[] a) {
  313. int iMax = a.length - 1;
  314. StringBuilder b = new StringBuilder();
  315. b.append('(');
  316. for (int i = 0;; i++) {
  317. b.append(String.valueOf(a[i]));
  318. if (i == iMax)
  319. return b.append(')').toString();
  320. b.append(", ");
  321. }
  322. }
  323. protected void buildOrderBy(String orderBy, StringBuilder fromBuilder) {
  324. if (StringUtils.isBlank(orderBy)) {
  325. fromBuilder.append(" ORDER BY c.modified DESC");
  326. return;
  327. }
  328. //contentsTag标签增加按元数据排序
  329. if (orderBy.startsWith("meta:")) {
  330. //将源码固定desc排序方式改为动态获取
  331. String[] orderArr = orderBy.split(" ");
  332. String orderWay = "desc";
  333. if (orderArr.length > 1) {
  334. orderWay = orderArr[orderArr.length-1];
  335. }
  336. fromBuilder.append(" order by d.`meta_value` " + orderWay);
  337. return;
  338. }
  339. // maybe orderby == "view_count desc";
  340. String orderbyInfo[] = orderBy.trim().split("\\s+");
  341. orderBy = orderbyInfo[0];
  342. if ("view_count".equals(orderBy)) {
  343. fromBuilder.append(" ORDER BY c.view_count ");
  344. }
  345. else if ("comment_count".equals(orderBy)) {
  346. fromBuilder.append(" ORDER BY c.comment_count ");
  347. }
  348. else if ("created".equals(orderBy)) {
  349. fromBuilder.append(" ORDER BY c.created ");
  350. }
  351. else if ("vote_up".equals(orderBy)) {
  352. fromBuilder.append(" ORDER BY c.vote_up ");
  353. }
  354. else if ("vote_down".equals(orderBy)) {
  355. fromBuilder.append(" ORDER BY c.vote_down ");
  356. }
  357. else if ("order_number".equals(orderBy)) {
  358. fromBuilder.append(" ORDER BY c.order_number ");
  359. }
  360. else if ("parent_id".equals(orderBy)) {
  361. fromBuilder.append(" ORDER BY c.parent_id ");
  362. }
  363. else if ("object_id".equals(orderBy)) {
  364. fromBuilder.append(" ORDER BY c.object_id ");
  365. }
  366. else if ("price".equals(orderBy)) {
  367. fromBuilder.append(" ORDER BY c.price ");
  368. }
  369. else if ("comment_time".equals(orderBy)) {
  370. fromBuilder.append(" ORDER BY c.comment_time ");
  371. }
  372. else if ("rate".equals(orderBy)) {
  373. fromBuilder.append(" ORDER BY c.rate ");
  374. }
  375. else if ("rate_count".equals(orderBy)) {
  376. fromBuilder.append(" ORDER BY c.rate_count ");
  377. }
  378. else {
  379. fromBuilder.append(" ORDER BY c.created ");
  380. }
  381. if (orderbyInfo.length == 1) {
  382. fromBuilder.append(" DESC ");
  383. } else {
  384. fromBuilder.append(orderbyInfo[1]);
  385. }
  386. }
  387. public Long findCountByModuleAndStatus(String module, String status) {
  388. return DAO.doFindCount("module = ? and status=?", module, status);
  389. }
  390. public List<Content> findListInNormal(int page, int pagesize) {
  391. return findListInNormal(page, pagesize, null, null, null, null, null, null, null, null, null, null, null, null,
  392. null);
  393. }
  394. public List<Content> findListInNormal(int page, int pagesize, String module) {
  395. String[] modules = new String[] { module };
  396. return findListInNormal(page, pagesize, null, null, null, null, modules, null, null, null, null, null, null,
  397. null, null);
  398. }
  399. public List<Content> findListInNormal(int page, int pagesize, BigInteger taxonomyId) {
  400. return findListInNormal(page, pagesize, null, null, new BigInteger[] { taxonomyId }, null, null, null, null,
  401. null, null, null, null, null, null);
  402. }
  403. public List<Content> findListInNormal(int page, int pagesize, String orderBy, String keyword, BigInteger[] typeIds,
  404. String[] typeSlugs, String[] modules, String[] styles, String[] flags, String[] slugs, BigInteger[] userIds,
  405. BigInteger[] parentIds, String[] tags, Boolean hasThumbnail, String month) {
  406. if (modules == null) {
  407. modules = TemplateManager.me().currentTemplateModulesAsArray();
  408. }
  409. StringBuilder sql = new StringBuilder(" select c.* from content c ");
  410. sql.append(" left join mapping m on c.id = m.`content_id`");
  411. sql.append(" left join taxonomy t on m.`taxonomy_id` = t.id");
  412. //连接元数据表
  413. sql.append(" left join metadata d on c.id = d.`object_id`");
  414. sql.append(" where c.status = 'normal' ");
  415. LinkedList<Object> params = new LinkedList<Object>();
  416. appendIfNotEmpty(sql, "m.taxonomy_id", typeIds, params, false);
  417. appendIfNotEmpty(sql, "c.module", modules, params, false);
  418. appendIfNotEmpty(sql, "c.style", styles, params, false);
  419. appendIfNotEmpty(sql, "c.slug", slugs, params, false);
  420. appendIfNotEmpty(sql, "c.user_id", userIds, params, false);
  421. appendIfNotEmpty(sql, "c.parent_id", parentIds, params, false);
  422. appendIfNotEmpty(sql, "t.slug", typeSlugs, params, false);
  423. appendIfNotEmptyWithLike(sql, "c.flag", flags, params, false);
  424. if (null != tags && tags.length > 0) {
  425. appendIfNotEmpty(sql, "t.title", tags, params, false);
  426. sql.append(" AND t.`type`='tag' ");
  427. }
  428. if (StringUtils.isNotBlank(keyword)) {
  429. sql.append(" AND c.title like ?");
  430. params.add("%" + keyword + "%");
  431. }
  432. if (StringUtils.isNotBlank(month)) {
  433. sql.append(" AND DATE_FORMAT( c.created, \"%Y-%m\" ) = ?");
  434. params.add(month);
  435. }
  436. if (null != hasThumbnail) {
  437. if (hasThumbnail) {
  438. sql.append(" AND c.thumbnail is not null ");
  439. } else {
  440. sql.append(" AND c.thumbnail is null ");
  441. }
  442. }
  443. sql.append("GROUP BY c.id");
  444. buildOrderBy(orderBy, sql);
  445. sql.append(" LIMIT ?, ?");
  446. params.add(page - 1);
  447. params.add(pagesize);
  448. return DAO.find(sql.toString(), params.toArray());
  449. }
  450. public List<Content> findByModule(String module) {
  451. return DAO.doFind("module = ? ", module);
  452. }
  453. public List<Content> findByModuleAndTitle(String module, String title, int limit) {
  454. return DAO.doFind("module = ? and title = ? order by id desc limit ?", module, title, limit);
  455. }
  456. public Content findFirstByModuleAndTitle(String module, String title) {
  457. return DAO.doFindFirst("module = ? and title = ? order by id desc", module, title);
  458. }
  459. public Content findFirstByModuleAndText(String module, String text) {
  460. return DAO.doFindFirst("module = ? and text = ? order by id desc", module, text);
  461. }
  462. public Content findFirstByModuleAndObjectId(String module, BigInteger objectId) {
  463. return DAO.doFindFirst("module = ? and object_id = ? order by id desc", module, objectId);
  464. }
  465. public Content findFirstByModuleAndObjectId(String module, BigInteger objectId, BigInteger userId) {
  466. return DAO.doFindFirst("module = ? and object_id = ? and user_id = ? order by id desc", module, objectId,
  467. userId);
  468. }
  469. public Content findFirstByModuleAndUserId(String module, BigInteger userId) {
  470. return DAO.doFindFirst("module = ? and user_id = ? order by id desc", module, userId);
  471. }
  472. public List<Content> findListByModuleAndObjectId(String module, BigInteger objectId) {
  473. return DAO.doFind("module = ? and object_id = ? order by id desc", module, objectId);
  474. }
  475. public List<Content> findListByModuleAndUserId(String module, BigInteger userId) {
  476. return DAO.doFind("module = ? and user_id = ? order by id desc", module, userId);
  477. }
  478. public List<Content> searchByModuleAndTitle(String module, String title, int limit) {
  479. return DAO.doFind("module = ? and title like ? order by id desc limit ?", module, "%" + title + "%", limit);
  480. }
  481. public List<Content> findByModule(final String module, final BigInteger parentId, String orderby) {
  482. final StringBuilder sqlBuilder = new StringBuilder("select * from content c");
  483. sqlBuilder.append(" where module = ? ");
  484. final List<Object> params = new ArrayList<Object>();
  485. params.add(module);
  486. appendIfNotEmpty(sqlBuilder, "parent_id", parentId, params, false);
  487. buildOrderBy(orderby, sqlBuilder);
  488. List<Content> data = DAO.getFromListCache(buildKey(module, parentId, orderby), new IDataLoader() {
  489. @Override
  490. public Object load() {
  491. return DAO.find(sqlBuilder.toString(), params.toArray());
  492. }
  493. });
  494. if (data == null)
  495. return null;
  496. return new ArrayList<Content>(data);
  497. }
  498. private String buildKey(String module, Object... params) {
  499. StringBuffer keyBuffer = new StringBuffer(module == null ? "" : "module:" + module);
  500. if (params != null && params.length > 0) {
  501. for (int i = 0; i < params.length; i++) {
  502. keyBuffer.append("-p").append(i).append(":").append(params[i]);
  503. }
  504. }
  505. return keyBuffer.toString().replace(" ", "");
  506. }
  507. public List<Content> findArchiveByModule(String module) {
  508. StringBuilder sqlBuilder = new StringBuilder(
  509. " select c.*,DATE_FORMAT( c.created, \"%Y-%m\" ) as archiveDate from content c ");
  510. sqlBuilder.append(" where module = ? ");
  511. sqlBuilder.append(" order by c.created DESC");
  512. return DAO.find(sqlBuilder.toString(), module);
  513. }
  514. public Content findBySlug(final String slug) {
  515. final StringBuilder sql = new StringBuilder(" select c.* from content c ");
  516. sql.append(" WHERE c.slug = ?");
  517. return DAO.getCache(slug, new IDataLoader() {
  518. @Override
  519. public Object load() {
  520. return DAO.findFirst(sql.toString(), slug);
  521. }
  522. });
  523. }
  524. public Content findById(final BigInteger id) {
  525. return DAO.getCache(id, new IDataLoader() {
  526. @Override
  527. public Object load() {
  528. return DAO.findById(id);
  529. }
  530. });
  531. }
  532. public Content findNext(final Content currentContent) {
  533. /*return DAO.getFromListCache(buildKey(currentContent.getModule(), "next:" + currentContent.getId()),
  534. new IDataLoader() {
  535. @Override
  536. public Object load() {
  537. StringBuilder sqlBuilder = new StringBuilder(" select ");
  538. sqlBuilder.append(" c.*,u.username,u.nickname,u.avatar ");
  539. sqlBuilder.append(" from content c");
  540. sqlBuilder.append(" left join user u on c.user_id = u.id ");
  541. sqlBuilder.append(" WHERE c.id > ?");
  542. sqlBuilder.append(" AND c.module = ?");
  543. sqlBuilder.append(" AND c.status = 'normal'");
  544. sqlBuilder.append(" ORDER BY c.created ASC");
  545. sqlBuilder.append(" LIMIT 1");
  546. return DAO.findFirst(sqlBuilder.toString(), currentContent.getId(), currentContent.getModule());
  547. }
  548. });*/
  549. Content content = null;
  550. if("customers".equals(currentContent.getModule())){
  551. List<Content> list = DAO.getFromListCache(buildKey(currentContent.getModule(), "next:" + currentContent.getId()),
  552. new IDataLoader() {
  553. @Override
  554. public Object load() {
  555. StringBuilder sqlBuilder = new StringBuilder(" select c.* from content c ,mapping m, taxonomy t\n" +
  556. "where c.module='customers' and c.status='normal' and m.content_id=c.id and m.taxonomy_id=t.id and t.type = 'category'\n" +
  557. "order by t.id asc,c.order_number desc");
  558. return DAO.find(sqlBuilder.toString());
  559. }
  560. });
  561. int index = 0;
  562. for(int i=0;i<list.size();i++){
  563. if (list.get(i).getId().toString().equals(currentContent.getId().toString())){
  564. index = i;
  565. }
  566. }
  567. if(index==list.size()-1){return null;}
  568. content = list.get(index+1);
  569. } else if ("news".equals(currentContent.getModule())) {
  570. List<Content> list = DAO.getFromListCache(buildKey(currentContent.getModule(), "next:" + currentContent.getId()),
  571. new IDataLoader() {
  572. @Override
  573. public Object load() {
  574. StringBuilder sqlBuilder = new StringBuilder(" select c.* from content c ,mapping m, taxonomy t where c.module='news' and c.status='normal' and m.content_id=c.id and m.taxonomy_id=t.id and t.type = 'category' ");
  575. List<Taxonomy> taxonomys = currentContent.getTaxonomys();
  576. for (int i = 0; i < taxonomys.size(); i++) {
  577. String slug = taxonomys.get(i).getSlug();
  578. if ("tradeNews".equals(slug) || "companyDynamic".equals(slug)) {
  579. sqlBuilder.append(" and t.slug = '" + slug +"'");
  580. }
  581. }
  582. sqlBuilder.append(" and c.created < '" + currentContent.getCreated().toString() +"'" );
  583. sqlBuilder.append(" order by c.created desc limit 1");
  584. return DAO.find(sqlBuilder.toString());
  585. }
  586. });
  587. if (list != null && list.size() != 0) {
  588. return list.get(0);
  589. } else {
  590. return null;
  591. }
  592. } else{
  593. content = DAO.getFromListCache(buildKey(currentContent.getModule(), "next:" + currentContent.getId()),
  594. new IDataLoader() {
  595. @Override
  596. public Object load() {
  597. StringBuilder sqlBuilder = new StringBuilder(" select ");
  598. sqlBuilder.append(" c.*,u.username,u.nickname,u.avatar ");
  599. sqlBuilder.append(" from content c");
  600. sqlBuilder.append(" left join user u on c.user_id = u.id ");
  601. sqlBuilder.append(" WHERE c.id > ?");
  602. sqlBuilder.append(" AND c.module = ?");
  603. sqlBuilder.append(" AND c.status = 'normal'");
  604. sqlBuilder.append(" ORDER BY c.created ASC");
  605. sqlBuilder.append(" LIMIT 1");
  606. return DAO.findFirst(sqlBuilder.toString(), currentContent.getId(), currentContent.getModule());
  607. }
  608. });
  609. }
  610. return content;
  611. }
  612. public Content findPrevious(final Content currentContent) {
  613. /*return DAO.getFromListCache(buildKey(currentContent.getModule(), "previous:" + currentContent.getId()),
  614. new IDataLoader() {
  615. @Override
  616. public Object load() {
  617. StringBuilder sqlBuilder = new StringBuilder(" select ");
  618. sqlBuilder.append(" c.*,u.username,u.nickname,u.avatar ");
  619. sqlBuilder.append(" from content c");
  620. sqlBuilder.append(" left join user u on c.user_id = u.id ");
  621. sqlBuilder.append(" WHERE c.id < ?");
  622. sqlBuilder.append(" AND c.module = ?");
  623. sqlBuilder.append(" AND c.status = 'normal'");
  624. sqlBuilder.append(" ORDER BY c.created DESC");
  625. sqlBuilder.append(" LIMIT 1");
  626. return DAO.findFirst(sqlBuilder.toString(), currentContent.getId(), currentContent.getModule());
  627. }
  628. });*/
  629. Content content = null;
  630. if("customers".equals(currentContent.getModule())) {
  631. List<Content> list = DAO.getFromListCache(buildKey(currentContent.getModule(), "previous:" + currentContent.getId()),
  632. new IDataLoader() {
  633. @Override
  634. public Object load() {
  635. StringBuilder sqlBuilder = new StringBuilder(" select c.* from content c ,mapping m, taxonomy t\n" +
  636. "where c.module='customers' and c.status='normal' and m.content_id=c.id and m.taxonomy_id=t.id and t.type = 'category'\n" +
  637. "order by t.id asc,c.order_number desc");
  638. return DAO.find(sqlBuilder.toString());
  639. }
  640. });
  641. int index = 0;
  642. for (int i = 0; i < list.size(); i++) {
  643. if (list.get(i).getId().toString().equals(currentContent.getId().toString())) {
  644. index = i;
  645. }
  646. }
  647. if (index == 0) {
  648. return null;
  649. }
  650. content = list.get(index - 1);
  651. } else if ("news".equals(currentContent.getModule())) {
  652. List<Content> list = DAO.getFromListCache(buildKey(currentContent.getModule(), "previous:" + currentContent.getId()),
  653. new IDataLoader() {
  654. @Override
  655. public Object load() {
  656. StringBuilder sqlBuilder = new StringBuilder(" select c.* from content c ,mapping m, taxonomy t where c.module='news' and c.status='normal' and m.content_id=c.id and m.taxonomy_id=t.id and t.type = 'category' ");
  657. List<Taxonomy> taxonomys = currentContent.getTaxonomys();
  658. for (int i = 0; i < taxonomys.size(); i++) {
  659. String slug = taxonomys.get(i).getSlug();
  660. if ("tradeNews".equals(slug) || "companyDynamic".equals(slug)) {
  661. sqlBuilder.append(" and t.slug = '" + slug +"'");
  662. }
  663. }
  664. sqlBuilder.append(" and c.created > '" + currentContent.getCreated().toString() +"'" );
  665. sqlBuilder.append(" order by c.created asc limit 1");
  666. return DAO.find(sqlBuilder.toString());
  667. }
  668. });
  669. if (list != null && list.size() != 0) {
  670. return list.get(0);
  671. } else {
  672. return null;
  673. }
  674. } else{
  675. content = DAO.getFromListCache(buildKey(currentContent.getModule(), "previous:" + currentContent.getId()),
  676. new IDataLoader() {
  677. @Override
  678. public Object load() {
  679. StringBuilder sqlBuilder = new StringBuilder(" select ");
  680. sqlBuilder.append(" c.*,u.username,u.nickname,u.avatar ");
  681. sqlBuilder.append(" from content c");
  682. sqlBuilder.append(" left join user u on c.user_id = u.id ");
  683. sqlBuilder.append(" WHERE c.id < ?");
  684. sqlBuilder.append(" AND c.module = ?");
  685. sqlBuilder.append(" AND c.status = 'normal'");
  686. sqlBuilder.append(" ORDER BY c.created DESC");
  687. sqlBuilder.append(" LIMIT 1");
  688. return DAO.findFirst(sqlBuilder.toString(), currentContent.getId(), currentContent.getModule());
  689. }
  690. });
  691. }
  692. return content;
  693. }
  694. public long findCountByModule(String module) {
  695. return DAO.doFindCount("module = ?", module);
  696. }
  697. public long findCountInNormalByModule(String module) {
  698. return DAO.doFindCount("module = ? AND status <> ?", module, Content.STATUS_DELETE);
  699. }
  700. public long findCountInNormalByModuleAndUserId(String module, BigInteger userId) {
  701. return DAO.doFindCount("module = ? AND status <> ? and user_id = ? ", module, Content.STATUS_DELETE, userId);
  702. }
  703. public long findCountInNormalByParentId(BigInteger id, String module) {
  704. if (id == null) {
  705. return DAO.doFindCount("parent_id is null AND module = ? AND status <> ?", module, Content.STATUS_DELETE);
  706. }
  707. return DAO.doFindCount("parent_id = ? AND module = ? AND status <> ?", id, module, Content.STATUS_DELETE);
  708. }
  709. public int batchTrash(BigInteger... ids) {
  710. if (ids != null && ids.length > 0) {
  711. int trashCount = 0;
  712. for (int i = 0; i < ids.length; i++) {
  713. Content content = findById(ids[i]);
  714. if (content != null) {
  715. content.setStatus(Content.STATUS_DELETE);
  716. if (content.update()) {
  717. ++trashCount;
  718. }
  719. }
  720. }
  721. return trashCount;
  722. }
  723. return 0;
  724. }
  725. public int batchDelete(BigInteger... ids) {
  726. if (ids != null && ids.length > 0) {
  727. int deleteCount = 0;
  728. for (int i = 0; i < ids.length; i++) {
  729. if (deleteById(ids[i])) {
  730. ++deleteCount;
  731. }
  732. }
  733. return deleteCount;
  734. }
  735. return 0;
  736. }
  737. public List<Archive> findArchives(String module) {
  738. String sql = "SELECT DATE_FORMAT( c.created, \"%Y-%m\" ) as d, COUNT( * ) count FROM content c"
  739. + " WHERE c.module = ? GROUP BY d";
  740. List<Record> list = Jdb.find(sql, module);
  741. if (list == null || list.isEmpty())
  742. return null;
  743. List<Archive> datas = new ArrayList<Archive>();
  744. for (Record r : list) {
  745. String date = r.getStr("d");
  746. if (StringUtils.isNotBlank(date)) {
  747. datas.add(new Archive(date, r.getLong("count")));
  748. }
  749. }
  750. return datas;
  751. }
  752. }