NewsService.java 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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+'domainName:'+#domainName")
  47. public List<NewsSummary> findNewsList(Integer pageNo, Integer type, Integer pageSize,String domainName) {
  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. params.put("website", domainName);
  53. return newsMapper.findList(params);
  54. }
  55. @CacheEvict(value = "NewsListCache", allEntries = true)
  56. public void cleanList() {
  57. LoggerUtils.debug(logger, "清除新闻列表缓存");
  58. }
  59. @CacheEvict(value = "NewsCache", key = "'News:'+#id")
  60. public void clean(String id) {
  61. LoggerUtils.debug(logger, "清除新闻缓存:[%s]", id);
  62. }
  63. @CachePut(value = "NewsCache", key = "'News:'+#id")
  64. public News save(Long id, News news) {
  65. news.setId(id);
  66. newsMapper.insert(news);
  67. return news;
  68. }
  69. @CachePut(value = "NewsCache", key = "'News:'+#id")
  70. public News update(long id, News news) {
  71. newsMapper.updateByPrimaryKeyWithBLOBs(news);
  72. return news;
  73. }
  74. @SuppressWarnings("unchecked")
  75. public Pagination<News> listNews(Integer type, String title, String author, String startCreateTime,
  76. String endCreateTime, String source, Integer hot, Integer pNo, Integer pSize) {
  77. Map<String, Object> params = new HashMap<>();
  78. if (null != type) {
  79. params.put("type", type);
  80. }
  81. if (StringUtils.isNotBlank(title)) {
  82. params.put("title", title);
  83. }
  84. if (StringUtils.isNotBlank(author)) {
  85. params.put("author", author);
  86. }
  87. if (StringUtils.isNotBlank(startCreateTime)) {
  88. try {
  89. params.put("sDate", DateUtils.parseDate(startCreateTime, AFTConstants.YYYYMMDD));
  90. } catch (ParseException e) {
  91. }
  92. }
  93. if (StringUtils.isNotBlank(endCreateTime)) {
  94. try {
  95. params.put("eDate", DateUtils.addDays(DateUtils.parseDate(endCreateTime, AFTConstants.YYYYMMDD), 1));
  96. } catch (ParseException e) {
  97. }
  98. }
  99. if (StringUtils.isNotBlank(source)) {
  100. params.put("source", source);
  101. }
  102. if (null != hot){
  103. params.put("hot", hot);
  104. }
  105. if (pNo == null || pNo < 0) {
  106. pNo = 1;
  107. }
  108. if (pSize == null || pSize < 0 || pSize > 10) {
  109. pSize = 10;
  110. }
  111. return (Pagination<News>) findPage("findNewsListByPage", "findNewsCount", params, pNo, pSize);
  112. }
  113. public News findNewsDetail(Long id) {
  114. return newsMapper.selectByPrimaryKey(id);
  115. }
  116. public int batchDeleteByPrimaryKey(List<String> id) {
  117. return newsMapper.batchDeleteByPrimaryKey(id);
  118. }
  119. @Cacheable(value = "NewsPortalListCache", key = "'NewsPortalList:Page:'+#pageNo+'Type:'+#type+'Size:'+#pageSize+'domainName:'+#domainName")
  120. public List<NewsPortalList> findPortalList(Integer pSize, Integer pNo, Integer type,String domainName) {
  121. Map<String, Object> params = new HashMap<String, Object>();
  122. params.put("type", type);
  123. params.put("pageSize", pSize);
  124. params.put("pageNo", pNo);
  125. params.put("website", domainName);
  126. return newsMapper.findPortalList(params);
  127. }
  128. @CacheEvict(value = "NewsPortalListCache", allEntries = true)
  129. public void cleanPortalList() {
  130. LoggerUtils.debug(logger, "清除门户端新闻列表缓存");
  131. }
  132. public List<NewsSummary> findJmrhNewsList(String type){
  133. Map<String, Object> params = new HashMap<String, Object>();
  134. params.put("type", type);
  135. return newsMapper.findJmrhNewsList(params);
  136. }
  137. }