Browse Source

官网新闻中心在分类情况下排序获取上一篇下一篇

huangct 8 years ago
parent
commit
9f6bbee16a
1 changed files with 58 additions and 3 deletions
  1. 58 3
      jpress-model/src/main/java/io/jpress/model/query/ContentQuery.java

+ 58 - 3
jpress-model/src/main/java/io/jpress/model/query/ContentQuery.java

@@ -19,6 +19,7 @@ import com.jfinal.plugin.activerecord.Page;
 import com.jfinal.plugin.activerecord.Record;
 import com.jfinal.plugin.ehcache.IDataLoader;
 import io.jpress.model.Content;
+import io.jpress.model.Taxonomy;
 import io.jpress.model.core.Jdb;
 import io.jpress.model.vo.Archive;
 import io.jpress.template.TemplateManager;
@@ -130,7 +131,7 @@ public class ContentQuery extends JBaseQuery {
 
 		}
 
-		sql.append(" WHERE c.status = 'normal' ");
+		sql.append(" WHERE c.status != 'normal' ");
 
 		appendIfNotEmpty(sql, "c.module", module, params, false);
 
@@ -557,7 +558,33 @@ public class ContentQuery extends JBaseQuery {
 			}
 			if(index==list.size()-1){return null;}
 			content = list.get(index+1);
-		}else{
+		} else if ("news".equals(currentContent.getModule())) {
+            List<Content> list = DAO.getFromListCache(buildKey(currentContent.getModule(), "next:" + currentContent.getId()),
+                    new IDataLoader() {
+                        @Override
+                        public Object load() {
+                            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' ");
+                            List<Taxonomy> taxonomys =  currentContent.getTaxonomys();
+                            for (int i = 0; i < taxonomys.size(); i++) {
+                                String slug = taxonomys.get(i).getSlug();
+                                if ("tradeNews".equals(slug) || "companyDynamic".equals(slug)) {
+                                    sqlBuilder.append(" and t.slug = '" + slug +"'");
+                                }
+                            }
+                            sqlBuilder.append(" order by c.created asc");
+
+                            return DAO.find(sqlBuilder.toString());
+                        }
+                    });
+            int index = 0;
+            for(int i=0;i<list.size();i++){
+                if (list.get(i).getId().toString().equals(currentContent.getId().toString())){
+                    index = i;
+                }
+            }
+            if(index==list.size()-1){return null;}
+            content = list.get(index+1);
+        } else{
 			content = DAO.getFromListCache(buildKey(currentContent.getModule(), "next:" + currentContent.getId()),
 					new IDataLoader() {
 						@Override
@@ -620,7 +647,35 @@ public class ContentQuery extends JBaseQuery {
 				return null;
 			}
 			content = list.get(index - 1);
-		}else{
+		} else if ("news".equals(currentContent.getModule())) {
+            List<Content> list = DAO.getFromListCache(buildKey(currentContent.getModule(), "previous:" + currentContent.getId()),
+                    new IDataLoader() {
+                        @Override
+                        public Object load() {
+                            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' ");
+                            List<Taxonomy> taxonomys =  currentContent.getTaxonomys();
+                            for (int i = 0; i < taxonomys.size(); i++) {
+                                String slug = taxonomys.get(i).getSlug();
+                                if ("tradeNews".equals(slug) || "companyDynamic".equals(slug)) {
+                                    sqlBuilder.append(" and t.slug = '" + slug +"'");
+                                }
+                            }
+                            sqlBuilder.append(" order by c.created asc");
+
+                            return DAO.find(sqlBuilder.toString());
+                        }
+                    });
+            int index = 0;
+            for (int i = 0; i < list.size(); i++) {
+                if (list.get(i).getId().toString().equals(currentContent.getId().toString())) {
+                    index = i;
+                }
+            }
+            if (index == 0) {
+                return null;
+            }
+            content = list.get(index - 1);
+        } else{
 			content = DAO.getFromListCache(buildKey(currentContent.getModule(), "previous:" + currentContent.getId()),
 					new IDataLoader() {
 						@Override