zqy
2025-09-21 bd2df0a3e0634bb5469472f10315797365748559
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
package com.ruoyi.web.controller.archive;
 
import com.itextpdf.text.*;
import com.itextpdf.text.Font;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.utils.file.FileUtils;
import com.ruoyi.common.utils.poi.ExcelExp;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.poi.ExcelUtilManySheet;
import com.ruoyi.common.utils.poi.ExcelUtilManySheetSecond;
import com.ruoyi.domain.ArchiveRecords;
import com.ruoyi.domain.DocumentMaterials;
import com.ruoyi.domain.vo.*;
import com.ruoyi.framework.web.domain.server.Sys;
import com.ruoyi.service.IArchiveRecordsService;
import com.ruoyi.service.IDocumentMaterialsService;
import com.ruoyi.service.impl.BarcodeService;
import com.ruoyi.service.impl.pdfGenerateService;
import com.sun.xml.internal.messaging.saaj.util.ByteOutputStream;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellUtil;
import org.apache.poi.xssf.usermodel.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
 
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.lang.reflect.Field;
import java.net.URLEncoder;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.List;
import java.util.stream.Collectors;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;
 
import static org.apache.poi.hssf.usermodel.HSSFCell.*;
 
@RestController
@RequestMapping("/system/archiveAllExport")
public class archiveAllExportController {
 
    @Autowired
    private pdfGenerateService pdfGenerateService;
    @Autowired
    private BarcodeService barcodeService;
    @Autowired
    private IDocumentMaterialsService documentMaterialsService;
 
