소스 검색

修改新闻-图片关联表

xusonglin 5 년 전
부모
커밋
d9e5da754d

+ 79 - 8
src/main/java/com/jkcredit/sysnews/service/newsArticle/NewsArticleServiceImpl.java

@@ -35,10 +35,7 @@ import org.springframework.transaction.annotation.Transactional;
 
 import javax.annotation.Resource;
 import java.io.File;
-import java.util.ArrayList;
-import java.util.Date;
-import java.util.HashSet;
-import java.util.List;
+import java.util.*;
 
 /**
  * @description:
@@ -132,7 +129,7 @@ public class NewsArticleServiceImpl extends BaseService implements NewsArticleSe
         if (newsArticleDto.getUrlList() != null) {
             try {
                 for (String url : newsArticleDto.getUrlList()) {
-                    url = File.separator + url.replace(accessPath, "").replace(accessResource, "");
+                    url = "/" + url.replace(accessPath, "").replace(accessResource, "");
                     PhotoPo photoPo = photoService.getPhotoByUrl(url);
                     photoDto.setId(photoPo.getId());
                     // 更新新闻content中的图片
@@ -203,7 +200,14 @@ public class NewsArticleServiceImpl extends BaseService implements NewsArticleSe
             throw new ServiceException("编辑新闻失败");
         }
         // 更新导航栏
-        editNewsArticleNavigationBar(newsArticleDto);
+        if (newsArticleDto.getNavigationBarIds() != null && !newsArticleDto.getNavigationBarIds().isEmpty()) {
+            editNewsArticleNavigationBar(newsArticleDto);
+        }
+
+        // 更新新闻内容中的图片
+        if (newsArticleDto.getUrlList() != null && !newsArticleDto.getUrlList().isEmpty()) {
+            editNewsArticlePhoto(newsArticleDto);
+        }
     }
 
     private void editNewsArticleNavigationBar(NewsArticleDto newsArticleDto) {
@@ -213,10 +217,10 @@ public class NewsArticleServiceImpl extends BaseService implements NewsArticleSe
         for (NewsArticleNavigationBarDto dto : newsArticleNavigationBarDtoList) {
             idFromDataBase.add(dto.getNavigationBarId());
         }
-        List<Long> deleleNavigationBarIdList = CommonUtil.getSubtraction(new HashSet<>(idFromDataBase), new HashSet<>(newsArticleDto.getNavigationBarIds()));
+        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 : deleleNavigationBarIdList) {
+            for (Long navigationBarId : deleteNavigationBarIdList) {
                 Long id = newsArticleNavigationBarService.getArticleNavigationBarId(newsArticleDto.getId(), navigationBarId);
                 newsArticleNavigationBarService.deleteNewsArticleNavigationBar(id);
             }
@@ -232,6 +236,63 @@ public class NewsArticleServiceImpl extends BaseService implements NewsArticleSe
         }
     }
 
+    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) {
@@ -247,6 +308,16 @@ public class NewsArticleServiceImpl extends BaseService implements NewsArticleSe
         }
 
         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());

+ 7 - 0
src/main/java/com/jkcredit/sysnews/service/newsArticlePhoto/NewsArticlePhotoService.java

@@ -1,6 +1,9 @@
 package com.jkcredit.sysnews.service.newsArticlePhoto;
 
 import com.jkcredit.sysnews.model.dto.newsArticlePhoto.NewsArticlePhotoDto;
+import com.jkcredit.sysnews.model.po.newsArticlePhoto.NewsArticlePhotoPo;
+
+import java.util.List;
 
 /**
  * @description:
@@ -10,4 +13,8 @@ import com.jkcredit.sysnews.model.dto.newsArticlePhoto.NewsArticlePhotoDto;
  **/
 public interface NewsArticlePhotoService {
     void saveNewsArticlePhoto(NewsArticlePhotoDto dto);
+
+    void deleteNewsArticlePhoto(Long id);
+
+    List<NewsArticlePhotoPo> getArticlePhotoByNewsId(Long id);
 }

+ 20 - 0
src/main/java/com/jkcredit/sysnews/service/newsArticlePhoto/NewsArticlePhotoServiceImpl.java

@@ -4,6 +4,7 @@ import com.jkcredit.sysnews.mapper.NewsArticlePhotoMapper;
 import com.jkcredit.sysnews.model.dto.newsArticlePhoto.NewsArticlePhotoDto;
 import com.jkcredit.sysnews.model.po.newsArticlePhoto.NewsArticlePhotoPo;
 import com.jkcredit.sysnews.service.base.BaseService;
+import com.jkcredit.sysnews.spi.lang.constant.CommonConstant;
 import com.jkcredit.sysnews.spi.lang.exception.ServiceException;
 import com.jkcredit.sysnews.util.BeanUtil;
 import lombok.extern.slf4j.Slf4j;
@@ -11,6 +12,7 @@ import org.springframework.stereotype.Service;
 
 import javax.annotation.Resource;
 import java.util.Date;
+import java.util.List;
 
 /**
  * @description:
@@ -36,4 +38,22 @@ public class NewsArticlePhotoServiceImpl extends BaseService implements NewsArti
             throw new ServiceException("插入新闻-图片关联表失败");
         }
     }
+
+    @Override
+    public void deleteNewsArticlePhoto(Long id) {
+        NewsArticlePhotoPo po = mapper.selectById(id);
+        po.setUpdateTime(new Date());
+        po.setActivated(CommonConstant.ACTIVATED_DELETED);
+        try {
+            mapper.updateById(po);
+        } catch (Exception e) {
+            log.error("删除新闻-图片关联表失败,失败原因{}", e.getMessage());
+            throw new ServiceException("删除新闻-图片关联表失败");
+        }
+    }
+
+    @Override
+    public List<NewsArticlePhotoPo> getArticlePhotoByNewsId(Long id) {
+        return mapper.getArticlePhotoByNewsId(id);
+    }
 }

+ 1 - 1
src/main/resources/mapper/NewsArticlePhotoMapper.xml

@@ -4,7 +4,7 @@
     <resultMap id="baseResultMap" type="com.jkcredit.sysnews.model.po.newsArticlePhoto.NewsArticlePhotoPo">
         <id column="id" property="id"/>
         <result column="news_article_id" property="newsArticleId"/>
-        <result column="photo_id" property="photo_id"/>
+        <result column="photo_id" property="photoId"/>
         <result column="create_time" property="createTime"/>
         <result column="update_time" property="updateTime"/>
         <result column="activated" property="activated"/>