瀏覽代碼

质检-规范管理-规范更新空数据报错处理

LHB 2 月之前
父節點
當前提交
862d4ab15e

+ 92 - 78
blade-service/blade-business/src/main/java/org/springblade/business/service/impl/PrivateStandardServiceImpl.java

@@ -207,16 +207,16 @@ public class PrivateStandardServiceImpl extends ServiceImpl<PrivateStandardMappe
         BladeUser user = SecureUtil.getUser();
         //先删除文件
         try {
-            if(CollectionUtils.isNotEmpty(delFileIds)){
+            if (CollectionUtils.isNotEmpty(delFileIds)) {
                 List<StandardFile> standardFiles = standardFileService.listByIds(delFileIds);
-                if(CollectionUtils.isNotEmpty(standardFiles)){
+                if (CollectionUtils.isNotEmpty(standardFiles)) {
                     for (StandardFile standardFile : standardFiles) {
                         this.deleteFile(standardFile.getId());
                     }
                 }
             }
             //删除规范文件
-            if(CollectionUtils.isNotEmpty(delIds)){
+            if (CollectionUtils.isNotEmpty(delIds)) {
                 for (Long delId : delIds) {
                     delete(delId);
                 }
@@ -231,7 +231,7 @@ public class PrivateStandardServiceImpl extends ServiceImpl<PrivateStandardMappe
             //空文件曲中
             List<MultipartFile> files = new ArrayList<>();
             for (MultipartFile allFile : allFiles) {
-                if(allFile != null){
+                if (allFile != null) {
                     files.add(allFile);
                 }
             }
@@ -241,17 +241,17 @@ public class PrivateStandardServiceImpl extends ServiceImpl<PrivateStandardMappe
             for (PrivateStandardDTO dto : data) {
                 // 获取当前对象需要的文件数量
                 int fileCount = dto.getFilesCount();
-                if(files != null && files.size() > 0){
-                    if(fileCount > 0 && fileIndex < files.size()){
+                if (files != null && files.size() > 0) {
+                    if (fileCount > 0 && fileIndex < files.size()) {
                         dto.setFile(files.get(fileIndex++));
                     }
                     //先上传文件,上传成功在执行添加
-                    if(dto.getFile() != null){
+                    if (dto.getFile() != null) {
                         //再去查询当前规范文件是否在数据库中存在未删除的文件
                         List<StandardFile> list = standardFileService.list(Wrappers.<StandardFile>lambdaQuery()
                                 .eq(StandardFile::getStandardId, dto.getId())
                                 .eq(StandardFile::getIsDeleted, 0));
-                        if(CollectionUtils.isNotEmpty(list)){
+                        if (CollectionUtils.isNotEmpty(list)) {
                             //删除文件
                             for (StandardFile standardFile : list) {
                                 String pdfName = standardFile.getStandardFileUrl().split("upload")[1];
@@ -290,7 +290,7 @@ public class PrivateStandardServiceImpl extends ServiceImpl<PrivateStandardMappe
             return true;
         } catch (Exception e) {
             //删除之前上传的文件
-            if(CollectionUtils.isNotEmpty(standardFiles)){
+            if (CollectionUtils.isNotEmpty(standardFiles)) {
                 for (StandardFile standardFile : standardFiles) {
                     String pdfName = standardFile.getStandardFileUrl().split("upload")[1];
                     this.newIOSSClient.removeFile("upload" + pdfName);
@@ -308,22 +308,31 @@ public class PrivateStandardServiceImpl extends ServiceImpl<PrivateStandardMappe
         BladeUser user = SecureUtil.getUser();
         //获取当前规范文件信息
         PrivateStandard privateStandard = baseMapper.selectById(id);
-
+        if(privateStandard == null){
+            throw new ServiceException("未查询到规范文件");
+        }
 
         //获取信息
         List<StandardInfo> standardInfos = standardInfoMapper.selectList(Wrappers.<StandardInfo>lambdaQuery()
                 .eq(StandardInfo::getStandardId, id)
                 .eq(StandardInfo::getIsDeleted, 0));
-        List<Long> infoIds = standardInfos.stream().filter(f -> f.getType() == 2).map(StandardInfo::getId).collect(Collectors.toList());
 
-        //获取绑定信息
-        List<StandardInfoJoin> standardInfoJoins = standardInfoJoinMapper.selectList(Wrappers.<StandardInfoJoin>lambdaQuery()
-                .in(StandardInfoJoin::getStandardInfoLeftId, infoIds));
+        List<Long> infoIds = null;
+        if (CollectionUtils.isNotEmpty(standardInfos)) {
+            infoIds = standardInfos.stream().filter(f -> f.getType() == 2).map(StandardInfo::getId).collect(Collectors.toList());
+        }
 
-        //获取绑定表单信息
-        List<StandardInfoPrivateJoin> standardInfoPrivateJoins = standardInfoPrivateJoinMapper.selectList(Wrappers.<StandardInfoPrivateJoin>lambdaQuery()
-                .in(StandardInfoPrivateJoin::getStandardInfoId, infoIds));
+        List<StandardInfoJoin> standardInfoJoins = null;
+        List<StandardInfoPrivateJoin> standardInfoPrivateJoins = null;
+        if (CollectionUtils.isNotEmpty(infoIds)) {
+            //获取绑定信息
+            standardInfoJoins = standardInfoJoinMapper.selectList(Wrappers.<StandardInfoJoin>lambdaQuery()
+                    .in(StandardInfoJoin::getStandardInfoLeftId, infoIds));
 
+            //获取绑定表单信息
+            standardInfoPrivateJoins = standardInfoPrivateJoinMapper.selectList(Wrappers.<StandardInfoPrivateJoin>lambdaQuery()
+                    .in(StandardInfoPrivateJoin::getStandardInfoId, infoIds));
+        }
         //获取文件信息
         List<StandardFile> standardFiles = standardFileService.list(Wrappers.<StandardFile>lambdaQuery()
                 .eq(StandardFile::getStandardId, id)
@@ -337,7 +346,7 @@ public class PrivateStandardServiceImpl extends ServiceImpl<PrivateStandardMappe
 
         privateStandard.setId(newId);
         if (privateStandard.getStatus() == 2) {
-            privateStandard.setName(privateStandard.getName().replace("-已过期",""));
+            privateStandard.setName(privateStandard.getName().replace("-已过期", ""));
             privateStandard.setStatus(1);
         }
         privateStandard.setCreateTime(LocalDateTime.now());
@@ -349,76 +358,81 @@ public class PrivateStandardServiceImpl extends ServiceImpl<PrivateStandardMappe
 
             baseMapper.insert(privateStandard);
 
-            //旧id与新id的映射关系
-            Map<Long, Long> map = new HashMap<>();
-            standardInfos.forEach(f -> {
-                Long newInfoId = SnowFlakeUtil.getId();
-                map.put(f.getId(),newInfoId);
-            });
-            standardInfos.forEach(f -> {
-                f.setId(map.get(f.getId()));
-                f.setParentId(map.get(f.getParentId())==null?0:map.get(f.getParentId()));
-                f.setStandardId(newId);
-                f.setCreateTime(DateTime.now());
-                f.setCreateUser(user.getUserId());
-                standardInfoMapper.insert(f);
-            });
-
-
-            standardInfoJoins.forEach(f -> {
-                f.setId(SnowFlakeUtil.getId());
-                f.setStandardInfoLeftId(map.get(f.getStandardInfoLeftId()));
-                f.setStandardInfoRightId(map.get(f.getStandardInfoRightId()));
-                f.setCreateTime(DateTime.now());
-                f.setCreateUser(user.getUserId());
-                standardInfoJoinMapper.insert(f);
-            });
-
-            standardInfoPrivateJoins.forEach(f -> {
-                f.setId(SnowFlakeUtil.getId());
-                f.setStandardInfoId(map.get(f.getStandardInfoId()));
-                f.setCreateTime(DateTime.now());
-                f.setCreateUser(user.getUserId());
-                standardInfoPrivateJoinMapper.insert(f);
-            });
+            if(CollectionUtils.isNotEmpty(standardInfos)){
+                //旧id与新id的映射关系
+                Map<Long, Long> map = new HashMap<>();
+                standardInfos.forEach(f -> {
+                    Long newInfoId = SnowFlakeUtil.getId();
+                    map.put(f.getId(), newInfoId);
+                });
+                standardInfos.forEach(f -> {
+                    f.setId(map.get(f.getId()));
+                    f.setParentId(map.get(f.getParentId()) == null ? 0 : map.get(f.getParentId()));
+                    f.setStandardId(newId);
+                    f.setCreateTime(DateTime.now());
+                    f.setCreateUser(user.getUserId());
+                    standardInfoMapper.insert(f);
+                });
+                if(CollectionUtils.isNotEmpty(standardInfoJoins)){
+                    standardInfoJoins.forEach(f -> {
+                        f.setId(SnowFlakeUtil.getId());
+                        f.setStandardInfoLeftId(map.get(f.getStandardInfoLeftId()));
+                        f.setStandardInfoRightId(map.get(f.getStandardInfoRightId()));
+                        f.setCreateTime(DateTime.now());
+                        f.setCreateUser(user.getUserId());
+                        standardInfoJoinMapper.insert(f);
+                    });
+                }
+                if(CollectionUtils.isNotEmpty(standardInfoPrivateJoins)){
+                    standardInfoPrivateJoins.forEach(f -> {
+                        f.setId(SnowFlakeUtil.getId());
+                        f.setStandardInfoId(map.get(f.getStandardInfoId()));
+                        f.setCreateTime(DateTime.now());
+                        f.setCreateUser(user.getUserId());
+                        standardInfoPrivateJoinMapper.insert(f);
+                    });
+                }
+            }
         } catch (Exception e) {
             e.printStackTrace();
             throw new ServiceException("复制失败");
         }
 
 
-        //文件集合
-        List<String> urls = new ArrayList<>();
-        try {
-            for (StandardFile f : standardFiles) {
-                f.setId(SnowFlakeUtil.getId());
-                f.setStandardId(newId);
-                f.setCreateTime(DateTime.now());
-                f.setCreateUser(user.getUserId());
-                if(StringUtils.isNotEmpty(f.getStandardFileUrl())){
-                    URL urlFile = new URL(f.getStandardFileUrl());
-                    InputStream inputStream = urlFile.openStream();
-                    String originalFilename = "standard/" + f.getId() + "|" + f.getFileName();
-                    MockMultipartFile multipartFile = new MockMultipartFile("file", originalFilename, "application/pdf", inputStream);
-                    //Oss上传 传特殊文件名 在oss中做特殊路径处理
-                    BladeFile bladeFile = newIOSSClient.uploadFileByInputStream(multipartFile);
-                    if (bladeFile == null) {
-                        throw new ServiceException("Oss异常");
+
+        if(CollectionUtils.isNotEmpty(standardFiles)){
+            //文件集合
+            List<String> urls = new ArrayList<>();
+            try {
+                for (StandardFile f : standardFiles) {
+                    f.setId(SnowFlakeUtil.getId());
+                    f.setStandardId(newId);
+                    f.setCreateTime(DateTime.now());
+                    f.setCreateUser(user.getUserId());
+                    if (StringUtils.isNotEmpty(f.getStandardFileUrl())) {
+                        URL urlFile = new URL(f.getStandardFileUrl());
+                        InputStream inputStream = urlFile.openStream();
+                        String originalFilename = "standard/" + f.getId() + "|" + f.getFileName();
+                        MockMultipartFile multipartFile = new MockMultipartFile("file", originalFilename, "application/pdf", inputStream);
+                        //Oss上传 传特殊文件名 在oss中做特殊路径处理
+                        BladeFile bladeFile = newIOSSClient.uploadFileByInputStream(multipartFile);
+                        if (bladeFile == null) {
+                            throw new ServiceException("Oss异常");
+                        }
+                        f.setStandardFileUrl(bladeFile.getLink());
+                        urls.add(bladeFile.getLink());
                     }
-                    f.setStandardFileUrl(bladeFile.getLink());
-                    urls.add(bladeFile.getLink());
                 }
+            } catch (Exception e) {
+                for (String url : urls) {
+                    String pdfName = url.split("upload")[1];
+                    this.newIOSSClient.removeFile("upload" + pdfName);
+                }
+                e.printStackTrace();
+                throw new ServiceException("Oss异常");
             }
-        } catch (Exception e) {
-            for (String url : urls) {
-                String pdfName = url.split("upload")[1];
-                this.newIOSSClient.removeFile("upload" + pdfName);
-            }
-            e.printStackTrace();
-            throw new ServiceException("Oss异常");
+            standardFileService.saveBatch(standardFiles);
         }
-
-        standardFileService.saveBatch(standardFiles);
         return newId;
     }
 }