123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367 |
- package com.jkcredit.sysnews.service.newsArticle;
- import com.baomidou.mybatisplus.core.metadata.IPage;
- import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
- import com.jkcredit.sysnews.enums.PhotoStatusEnum;
- import com.jkcredit.sysnews.enums.PhotoTypeEnum;
- import com.jkcredit.sysnews.mapper.NewsArticleMapper;
- import com.jkcredit.sysnews.model.dto.newsArticle.NewsArticleDto;
- import com.jkcredit.sysnews.model.dto.newsArticleNavigationBar.NewsArticleNavigationBarDto;
- import com.jkcredit.sysnews.model.dto.newsArticlePhoto.NewsArticlePhotoDto;
- import com.jkcredit.sysnews.model.dto.photo.PhotoDto;
- import com.jkcredit.sysnews.model.po.newsArticle.NewsArticlePo;
- import com.jkcredit.sysnews.model.po.newsArticleNavigationBar.NewsArticleNavigationBarPo;
- import com.jkcredit.sysnews.model.po.newsArticlePhoto.NewsArticlePhotoPo;
- import com.jkcredit.sysnews.model.po.photo.PhotoPo;
- import com.jkcredit.sysnews.model.vo.newsArticle.NewsArticleBizVo;
- import com.jkcredit.sysnews.model.vo.newsArticle.NewsArticleVo;
- import com.jkcredit.sysnews.model.vo.photo.PhotoVo;
- import com.jkcredit.sysnews.service.base.BaseService;
- import com.jkcredit.sysnews.service.newsArticleNavigationBar.NewsArticleNavigationBarService;
- import com.jkcredit.sysnews.service.newsArticlePhoto.NewsArticlePhotoService;
- import com.jkcredit.sysnews.service.photo.PhotoService;
- import com.jkcredit.sysnews.spi.lang.constant.CommonConstant;
- import com.jkcredit.sysnews.spi.lang.exception.ServiceException;
- import com.jkcredit.sysnews.util.AssertUtils;
- import com.jkcredit.sysnews.util.BeanUtil;
- import com.jkcredit.sysnews.util.CommonUtil;
- import lombok.extern.slf4j.Slf4j;
- import org.apache.commons.lang.StringUtils;
- import org.apache.commons.lang.enums.EnumUtils;
- import org.springframework.beans.BeanUtils;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.beans.factory.annotation.Value;
- import org.springframework.stereotype.Service;
- import org.springframework.transaction.annotation.Transactional;
- import javax.annotation.Resource;
- import java.io.File;
- import java.util.*;
- /**
- * @description:
- * @author: xusonglin
- * @create: 2020/3/1 22:46
- * @version: V1.0
- **/
- @Slf4j
- @Service
- @Transactional(readOnly = true)
- public class NewsArticleServiceImpl extends BaseService implements NewsArticleService {
- @Value("${photo.uploadPath}")
- private String uploadPath;
- @Value("${photo.accessPath}")
- private String accessPath;
- @Value("${photo.accessResource}")
- private String accessResource;
- @Resource
- NewsArticleMapper mapper;
- @Autowired
- PhotoService photoService;
- @Autowired
- NewsArticleNavigationBarService newsArticleNavigationBarService;
- @Autowired
- NewsArticlePhotoService newsArticlePhotoService;
- @Override
- public IPage<NewsArticleVo> getNewsArticles(Page page, NewsArticleDto newsArticleDto) {
- IPage<NewsArticlePo> newsArticlePoIPage = mapper.getNewsArticles(page, newsArticleDto);
- return newsArticlePoIPage.convert(this::convert);
- }
- private NewsArticleVo convert(NewsArticlePo po) {
- NewsArticleVo vo = new NewsArticleVo();
- BeanUtils.copyProperties(po, vo);
- if (po.getPhotoUrl() != null) {
- String url = accessPath + accessResource + po.getPhotoUrl().replace("/","");
- vo.setPhotoUrl(url);
- }
- return vo;
- }
- @Override
- public NewsArticleVo getNewsArticleById(Long id) {
- NewsArticlePo newsArticlePo = mapper.getNewsArticleById(id);
- NewsArticleVo newsArticleVo = new NewsArticleVo();
- BeanUtil.copyProperties(newsArticleVo, newsArticlePo);
- if (newsArticlePo.getPhotoUrl() != null) {
- String url = accessPath + accessResource + newsArticlePo.getPhotoUrl().replace("/","");
- newsArticleVo.setPhotoUrl(url);
- }
- // 查询新闻所属导航
- List<Long> navigationBarIdList = newsArticleNavigationBarService.getNavigationBarIdByNewsId(id);
- newsArticleVo.setNavigationBarIds(navigationBarIdList);
- // 查询新闻内图片url地址
- List<NewsArticlePhotoPo> newsArticlePhotoPoList = newsArticlePhotoService.getArticlePhotoByNewsId(id);
- List<String> urlList = new ArrayList<>();
- for (NewsArticlePhotoPo po : newsArticlePhotoPoList) {
- PhotoVo photoVo = photoService.getPhotoById(po.getPhotoId());
- urlList.add(photoVo.getUrl());
- }
- newsArticleVo.setUrlList(urlList);
- return newsArticleVo;
- }
- @Override
- @Transactional(rollbackFor = ServiceException.class)
- public void saveNewsArticle(NewsArticleDto newsArticleDto) {
- validate(newsArticleDto);
- PhotoDto photoDto = new PhotoDto();
- photoDto.setName(newsArticleDto.getTitle());
- photoDto.setType(PhotoTypeEnum.NEWS_PHOTO.getValue());
- photoDto.setStatus(PhotoStatusEnum.UPLOAD_SUCCESS_HAVE_USED.getValue());
- // 保存新闻首图
- PhotoVo photoVo;
- try {
- photoDto.setPhoto(newsArticleDto.getPhoto());
- photoVo = photoService.savePhoto(photoDto);
- } catch (ServiceException e) {
- log.error("新增新闻-新增首图失败,失败原因:{}", e.getMessage());
- throw new ServiceException("新增新闻失败");
- }
- // 保存新闻
- NewsArticlePo newsArticlePo = new NewsArticlePo();
- BeanUtil.copyProperties(newsArticlePo, newsArticleDto);
- newsArticlePo.setCreateTime(new Date());
- newsArticlePo.setPhotoId(photoVo.getId());
- try {
- mapper.insert(newsArticlePo);
- } catch (Exception e) {
- log.error("新增新闻失败,失败原因:{}", e.getMessage());
- throw new ServiceException("新增新闻失败");
- }
- // 更新新闻content中的图片, 并插入新闻-图片关联表
- if (newsArticleDto.getUrlList() != null) {
- try {
- for (String url : newsArticleDto.getUrlList()) {
- url = "/" + url.replace(accessPath, "").replace(accessResource, "");
- PhotoPo photoPo = photoService.getPhotoByUrl(url);
- photoDto.setId(photoPo.getId());
- // 更新新闻content中的图片
- photoService.editPhoto(photoDto);
- NewsArticlePhotoDto newsArticlePhotoDto = new NewsArticlePhotoDto();
- newsArticlePhotoDto.setPhotoId(photoPo.getId());
- newsArticlePhotoDto.setNewsArticleId(newsArticlePo.getId());
- newsArticlePhotoService.saveNewsArticlePhoto(newsArticlePhotoDto);
- }
- } catch (ServiceException e) {
- log.error("新增新闻-更新图片或并插入新闻-图片关联表失败,失败原因:{}", e.getMessage());
- throw new ServiceException("新增新闻失败");
- }
- }
- // 保存导航栏
- try {
- for (Long navigationBarId : newsArticleDto.getNavigationBarIds()) {
- NewsArticleNavigationBarDto dto = new NewsArticleNavigationBarDto();
- dto.setNewsArticleId(newsArticlePo.getId());
- dto.setNavigationBarId(navigationBarId);
- newsArticleNavigationBarService.saveNewsArticleNavigationBar(dto);
- }
- } catch (Exception e) {
- log.error("新增新闻-新增新闻导航关联表失败,失败原因:{}", e.getMessage());
- throw new ServiceException("新增新闻失败");
- }
- }
- @Override
- @Transactional(rollbackFor = ServiceException.class)
- public void editNewsArticle(NewsArticleDto newsArticleDto) {
- validate(newsArticleDto);
- NewsArticlePo newsArticlePo = mapper.getNewsArticleById(newsArticleDto.getId());
- AssertUtils.assertNotNull(newsArticleDto, CommonConstant.ARTICLE_NOT_EXIST);
- try {
- if (!StringUtils.isBlank(newsArticleDto.getTitle())) {
- newsArticlePo.setTitle(newsArticleDto.getTitle());
- }
- if (!StringUtils.isBlank(newsArticleDto.getNewsAbstract())) {
- newsArticlePo.setNewsAbstract(newsArticleDto.getNewsAbstract());
- }
- if (!StringUtils.isBlank(newsArticleDto.getContent())) {
- newsArticlePo.setContent(newsArticleDto.getContent());
- }
- if (newsArticleDto.getTopFlag() != null) {
- newsArticlePo.setTopFlag(newsArticleDto.getTopFlag());
- }
- if (newsArticleDto.getPhoto() != null) {
- try {
- photoService.deletePhoto(newsArticlePo.getPhotoId());
- PhotoDto photoDto = new PhotoDto();
- photoDto.setName(newsArticlePo.getTitle());
- photoDto.setType(PhotoTypeEnum.NEWS_PHOTO.getValue());
- photoDto.setStatus(PhotoStatusEnum.UPLOAD_SUCCESS_HAVE_USED.getValue());
- photoDto.setPhoto(newsArticleDto.getPhoto());
- PhotoVo photoVo = photoService.savePhoto(photoDto);
- newsArticlePo.setPhotoId(photoVo.getId());
- } catch (ServiceException e) {
- log.error("更换新闻封面图失败,失败原因:{}", e.getMessage());
- throw new ServiceException("更换新闻封面图失败");
- }
- }
- newsArticlePo.setUpdateTime(new Date());
- newsArticlePo.setPhotoUrl(null);
- mapper.updateById(newsArticlePo);
- } catch (Exception e) {
- log.error("编辑新闻失败,失败原因:{}", e.getMessage());
- throw new ServiceException("编辑新闻失败");
- }
- // 更新导航栏
- if (newsArticleDto.getNavigationBarIds() != null && !newsArticleDto.getNavigationBarIds().isEmpty()) {
- editNewsArticleNavigationBar(newsArticleDto);
- }
- // 更新新闻内容中的图片
- if (newsArticleDto.getUrlList() != null && !newsArticleDto.getUrlList().isEmpty()) {
- editNewsArticlePhoto(newsArticleDto);
- }
- }
- private void editNewsArticleNavigationBar(NewsArticleDto newsArticleDto) {
- List<NewsArticleNavigationBarDto> newsArticleNavigationBarDtoList = newsArticleNavigationBarService.getArticleNavigationBarByNewsId(newsArticleDto.getId());
- // 现有1 2 3 4 更新后 2 3 4 5 删掉1 新增 5
- List<Long> idFromDataBase = new ArrayList<>();
- for (NewsArticleNavigationBarDto dto : newsArticleNavigationBarDtoList) {
- idFromDataBase.add(dto.getNavigationBarId());
- }
- List<Long> deleteNavigationBarIdList = CommonUtil.getSubtraction(new HashSet<>(idFromDataBase), new HashSet<>(newsArticleDto.getNavigationBarIds()));
- List<Long> addNavigationBarIdList = CommonUtil.getSubtraction(new HashSet<>(newsArticleDto.getNavigationBarIds()), new HashSet<>(idFromDataBase));
- try {
- for (Long navigationBarId : deleteNavigationBarIdList) {
- Long id = newsArticleNavigationBarService.getArticleNavigationBarId(newsArticleDto.getId(), navigationBarId);
- newsArticleNavigationBarService.deleteNewsArticleNavigationBar(id);
- }
- for (Long navigationBarId : addNavigationBarIdList) {
- NewsArticleNavigationBarDto dto = new NewsArticleNavigationBarDto();
- dto.setNavigationBarId(navigationBarId);
- dto.setNewsArticleId(newsArticleDto.getId());
- newsArticleNavigationBarService.saveNewsArticleNavigationBar(dto);
- }
- } catch (ServiceException e) {
- log.error("编辑新闻失败-编辑新闻导航栏关联表失败,失败原因:{}", e.getMessage());
- throw new ServiceException("编辑新闻失败");
- }
- }
- private void editNewsArticlePhoto(NewsArticleDto newsArticleDto) {
- // 查询已有新闻-图片关联数据
- List<NewsArticlePhotoPo> newsArticlePhotoPoList = newsArticlePhotoService.getArticlePhotoByNewsId(newsArticleDto.getId());
- // 封装数据库数据
- Map<Long, NewsArticlePhotoPo> newsArticlePhotoPoMap = new HashMap<>();
- List<Long> idFromDataBase = new ArrayList<>();
- for (NewsArticlePhotoPo po : newsArticlePhotoPoList) {
- idFromDataBase.add(po.getPhotoId());
- newsArticlePhotoPoMap.put(po.getPhotoId(), po);
- }
- // 封装前端传入图片
- List<Long> idFromWeb = new ArrayList<>();
- Map<Long, PhotoPo> photoPoMap = new HashMap<>();
- for (String url : newsArticleDto.getUrlList()) {
- url = "/" + url.replace(accessPath, "").replace(accessResource, "");
- PhotoPo photoPo = photoService.getPhotoByUrl(url);
- idFromWeb.add(photoPo.getId());
- photoPoMap.put(photoPo.getId(), photoPo);
- }
- List<Long> deleteArticlePhotoList = CommonUtil.getSubtraction(new HashSet<>(idFromDataBase), new HashSet<>(idFromWeb));
- List<Long> addArticlePhotoList = CommonUtil.getSubtraction(new HashSet<>(idFromWeb), new HashSet<>(idFromDataBase));
- try {
- for (Long photoId : deleteArticlePhotoList) {
- NewsArticlePhotoPo newsArticlePhotoPo = newsArticlePhotoPoMap.get(photoId);
- // 删除新闻-图片关联数据
- newsArticlePhotoService.deleteNewsArticlePhoto(newsArticlePhotoPo.getId());
- // 删除图片
- photoService.deletePhoto(photoId);
- }
- if (addArticlePhotoList != null && addArticlePhotoList.size() != 0) {
- NewsArticlePo newsArticlePo = mapper.getNewsArticleById(newsArticleDto.getId());
- for (Long photoId : addArticlePhotoList) {
- NewsArticlePhotoDto newsArticlePhotoDto = new NewsArticlePhotoDto();
- newsArticlePhotoDto.setNewsArticleId(newsArticleDto.getId());
- newsArticlePhotoDto.setPhotoId(photoId);
- // 新增新闻-图片关联数据
- newsArticlePhotoService.saveNewsArticlePhoto(newsArticlePhotoDto);
- // todo 为新闻内图片增加标题状态
- PhotoPo photoPo = photoPoMap.get(photoId);
- PhotoDto photoDto = new PhotoDto();
- BeanUtil.copyProperties(photoDto, photoPo);
- photoDto.setName(newsArticlePo.getTitle());
- photoDto.setType(PhotoTypeEnum.NEWS_PHOTO.getValue());
- photoDto.setStatus(PhotoStatusEnum.UPLOAD_SUCCESS_HAVE_USED.getValue());
- photoService.editPhoto(photoDto);
- }
- }
- } catch (Exception e) {
- log.error("编辑新闻失败-编辑新闻图片关联表失败,失败原因:{}", e.getMessage());
- throw new ServiceException("编辑新闻失败");
- }
- }
- @Override
- @Transactional(rollbackFor = ServiceException.class)
- public void deleteNewsArticle(Long id) {
- NewsArticlePo newsArticlePo = mapper.getNewsArticleById(id);
- newsArticlePo.setActivated(CommonConstant.ACTIVATED_DELETED);
- newsArticlePo.setUpdateTime(new Date());
- newsArticlePo.setPhotoUrl(null);
- try {
- mapper.updateById(newsArticlePo);
- } catch (Exception e) {
- log.error("删除新闻失败,失败原因:{}", e.getMessage());
- throw new ServiceException("删除新闻失败");
- }
- try {
- List<NewsArticlePhotoPo> newsArticlePhotoPoList = newsArticlePhotoService.getArticlePhotoByNewsId(id);
- for (NewsArticlePhotoPo po : newsArticlePhotoPoList) {
- newsArticlePhotoService.deleteNewsArticlePhoto(po.getId());
- photoService.deletePhoto(po.getPhotoId());
- }
- } catch (ServiceException e) {
- log.error("删除新闻图片/新闻-图片关联表失败,失败原因:{}", e.getMessage());
- throw new ServiceException("删除新闻失败");
- }
- try {
- List<NewsArticleNavigationBarDto> newsArticleNavigationBarDtoList = newsArticleNavigationBarService.getArticleNavigationBarByNewsId(id);
- for (NewsArticleNavigationBarDto dto : newsArticleNavigationBarDtoList) {
- newsArticleNavigationBarService.deleteNewsArticleNavigationBar(dto.getId());
- }
- } catch (ServiceException e) {
- log.error("删除新闻导航栏关联表数据失败,失败原因:{}", e.getMessage());
- throw new ServiceException("删除新闻失败");
- }
- }
- @Override
- public IPage<NewsArticleBizVo> getNewsArticleByNavigationBarId(Page page, Long id) {
- AssertUtils.assertNotNull(id, "id不能为空");
- try {
- IPage<NewsArticlePo> newsArticlePoList = mapper.getNewsArticleByNavigationBarId(page, id);
- return newsArticlePoList.convert(this::convertNewsArticleBizVo);
- } catch (Exception e) {
- log.error("获取新闻数据失败,失败原因:{}", e.getMessage());
- throw new ServiceException("获取新闻数据失败");
- }
- }
- private NewsArticleBizVo convertNewsArticleBizVo(NewsArticlePo po) {
- NewsArticleBizVo vo = new NewsArticleBizVo();
- BeanUtils.copyProperties(po, vo);
- if (po.getPhotoUrl() != null) {
- String url = accessPath + accessResource + po.getPhotoUrl().replace("/","");
- vo.setPhotoUrl(url);
- }
- return vo;
- }
- }
|