zhuang 8 місяців тому
батько
коміт
87da1c80c6

+ 2 - 0
business-common/src/main/java/com/rongwei/bscommon/sys/service/ZhcxOutsideInspectionItpService.java

@@ -72,5 +72,7 @@ public interface ZhcxOutsideInspectionItpService extends IService<ZhcxOutsideIns
      * 获取初始化报验点状态报验单
      */
     List<JSONObject> getInitNodeStateInspection();
+
+    void batchDownloadFile(List<ZhcxOutsideExportVo> list, HttpServletResponse response);
 }
 

+ 166 - 0
business-common/src/main/java/com/rongwei/bscommon/sys/service/impl/ZhcxOutsideInspectionItpServiceImpl.java

@@ -48,8 +48,12 @@ import org.springframework.transaction.annotation.Transactional;
 import javax.servlet.http.HttpServletResponse;
 import java.io.*;
 import java.net.URLEncoder;
+import java.nio.file.Files;
+import java.nio.file.Paths;
 import java.text.SimpleDateFormat;
+import java.time.LocalTime;
 import java.util.*;
+import java.util.concurrent.atomic.AtomicInteger;
 
 /**
  * <p>
@@ -1295,6 +1299,168 @@ public class ZhcxOutsideInspectionItpServiceImpl extends ServiceImpl<ZhcxOutside
         return dao.getInitNodeStateInspection();
     }
 
+    @Override
+    public void batchDownloadFile(List<ZhcxOutsideExportVo> ExportVos, HttpServletResponse response) {
+        if (!isWithinAllowedTime()) {
+            throw new CustomException("请在20:00-7:00之间批量下载资料");
+        }
+        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
+        String classPath = filepath;
+        // 临时目录 以uuid+时间戳 为目录名称
+        String temp = classPath +File.separator + IdUtil.simpleUUID() + System.currentTimeMillis();
+        log.info("临时目录:"+temp);
+        // 临时目录如果存在则删除
+        File tempDir = new File(temp);
+        File zipFile = null;
+        log.info("临时目录是否存在:"+tempDir.exists());
+        try {
+            if (tempDir.exists()) {
+                FileUtil.del(tempDir);
+            }
+            // 创建临时目录
+            boolean mkdir = tempDir.mkdir();
+            log.info("临时目录是否创建:"+mkdir);
+//            ForkJoinPool forkJoinPool = new ForkJoinPool(4);
+//            forkJoinPool.submit(() ->
+            ExportVos.parallelStream().forEach(vo->{
+                String itpId = vo.getItpid();
+                if(StringUtils.isEmpty(itpId)){
+                    log.info("批量下载,itpId为空");
+                    return;
+                }
+                ZhcxOutsideInspectionItpDo inspectionItpDo = getById(itpId);
+                LambdaQueryWrapper<ZhcxOutsideInspectionItpSupervisionDispatchDo> wrapper = Wrappers.lambdaQuery();
+                wrapper.eq(ZhcxOutsideInspectionItpSupervisionDispatchDo::getItpid,itpId);
+                ZhcxOutsideInspectionItpSupervisionDispatchDo dispatchDo = dispatchService.getOne(wrapper);
+                List<String> fileIds = new ArrayList<>();
+                if(dispatchDo != null){
+                    String sfiles = dispatchDo.getSfiles();
+                    if(StringUtils.isNotBlank(sfiles)){
+                        getIds(sfiles, fileIds);
+                    }
+                }
+                String files = inspectionItpDo.getFiles();
+                if(StringUtils.isNotBlank(files)){
+                    getIds(files, fileIds);
+                }
+                Map<String, Integer> fileNameCount = new HashMap<>();
+                String format = formatter.format(inspectionItpDo.getReservationinspectiontime());
+                String fileFullName = inspectionItpDo.getProjectcode()+"-"+inspectionItpDo.getProjectname()+
+                        "-"+inspectionItpDo.getStructurename()+"-"+inspectionItpDo.getInspectionpoint()+
+                        "-"+format;
+                fileFullName = fileFullName.replaceAll("/","_");
+                String s = inspectionItpDo.getInspectioncode() == null ? "" : inspectionItpDo.getInspectioncode();
+                if(StringUtils.isNotBlank(s)){
+                    fileFullName += "-"+s;
+                }
+                ExcelFormData excelData = vo.getExcelData();
+                excelData.setExcelName(fileFullName+".pdf");
+                SysFileItemDo sysFileItemDo = sysFileItemService.getById(excelData.getTemplateId());
+                FormData formData = zhcxCommon.parseExcelPage2Data(excelData, sysFileItemDo);
+                zhcxCommon.parseTemplateByAspose(sysFileItemDo, excelData, temp, formData);
+
+                if(fileIds.size() > 0){
+                    QueryWrapper<SysFileItemDo> qw = new QueryWrapper();
+                    qw.in("ID", fileIds);
+                    List<SysFileItemDo> fileList = sysFileItemService.list(qw);
+                    //log.info("下载数据数量:"+fileList.size());
+                    if(fileList.size() > 0){
+                        if (mkdir) {
+                            String finalFileFullName = fileFullName;
+                            AtomicInteger i = new AtomicInteger();
+                            fileList.forEach(item->{
+                                String filename = item.getFilename();
+                                //boolean boo = fileNameCount.containsKey(filename);
+
+//                                if(boo){
+//                                    i = fileNameCount.get(filename);
+//                                }
+                                //fileNameCount.put(filename,i+1);
+//                                if(i.get() > 0){
+//                                    String prefix = FileNameUtil.getPrefix(finalFileFullName);
+//                                    String extName = FileNameUtil.extName(filename);
+//                                    filename = prefix+"附件"+(i.get() +1)+"."+extName;
+//                                }
+                                String prefix = FileNameUtil.getPrefix(finalFileFullName);
+                                String extName = FileNameUtil.extName(filename);
+                                filename = prefix+"附件"+(i.get() +1)+"."+extName;
+//                                FileUtil.copyFile(new File(item.getFullpath()),
+//                                        new File(temp+File.separator+filename));
+                                try {
+                                    Files.copy(Paths.get(item.getFullpath()), Paths.get(temp + File.separator + filename));
+                                } catch (IOException e) {
+                                    e.printStackTrace();
+                                }
+                                i.getAndIncrement();
+                            });
+                        }
+                    }
+                }
+            });
+            String encodedFileName = URLEncoder.encode("批量下载资料.zip", "UTF-8").replaceAll("\\+", "%20");
+            response.setContentType("application/octet-stream");
+            response.setHeader("content-type", "application/octet-stream");
+            response.setHeader("Content-Disposition", String.format("attachment; filename=\"%s\"", encodedFileName));
+            // 将临时目录压缩成zip
+            zipFile = ZipUtil.zip(temp);
+            byte[] buffer = new byte[1024];
+            FileInputStream fis = null;
+            BufferedInputStream bis = null;
+            try{
+                fis = new FileInputStream(zipFile);
+                bis = new BufferedInputStream(fis);
+                OutputStream os = response.getOutputStream();
+                int i = bis.read(buffer);
+                while (i != -1) {
+                    os.write(buffer, 0, i);
+                    i = bis.read(buffer);
+                }
+            } catch (IOException e) {
+                e.printStackTrace();
+                log.info("下载压缩资料报错:"+e);
+            }finally {
+                if (bis != null) {
+                    try {
+                        bis.close();
+                    } catch (IOException e) {
+
+                    }
+                }
+                if (fis != null) {
+                    try {
+                        fis.close();
+                    } catch (IOException e) {
+
+                    }
+                }
+            }
+        } catch (Exception e) {
+            log.info("批量下载资料报错:"+e);
+        } finally {
+            FileUtil.del(tempDir);
+            FileUtil.del(zipFile);
+        }
+    }
+
+    private boolean isWithinAllowedTime() {
+        // 获取当前时间
+        LocalTime now = LocalTime.now();
+
+        // 定义开始和结束时间
+        LocalTime startTime = LocalTime.of(20, 0); // 20:00
+        LocalTime endTime = LocalTime.of(7, 0);   // 次日 07:00
+
+        // 判断是否在允许时间段内
+        // 跨日判断
+        if (startTime.isBefore(endTime)) {
+            // 如果开始时间小于结束时间(不跨日),直接判断是否在范围内
+            return now.isAfter(startTime) && now.isBefore(endTime);
+        } else {
+            // 如果开始时间大于结束时间(跨日),分两段判断
+            return now.isAfter(startTime) || now.isBefore(endTime);
+        }
+    }
+
     private void getIds(String files, List<String> fileIds) {
         String[] split = files.split("\\^_\\^");
         for(String fileItem : split){

+ 12 - 0
business-server/src/main/java/com/rongwei/bsserver/controller/ZhcxOperOutsideInspectionController.java

@@ -62,4 +62,16 @@ public class ZhcxOperOutsideInspectionController {
             throw new CustomException("下载附件失败");
         }
     }
+
+    @ApiOperation("文件批量下载")
+    @PostMapping("/batchDownloadFile")
+    public void batchDownloadFile(@RequestBody List<ZhcxOutsideExportVo> list, HttpServletResponse response) throws FileNotFoundException {
+        inspectionService.batchDownloadFile(list, response);
+//        int i = inspectionService.downloadFile(zhcxOutsideExportVo, response);
+//        if(i == 0){
+//            throw new CustomException("暂无相关附件");
+//        }else if(i == 1){
+//            throw new CustomException("下载附件失败");
+//        }
+    }
 }