NewsServiceImpl.java 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. package sqgxy.xxydz.service.impl;
  2. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  3. import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
  4. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
  5. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  6. import sqgxy.xxydz.dto.*;
  7. import sqgxy.xxydz.entity.News;
  8. import sqgxy.xxydz.entity.NewsCategory;
  9. import sqgxy.xxydz.enums.DisplayContent;
  10. import sqgxy.xxydz.exception.HintException;
  11. import sqgxy.xxydz.mapper.NewsCategoryMapper;
  12. import sqgxy.xxydz.mapper.NewsMapper;
  13. import sqgxy.xxydz.module.HeaderImgUpload;
  14. import sqgxy.xxydz.service.NewsCategoryService;
  15. import sqgxy.xxydz.service.NewsService;
  16. import org.modelmapper.ModelMapper;
  17. import org.modelmapper.TypeToken;
  18. import org.springframework.beans.factory.annotation.Autowired;
  19. import org.springframework.stereotype.Service;
  20. import java.io.IOException;
  21. import java.util.List;
  22. /**
  23. * @author xmp
  24. * @date 2022-11-19 13:32
  25. */
  26. @Service
  27. public class NewsServiceImpl extends ServiceImpl<NewsMapper, News> implements NewsService {
  28. @Autowired
  29. ModelMapper modelMapper;
  30. @Autowired
  31. NewsCategoryMapper newsCategoryMapper;
  32. @Override
  33. public boolean saveNews(NewsAddDTO dto) throws IOException {
  34. if (nonentityNewsCategory(dto.getNewsCategoryId())) {
  35. throw new HintException("此新闻类别不存在");
  36. }
  37. Integer newCategoryId = dto.getNewsCategoryId();
  38. NewsCategory newsCategory = newsCategoryMapper.selectOne(new QueryWrapper<NewsCategory>().select(NewsCategoryService.DISPLAY_CONTENT).eq(NewsCategoryService.ID, newCategoryId));
  39. // 小标题为新闻类型时,该标题下只能存储一篇新闻
  40. if (newsCategory.getDisplayContent() == DisplayContent.NEWS) {
  41. // 检查该标题下是否存在新闻
  42. int count = count(new QueryWrapper<News>().eq(NEWS_CATEGORY_ID, newCategoryId));
  43. if (count >= 10) {//todo
  44. throw new HintException("该类别下只能存在一篇新闻");
  45. }
  46. }
  47. // 判断图片是否为空
  48. if (dto.getPictureFile() != null) {
  49. String s = HeaderImgUpload.headPortraitUpload(dto.getPictureFile());
  50. dto.setPicturePath(s);
  51. }
  52. // 判断附件是否为空
  53. if (dto.getAttachment() != null) {
  54. String s = HeaderImgUpload.headPortraitUpload(dto.getAttachment());
  55. dto.setAttachmentPath(s);
  56. }
  57. return save(modelMapper.map(dto, News.class));
  58. }
  59. @Override
  60. public boolean removeNews(Integer id) {
  61. return removeById(id);
  62. }
  63. @Override
  64. public boolean updateNews(NewsUpdateDTO dto) throws IOException {
  65. // 查看新闻是否存在
  66. int count1 = count(new QueryWrapper<News>().eq(ID, dto.getId()));
  67. if (count1 < 1) {
  68. throw new HintException("id为" + dto.getId() + "的新闻不存在");
  69. }
  70. if (dto.getNewsCategoryId() != null && nonentityNewsCategory(dto.getNewsCategoryId())) {
  71. throw new HintException("此新闻类别不存在");
  72. }
  73. // dto.getNewsCategoryId() 不为null 说明新闻类别可能会更改,需要判断更改后的类别是否是只能存储一篇新闻的展示新闻类型
  74. if (dto.getNewsCategoryId() != null) {
  75. Integer newCategoryId = dto.getNewsCategoryId();
  76. NewsCategory newsCategory = newsCategoryMapper.selectOne(new QueryWrapper<NewsCategory>().select(NewsCategoryService.DISPLAY_CONTENT).eq(NewsCategoryService.ID, newCategoryId));
  77. if (newsCategory.getDisplayContent() == DisplayContent.NEWS) {
  78. // 检查该标题下是否存在新闻
  79. int count = count(new QueryWrapper<News>().eq(NEWS_CATEGORY_ID, newCategoryId));
  80. if (count >= 1) {
  81. throw new HintException("该类别下只能存在一篇新闻");
  82. }
  83. }
  84. }
  85. // 需要修改图片
  86. if (dto.getPictureFile() != null) {
  87. String s = HeaderImgUpload.headPortraitUpload(dto.getPictureFile());
  88. dto.setPicturePath(s);
  89. }
  90. return updateById(modelMapper.map(dto, News.class));
  91. }
  92. @Override
  93. public NewsPaging getNewsListByNewsCategoryId(Integer newsCategoryId, Integer current, Integer size) {
  94. Page<News> page = page(new Page<>(current, size), new QueryWrapper<News>()
  95. .select(ID, TITLE, RELEASE_TIME, PICTURE_PATH)
  96. .eq(NEWS_CATEGORY_ID, newsCategoryId).orderByDesc(RELEASE_TIME));
  97. NewsPaging newsPaging = modelMapper.map(page, NewsPaging.class);
  98. List<News> records = page.getRecords();
  99. newsPaging.setRecords(modelMapper.map(records, new TypeToken<List<NewsQueryListDTO>>(){}.getType()));
  100. return newsPaging;
  101. }
  102. @Override
  103. public NewsQueryDTO getNewsById(Integer id) {
  104. // 新闻点击率加一
  105. News one = getOne(new QueryWrapper<News>().select(HITS).eq(ID, id));
  106. if (one == null) {
  107. return null;
  108. }
  109. update(new UpdateWrapper<News>().set(HITS, one.getHits() + 1).eq(ID, id));
  110. return modelMapper.map(getById(id), NewsQueryDTO.class);
  111. }
  112. @Override
  113. public NewsPaging fuzzyQueryListByTitle(String title, Integer current, Integer size) {
  114. Page<News> page = page(new Page<>(current, size), new QueryWrapper<News>().select(ID, TITLE, RELEASE_TIME, PICTURE_PATH).like(TITLE, title).orderByDesc(RELEASE_TIME));
  115. NewsPaging newsPaging = modelMapper.map(page, NewsPaging.class);
  116. List<News> records = page.getRecords();
  117. newsPaging.setRecords(modelMapper.map(records, new TypeToken<List<NewsQueryListDTO>>(){}.getType()));
  118. return newsPaging;
  119. }
  120. /**
  121. * 不存在 NewsCategory
  122. */
  123. private boolean nonentityNewsCategory(Integer newsCategoryId) {
  124. Integer count = newsCategoryMapper.selectCount(new QueryWrapper<NewsCategory>().eq(NewsCategoryService.ID, newsCategoryId));
  125. return count < 1;
  126. }
  127. }