Browse Source

修改图片

qinhouyu 1 year ago
parent
commit
024006cd67

+ 2 - 1
ruoyi-zdsz/src/main/java/com/ruoyi/zdsz/domain/vo/ZEngiineeringPhotoVo.java

@@ -4,6 +4,7 @@ import com.alibaba.excel.annotation.ExcelIgnoreUnannotated;
 import com.alibaba.excel.annotation.ExcelProperty;
 import com.ruoyi.common.annotation.ExcelDictFormat;
 import com.ruoyi.common.convert.ExcelDictConvert;
+import com.ruoyi.common.core.domain.BaseEntity;
 import lombok.Data;
 import java.util.Date;
 
@@ -17,7 +18,7 @@ import java.io.Serializable;
  */
 @Data
 @ExcelIgnoreUnannotated
-public class ZEngiineeringPhotoVo implements Serializable {
+public class ZEngiineeringPhotoVo extends BaseEntity implements Serializable {
 
     private static final long serialVersionUID = 1L;
 

+ 35 - 19
ruoyi-zdsz/src/main/java/com/ruoyi/zdsz/service/impl/ZComprehensiveServiceImpl.java

@@ -132,33 +132,49 @@ public class ZComprehensiveServiceImpl implements IZComprehensiveService {
     public Boolean updateByBo(ZComprehensiveBo bo) {
         ZComprehensive update = BeanUtil.toBean(bo, ZComprehensive.class);
         validEntityBeforeSave(update);
+
         if (StringUtils.isNotBlank(bo.getId())) {
-            bo.setId(bo.getId());
             List<ZEngiineeringPhoto> list = new ArrayList<>();
-            if (!bo.getPicIds().isEmpty()) {
-                ZEngiineeringPhotoBo bo1 = new ZEngiineeringPhotoBo();
-                bo1.setParentId(update.getId());
-                bo1.setCreateTime(update.getCreateTime());
-//                List<String> collect = photoService.queryList(bo1).stream().map(ele -> ele.getPicUrl()).collect(Collectors.toList());
-//                if (!collect.isEmpty()) {
-//                    bo.setPicIds(bo.getPicIds().stream().filter(pic -> !collect.contains(pic)).collect(Collectors.toList()));
-//                }
-                bo.getPicIds().stream().forEach(o -> {
-                    ZEngiineeringPhoto pic = new ZEngiineeringPhoto();
-                    pic.setParentId(bo.getId());
-                    pic.setPicUrl(o);
-                    pic.setCreateBy(String.valueOf(LoginHelper.getUserId()));
-                    pic.setCreateTime(new Date());
-                    list.add(pic);
-                });
-                if (!list.isEmpty()) {
-                    photoService.updateBatch(list);
+
+            // 查询数据库中已存在的图片列表
+            ZEngiineeringPhotoBo bo1 = new ZEngiineeringPhotoBo();
+            bo1.setParentId(update.getId());
+            List<String> existingPhotos = photoService.queryList(bo1).stream()
+                .map(ZEngiineeringPhotoVo::getPicUrl)
+                .collect(Collectors.toList());
+
+            // 插入前端传来的新图片
+            for (String pic : bo.getPicIds()) {
+                if (!existingPhotos.contains(pic)) {
+                    ZEngiineeringPhoto newPhoto = new ZEngiineeringPhoto();
+                    newPhoto.setParentId(bo.getId());
+                    newPhoto.setPicUrl(pic);
+                    newPhoto.setCreateBy(String.valueOf(LoginHelper.getUserId()));
+                    newPhoto.setCreateTime(new Date());
+                    list.add(newPhoto);
                 }
             }
+
+            // 删除数据库中多余的图片
+            List<String> photosToDelete = existingPhotos.stream()
+                .filter(pic -> !bo.getPicIds().contains(pic))
+                .collect(Collectors.toList());
+            if (!photosToDelete.isEmpty()) {
+                photosToDelete.forEach(item->{
+                    photoService.deleteWithValidByurls(item);
+                });
+            }
+
+            // 批量插入新图片
+            if (!list.isEmpty()) {
+                photoMapper.insertBatch(list);
+            }
         }
+
         return baseMapper.updateById(update) > 0;
     }
 
+
     /**
      * 保存前的数据校验
      */