1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
| @Service public class ArchiveServiceImpl implements ArchiveService {
@Autowired private ArticleMapper articleMapper;
@Override public ResponseResult getArchiveList() { LambdaQueryWrapper<Article> queryWrapper = new LambdaQueryWrapper<>(); queryWrapper.eq(Article::getStatus, SystemConstants.ARTICLE_STATUS_NORMAL) .eq(Article::getDelFlag, SystemConstants.NOT_DELETED) .orderByDesc(Article::getCreateTime);
List<Article> articles = articleMapper.selectList(queryWrapper);
Map<String, List<Article>> groupedArticles = articles.stream() .collect(Collectors.groupingBy(article -> { Calendar calendar = Calendar.getInstance(); calendar.setTime(article.getCreateTime()); int year = calendar.get(Calendar.YEAR); int month = calendar.get(Calendar.MONTH) + 1; return year + "-" + month; }, LinkedHashMap::new, Collectors.toList()));
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); List<ArchiveVo> archiveVos = groupedArticles.entrySet().stream() .map(entry -> { String[] yearMonth = entry.getKey().split("-"); Integer year = Integer.parseInt(yearMonth[0]); Integer month = Integer.parseInt(yearMonth[1]);
List<ArchiveArticleVo> articleVos = entry.getValue().stream() .map(article -> new ArchiveArticleVo( article.getId(), article.getTitle(), sdf.format(article.getCreateTime()) )) .collect(Collectors.toList());
return new ArchiveVo(year, month, articleVos.size(), articleVos); }) .collect(Collectors.toList());
return ResponseResult.okResult(archiveVos); }
@Override public ResponseResult getArticlesByYearMonth(Integer year, Integer month, Integer pageNum, Integer pageSize) { LambdaQueryWrapper<Article> queryWrapper = new LambdaQueryWrapper<>(); queryWrapper.eq(Article::getStatus, SystemConstants.ARTICLE_STATUS_NORMAL) .eq(Article::getDelFlag, SystemConstants.NOT_DELETED) .apply("YEAR(create_time) = {0}", year) .apply("MONTH(create_time) = {0}", month) .orderByDesc(Article::getCreateTime);
Page<Article> page = new Page<>(pageNum, pageSize); articleMapper.selectPage(page, queryWrapper);
List<ArchiveArticleVo> articleVos = page.getRecords().stream() .map(article -> { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); return new ArchiveArticleVo( article.getId(), article.getTitle(), sdf.format(article.getCreateTime()) ); }) .collect(Collectors.toList());
PageVo pageVo = new PageVo(articleVos, page.getTotal()); return ResponseResult.okResult(pageVo); } }
|