Browse Source

Merge remote-tracking branch 'origin/dev' into dev

LHB 1 day ago
parent
commit
09a45eed36

+ 85 - 8
blade-service/blade-meter/src/main/java/org/springblade/meter/controller/TaskController.java

@@ -3,6 +3,7 @@ package org.springblade.meter.controller;
 
 import cn.hutool.log.StaticLog;
 import com.alibaba.fastjson.JSON;
+import com.alibaba.fastjson.JSONArray;
 import com.alibaba.fastjson.JSONObject;
 import com.aspose.cells.PageSetup;
 import com.aspose.cells.SaveFormat;
@@ -28,6 +29,8 @@ import lombok.Data;
 import net.logstash.logback.encoder.org.apache.commons.lang3.ObjectUtils;
 import org.apache.commons.lang.StringUtils;
 import org.apache.commons.lang.time.DateUtils;
+import org.apache.pdfbox.pdmodel.PDDocument;
+import org.apache.pdfbox.pdmodel.PDPage;
 import org.apache.poi.ss.usermodel.*;
 import org.apache.poi.ss.util.CellRangeAddress;
 import org.apache.poi.util.IOUtils;
@@ -4583,7 +4586,7 @@ public class TaskController extends BladeController {
                     return R.fail(sb.toString());
                 }
                 executionTime.info("生成PDF");
-                /*合并所有表*/
+                /*合并所有表 并将每个类型进行 分割*/
                 fileUrl = report.getReportPdf(file_path, reportResults);
                 //进行分页 400页一份,不超过400页
                 executionTime.info("合并PDF");
@@ -4897,7 +4900,7 @@ public class TaskController extends BladeController {
     }
 
     @Data
-    class Report {
+    public class Report {
         public final String[] REPORT_TYPE = new String[]{"s_interim_pay_certificate", "s_material_start_statement", "s_material_start_statement"};
         public final String[] PERIOD_TYPE = new String[]{"s_contract_meter_period", "s_meter_period", "s_meter_period"};
         /**
@@ -4939,16 +4942,91 @@ public class TaskController extends BladeController {
                     return "无效地址";
                 }
             }, (v1, v2) -> v1, LinkedHashMap::new));
-            String upSql = "update " + REPORT_TYPE[this.type] + " set raw_url=?,file_url_list=? ,calculate_date=SYSDATE() where id=" + this.id;
+            String upSql = "update " + REPORT_TYPE[this.type] + " set raw_url=?,file_url_list=? ,calculate_date=SYSDATE(),pre_pdf_url='' where id=" + this.id;
             jdbcTemplate.update(upSql, this.pdfUrl, JSON.toJSONString(fileListMap));
-            //如果是中间计量,需要对pdfUrl进行分页
-            if(this.type==0){
+
+            //由于之前需求每400 进行分割,现在需求需要根据类型+页码 分页
+            if(this.type==0) {
+                JSONArray jsonArray = new JSONArray();
+                for (Map.Entry<String, String> entry : fileListMap.entrySet()) {
+                    JSONObject jsonObject = new JSONObject();
+                    String pdfUrl = entry.getValue();
+                    String result = commonFileClient.getPdfNum(pdfUrl);
+                    Integer pageNum=0;
+                    if(StringUtils.isNotEmpty(result)){
+                        pageNum= Integer.parseInt(result);
+                    }
+                    if(pageNum<=100){
+                        jsonObject.put("title",entry.getKey());
+                        jsonObject.put("state",0); // 初始化
+                        jsonObject.put("url",pdfUrl); //路径
+                        jsonArray.add(jsonObject);
+                    }else{
+                         Double pdfPageNo = Math.ceil(pageNum / 100.00);
+                         for (int i=0;i<pdfPageNo;i++){
+                             jsonObject = new JSONObject();
+                             Long id = SnowFlakeUtil.getId();
+                             String localPdf = FileUtils.getSysLocalFileUrl() + "/pdf/" + id + ".pdf";
+                             int pageStart = 0 ;
+                             int pageEnd = 0 ;
+                             if(i<pdfPageNo-1){
+                                 pageStart = 100 * i;
+                                 pageEnd = 100 * (i+1);
+                             }else{
+                                 pageStart = 100 * i;
+                                 pageEnd = pageNum;
+                             }
+                             int pdfByPage = this.getPdfByPage(pageStart, pageEnd, pdfUrl, localPdf);
+                             if(pdfByPage==0){
+                                 BladeFile bladeFile1 = newIOSSClient.uploadFile( SnowFlakeUtil.get() + ".pdf", localPdf);
+                                 String pagePdfUrl=bladeFile1.getLink();
+                                 jsonObject.put("title",entry.getKey()+""+(i+1));
+                                 jsonObject.put("state",0); // 初始化
+                                 jsonObject.put("url",pagePdfUrl); //路径
+                                 System.out.println("--"+jsonObject);
+                                 jsonArray.add(jsonObject);
+                             }
+                         }
+                    }
+                }
+                jdbcTemplate.update("update s_interim_pay_certificate set page_pdf_url=? where id=?", JSON.toJSONString(jsonArray), this.id);
+                System.out.println("");
+            }
+            return this.pdfUrl;
+        }
+        public int getPdfByPage(int startPage, int endPage, String filePath, String savePath) {
+            try {
+                InputStream inputStreamByUrl = CommonUtil.getOSSInputStream3(filePath);
+                // 加载PDF文件
+                PDDocument document = PDDocument.load(inputStreamByUrl);
+                // 创建新文档
+                PDDocument newDocument = new PDDocument();
+
+                // 注意:PDFBox中的页面索引从0开始
+                int actualStart = Math.max(0, startPage - 1); // 将用户输入的1转换为0
+                int actualEnd = Math.min(document.getNumberOfPages() - 1, endPage - 1); // 将用户输入的10转换为9
+
+                // 添加指定范围的页面
+                for (int i = actualStart; i <= actualEnd; i++) {
+                    PDPage page = document.getPage(i);
+                    newDocument.addPage(page);
+                }
+
+                // 保存为新文件
+                newDocument.save(savePath);
+                newDocument.close();
+                document.close();
+                return 0;
+            } catch (Exception e) {
+                return 1;
+            }
+        }
+            /*if(this.type==0){
                 Map<String,String>pageMap=new HashMap<>();
                 List<String> currentBatch = new ArrayList<>();
                 int currentPageCount = 0;
 
                 for (Map.Entry<String, String> entry : fileListMap.entrySet()) {
-
                     String pdfUrl = entry.getValue();
                     String result = commonFileClient.getPdfNum(pdfUrl);
                     Integer pageNum=0;
@@ -4973,7 +5051,6 @@ public class TaskController extends BladeController {
                             currentBatch = new ArrayList<>();
                             currentPageCount = 0;
                         }
-
                     }
                     // 添加当前PDF到批次
                     currentBatch.add(pdfUrl);
@@ -5013,7 +5090,7 @@ public class TaskController extends BladeController {
                 jdbcTemplate.update("update s_interim_pay_certificate set page_pdf_url=? where id=?", JSON.toJSONString(sortedMap), this.id);
             }
             return this.pdfUrl;
-        }
+        }*/
 
         public Report(Long id, Integer type) {
             this.id = id;