NewsArticleServiceImpl.java 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. package com.jkcredit.sysnews.service.newsArticle;
  2. import com.baomidou.mybatisplus.core.metadata.IPage;
  3. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
  4. import com.jkcredit.sysnews.enums.PhotoStatusEnum;
  5. import com.jkcredit.sysnews.enums.PhotoTypeEnum;
  6. import com.jkcredit.sysnews.mapper.NewsArticleMapper;
  7. import com.jkcredit.sysnews.model.dto.newsArticle.NewsArticleDto;
  8. import com.jkcredit.sysnews.model.dto.photo.PhotoDto;
  9. import com.jkcredit.sysnews.model.po.newsArticle.NewsArticlePo;
  10. import com.jkcredit.sysnews.model.po.photo.PhotoPo;
  11. import com.jkcredit.sysnews.model.vo.newsArticle.NewsArticleVo;
  12. import com.jkcredit.sysnews.model.vo.photo.PhotoVo;
  13. import com.jkcredit.sysnews.service.base.BaseService;
  14. import com.jkcredit.sysnews.service.photo.PhotoService;
  15. import com.jkcredit.sysnews.spi.lang.constant.CommonConstant;
  16. import com.jkcredit.sysnews.spi.lang.exception.ServiceException;
  17. import com.jkcredit.sysnews.util.AssertUtils;
  18. import com.jkcredit.sysnews.util.BeanUtil;
  19. import lombok.extern.slf4j.Slf4j;
  20. import org.apache.commons.lang.StringUtils;
  21. import org.apache.commons.lang.enums.EnumUtils;
  22. import org.springframework.beans.BeanUtils;
  23. import org.springframework.beans.factory.annotation.Autowired;
  24. import org.springframework.beans.factory.annotation.Value;
  25. import org.springframework.stereotype.Service;
  26. import org.springframework.transaction.annotation.Transactional;
  27. import javax.annotation.Resource;
  28. import java.util.Date;
  29. /**
  30. * @description:
  31. * @author: xusonglin
  32. * @create: 2020/3/1 22:46
  33. * @version: V1.0
  34. **/
  35. @Slf4j
  36. @Service
  37. @Transactional(readOnly = true)
  38. public class NewsArticleServiceImpl extends BaseService implements NewsArticleService {
  39. @Value("${photo.relativePath}")
  40. private String relativePath;
  41. @Resource
  42. NewsArticleMapper mapper;
  43. @Autowired
  44. PhotoService photoService;
  45. @Override
  46. public IPage<NewsArticleVo> getNewsArticles(Page page, NewsArticleDto newsArticleDto) {
  47. IPage<NewsArticlePo> newsArticlePoIPage = mapper.getNewsArticles(page, newsArticleDto);
  48. return newsArticlePoIPage.convert(this::convert);
  49. }
  50. private NewsArticleVo convert(NewsArticlePo po) {
  51. NewsArticleVo vo = new NewsArticleVo();
  52. BeanUtils.copyProperties(po, vo);
  53. return vo;
  54. }
  55. @Override
  56. public NewsArticleVo getNewsArticleById(Long id) {
  57. NewsArticlePo newsArticlePo = mapper.getNewsArticleById(id);
  58. NewsArticleVo newsArticleVo = new NewsArticleVo();
  59. BeanUtil.copyProperties(newsArticleVo, newsArticlePo);
  60. return newsArticleVo;
  61. }
  62. @Override
  63. @Transactional(rollbackFor = ServiceException.class)
  64. public void saveNewsArticle(NewsArticleDto newsArticleDto) {
  65. validate(newsArticleDto);
  66. PhotoDto photoDto = new PhotoDto();
  67. photoDto.setName(newsArticleDto.getTitle());
  68. photoDto.setType(PhotoTypeEnum.NEWS_PHOTO.getValue());
  69. photoDto.setStatus(PhotoStatusEnum.UPLOAD_SUCCESS_HAVE_USED.getValue());
  70. // 更新新闻content中的图片
  71. try {
  72. for (String url : newsArticleDto.getUrlList()) {
  73. PhotoPo photoPo = photoService.getPhotoByUrl(url);
  74. photoDto.setId(photoPo.getId());
  75. photoService.editPhoto(photoDto);
  76. }
  77. } catch (ServiceException e) {
  78. log.error("新增新闻-更新图片失败,失败原因:{}", e.getMessage());
  79. throw new ServiceException("新增新闻失败");
  80. }
  81. // 保存新闻首图
  82. PhotoVo photoVo;
  83. try {
  84. photoDto.setPhoto(newsArticleDto.getPhoto());
  85. photoDto.setId(null);
  86. photoVo = photoService.savePhoto(photoDto);
  87. } catch (ServiceException e) {
  88. log.error("新增新闻-新增首图失败,失败原因:{}", e.getMessage());
  89. throw new ServiceException("新增新闻失败");
  90. }
  91. // 保存新闻
  92. NewsArticlePo newsArticlePo = new NewsArticlePo();
  93. BeanUtil.copyProperties(newsArticlePo, newsArticleDto);
  94. newsArticlePo.setCreateTime(new Date());
  95. newsArticlePo.setPhotoId(photoVo.getId());
  96. try {
  97. mapper.insert(newsArticlePo);
  98. } catch (Exception e) {
  99. log.error("新增新闻失败,失败原因:{}", e.getMessage());
  100. throw new ServiceException("新增新闻失败");
  101. }
  102. }
  103. @Override
  104. @Transactional(rollbackFor = ServiceException.class)
  105. public void editNewsArticle(NewsArticleDto newsArticleDto) {
  106. validate(newsArticleDto);
  107. NewsArticlePo newsArticlePo = mapper.getNewsArticleById(newsArticleDto.getId());
  108. AssertUtils.assertNotNull(newsArticleDto, CommonConstant.ARTICLE_NOT_EXIST);
  109. try {
  110. if (!StringUtils.isBlank(newsArticleDto.getTitle())) {
  111. newsArticlePo.setTitle(newsArticleDto.getTitle());
  112. }
  113. if (!StringUtils.isBlank(newsArticleDto.getNewsAbstract())) {
  114. newsArticlePo.setNewsAbstract(newsArticleDto.getNewsAbstract());
  115. }
  116. if (!StringUtils.isBlank(newsArticleDto.getContent())) {
  117. newsArticlePo.setContent(newsArticleDto.getContent());
  118. }
  119. if (newsArticleDto.getPhoto() != null) {
  120. try {
  121. } catch (ServiceException e) {
  122. }
  123. photoService.deletePhoto(newsArticlePo.getPhotoId());
  124. newsArticlePo.setPhotoId(newsArticleDto.getPhotoId());
  125. }
  126. mapper.updateById(newsArticlePo);
  127. } catch (Exception e) {
  128. log.error("编辑新闻失败,失败原因:{}", e.getMessage());
  129. throw new ServiceException("编辑新闻失败");
  130. }
  131. }
  132. @Override
  133. @Transactional(rollbackFor = ServiceException.class)
  134. public void deleteNewsArticle(Long id) {
  135. NewsArticlePo newsArticlePo = mapper.getNewsArticleById(id);
  136. newsArticlePo.setActivated(CommonConstant.ACTIVATED_DELETED);
  137. newsArticlePo.setUpdateTime(new Date());
  138. try {
  139. mapper.updateById(newsArticlePo);
  140. } catch (Exception e) {
  141. log.error("删除新闻失败,失败原因:{}", e.getMessage());
  142. throw new ServiceException("删除新闻失败");
  143. }
  144. }
  145. }