NewsService.java 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. package com.goafanti.news.service;
  2. import java.text.ParseException;
  3. import java.util.HashMap;
  4. import java.util.List;
  5. import java.util.Map;
  6. import org.slf4j.Logger;
  7. import org.slf4j.LoggerFactory;
  8. import org.springframework.beans.factory.annotation.Autowired;
  9. import org.springframework.cache.annotation.CacheEvict;
  10. import org.springframework.cache.annotation.CachePut;
  11. import org.springframework.cache.annotation.Cacheable;
  12. import org.springframework.stereotype.Service;
  13. import org.springframework.transaction.annotation.Transactional;
  14. import com.goafanti.common.constant.AFTConstants;
  15. import com.goafanti.common.dao.NewsMapper;
  16. import com.goafanti.common.model.News;
  17. import com.goafanti.common.utils.DateUtils;
  18. import com.goafanti.common.utils.LoggerUtils;
  19. import com.goafanti.common.utils.StringUtils;
  20. import com.goafanti.core.mybatis.BaseMybatisDao;
  21. import com.goafanti.core.mybatis.page.Pagination;
  22. import com.goafanti.news.bo.NewsPortalList;
  23. import com.goafanti.news.bo.NewsSummary;
  24. @Service
  25. @Transactional
  26. public class NewsService extends BaseMybatisDao<NewsMapper> {
  27. @Autowired
  28. NewsMapper newsMapper;
  29. private static final Logger logger = LoggerFactory.getLogger(NewsService.class);
  30. @Cacheable(value = "IndexNewsListCache", key = "'NewsList:Page:'+#pageNo+'Type:'+#type+'Size:'+#pageSize")
  31. public List<NewsSummary> findIndexNewsList(Integer pageNo, Integer type, Integer pageSize) {
  32. Map<String, Object> params = new HashMap<String, Object>();
  33. params.put("type", type);
  34. params.put("pageSize", pageSize);
  35. params.put("pageNo", pageNo);
  36. return newsMapper.findIndexNewsList(params);
  37. }
  38. @CacheEvict(value = "IndexNewsListCache", allEntries = true)
  39. public void cleanIndexNewsList() {
  40. LoggerUtils.debug(logger, "清除首页新闻列表缓存");
  41. }
  42. @Cacheable(value = "NewsCache", key = "'News:'+#id")
  43. public News selectById(Long id) {
  44. return newsMapper.selectByPrimaryKey(id);
  45. }
  46. @Cacheable(value = "NewsListCache", key = "'NewsList:Page:'+#pageNo+'Type:'+#type+'Size:'+#pageSize")
  47. public List<NewsSummary> findNewsList(Integer pageNo, Integer type, Integer pageSize) {
  48. Map<String, Object> params = new HashMap<String, Object>();
  49. params.put("type", type);
  50. params.put("pageSize", pageSize);
  51. params.put("pageNo", pageNo);
  52. return newsMapper.findList(params);
  53. }
  54. @CacheEvict(value = "NewsListCache", allEntries = true)
  55. public void cleanList() {
  56. LoggerUtils.debug(logger, "清除新闻列表缓存");
  57. }
  58. @CacheEvict(value = "NewsCache", key = "'News:'+#id")
  59. public void clean(String id) {
  60. LoggerUtils.debug(logger, "清除新闻缓存:[%s]", id);
  61. }
  62. @CachePut(value = "NewsCache", key = "'News:'+#id")
  63. public News save(Long id, News news) {
  64. news.setId(id);
  65. newsMapper.insert(news);
  66. return news;
  67. }
  68. @CachePut(value = "NewsCache", key = "'News:'+#id")
  69. public News update(long id, News news) {
  70. newsMapper.updateByPrimaryKeyWithBLOBs(news);
  71. return news;
  72. }
  73. @SuppressWarnings("unchecked")
  74. public Pagination<News> listNews(Integer type, String title, String author, String startCreateTime,
  75. String endCreateTime, String source, Integer hot, Integer pNo, Integer pSize) {
  76. Map<String, Object> params = new HashMap<>();
  77. if (null != type) {
  78. params.put("type", type);
  79. }
  80. if (StringUtils.isNotBlank(title)) {
  81. params.put("title", title);
  82. }
  83. if (StringUtils.isNotBlank(author)) {
  84. params.put("author", author);
  85. }
  86. if (StringUtils.isNotBlank(startCreateTime)) {
  87. try {
  88. params.put("sDate", DateUtils.parseDate(startCreateTime, AFTConstants.YYYYMMDD));
  89. } catch (ParseException e) {
  90. }
  91. }
  92. if (StringUtils.isNotBlank(endCreateTime)) {
  93. try {
  94. params.put("eDate", DateUtils.addDays(DateUtils.parseDate(endCreateTime, AFTConstants.YYYYMMDD), 1));
  95. } catch (ParseException e) {
  96. }
  97. }
  98. if (StringUtils.isNotBlank(source)) {
  99. params.put("source", source);
  100. }
  101. if (null != hot){
  102. params.put("hot", hot);
  103. }
  104. if (pNo == null || pNo < 0) {
  105. pNo = 1;
  106. }
  107. if (pSize == null || pSize < 0 || pSize > 10) {
  108. pSize = 10;
  109. }
  110. return (Pagination<News>) findPage("findNewsListByPage", "findNewsCount", params, pNo, pSize);
  111. }
  112. public News findNewsDetail(Long id) {
  113. return newsMapper.selectByPrimaryKey(id);
  114. }
  115. public int batchDeleteByPrimaryKey(List<String> id) {
  116. return newsMapper.batchDeleteByPrimaryKey(id);
  117. }
  118. @Cacheable(value = "NewsPortalListCache", key = "'NewsPortalList:Page:'+#pageNo+'Type:'+#type+'Size:'+#pageSize")
  119. public List<NewsPortalList> findPortalList(Integer pSize, Integer pNo, Integer type) {
  120. Map<String, Object> params = new HashMap<String, Object>();
  121. params.put("type", type);
  122. params.put("pageSize", pSize);
  123. params.put("pageNo", pNo);
  124. return newsMapper.findPortalList(params);
  125. }
  126. @CacheEvict(value = "NewsPortalListCache", allEntries = true)
  127. public void cleanPortalList() {
  128. LoggerUtils.debug(logger, "清除门户端新闻列表缓存");
  129. }
  130. public List<NewsSummary> findJmrhNewsList(String type){
  131. Map<String, Object> params = new HashMap<String, Object>();
  132. params.put("type", type);
  133. return newsMapper.findJmrhNewsList(params);
  134. }
  135. }