Browse Source

完善商城消息服务(摘要提取,内容控制,分页处理)

wangdy 8 years ago
parent
commit
099d15b55b

+ 5 - 8
src/main/java/com/uas/cloud/mall/shop/news/api/NewsController.java

@@ -9,10 +9,7 @@ import org.apache.log4j.Logger;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.cloud.client.ServiceInstance;
 import org.springframework.cloud.client.discovery.DiscoveryClient;
-import org.springframework.web.bind.annotation.PathVariable;
-import org.springframework.web.bind.annotation.RequestMapping;
-import org.springframework.web.bind.annotation.RequestMethod;
-import org.springframework.web.bind.annotation.RestController;
+import org.springframework.web.bind.annotation.*;
 
 import javax.servlet.http.HttpServletRequest;
 import java.util.List;
@@ -54,10 +51,10 @@ public class NewsController {
      * @return
      */
     @RequestMapping(value="/created" ,method = RequestMethod.GET, produces = "application/json")
-    public List<News> getNewsByCreated(HttpServletRequest request) {
+    public List<News> getNewsByCreated(@RequestParam(defaultValue = "1") int pagenumber , @RequestParam(defaultValue = "10") int pagesize) {
         ServiceInstance instance = client.getLocalServiceInstance();
         logger.info("/news, get, host:" + instance.getHost() + ", serviceId:" + instance.getServiceId() + "get news order by created time");
-        List<News> newsList = newsService.findBycreated();
+        List<News> newsList = newsService.findBycreated(pagenumber,pagesize);
         return newsList;
     }
 
@@ -67,10 +64,10 @@ public class NewsController {
      * @return
      */
     @RequestMapping(value="/viewcount" ,method = RequestMethod.GET, produces = "application/json")
-    public List<News> getNewsByViewCount() {
+    public List<News> getNewsByViewCount(@RequestParam(defaultValue = "1") int pagenumber , @RequestParam(defaultValue = "10") int pagesize) {
         ServiceInstance instance = client.getLocalServiceInstance();
         logger.info("/news, get, host:" + instance.getHost() + ", serviceId:" + instance.getServiceId() + "get news order by viewcount");
-        return newsService.findByViewCount();
+        return newsService.findByViewCount(pagenumber,pagesize);
     }
 
 }

+ 34 - 14
src/main/java/com/uas/cloud/mall/shop/news/model/News.java

@@ -35,17 +35,17 @@ public class News{
     @Column(name = "title")
     private String title;
 
-//    /**
-//     *  摘要
-//     */
-//    @Column(name = "summary")
-//    private String summary;
-//
-//    /**
-//     *  内容
-//     */
-//    @Column(name = "text")
-//    private String content;
+    /**
+     *  摘要
+     */
+    @Column(name = "summary")
+    private String summary;
+
+    /**
+     *  内容
+     */
+    @Column(name = "text")
+    private String content;
 
     /**
      *  缩略图
@@ -89,14 +89,34 @@ public class News{
 
     public News() {}
 
-    public News(String title, String thumbnail, String hrefUrl, Long viewCount, Date created, Date modified , String url_suffix) {
+    public News(Long id, String title, String summary, String content, String thumbnail, Long viewCount, String url_suffix, Date created, String module, Date modified, String hrefUrl) {
+        this.id = id;
         this.title = title;
+        this.summary = summary;
+        this.content = content;
         this.thumbnail = thumbnail;
-        this.hrefUrl = hrefUrl;
         this.viewCount = viewCount;
+        this.url_suffix = url_suffix;
         this.created = created;
+        this.module = module;
         this.modified = modified;
-        this.url_suffix = url_suffix;
+        this.hrefUrl = hrefUrl;
+    }
+
+    public String getSummary() {
+        return summary;
+    }
+
+    public void setSummary(String summary) {
+        this.summary = summary;
+    }
+
+    public String getContent() {
+        return content;
+    }
+
+    public void setContent(String content) {
+        this.content = content;
     }
 
     public Long getId() {

+ 2 - 2
src/main/java/com/uas/cloud/mall/shop/news/service/NewsService.java

@@ -16,7 +16,7 @@ public interface NewsService {
      *
      * @return
      */
-    List<News> findBycreated();
+    List<News> findBycreated(int pagenumber , int pagesize);
 
 
     /**
@@ -24,5 +24,5 @@ public interface NewsService {
      *
      * @return
      */
-    List<News> findByViewCount();
+    List<News> findByViewCount(int pagenumber , int pagesize);
 }

+ 15 - 7
src/main/java/com/uas/cloud/mall/shop/news/service/impl/NewsServiceImpl.java

@@ -3,6 +3,7 @@ package com.uas.cloud.mall.shop.news.service.impl;
 import com.uas.cloud.mall.shop.news.data.NewsRepository;
 import com.uas.cloud.mall.shop.news.model.News;
 import com.uas.cloud.mall.shop.news.service.NewsService;
+import com.uas.cloud.mall.shop.news.utils.NewsCloumnCutUtil;
 import org.apache.commons.collections.CollectionUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.data.domain.Page;
@@ -12,6 +13,7 @@ import org.springframework.data.domain.Sort;
 import org.springframework.stereotype.Service;
 
 import javax.persistence.criteria.Predicate;
+import java.util.ArrayList;
 import java.util.Collections;
 import java.util.List;
 
@@ -26,35 +28,41 @@ public class NewsServiceImpl implements NewsService {
 
     @Override
     public News findone(Long id) {
-        return newsRepository.findOne(id);
+        News news = newsRepository.findOne(id);
+        //判断summary字段是否为空
+        if ("".equals(news.getSummary()) || news.getSummary() == null){
+            news.setSummary(NewsCloumnCutUtil.getHtmlText(100,news.getContent()));
+        }
+        return news;
     }
 
     @Override
-    public List<News> findBycreated() {
-        Pageable pageable = new PageRequest(0 ,8, new Sort(Sort.Direction.DESC, "modified"));
+    public List<News> findBycreated(int pagenumber , int pagesize) {
+        Pageable pageable = new PageRequest(pagenumber-1 ,pagesize, new Sort(Sort.Direction.DESC, "modified"));
         Page<News> page = newsRepository.findAll((root, query, builder) -> {
             Predicate predicate = builder.equal(root.get("module"), "news");
             query.where(predicate);
             return null;
         }, pageable);
         if (page != null && !CollectionUtils.isEmpty(page.getContent())) {
-            return page.getContent();
+            return NewsCloumnCutUtil.cutText(page.getContent());
         }
         return Collections.emptyList();
     }
 
 
     @Override
-    public List<News> findByViewCount() {
-        Pageable pageable = new PageRequest(0 ,8, new Sort(Sort.Direction.DESC, "viewCount"));
+    public List<News> findByViewCount(int pagenumber , int pagesize) {
+        Pageable pageable = new PageRequest(pagenumber-1 ,pagesize, new Sort(Sort.Direction.DESC, "viewCount"));
         Page<News> page = newsRepository.findAll((root, query, builder) -> {
             Predicate predicate = builder.equal(root.get("module"), "news");
             query.where(predicate);
             return null;
         }, pageable);
         if (page != null && !CollectionUtils.isEmpty(page.getContent())) {
-            return page.getContent();
+            return NewsCloumnCutUtil.cutText(page.getContent());
         }
         return Collections.emptyList();
     }
+
 }

+ 45 - 0
src/main/java/com/uas/cloud/mall/shop/news/utils/NewsCloumnCutUtil.java

@@ -0,0 +1,45 @@
+package com.uas.cloud.mall.shop.news.utils;
+
+import com.uas.cloud.mall.shop.news.model.News;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * Created by wangdy on 2017/5/27.
+ */
+public class NewsCloumnCutUtil {
+
+    public static List<News> cutText(List<News> oldnewsList) {
+        List<News> newsList = new ArrayList<>();
+        for (News news : oldnewsList) {
+            if ("".equals(news.getSummary()) || news.getSummary() == null) {
+                news.setSummary(getHtmlText(100, news.getContent()));
+            }
+            news.setContent("");
+            newsList.add(news);
+        }
+        return newsList;
+    }
+
+    //从html中提取纯文本
+    public static String getHtmlText(int len , String inputString) {
+        String htmlStr = inputString; // 含html标签的字符串
+        String textStr = "";
+        if (htmlStr == null ){
+            return htmlStr;
+        }
+        textStr = htmlStr.replaceAll("\n", ""); //剔出回车
+        textStr = textStr.replaceAll("&nbsp", ""); //剔出空格
+        textStr = textStr.replaceAll("</?[^>]+>", ""); //剔出<html>的标签
+        textStr = textStr.replaceAll("<a>\\s*|\t|\r|\n</a>", "");//去除字符串中的空格,回车,换行符,制表符
+
+        if (len > textStr.length()){
+            return textStr;
+        } else {
+            return textStr.substring(0,len);
+        }
+    }
+
+
+}