NewsService.java 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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.beans.factory.annotation.Value;
  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. @Value("${spring.profiles.active}")
  31. private String env;
  32. private static final Logger logger = LoggerFactory.getLogger(NewsService.class);
  33. @Cacheable(value = "IndexNewsListCache", key = "'NewsList:Page:'+#pageNo+'Type:'+#type+'Size:'+#pageSize")
  34. public List<NewsSummary> findIndexNewsList(Integer pageNo, Integer type, Integer pageSize) {
  35. Map<String, Object> params = new HashMap<String, Object>();
  36. params.put("type", type);
  37. params.put("pageSize", pageSize);
  38. params.put("pageNo", pageNo);
  39. return newsMapper.findIndexNewsLists(params);
  40. }
  41. @CacheEvict(value = "IndexNewsListCache", allEntries = true)
  42. public void cleanIndexNewsList() {
  43. LoggerUtils.debug(logger, "清除首页新闻列表缓存");
  44. }
  45. @Cacheable(value = "NewsCache", key = "'News:'+#id")
  46. public News selectById(Long id) {
  47. return newsMapper.selectByPrimaryKey(id);
  48. }
  49. @Cacheable(value = "NewsListCache", key = "'NewsList:Page:'+#pageNo+'Type:'+#type+'Size:'+#pageSize+'domainName:'+#domainName+'picReq:'+#picRequired")
  50. public List<NewsSummary> findNewsList(Integer pageNo, Integer type, Integer pageSize, String domainName,
  51. Boolean picRequired) {
  52. Map<String, Object> params = new HashMap<String, Object>();
  53. params.put("type", type);
  54. params.put("pageSize", pageSize);
  55. params.put("pageNo", pageNo);
  56. if (!StringUtils.equals(env, "dev") && !StringUtils.equals(env, "local")) {
  57. params.put("website", domainName);
  58. }
  59. if (picRequired) {
  60. params.put("picRequired", picRequired);
  61. }
  62. return newsMapper.findList(params);
  63. }
  64. @CacheEvict(value = "NewsListCache", allEntries = true)
  65. public void cleanList() {
  66. LoggerUtils.debug(logger, "清除新闻列表缓存");
  67. }
  68. @CacheEvict(value = "NewsCache", key = "'News:'+#id")
  69. public void clean(String id) {
  70. LoggerUtils.debug(logger, "清除新闻缓存:[%s]", id);
  71. }
  72. @CachePut(value = "NewsCache", key = "'News:'+#id")
  73. public News save(Long id, News news) {
  74. news.setId(id);
  75. newsMapper.insert(news);
  76. return news;
  77. }
  78. @CachePut(value = "NewsCache", key = "'News:'+#id")
  79. public News update(long id, News news) {
  80. newsMapper.updateByPrimaryKeyWithBLOBs(news);
  81. return news;
  82. }
  83. @SuppressWarnings("unchecked")
  84. public Pagination<News> listNews(Integer type, String title, String author, String startCreateTime,
  85. String endCreateTime, String source, Integer hot, Integer pNo, Integer pSize) {
  86. Map<String, Object> params = new HashMap<>();
  87. if (null != type) {
  88. params.put("type", type);
  89. }
  90. if (StringUtils.isNotBlank(title)) {
  91. params.put("title", title);
  92. }
  93. if (StringUtils.isNotBlank(author)) {
  94. params.put("author", author);
  95. }
  96. if (StringUtils.isNotBlank(startCreateTime)) {
  97. try {
  98. params.put("sDate", DateUtils.parseDate(startCreateTime, AFTConstants.YYYYMMDD));
  99. } catch (ParseException e) {
  100. }
  101. }
  102. if (StringUtils.isNotBlank(endCreateTime)) {
  103. try {
  104. params.put("eDate", DateUtils.addDays(DateUtils.parseDate(endCreateTime, AFTConstants.YYYYMMDD), 1));
  105. } catch (ParseException e) {
  106. }
  107. }
  108. if (StringUtils.isNotBlank(source)) {
  109. params.put("source", source);
  110. }
  111. if (null != hot) {
  112. params.put("hot", hot);
  113. }
  114. if (pNo == null || pNo < 0) {
  115. pNo = 1;
  116. }
  117. if (pSize == null || pSize < 0 || pSize > 10) {
  118. pSize = 10;
  119. }
  120. return (Pagination<News>) findPage("findNewsListByPage", "findNewsCount", params, pNo, pSize);
  121. }
  122. public News findNewsDetail(Long id) {
  123. return newsMapper.selectByPrimaryKey(id);
  124. }
  125. public int batchDeleteByPrimaryKey(List<String> id) {
  126. return newsMapper.batchDeleteByPrimaryKey(id);
  127. }
  128. @SuppressWarnings("unchecked")
  129. public Pagination<NewsPortalList> portalNewsList(Integer pSize, Integer pNo, Integer type, Integer hot,
  130. Integer provinceId, String keyword) {
  131. Map<String, Object> params = new HashMap<String, Object>();
  132. if (null != hot) {
  133. params.put("hot", hot);
  134. }
  135. if (null != type) {
  136. params.put("type", type);
  137. }
  138. if (null != provinceId) {
  139. params.put("provinceId", provinceId);
  140. }
  141. if (null != keyword) {
  142. params.put("keyword", keyword);
  143. }
  144. if (pNo == null || pNo < 0) {
  145. pNo = 1;
  146. }
  147. if (pSize == null || pSize < 0 || pSize > 10) {
  148. pSize = 10;
  149. }
  150. return (Pagination<NewsPortalList>) findPage("portalNewsList", "portalNewsCount", params, pNo, pSize);
  151. }
  152. @CacheEvict(value = "NewsPortalListCache", allEntries = true)
  153. public void cleanPortalList() {
  154. LoggerUtils.debug(logger, "清除门户端新闻列表缓存");
  155. }
  156. public List<NewsSummary> findJmrhNewsList(String type) {
  157. Map<String, Object> params = new HashMap<String, Object>();
  158. params.put("type", type);
  159. return newsMapper.findJmrhNewsList(params);
  160. }
  161. public List<NewsPortalList> portalNewsPolicy(){
  162. return newsMapper.portalNewsPolicy();
  163. }
  164. public List<NewsPortalList> portalNewsQuestion(){
  165. return newsMapper.portalNewsQuestion();
  166. }
  167. }