NewsArticleServiceImpl.java 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  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.newsArticleNavigationBar.NewsArticleNavigationBarDto;
  9. import com.jkcredit.sysnews.model.dto.newsArticlePhoto.NewsArticlePhotoDto;
  10. import com.jkcredit.sysnews.model.dto.photo.PhotoDto;
  11. import com.jkcredit.sysnews.model.po.newsArticle.NewsArticlePo;
  12. import com.jkcredit.sysnews.model.po.newsArticleNavigationBar.NewsArticleNavigationBarPo;
  13. import com.jkcredit.sysnews.model.po.newsArticlePhoto.NewsArticlePhotoPo;
  14. import com.jkcredit.sysnews.model.po.photo.PhotoPo;
  15. import com.jkcredit.sysnews.model.vo.newsArticle.NewsArticleBizVo;
  16. import com.jkcredit.sysnews.model.vo.newsArticle.NewsArticleVo;
  17. import com.jkcredit.sysnews.model.vo.photo.PhotoVo;
  18. import com.jkcredit.sysnews.service.base.BaseService;
  19. import com.jkcredit.sysnews.service.newsArticleNavigationBar.NewsArticleNavigationBarService;
  20. import com.jkcredit.sysnews.service.newsArticlePhoto.NewsArticlePhotoService;
  21. import com.jkcredit.sysnews.service.photo.PhotoService;
  22. import com.jkcredit.sysnews.spi.lang.constant.CommonConstant;
  23. import com.jkcredit.sysnews.spi.lang.exception.ServiceException;
  24. import com.jkcredit.sysnews.util.AssertUtils;
  25. import com.jkcredit.sysnews.util.BeanUtil;
  26. import com.jkcredit.sysnews.util.CommonUtil;
  27. import lombok.extern.slf4j.Slf4j;
  28. import org.apache.commons.lang.StringUtils;
  29. import org.apache.commons.lang.enums.EnumUtils;
  30. import org.springframework.beans.BeanUtils;
  31. import org.springframework.beans.factory.annotation.Autowired;
  32. import org.springframework.beans.factory.annotation.Value;
  33. import org.springframework.stereotype.Service;
  34. import org.springframework.transaction.annotation.Transactional;
  35. import javax.annotation.Resource;
  36. import java.io.File;
  37. import java.util.*;
  38. /**
  39. * @description:
  40. * @author: xusonglin
  41. * @create: 2020/3/1 22:46
  42. * @version: V1.0
  43. **/
  44. @Slf4j
  45. @Service
  46. @Transactional(readOnly = true)
  47. public class NewsArticleServiceImpl extends BaseService implements NewsArticleService {
  48. @Value("${photo.uploadPath}")
  49. private String uploadPath;
  50. @Value("${photo.accessPath}")
  51. private String accessPath;
  52. @Value("${photo.accessResource}")
  53. private String accessResource;
  54. @Resource
  55. NewsArticleMapper mapper;
  56. @Autowired
  57. PhotoService photoService;
  58. @Autowired
  59. NewsArticleNavigationBarService newsArticleNavigationBarService;
  60. @Autowired
  61. NewsArticlePhotoService newsArticlePhotoService;
  62. @Override
  63. public IPage<NewsArticleVo> getNewsArticles(Page page, NewsArticleDto newsArticleDto) {
  64. IPage<NewsArticlePo> newsArticlePoIPage = mapper.getNewsArticles(page, newsArticleDto);
  65. return newsArticlePoIPage.convert(this::convert);
  66. }
  67. private NewsArticleVo convert(NewsArticlePo po) {
  68. NewsArticleVo vo = new NewsArticleVo();
  69. BeanUtils.copyProperties(po, vo);
  70. if (po.getPhotoUrl() != null) {
  71. String url = accessPath + accessResource + po.getPhotoUrl().replace("/","");
  72. vo.setPhotoUrl(url);
  73. }
  74. return vo;
  75. }
  76. @Override
  77. public NewsArticleVo getNewsArticleById(Long id) {
  78. NewsArticlePo newsArticlePo = mapper.getNewsArticleById(id);
  79. NewsArticleVo newsArticleVo = new NewsArticleVo();
  80. BeanUtil.copyProperties(newsArticleVo, newsArticlePo);
  81. if (newsArticlePo.getPhotoUrl() != null) {
  82. String url = accessPath + accessResource + newsArticlePo.getPhotoUrl().replace("/","");
  83. newsArticleVo.setPhotoUrl(url);
  84. }
  85. // 查询新闻所属导航
  86. List<Long> navigationBarIdList = newsArticleNavigationBarService.getNavigationBarIdByNewsId(id);
  87. newsArticleVo.setNavigationBarIds(navigationBarIdList);
  88. // 查询新闻内图片url地址
  89. List<NewsArticlePhotoPo> newsArticlePhotoPoList = newsArticlePhotoService.getArticlePhotoByNewsId(id);
  90. List<String> urlList = new ArrayList<>();
  91. for (NewsArticlePhotoPo po : newsArticlePhotoPoList) {
  92. PhotoVo photoVo = photoService.getPhotoById(po.getPhotoId());
  93. urlList.add(photoVo.getUrl());
  94. }
  95. newsArticleVo.setUrlList(urlList);
  96. return newsArticleVo;
  97. }
  98. @Override
  99. @Transactional(rollbackFor = ServiceException.class)
  100. public void saveNewsArticle(NewsArticleDto newsArticleDto) {
  101. validate(newsArticleDto);
  102. PhotoDto photoDto = new PhotoDto();
  103. photoDto.setName(newsArticleDto.getTitle());
  104. photoDto.setType(PhotoTypeEnum.NEWS_PHOTO.getValue());
  105. photoDto.setStatus(PhotoStatusEnum.UPLOAD_SUCCESS_HAVE_USED.getValue());
  106. // 保存新闻首图
  107. PhotoVo photoVo;
  108. try {
  109. photoDto.setPhoto(newsArticleDto.getPhoto());
  110. photoVo = photoService.savePhoto(photoDto);
  111. } catch (ServiceException e) {
  112. log.error("新增新闻-新增首图失败,失败原因:{}", e.getMessage());
  113. throw new ServiceException("新增新闻失败");
  114. }
  115. // 保存新闻
  116. NewsArticlePo newsArticlePo = new NewsArticlePo();
  117. BeanUtil.copyProperties(newsArticlePo, newsArticleDto);
  118. newsArticlePo.setCreateTime(new Date());
  119. newsArticlePo.setPhotoId(photoVo.getId());
  120. try {
  121. mapper.insert(newsArticlePo);
  122. } catch (Exception e) {
  123. log.error("新增新闻失败,失败原因:{}", e.getMessage());
  124. throw new ServiceException("新增新闻失败");
  125. }
  126. // 更新新闻content中的图片, 并插入新闻-图片关联表
  127. if (newsArticleDto.getUrlList() != null) {
  128. try {
  129. for (String url : newsArticleDto.getUrlList()) {
  130. url = "/" + url.replace(accessPath, "").replace(accessResource, "");
  131. PhotoPo photoPo = photoService.getPhotoByUrl(url);
  132. photoDto.setId(photoPo.getId());
  133. // 更新新闻content中的图片
  134. photoService.editPhoto(photoDto);
  135. NewsArticlePhotoDto newsArticlePhotoDto = new NewsArticlePhotoDto();
  136. newsArticlePhotoDto.setPhotoId(photoPo.getId());
  137. newsArticlePhotoDto.setNewsArticleId(newsArticlePo.getId());
  138. newsArticlePhotoService.saveNewsArticlePhoto(newsArticlePhotoDto);
  139. }
  140. } catch (ServiceException e) {
  141. log.error("新增新闻-更新图片或并插入新闻-图片关联表失败,失败原因:{}", e.getMessage());
  142. throw new ServiceException("新增新闻失败");
  143. }
  144. }
  145. // 保存导航栏
  146. try {
  147. for (Long navigationBarId : newsArticleDto.getNavigationBarIds()) {
  148. NewsArticleNavigationBarDto dto = new NewsArticleNavigationBarDto();
  149. dto.setNewsArticleId(newsArticlePo.getId());
  150. dto.setNavigationBarId(navigationBarId);
  151. newsArticleNavigationBarService.saveNewsArticleNavigationBar(dto);
  152. }
  153. } catch (Exception e) {
  154. log.error("新增新闻-新增新闻导航关联表失败,失败原因:{}", e.getMessage());
  155. throw new ServiceException("新增新闻失败");
  156. }
  157. }
  158. @Override
  159. @Transactional(rollbackFor = ServiceException.class)
  160. public void editNewsArticle(NewsArticleDto newsArticleDto) {
  161. validate(newsArticleDto);
  162. NewsArticlePo newsArticlePo = mapper.getNewsArticleById(newsArticleDto.getId());
  163. AssertUtils.assertNotNull(newsArticleDto, CommonConstant.ARTICLE_NOT_EXIST);
  164. try {
  165. if (!StringUtils.isBlank(newsArticleDto.getTitle())) {
  166. newsArticlePo.setTitle(newsArticleDto.getTitle());
  167. }
  168. if (!StringUtils.isBlank(newsArticleDto.getNewsAbstract())) {
  169. newsArticlePo.setNewsAbstract(newsArticleDto.getNewsAbstract());
  170. }
  171. if (!StringUtils.isBlank(newsArticleDto.getContent())) {
  172. newsArticlePo.setContent(newsArticleDto.getContent());
  173. }
  174. if (newsArticleDto.getTopFlag() != null) {
  175. newsArticlePo.setTopFlag(newsArticleDto.getTopFlag());
  176. }
  177. if (newsArticleDto.getPhoto() != null) {
  178. try {
  179. photoService.deletePhoto(newsArticlePo.getPhotoId());
  180. PhotoDto photoDto = new PhotoDto();
  181. photoDto.setName(newsArticlePo.getTitle());
  182. photoDto.setType(PhotoTypeEnum.NEWS_PHOTO.getValue());
  183. photoDto.setStatus(PhotoStatusEnum.UPLOAD_SUCCESS_HAVE_USED.getValue());
  184. photoDto.setPhoto(newsArticleDto.getPhoto());
  185. PhotoVo photoVo = photoService.savePhoto(photoDto);
  186. newsArticlePo.setPhotoId(photoVo.getId());
  187. } catch (ServiceException e) {
  188. log.error("更换新闻封面图失败,失败原因:{}", e.getMessage());
  189. throw new ServiceException("更换新闻封面图失败");
  190. }
  191. }
  192. newsArticlePo.setUpdateTime(new Date());
  193. newsArticlePo.setPhotoUrl(null);
  194. mapper.updateById(newsArticlePo);
  195. } catch (Exception e) {
  196. log.error("编辑新闻失败,失败原因:{}", e.getMessage());
  197. throw new ServiceException("编辑新闻失败");
  198. }
  199. // 更新导航栏
  200. if (newsArticleDto.getNavigationBarIds() != null && !newsArticleDto.getNavigationBarIds().isEmpty()) {
  201. editNewsArticleNavigationBar(newsArticleDto);
  202. }
  203. // 更新新闻内容中的图片
  204. if (newsArticleDto.getUrlList() != null && !newsArticleDto.getUrlList().isEmpty()) {
  205. editNewsArticlePhoto(newsArticleDto);
  206. }
  207. }
  208. private void editNewsArticleNavigationBar(NewsArticleDto newsArticleDto) {
  209. List<NewsArticleNavigationBarDto> newsArticleNavigationBarDtoList = newsArticleNavigationBarService.getArticleNavigationBarByNewsId(newsArticleDto.getId());
  210. // 现有1 2 3 4 更新后 2 3 4 5 删掉1 新增 5
  211. List<Long> idFromDataBase = new ArrayList<>();
  212. for (NewsArticleNavigationBarDto dto : newsArticleNavigationBarDtoList) {
  213. idFromDataBase.add(dto.getNavigationBarId());
  214. }
  215. List<Long> deleteNavigationBarIdList = CommonUtil.getSubtraction(new HashSet<>(idFromDataBase), new HashSet<>(newsArticleDto.getNavigationBarIds()));
  216. List<Long> addNavigationBarIdList = CommonUtil.getSubtraction(new HashSet<>(newsArticleDto.getNavigationBarIds()), new HashSet<>(idFromDataBase));
  217. try {
  218. for (Long navigationBarId : deleteNavigationBarIdList) {
  219. Long id = newsArticleNavigationBarService.getArticleNavigationBarId(newsArticleDto.getId(), navigationBarId);
  220. newsArticleNavigationBarService.deleteNewsArticleNavigationBar(id);
  221. }
  222. for (Long navigationBarId : addNavigationBarIdList) {
  223. NewsArticleNavigationBarDto dto = new NewsArticleNavigationBarDto();
  224. dto.setNavigationBarId(navigationBarId);
  225. dto.setNewsArticleId(newsArticleDto.getId());
  226. newsArticleNavigationBarService.saveNewsArticleNavigationBar(dto);
  227. }
  228. } catch (ServiceException e) {
  229. log.error("编辑新闻失败-编辑新闻导航栏关联表失败,失败原因:{}", e.getMessage());
  230. throw new ServiceException("编辑新闻失败");
  231. }
  232. }
  233. private void editNewsArticlePhoto(NewsArticleDto newsArticleDto) {
  234. // 查询已有新闻-图片关联数据
  235. List<NewsArticlePhotoPo> newsArticlePhotoPoList = newsArticlePhotoService.getArticlePhotoByNewsId(newsArticleDto.getId());
  236. // 封装数据库数据
  237. Map<Long, NewsArticlePhotoPo> newsArticlePhotoPoMap = new HashMap<>();
  238. List<Long> idFromDataBase = new ArrayList<>();
  239. for (NewsArticlePhotoPo po : newsArticlePhotoPoList) {
  240. idFromDataBase.add(po.getPhotoId());
  241. newsArticlePhotoPoMap.put(po.getPhotoId(), po);
  242. }
  243. // 封装前端传入图片
  244. List<Long> idFromWeb = new ArrayList<>();
  245. Map<Long, PhotoPo> photoPoMap = new HashMap<>();
  246. for (String url : newsArticleDto.getUrlList()) {
  247. url = "/" + url.replace(accessPath, "").replace(accessResource, "");
  248. PhotoPo photoPo = photoService.getPhotoByUrl(url);
  249. idFromWeb.add(photoPo.getId());
  250. photoPoMap.put(photoPo.getId(), photoPo);
  251. }
  252. List<Long> deleteArticlePhotoList = CommonUtil.getSubtraction(new HashSet<>(idFromDataBase), new HashSet<>(idFromWeb));
  253. List<Long> addArticlePhotoList = CommonUtil.getSubtraction(new HashSet<>(idFromWeb), new HashSet<>(idFromDataBase));
  254. try {
  255. for (Long photoId : deleteArticlePhotoList) {
  256. NewsArticlePhotoPo newsArticlePhotoPo = newsArticlePhotoPoMap.get(photoId);
  257. // 删除新闻-图片关联数据
  258. newsArticlePhotoService.deleteNewsArticlePhoto(newsArticlePhotoPo.getId());
  259. // 删除图片
  260. photoService.deletePhoto(photoId);
  261. }
  262. if (addArticlePhotoList != null && addArticlePhotoList.size() != 0) {
  263. NewsArticlePo newsArticlePo = mapper.getNewsArticleById(newsArticleDto.getId());
  264. for (Long photoId : addArticlePhotoList) {
  265. NewsArticlePhotoDto newsArticlePhotoDto = new NewsArticlePhotoDto();
  266. newsArticlePhotoDto.setNewsArticleId(newsArticleDto.getId());
  267. newsArticlePhotoDto.setPhotoId(photoId);
  268. // 新增新闻-图片关联数据
  269. newsArticlePhotoService.saveNewsArticlePhoto(newsArticlePhotoDto);
  270. // todo 为新闻内图片增加标题状态
  271. PhotoPo photoPo = photoPoMap.get(photoId);
  272. PhotoDto photoDto = new PhotoDto();
  273. BeanUtil.copyProperties(photoDto, photoPo);
  274. photoDto.setName(newsArticlePo.getTitle());
  275. photoDto.setType(PhotoTypeEnum.NEWS_PHOTO.getValue());
  276. photoDto.setStatus(PhotoStatusEnum.UPLOAD_SUCCESS_HAVE_USED.getValue());
  277. photoService.editPhoto(photoDto);
  278. }
  279. }
  280. } catch (Exception e) {
  281. log.error("编辑新闻失败-编辑新闻图片关联表失败,失败原因:{}", e.getMessage());
  282. throw new ServiceException("编辑新闻失败");
  283. }
  284. }
  285. @Override
  286. @Transactional(rollbackFor = ServiceException.class)
  287. public void deleteNewsArticle(Long id) {
  288. NewsArticlePo newsArticlePo = mapper.getNewsArticleById(id);
  289. newsArticlePo.setActivated(CommonConstant.ACTIVATED_DELETED);
  290. newsArticlePo.setUpdateTime(new Date());
  291. newsArticlePo.setPhotoUrl(null);
  292. try {
  293. mapper.updateById(newsArticlePo);
  294. } catch (Exception e) {
  295. log.error("删除新闻失败,失败原因:{}", e.getMessage());
  296. throw new ServiceException("删除新闻失败");
  297. }
  298. try {
  299. List<NewsArticlePhotoPo> newsArticlePhotoPoList = newsArticlePhotoService.getArticlePhotoByNewsId(id);
  300. for (NewsArticlePhotoPo po : newsArticlePhotoPoList) {
  301. newsArticlePhotoService.deleteNewsArticlePhoto(po.getId());
  302. photoService.deletePhoto(po.getPhotoId());
  303. }
  304. } catch (ServiceException e) {
  305. log.error("删除新闻图片/新闻-图片关联表失败,失败原因:{}", e.getMessage());
  306. throw new ServiceException("删除新闻失败");
  307. }
  308. try {
  309. List<NewsArticleNavigationBarDto> newsArticleNavigationBarDtoList = newsArticleNavigationBarService.getArticleNavigationBarByNewsId(id);
  310. for (NewsArticleNavigationBarDto dto : newsArticleNavigationBarDtoList) {
  311. newsArticleNavigationBarService.deleteNewsArticleNavigationBar(dto.getId());
  312. }
  313. } catch (ServiceException e) {
  314. log.error("删除新闻导航栏关联表数据失败,失败原因:{}", e.getMessage());
  315. throw new ServiceException("删除新闻失败");
  316. }
  317. }
  318. @Override
  319. public IPage<NewsArticleBizVo> getNewsArticleByNavigationBarId(Page page, Long id) {
  320. AssertUtils.assertNotNull(id, "id不能为空");
  321. try {
  322. IPage<NewsArticlePo> newsArticlePoList = mapper.getNewsArticleByNavigationBarId(page, id);
  323. return newsArticlePoList.convert(this::convertNewsArticleBizVo);
  324. } catch (Exception e) {
  325. log.error("获取新闻数据失败,失败原因:{}", e.getMessage());
  326. throw new ServiceException("获取新闻数据失败");
  327. }
  328. }
  329. private NewsArticleBizVo convertNewsArticleBizVo(NewsArticlePo po) {
  330. NewsArticleBizVo vo = new NewsArticleBizVo();
  331. BeanUtils.copyProperties(po, vo);
  332. if (po.getPhotoUrl() != null) {
  333. String url = accessPath + accessResource + po.getPhotoUrl().replace("/","");
  334. vo.setPhotoUrl(url);
  335. }
  336. return vo;
  337. }
  338. }