    @Autowired
    private IArchiveRecordsService iArchiveRecordsService;
 
 
 
 
 
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) throws IOException
    {
        //导出卷面封面代码
 
        ArchiveInfoVo aIV = iArchiveRecordsService.selectByRecordId(55L);
        List<ArchiveInfoVo> arsi = new ArrayList<>();
        arsi.add(aIV);
 
        String recordId = aIV.getRecordId();
        byte[] imgr = barcodeService.generateBarcodeImage(recordId);
        byte[] sedcode = pdfGenerateService.createQrCodeN(recordId, 30, 30);
        ExcelExp e1 = new ExcelExp("案卷封面数据",arsi, ArchiveInfoVo.class);
        ExcelExp e2 = new ExcelExp("案卷封面",  arsi, recordId, imgr,sedcode, ArchiveInfoVo.class);
        List<ExcelExp> mysheet = new ArrayList<ExcelExp>();
        mysheet.add(e1);
        mysheet.add(e2);
        ByteOutputStream bos1 = new ByteOutputStream();
        ExcelUtilManySheetSecond<List<ExcelExp>> util2 = new ExcelUtilManySheetSecond<List<ExcelExp>>(mysheet);
 
      //  util2.exportExcelManySheet(response, mysheet);
 
 
 
        //导出卷面目录代码
//        DocumentMaterials documentMaterials = new DocumentMaterials();
//        documentMaterials.setRecordId(55L);
//      //  List<DocumentMaterials> docs = documentMaterialsService.selectDocumentMaterialsList(documentMaterials);
//        List<DocumentMaterialsVo> dsvs = documentMaterialsService.findArchMInfo("55");
//      //  dsvs.get(0).setUrl("/profile/upload/2025/08/14/30_20250814212128A031.jpg");
//
//
//      //  dsvs.stream().map()
//        List<DocumentMaterialsVoSmall> list2 = dsvs.stream().map(res -> new DocumentMaterialsVoSmall(res.getNum(), res.getDocumentNumber(),res.getCreator(),
//                res.getTitle(), res.getDate(), res.getPageNumber(), res.getRemarks())).collect(Collectors.toList());
//
//        String recordId = dsvs.get(0).getRecordId();
//        byte[] imgr = barcodeService.generateBarcodeImage(recordId);
//        ExcelExp e1 = new ExcelExp("卷内目录数据",dsvs, DocumentMaterialsVo.class);
//        ExcelExp e2 = new ExcelExp("卷内数据", list2, recordId, imgr, DocumentMaterialsVoSmall.class);
//        List<ExcelExp> mysheet = new ArrayList<ExcelExp>();
//        mysheet.add(e1);
//        mysheet.add(e2);
//        ExcelUtilManySheet<List<ExcelExp>> util2 = new ExcelUtilManySheet<List<ExcelExp>>(mysheet);
       // util2.exportExcelManySheet(response, mysheet);
 
    }
 
 
 
    @PostMapping("/exportChooseArchive/{ids}")
    public void exportChooseArchive(HttpServletResponse response,  @PathVariable Long[] ids)throws Exception
    {
        System.out.println("092939932");
        String zipFileName ="test" +".zip";
        //生成压缩包存储地址(最后会删掉)
        String fileZip = RuoYiConfig.getProfile() + "/download/" + zipFileName;
        OutputStream os=null;
        ZipOutputStream zos = null ;
        System.out.println("==============_______________");
        System.out.println(ids.length);
        File file = new File(fileZip);
        try {
            if (!file.getParentFile().exists()) {
                file.getParentFile().mkdirs();
            }
            os = new FileOutputStream(file);
            //压缩文件
            zos = new ZipOutputStream(os);
            //拿到当前的时间
            LocalDate date = LocalDate.now();
            System.out.println("当前日期: " + date);
 
 
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
            String formattedDate = date.format(formatter);
            System.out.println(formattedDate);
            String fna = "GH"+20250908+"/";
            zos.putNextEntry(new ZipEntry(fna));
 
 
            //添加ids的全部数据到excel
            ArchiveRecords archiveRecords = new ArchiveRecords();
            archiveRecords.setIds(ids);
            List<ArchiveRecords> lis = iArchiveRecordsService.selectArchiveRecordsList(archiveRecords);
            //案卷目录导出
            ZipEntry entry = new ZipEntry("案卷目录" + ".xls");
            ExcelUtil<ArchiveRecords> util = new ExcelUtil<ArchiveRecords>(ArchiveRecords.class);
 
            zos.putNextEntry(entry);
            ByteOutputStream bos = new ByteOutputStream();
            util.byteOutputStreamExcel(bos, lis,"案卷目录", "");
            bos.writeTo(zos);
 
            //移交清单
            List<ArchiveRecordSmall> lrs = iArchiveRecordsService.findByIds(archiveRecords);
            System.out.println(lrs);
            ZipEntry entry1 = new ZipEntry("移交清单" + ".xls");
            ExcelUtil<ArchiveRecordSmall> util1 = new ExcelUtil<ArchiveRecordSmall>(ArchiveRecordSmall.class);
 
            zos.putNextEntry(entry1);
            ByteOutputStream bos1 = new ByteOutputStream();
            util1.byteOutputStreamExcel(bos1, lrs,"移交清单", "");
            bos1.writeTo(zos);
            for(int i = 0; i < ids.length; i++) {
 
                // 获取文件的保存位置,读取数据库,
                DocumentMaterials documentMaterials = new DocumentMaterials();
                documentMaterials.setRecordId(ids[i]);
                List<DocumentMaterialsVoLarge> docs = documentMaterialsService.selectDocumentMaterialsAllByRecordId(ids[i]);
                System.out.println(docs.size()+"----009");
 
 
                //.selectDocumentMaterialsList(documentMaterials);
                List<DocumentMaterialsVo> dsvs = documentMaterialsService.findArchMInfo(ids[i].toString());
                ArchiveInfoVo aIV = iArchiveRecordsService.selectByRecordId(ids[i]);
 
                String adir = aIV.getInquiryNumber() + " " + aIV.getRecordId();
                System.out.println(fna+adir);
                zos.putNextEntry(new ZipEntry(fna + adir + "/"));
 
                //在里面添加文件
                boolean res = true;
                if(res) {
                    zos.putNextEntry(new ZipEntry(fna + adir + "/01-申请材料/"));
                    zos.putNextEntry(new ZipEntry(fna + adir + "/02-办案过程材料/"));
                    zos.putNextEntry(new ZipEntry(fna + adir + "/03-结论性文件/"));
                    zos.putNextEntry(new ZipEntry(fna + adir + "/04-其他材料/"));
                    zos.putNextEntry(new ZipEntry(fna + adir + "/05-档案变更材料/"));
                    zos.putNextEntry(new ZipEntry(fna + adir + "/06-业务数据/"));
                    res = false;
                }
                //添加07  的pdf
                //pdf目录封面
                String pdfPathF = "07-卷面封面.pdf";
                pdfGenerateService.generatePdf(pdfPathF, ids[i]);
                // 2. 压缩PDF到ZIP文件
                // 添加PDF文件到ZIP
                ZipEntry zipEntry2 = new ZipEntry(fna + adir +"/"+pdfPathF);
                zos.putNextEntry(zipEntry2);
 
                // 读取PDF文件内容并写入ZIP
                try (FileInputStream fis = new FileInputStream(pdfPathF)) {
                    byte[] buffer = new byte[1024];
                    int len;
                    while ((len = fis.read(buffer)) > 0) {
                        zos.write(buffer, 0, len);
                    }
                }
 
                //08-卷内卷内目录的pdf
                String pdf08Path= "08-卷内目录.pdf";
                List<DocumentMaterialsVo> list3 = dsvs;
                if(list3.size()>0) {
                    pdfGenerateService.generateFileDirectoryPdf(pdf08Path, list3);
                    ZipEntry zipEntry3 = new ZipEntry(fna + adir +"/"+pdf08Path);
                    zos.putNextEntry(zipEntry3);
 
                    // 读取PDF文件内容并写入ZIP
                    try (FileInputStream fis = new FileInputStream(pdf08Path)) {
                        byte[] buffer = new byte[1024];
                        int len;
                        while ((len = fis.read(buffer)) > 0) {
                            zos.write(buffer, 0, len);
                        }
                    }
                }
                //09-备考表.pdf
                String pdf09Path = "09-备考表.pdf";
                pdfGenerateService.generateFileStyleInfo(pdf09Path, aIV.getRecordId(), ids[i]);
                // 2. 压缩PDF到ZIP文件
                // 添加PDF文件到ZIP
                ZipEntry zipEntry4 = new ZipEntry(fna + adir +"/"+pdf09Path);
                zos.putNextEntry(zipEntry4);
 
                // 读取PDF文件内容并写入ZIP
                try (FileInputStream fis = new FileInputStream(pdf09Path)) {
                    byte[] buffer = new byte[1024];
                    int len;
                    while ((len = fis.read(buffer)) > 0) {
                        zos.write(buffer, 0, len);
                    }
                }
 
                //excel 卷内封面导出zip
                ZipEntry entryiv = new ZipEntry(fna + adir +"/"+"卷内封面" + ".xls");
 
                List<ArchiveInfoVo> arsi = new ArrayList<>();
                arsi.add(aIV);
 
                String recordId1 = aIV.getRecordId();
                byte[] imgr1 = barcodeService.generateBarcodeImage(recordId1);
                byte[] sedcode = pdfGenerateService.createQrCodeN(recordId1, 30, 30);
                ExcelExp e3 = new ExcelExp("案卷封面数据",arsi, ArchiveInfoVo.class);
                ExcelExp e4 = new ExcelExp("案卷封面",  arsi, recordId1, imgr1,sedcode, ArchiveInfoVo.class);
                List<ExcelExp> mysheet1 = new ArrayList<ExcelExp>();
                mysheet1.add(e3);
                mysheet1.add(e4);
                ByteOutputStream bos2 = new ByteOutputStream();
                ExcelUtilManySheetSecond<List<ExcelExp>> util3 = new ExcelUtilManySheetSecond<List<ExcelExp>>(mysheet1);
 
                util3.exportExcelManySheet(bos2, mysheet1);
 
                //  System.out.println(bos2);
                zos.putNextEntry(entryiv);
 
//            ExcelUtil<ArchiveInfoVo> utilsv = new ExcelUtil<ArchiveInfoVo>(ArchiveInfoVo.class);
//
//
//            ByteOutputStream boss = new ByteOutputStream();
//            List<ArchiveInfoVo> aivs = new ArrayList<>();
//            aivs.add(aIV);
//            utilsv.byteOutputStreamExcel(boss, aivs,"Date List", "");
                bos2.writeTo(zos);
 
 
 
                //写入电子目录  xsxl
 
                ZipEntry entry5 = new ZipEntry(fna + adir +"/"+"电子文件目录" + ".xls");
                ExcelUtil<DocumentMaterialsVoLarge> util5 = new ExcelUtil<DocumentMaterialsVoLarge>(DocumentMaterialsVoLarge.class);
 
                zos.putNextEntry(entry5);
                ByteOutputStream bos5 = new ByteOutputStream();
                util5.byteOutputStreamExcel(bos5, docs,"电子文件目录", "");
                bos.writeTo(zos);
 
                //拿到卷内目录的excel
                List<DocumentMaterialsVoSmall> list2 = dsvs.stream().map(res1 -> new DocumentMaterialsVoSmall(res1.getNum(), res1.getDocumentNumber(),res1.getCreator(),
                        res1.getTitle(), res1.getDate(), res1.getPageNumber(), res1.getRemarks())).collect(Collectors.toList());
                if(!dsvs.isEmpty()) {
                    String recordId = dsvs.get(0).getRecordId();
                    byte[] imgr = barcodeService.generateBarcodeImage(recordId);
                    ExcelExp e1 = new ExcelExp("卷内目录数据", dsvs, DocumentMaterialsVo.class);
                    ExcelExp e2 = new ExcelExp("卷内数据", list2, recordId, imgr, DocumentMaterialsVoSmall.class);
                    List<ExcelExp> mysheet = new ArrayList<ExcelExp>();
                    mysheet.add(e1);
                    mysheet.add(e2);
                    ExcelUtilManySheet<List<ExcelExp>> util2 = new ExcelUtilManySheet<List<ExcelExp>>(mysheet);
                    ZipEntry entr = new ZipEntry(fna + adir + "/" + "卷内目录" + ".xls");
                    // ExcelUtil<DocumentMaterialsVo> util1 = new ExcelUtil<DocumentMaterialsVo>(DocumentMaterialsVo.class);
                    System.out.println(dsvs);
                    zos.putNextEntry(entr);
                    ByteOutputStream bos6 = new ByteOutputStream();
 
                    util2.exportExcelManySheet(bos6, mysheet);
 
                    //   util1.byteOutputStreamExcel(bos1, dsvs,"Date List", "");
                    bos6.writeTo(zos);
                }
                //把excel转为pdf
 
 
 
                byte[] buf = new byte[1024];
                for (DocumentMaterialsVoLarge dc : docs) {
                    String filePath = dc.getUrl();
                    if(filePath==null)
                        continue;
                    filePath = filePath.replace("/profile/", RuoYiConfig.getProfile() + "/");
 
                    System.out.println(filePath);
                    File tempFile = new File(filePath);
 
                    //在压缩包中添加文件夹
 
                    //得到文件名frontCompWithZore(4, dc.get)+
                    String fname = frontCompWithZore(4, dc.getFileNumber().intValue())+"-"+dc.getTitle()+"-"+frontCompWithZore(4,dc.getPageNumber().intValue())+"."
                            +dc.getUrl().split("\\.")[1];
                    if(dc.getStage().equals("01-申请材料"))
                        zos.putNextEntry(new ZipEntry(fna + adir + "/01-申请材料/"+fname));
                    else if(dc.getStage().equals("02-办案过程材料"))
                        zos.putNextEntry(new ZipEntry(fna + adir + "/02-办案过程材料/"+fname));
                    else if(dc.getStage().equals("03-结论性文件"))
                        zos.putNextEntry(new ZipEntry(fna + adir + "/03-结论性文件/"+fname));
                    else if(dc.getStage().equals("04-其他材料")) {
                        zos.putNextEntry(new ZipEntry(fna + adir + "/04-其他材料/" + fname));
                    }
                    else if(dc.getStage().equals("05-档案变更材料"))
                        zos.putNextEntry(new ZipEntry(fna + adir + "/05-档案变更材料/"+fname));
                    else if(dc.getStage().equals("06-业务数据"))
                        zos.putNextEntry(new ZipEntry(fna + adir + "/06-业务数据/"+fname));
                    else
                        zos.putNextEntry(new ZipEntry(fna + adir + "/"+fname));
 
 
 
                    int len;
                    FileInputStream in = new FileInputStream(tempFile);
                    while ((len = in.read(buf)) != -1){
                        zos.write(buf, 0, len);
                    }
                    //  zos.putNextEntry(new ZipEntry("04-其他材料"));
                    zos.closeEntry();
                    in.close();
                }
            }
            System.out.println(file.getName());
            writeFileToRes(response, file.getName(), file);
 
        } catch (Exception e) {
            throw new RuntimeException(e);
        }finally {
            //关闭流
            if(zos != null){
                try {
                    zos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            //关闭流
            if(os!= null){
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
 
    }
 
 
 
 
 
 
    /**
     * 打包下载
     * @param response
     * @param
     */
    @PostMapping("/export/{id}")
 
    public void packDownload(HttpServletResponse response,  @PathVariable Long id) throws Exception {
        // 获取文件的保存位置,读取数据库,
        DocumentMaterials documentMaterials = new DocumentMaterials();
        documentMaterials.setRecordId(id);
        List<DocumentMaterialsVoLarge> docs = documentMaterialsService.selectDocumentMaterialsAllByRecordId(id);
       System.out.println(docs.size()+"----009");
 
 
                //.selectDocumentMaterialsList(documentMaterials);
        List<DocumentMaterialsVo> dsvs = documentMaterialsService.findArchMInfo(id.toString());
        ArchiveInfoVo aIV = iArchiveRecordsService.selectByRecordId(id);
 
        List<String> paths = new ArrayList<>();
//        System.out.println(docs);
        if (docs.size() == 1) {  // 直接下载
            String filePath = paths.get(0);
            filePath = filePath.replace("/profile/", RuoYiConfig.getProfile() + "/");
 
            File file = new File(filePath);
            if (!file.exists()) {
                throw new Exception("文件不存在");
            }
            String suffix = filePath.substring(filePath.lastIndexOf("."));
            // 输出文件流
            writeFileToRes(response, "user" + "-" + new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()) + suffix, file);
        } else {  // 压缩之后在进行下载
            //压缩包名称(会拼上当前时间)
            System.out.println(90);
            String datumName = "user";
            //压缩文件
            List<String> filePathList = paths;
            File file = compressedFileToZip(docs, dsvs, aIV, id);
            System.out.println(file.getName());
 
            String fileName =aIV.getRecordId()+".zip";
 
            //输出文件流
            writeFileToRes(response, file.getName(), file);
            //删除压缩包
            if(file.exists()){
                file.delete();
            }
        }
    }
    // 输出文件流到response
    private void writeFileToRes(HttpServletResponse response, String fileName, File file) throws IOException {
 
 
        String realFileName = System.currentTimeMillis() + fileName.substring(fileName.indexOf("_") + 1);
        String filePath = RuoYiConfig.getDownloadPath() + fileName;
 
 
        response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
        FileUtils.setAttachmentResponseHeader(response, realFileName);
        FileUtils.writeBytes(filePath, response.getOutputStream());
 
    }
    public String frontCompWithZore(int formatLength,int formatNumber){
        /**
         * 0 指前面补充零
         * formatLength 字符总长度为 formatLength
         * inputNumber 格式化数字
         * d 代表为正数。
         */
        String newString = String.format("%0"+formatLength+"d", formatNumber);
        return newString;
    }
 
 
 
 
 
 
    private  PdfPCell ImageSet(int high) throws Exception {
      //  byte[] fileByte = toByteArray("D:\\project\\archive\\download\\12.jpg");
//
//
//        FileInputStream fis = new FileInputStream("D:\\project\\archive\\download\\56.png");
//        byte[] imageBytes = new byte[fis.available()];
//        fis.read(imageBytes);
//        fis.close();
//        Jpeg jpeg = new Jpeg(imageBytes);
//        jpeg.scaleAbsolute(70, 50);
        byte[] imageBytes = barcodeService.generateBarcodeImage("D3.4.1-05-2024-0002");
// 加载PNG图片
        Image img = Image.getInstance(imageBytes);
        // 设置图片在PDF中的位置(可选)
        img.setAbsolutePosition(100, 700);
        // 将图片添加到PDF文档中
        PdfPCell pdfPCell = new PdfPCell(img);
        pdfPCell.setMinimumHeight(high);
        pdfPCell.setUseAscender(true); // 设置可以居中
        pdfPCell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER); // 设置水平居中
        pdfPCell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE); // 设置垂直居中
        return pdfPCell;
    }
 
 
    // 压缩文件
    private File compressedFileToZip(List<DocumentMaterialsVoLarge> docs, List<DocumentMaterialsVo> dsvs,   ArchiveInfoVo aIV, Long id) throws Exception {
        //压缩包具体名称(拼接时间戳防止重名)
        String datumName = "";
        String zipFileName =dsvs.get(0).getDocumentNumber()+aIV.getRecordId()+ ".zip";
        //生成压缩包存储地址(最后会删掉)
        String fileZip = RuoYiConfig.getProfile() + "/download/" + zipFileName;
        OutputStream os=null;
        ZipOutputStream zos = null ;
        System.out.println("==============_______________");
        File file = new File(fileZip);
        try {
            if (!file.getParentFile().exists()) {
                file.getParentFile().mkdirs();
            }
            os=new FileOutputStream(file);
            //压缩文件
            zos = new ZipOutputStream(os);
 
            //pdf目录封面
            String pdfPathF = "07-卷面封面.pdf";
            pdfGenerateService.generatePdf(pdfPathF, 55L);
            // 2. 压缩PDF到ZIP文件
                // 添加PDF文件到ZIP
                ZipEntry zipEntry = new ZipEntry(pdfPathF);
                zos.putNextEntry(zipEntry);
 
                // 读取PDF文件内容并写入ZIP
                try (FileInputStream fis = new FileInputStream(pdfPathF)) {
                    byte[] buffer = new byte[1024];
                    int len;
                    while ((len = fis.read(buffer)) > 0) {
                        zos.write(buffer, 0, len);
                    }
                }
            //08-卷内卷内目录的pdf
            String pdf08Path= "08-卷内目录.pdf";
            List<DocumentMaterialsVo> list3 = dsvs;
            pdfGenerateService.generateFileDirectoryPdf(pdf08Path, list3);
            ZipEntry zipEntry2 = new ZipEntry(pdf08Path);
            zos.putNextEntry(zipEntry2);
 
            // 读取PDF文件内容并写入ZIP
            try (FileInputStream fis = new FileInputStream(pdf08Path)) {
                byte[] buffer = new byte[1024];
                int len;
                while ((len = fis.read(buffer)) > 0) {
                    zos.write(buffer, 0, len);
                }
            }
                //09-备考表.pdf
            String pdf09Path = "09-备考表.pdf";
            pdfGenerateService.generateFileStyleInfo(pdf09Path, aIV.getRecordId(), id);
            // 2. 压缩PDF到ZIP文件
            // 添加PDF文件到ZIP
            ZipEntry zipEntry1 = new ZipEntry(pdf09Path);
            zos.putNextEntry(zipEntry1);
 
            // 读取PDF文件内容并写入ZIP
            try (FileInputStream fis = new FileInputStream(pdf09Path)) {
                byte[] buffer = new byte[1024];
                int len;
                while ((len = fis.read(buffer)) > 0) {
                    zos.write(buffer, 0, len);
                }
            }
 
 
 
 
                //excel 卷内封面导出zip
            ZipEntry entryiv = new ZipEntry("卷内封面" + ".xls");
 
            List<ArchiveInfoVo> arsi = new ArrayList<>();
            arsi.add(aIV);
 
            String recordId1 = aIV.getRecordId();
            byte[] imgr1 = barcodeService.generateBarcodeImage(recordId1);
            byte[] sedcode = pdfGenerateService.createQrCodeN(recordId1, 30, 30);
            ExcelExp e3 = new ExcelExp("案卷封面数据",arsi, ArchiveInfoVo.class);
            ExcelExp e4 = new ExcelExp("案卷封面",  arsi, recordId1, imgr1,sedcode, ArchiveInfoVo.class);
            List<ExcelExp> mysheet1 = new ArrayList<ExcelExp>();
            mysheet1.add(e3);
            mysheet1.add(e4);
            ByteOutputStream bos2 = new ByteOutputStream();
            ExcelUtilManySheetSecond<List<ExcelExp>> util3 = new ExcelUtilManySheetSecond<List<ExcelExp>>(mysheet1);
 
            util3.exportExcelManySheet(bos2, mysheet1);
 
          //  System.out.println(bos2);
            zos.putNextEntry(entryiv);
 
//            ExcelUtil<ArchiveInfoVo> utilsv = new ExcelUtil<ArchiveInfoVo>(ArchiveInfoVo.class);
//
//
//            ByteOutputStream boss = new ByteOutputStream();
//            List<ArchiveInfoVo> aivs = new ArrayList<>();
//            aivs.add(aIV);
//            utilsv.byteOutputStreamExcel(boss, aivs,"Date List", "");
            bos2.writeTo(zos);
                //写入电子目录  xsxl
 
            ZipEntry entry = new ZipEntry("电子文件目录" + ".xls");
            ExcelUtil<DocumentMaterialsVoLarge> util = new ExcelUtil<DocumentMaterialsVoLarge>(DocumentMaterialsVoLarge.class);
 
            zos.putNextEntry(entry);
            ByteOutputStream bos = new ByteOutputStream();
            util.byteOutputStreamExcel(bos, docs,"电子文件目录", "");
            bos.writeTo(zos);
 
            //拿到卷内目录的excel
            List<DocumentMaterialsVoSmall> list2 = dsvs.stream().map(res -> new DocumentMaterialsVoSmall(res.getNum(), res.getDocumentNumber(),res.getCreator(),
                    res.getTitle(), res.getDate(), res.getPageNumber(), res.getRemarks())).collect(Collectors.toList());
 
            String recordId = dsvs.get(0).getRecordId();
            byte[] imgr = barcodeService.generateBarcodeImage(recordId);
            ExcelExp e1 = new ExcelExp("卷内目录数据",dsvs, DocumentMaterialsVo.class);
            ExcelExp e2 = new ExcelExp("卷内数据", list2, recordId, imgr, DocumentMaterialsVoSmall.class);
            List<ExcelExp> mysheet = new ArrayList<ExcelExp>();
            mysheet.add(e1);
            mysheet.add(e2);
            ExcelUtilManySheet<List<ExcelExp>> util2 = new ExcelUtilManySheet<List<ExcelExp>>(mysheet);
            ZipEntry entr = new ZipEntry("卷内目录" + ".xls");
            ExcelUtil<DocumentMaterialsVo> util1 = new ExcelUtil<DocumentMaterialsVo>(DocumentMaterialsVo.class);
            System.out.println(dsvs);
            zos.putNextEntry(entr);
            ByteOutputStream bos1 = new ByteOutputStream();
 
            util2.exportExcelManySheet(bos1, mysheet);
 
         //   util1.byteOutputStreamExcel(bos1, dsvs,"Date List", "");
            bos1.writeTo(zos);
            //把excel转为pdf
 
 
 
 
            boolean res = true;
 
            byte[] buf = new byte[1024];
            for (DocumentMaterialsVoLarge dc : docs) {
                String filePath = dc.getUrl();
                if(filePath==null)
                    continue;
                filePath = filePath.replace("/profile/", RuoYiConfig.getProfile() + "/");
 
                System.out.println(filePath);
                File tempFile = new File(filePath);
                //在压缩包中添加文件夹
                if(res) {
                    zos.putNextEntry(new ZipEntry("01-申请材料/"));
                    zos.putNextEntry(new ZipEntry("02-办案过程材料/"));
                    zos.putNextEntry(new ZipEntry("03-结论性文件/"));
                    zos.putNextEntry(new ZipEntry("04-其他材料/"));
                    zos.putNextEntry(new ZipEntry("05-档案变更材料/"));
                    zos.putNextEntry(new ZipEntry("06-业务数据/"));
                    res = false;
                }
                //得到文件名frontCompWithZore(4, dc.get)+
                String fname = frontCompWithZore(4, dc.getFileNumber().intValue())+"-"+dc.getTitle()+"-"+frontCompWithZore(4,dc.getPageNumber().intValue())+"."
                        +dc.getUrl().split("\\.")[1];
                if(dc.getStage().equals("01-申请材料"))
                    zos.putNextEntry(new ZipEntry("01-申请材料/"+fname));
                else if(dc.getStage().equals("02-办案过程材料"))
                    zos.putNextEntry(new ZipEntry("02-办案过程材料/"+fname));
                else if(dc.getStage().equals("03-结论性文件"))
                    zos.putNextEntry(new ZipEntry("03-结论性文件/"+fname));
                else if(dc.getStage().equals("04-其他材料")) {
                    zos.putNextEntry(new ZipEntry("04-其他材料/" + fname));
                }
                else if(dc.getStage().equals("05-档案变更材料"))
                    zos.putNextEntry(new ZipEntry("05-档案变更材料/"+fname));
                else if(dc.getStage().equals("06-业务数据"))
                    zos.putNextEntry(new ZipEntry("06-业务数据/"+fname));
                else
                    zos.putNextEntry(new ZipEntry(fname));
 
 
 
                int len;
                FileInputStream in = new FileInputStream(tempFile);
                while ((len = in.read(buf)) != -1){
                    zos.write(buf, 0, len);
                }
                  //  zos.putNextEntry(new ZipEntry("04-其他材料"));
                zos.closeEntry();
                in.close();
            }
 
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println(e.toString());
            throw new Exception("文件打包:"+e.getMessage());
        }finally {
            //关闭流
            if(zos != null){
                try {
                    zos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            //关闭流
            if(os!= null){
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return file;
    }
 
}