Browse Source

news list

Antiloveg 8 years ago
parent
commit
ddac475c01

+ 1 - 0
schema/20170626-activities-news-billings.sql

@@ -11,6 +11,7 @@ CREATE TABLE `news` (
   `source` varchar(45) DEFAULT NULL COMMENT '来源',
   `source_url` varchar(255) DEFAULT NULL COMMENT '来源url',
   `summary` varchar(144) DEFAULT NULL COMMENT '简介',
+  `news_date` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '新闻时间',
   PRIMARY KEY (`id`)
 ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='新闻资讯';
 

+ 37 - 1
src/main/java/com/goafanti/admin/controller/AdminNewsApiController.java

@@ -3,16 +3,52 @@ package com.goafanti.admin.controller;
 import javax.annotation.Resource;
 
 import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RequestMethod;
 import org.springframework.web.bind.annotation.RestController;
 
+import com.goafanti.common.bo.Result;
+import com.goafanti.common.constant.ErrorConstants;
 import com.goafanti.common.controller.BaseApiController;
+import com.goafanti.common.utils.StringUtils;
 import com.goafanti.news.service.NewsService;
+
 /**
  * 新闻
  */
 @RestController
 @RequestMapping(value = "/api/admin/news")
-public class AdminNewsApiController extends BaseApiController{
+public class AdminNewsApiController extends BaseApiController {
 	@Resource
 	private NewsService newsService;
+	
+	/**
+	 * 新闻列表
+	 */
+	@RequestMapping(value = "/list", method = RequestMethod.GET)
+	public Result list(Integer type, String title, String author, String startCreateTime,
+			String endCreateTime, String source, Integer hot, String pageSize, String pageNo) {
+		Result res = new Result();
+		if (null == type){
+			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "", "新闻类型"));
+			return res;
+		}
+		Integer pNo = 1;
+		Integer pSize = 10;
+		if (StringUtils.isNumeric(pageSize)) {
+			pSize = Integer.parseInt(pageSize);
+		}
+		if (StringUtils.isNumeric(pageNo)) {
+			pNo = Integer.parseInt(pageNo);
+		}
+		res.setData(newsService.listNews(type, title, author, startCreateTime, endCreateTime, source, hot, pNo, pSize));
+		return res;
+	}
+	
+	/**
+	 * 新增
+	 */
+	public Result add(){
+		Result res = new Result();
+		return res;
+	}
 }

+ 78 - 5
src/main/java/com/goafanti/common/mapper/NewsMapper.xml

@@ -13,13 +13,14 @@
     <result column="source" jdbcType="VARCHAR" property="source" />
     <result column="source_url" jdbcType="VARCHAR" property="sourceUrl" />
     <result column="summary" jdbcType="VARCHAR" property="summary" />
+    <result column="news_date" jdbcType="TIMESTAMP" property="newsDate" />
   </resultMap>
   <resultMap extends="BaseResultMap" id="ResultMapWithBLOBs" type="com.goafanti.common.model.News">
     <result column="content" jdbcType="LONGVARCHAR" property="content" />
   </resultMap>
   <sql id="Base_Column_List">
     id, create_time, edit_time, title, title_img, author, type, hot, source, source_url, 
-    summary
+    summary, newsDate
   </sql>
   <sql id="Blob_Column_List">
     content
@@ -40,12 +41,13 @@
     insert into news (id, create_time, edit_time, 
       title, title_img, author, 
       type, hot, source, 
-      source_url, summary, content
+      source_url, summary, content, news_date
       )
     values (#{id,jdbcType=BIGINT}, #{createTime,jdbcType=TIMESTAMP}, #{editTime,jdbcType=TIMESTAMP}, 
       #{title,jdbcType=VARCHAR}, #{titleImg,jdbcType=VARCHAR}, #{author,jdbcType=VARCHAR}, 
       #{type,jdbcType=INTEGER}, #{hot,jdbcType=INTEGER}, #{source,jdbcType=VARCHAR}, 
-      #{sourceUrl,jdbcType=VARCHAR}, #{summary,jdbcType=VARCHAR}, #{content,jdbcType=LONGVARCHAR}
+      #{sourceUrl,jdbcType=VARCHAR}, #{summary,jdbcType=VARCHAR}, #{content,jdbcType=LONGVARCHAR},
+      #{newsDate,jdbcType=TIMESTAMP}
       )
   </insert>
   <insert id="insertSelective" parameterType="com.goafanti.common.model.News">
