package com.goafanti.news.service; import java.text.ParseException; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.servlet.http.HttpServletRequest; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cache.annotation.CacheEvict; import org.springframework.cache.annotation.CachePut; 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.NewsPortalList; import com.goafanti.news.bo.NewsSummary; @Service @Transactional public class NewsService extends BaseMybatisDao { @Autowired NewsMapper newsMapper; private static final Logger logger = LoggerFactory.getLogger(NewsService.class); @Cacheable(value = "IndexNewsListCache", key = "'NewsList:Page:'+#pageNo+'Type:'+#type+'Size:'+#pageSize+'domainName:'+#domainName") public List findIndexNewsList(Integer pageNo, Integer type, Integer pageSize,String domainName) { Map params = new HashMap(); params.put("type", type); params.put("pageSize", pageSize); params.put("pageNo", pageNo); params.put("website", domainName); return newsMapper.findIndexNewsList(params); } @CacheEvict(value = "IndexNewsListCache", allEntries = true) public void cleanIndexNewsList() { LoggerUtils.debug(logger, "清除首页新闻列表缓存"); } @Cacheable(value = "NewsCache", key = "'News:'+#id") public News selectById(Long id) { return newsMapper.selectByPrimaryKey(id); } @Cacheable(value = "NewsListCache", key = "'NewsList:Page:'+#pageNo+'Type:'+#type+'Size:'+#pageSize+'domainName:'+#domainName") public List findNewsList(Integer pageNo, Integer type, Integer pageSize,String domainName) { Map params = new HashMap(); params.put("type", type); params.put("pageSize", pageSize); params.put("pageNo", pageNo); params.put("website", domainName); return newsMapper.findList(params); } @CacheEvict(value = "NewsListCache", allEntries = true) public void cleanList() { LoggerUtils.debug(logger, "清除新闻列表缓存"); } @CacheEvict(value = "NewsCache", key = "'News:'+#id") public void clean(String id) { LoggerUtils.debug(logger, "清除新闻缓存:[%s]", id); } @CachePut(value = "NewsCache", key = "'News:'+#id") public News save(Long id, News news) { news.setId(id); newsMapper.insert(news); return news; } @CachePut(value = "NewsCache", key = "'News:'+#id") public News update(long id, News news) { newsMapper.updateByPrimaryKeyWithBLOBs(news); return news; } @SuppressWarnings("unchecked") public Pagination listNews(Integer type, String title, String author, String startCreateTime, String endCreateTime, String source, Integer hot, Integer pNo, Integer pSize) { Map 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) findPage("findNewsListByPage", "findNewsCount", params, pNo, pSize); } public News findNewsDetail(Long id) { return newsMapper.selectByPrimaryKey(id); } public int batchDeleteByPrimaryKey(List id) { return newsMapper.batchDeleteByPrimaryKey(id); } @Cacheable(value = "NewsPortalListCache", key = "'NewsPortalList:Page:'+#pageNo+'Type:'+#type+'Size:'+#pageSize+'domainName:'+#domainName") public List findPortalList(Integer pSize, Integer pNo, Integer type,String domainName) { Map params = new HashMap(); params.put("type", type); params.put("pageSize", pSize); params.put("pageNo", pNo); params.put("website", domainName); return newsMapper.findPortalList(params); } @CacheEvict(value = "NewsPortalListCache", allEntries = true) public void cleanPortalList() { LoggerUtils.debug(logger, "清除门户端新闻列表缓存"); } public List findJmrhNewsList(String type){ Map params = new HashMap(); params.put("type", type); return newsMapper.findJmrhNewsList(params); } }