NewsService.java 5.3 KB

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