@@ -87,6 +89,9 @@
       <if test="content != null">
         content,
       </if>
+      <if test="newsDate != null">
+        news_date,
+      </if>
     </trim>
     <trim prefix="values (" suffix=")" suffixOverrides=",">
       <if test="id != null">
@@ -125,6 +130,9 @@
       <if test="content != null">
         #{content,jdbcType=LONGVARCHAR},
       </if>
+      <if test="newsDate != null">
+        #{newsDate,jdbcType=TIMESTAMP},
+      </if>
     </trim>
   </insert>
   <update id="updateByPrimaryKeySelective" parameterType="com.goafanti.common.model.News">
@@ -163,6 +171,9 @@
       <if test="content != null">
         content = #{content,jdbcType=LONGVARCHAR},
       </if>
+      <if test="newsDate != null">
+        news_date  = #{newsDate ,jdbcType=TIMESTAMP},
+      </if>
     </set>
     where id = #{id,jdbcType=BIGINT}
   </update>
@@ -178,7 +189,8 @@
       source = #{source,jdbcType=VARCHAR},
       source_url = #{sourceUrl,jdbcType=VARCHAR},
       summary = #{summary,jdbcType=VARCHAR},
-      content = #{content,jdbcType=LONGVARCHAR}
+      content = #{content,jdbcType=LONGVARCHAR},
+      news_date  = #{newsDate ,jdbcType=TIMESTAMP}
     where id = #{id,jdbcType=BIGINT}
   </update>
   <update id="updateByPrimaryKey" parameterType="com.goafanti.common.model.News">
@@ -192,7 +204,68 @@
       hot = #{hot,jdbcType=INTEGER},
       source = #{source,jdbcType=VARCHAR},
       source_url = #{sourceUrl,jdbcType=VARCHAR},
-      summary = #{summary,jdbcType=VARCHAR}
+      summary = #{summary,jdbcType=VARCHAR},
+      news_date  = #{newsDate ,jdbcType=TIMESTAMP}
     where id = #{id,jdbcType=BIGINT}
   </update>
+  
+  <select id="findNewsListByPage" parameterType="String" resultMap="BaseResultMap">
+  	select
+  		id,
+  		create_time,
+  		title,
+  		author,
+  		type,
+  		hot,
+  		source
+  	from news
+  	where 1 =1
+  	<if test="type != null">
+  		and type = #{type,jdbcType=INTEGER}
+  	</if>
+  	<if test="title != null">
+  		and title like CONCAT('%',#{title,jdbcType=VARCHAR},'%')
+  	</if>
+  	<if test="author != null">
+  		and author like CONCAT('%',#{author,jdbcType=VARCHAR},'%')
+  	</if>
+  	<if test="sDate != null">
+  	    and create_time <![CDATA[ >= ]]>  #{sDate,jdbcType=TIMESTAMP}
+  	</if>
+  	<if test="eDate != null">
+  		and create_time <![CDATA[ < ]]>  #{eDate,jdbcType=TIMESTAMP}
+  	</if>
+  	<if test="source != null">
+  		and source like CONCAT('%',#{source,jdbcType=VARCHAR},'%')
+  	</if>
+  	order by edit_time desc
+  	<if test="page_sql!=null">
+			${page_sql}
+	</if>
+  </select>
+  
+   <select id="findNewsCount" parameterType="String" resultType="java.lang.Integer">
+  	select
+  		count(1)
+  	from news
+  	where 1 =1
+  	<if test="type != null">
+  		and type = #{type,jdbcType=INTEGER}
+  	</if>
+  	<if test="title != null">
+  		and title like CONCAT('%',#{title,jdbcType=VARCHAR},'%')
+  	</if>
+  	<if test="author != null">
+  		and author like CONCAT('%',#{author,jdbcType=VARCHAR},'%')
+  	</if>
+  	<if test="sDate != null">
+  	    and create_time <![CDATA[ >= ]]>  #{sDate,jdbcType=TIMESTAMP}
+  	</if>
+  	<if test="eDate != null">
+  		and create_time <![CDATA[ < ]]>  #{eDate,jdbcType=TIMESTAMP}
+  	</if>
+  	<if test="source != null">
+  		and source like CONCAT('%',#{source,jdbcType=VARCHAR},'%')
+  	</if>
+  </select>
 </mapper>

+ 186 - 155
src/main/java/com/goafanti/common/model/News.java

@@ -2,160 +2,191 @@ package com.goafanti.common.model;
 
 import java.util.Date;
 
+import org.apache.commons.lang3.time.DateFormatUtils;
+
+import com.fasterxml.jackson.annotation.JsonIgnore;
+import com.goafanti.common.constant.AFTConstants;
+
 public class News {
-    /**
-    * 主键
-    */
-    private Long id;
-
-    /**
-    * 创建时间
-    */
-    private Date createTime;
-
-    /**
-    * 修改时间
-    */
-    private Date editTime;
-
-    /**
-    * 标题
-    */
-    private String title;
-
-    /**
-    * 题图url
-    */
-    private String titleImg;
-
-    /**
-    * 作者
-    */
-    private String author;
-
-    /**
-    * 类型
-    */
-    private Integer type;
-
-    /**
-    * 是否放在首页
-    */
-    private Integer hot;
-
-    /**
-    * 来源
-    */
-    private String source;
-
-    /**
-    * 来源url
-    */
-    private String sourceUrl;
-
-    /**
-    * 简介
-    */
-    private String summary;
-
-    /**
-    * 内容
-    */
-    private String content;
-
-    public Long getId() {
-        return id;
-    }
-
-    public void setId(Long id) {
-        this.id = id;
-    }
-
-    public Date getCreateTime() {
-        return createTime;
-    }
-
-    public void setCreateTime(Date createTime) {
-        this.createTime = createTime;
-    }
-
-    public Date getEditTime() {
-        return editTime;
-    }
-
-    public void setEditTime(Date editTime) {
-        this.editTime = editTime;
-    }
-
-    public String getTitle() {
-        return title;
-    }
-
-    public void setTitle(String title) {
-        this.title = title;
-    }
-
-    public String getTitleImg() {
-        return titleImg;
-    }
-
-    public void setTitleImg(String titleImg) {
-        this.titleImg = titleImg;
-    }
-
-    public String getAuthor() {
-        return author;
-    }
-
-    public void setAuthor(String author) {
-        this.author = author;
-    }
-
-    public Integer getType() {
-        return type;
-    }
-
-    public void setType(Integer type) {
-        this.type = type;
-    }
-
-    public Integer getHot() {
-        return hot;
-    }
-
-    public void setHot(Integer hot) {
-        this.hot = hot;
-    }
-
-    public String getSource() {
-        return source;
-    }
-
-    public void setSource(String source) {
-        this.source = source;
-    }
-
-    public String getSourceUrl() {
-        return sourceUrl;
-    }
-
-    public void setSourceUrl(String sourceUrl) {
-        this.sourceUrl = sourceUrl;
-    }
-
-    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;
-    }
+	/**
+	 * 主键
+	 */
+	private Long	id;
+
+	/**
+	 * 创建时间
+	 */
+	private Date	createTime;
+
+	/**
+	 * 修改时间
+	 */
+	private Date	editTime;
+
+	/**
+	 * 标题
+	 */
+	private String	title;
+
+	/**
+	 * 题图url
+	 */
+	private String	titleImg;
+
+	/**
+	 * 作者
+	 */
+	private String	author;
+
+	/**
+	 * 类型
+	 */
+	private Integer	type;
+
+	/**
+	 * 是否放在首页
+	 */
+	private Integer	hot;
+
+	/**
+	 * 来源
+	 */
+	private String	source;
+
+	/**
+	 * 来源url
+	 */
+	private String	sourceUrl;
+
+	/**
+	 * 简介
+	 */
+	private String	summary;
+
+	/**
+	 * 内容
+	 */
+	private String	content;
+
+	/**
+	 * 新闻日期
+	 */
+	private Date	newsDate;
+
+	public Long getId() {
+		return id;
+	}
+
+	public void setId(Long id) {
+		this.id = id;
+	}
+
+	public Date getCreateTime() {
+		return createTime;
+	}
+
+	public void setCreateTime(Date createTime) {
+		this.createTime = createTime;
+	}
+
+	public Date getEditTime() {
+		return editTime;
+	}
+
+	public void setEditTime(Date editTime) {
+		this.editTime = editTime;
+	}
+
+	public String getTitle() {
+		return title;
+	}
+
+	public void setTitle(String title) {
+		this.title = title;
+	}
+
+	public String getTitleImg() {
+		return titleImg;
+	}
+
+	public void setTitleImg(String titleImg) {
+		this.titleImg = titleImg;
+	}
+
+	public String getAuthor() {
+		return author;
+	}
+
+	public void setAuthor(String author) {
+		this.author = author;
+	}
+
+	public Integer getType() {
+		return type;
+	}
+
+	public void setType(Integer type) {
+		this.type = type;
+	}
+
+	public Integer getHot() {
+		return hot;
+	}
+
+	public void setHot(Integer hot) {
+		this.hot = hot;
+	}
+
+	public String getSource() {
+		return source;
+	}
+
+	public void setSource(String source) {
+		this.source = source;
+	}
+
+	public String getSourceUrl() {
+		return sourceUrl;
+	}
+
+	public void setSourceUrl(String sourceUrl) {
+		this.sourceUrl = sourceUrl;
+	}
+
+	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;
+	}
+	
+	@JsonIgnore
+	public Date getNewsDate() {
+		return newsDate;
+	}
+
+	public void setNewsDate(Date newsDate) {
+		this.newsDate = newsDate;
+	}
+
+	public String getCreateTimeFormattedDate() {
+		if (this.createTime == null) {
+			return null;
+		} else {
+			return DateFormatUtils.format(this.createTime, AFTConstants.YYYYMMDD);
+		}
+	}
+
+	public void setCreateTimeFormattedDate(String createTimeFormattedDate) {
+
+	}
 }

+ 53 - 0
src/main/java/com/goafanti/news/service/NewsService.java

@@ -1,5 +1,6 @@
 package com.goafanti.news.service;
 
+import java.text.ParseException;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
@@ -13,10 +14,14 @@ import org.springframework.cache.annotation.Cacheable;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
 
+import com.goafanti.common.constant.AFTConstants;
 import com.goafanti.common.dao.NewsMapper;
 import com.goafanti.common.model.News;
+import com.goafanti.common.utils.DateUtils;
 import com.goafanti.common.utils.LoggerUtils;
+import com.goafanti.common.utils.StringUtils;
 import com.goafanti.core.mybatis.BaseMybatisDao;
+import com.goafanti.core.mybatis.page.Pagination;
 import com.goafanti.news.bo.NewsSummary;
 
 @Service
@@ -58,4 +63,52 @@ public class NewsService extends BaseMybatisDao<NewsMapper> {
 		return news;
 	}
 
+	@SuppressWarnings("unchecked")
+	public Pagination<News> listNews(Integer type, String title, String author, String startCreateTime,
+			String endCreateTime, String source, Integer hot, Integer pNo, Integer pSize) {
+		Map<String, Object> params = new HashMap<>();
+		if (null == type) {
+			params.put("type", type);
+		}
+
+		if (StringUtils.isNotBlank(title)) {
+			params.put("title", title);
+		}
+
+		if (StringUtils.isNotBlank(author)) {
+			params.put("author", author);
+		}
+
+		if (StringUtils.isNotBlank(startCreateTime)) {
+			try {
+				params.put("sDate", DateUtils.parseDate(startCreateTime, AFTConstants.YYYYMMDD));
+			} catch (ParseException e) {
+			}
+		}
+
+		if (StringUtils.isNotBlank(endCreateTime)) {
+			try {
+				params.put("eDate", DateUtils.addDays(DateUtils.parseDate(endCreateTime, AFTConstants.YYYYMMDD), 1));
+			} catch (ParseException e) {
+			}
+		}
+
+		if (StringUtils.isNotBlank(source)) {
+			params.put("source", source);
+		}
+		
+		if (null != hot){
+			params.put("hot", hot);
+		}
+
+		if (pNo == null || pNo < 0) {
+			pNo = 1;
+		}
+
+		if (pSize == null || pSize < 0 || pSize > 10) {
+			pSize = 10;
+		}
+		return (Pagination<News>) findPage("findNewsListByPage", "findNewsCount", params, pNo, pSize);
+	}
+
 }