Bladeren bron

新增定时修复服务器

chenr 2 maanden geleden
bovenliggende
commit
961e0814d5

+ 11 - 0
blade-service/blade-repair/pom.xml

@@ -65,5 +65,16 @@
             <groupId>org.springframework</groupId>
             <artifactId>spring-test</artifactId>
         </dependency>
+        <dependency>
+            <groupId>e-iceblue</groupId>
+            <artifactId>spire.xls.free</artifactId>
+            <version>5.3.0</version>
+            <scope>system</scope>
+            <systemPath>${basedir}/../../doc/lib/eVisaLib/spire.xls.free-5.3.0.jar</systemPath>
+        </dependency>
+        <dependency>
+            <groupId>org.jsoup</groupId>
+            <artifactId>jsoup</artifactId>
+        </dependency>
     </dependencies>
 </project>

+ 1 - 1
blade-service/blade-repair/src/main/java/org/springblade/RepairApplication.java

@@ -13,6 +13,6 @@ import org.springframework.scheduling.annotation.EnableAsync;
 @EnableCaching
 public class RepairApplication {
     public static void main(String[] args) {
-        BladeApplication.run(LauncherConstant.APPLICATION_METER_NAME, RepairApplication.class, args);
+        BladeApplication.run(LauncherConstant.APPLICATION_REPAIR_NAME, RepairApplication.class, args);
     }
 }

+ 82 - 36
blade-service/blade-repair/src/main/java/org/springblade/repair/controller/CheckAndRepairController.java

@@ -9,6 +9,9 @@ import org.springblade.core.oss.model.BladeFile;
 import org.springblade.core.tool.api.R;
 import org.springblade.core.tool.utils.ObjectUtil;
 import org.springblade.manager.entity.ExcelTab;
+import org.springblade.manager.entity.WbsTreeContract;
+import org.springblade.manager.entity.WbsTreePrivate;
+import org.springblade.repair.util.ExcelInfoUtils;
 import org.springblade.repair.util.FileUtils;
 import org.springblade.resource.feign.NewIOSSClient;
 import org.springframework.jdbc.core.BeanPropertyRowMapper;
@@ -21,6 +24,7 @@ import org.springframework.web.bind.annotation.RestController;
 import org.springblade.manager.feign.ExcelTabClient;
 import org.springframework.web.multipart.MultipartFile;
 import java.io.File;
+import java.io.FileInputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.nio.file.Files;
@@ -41,10 +45,9 @@ public class CheckAndRepairController {
      */
     @RequestMapping("/checkAndRepairExcelHtml")
     @ApiOperation("定时检测修复ExcelHtml")
-    @Scheduled(cron = "0  15 16 * * ?")
+    @Scheduled(cron = "00 22 10 * * ?")
     public void checkAndRepairExcelHtml() {
-        StringBuilder result=new StringBuilder("");
-        StringBuilder result1=new StringBuilder("");
+        StringBuilder result=new StringBuilder("清表损坏:");
         String sql = "Select * from m_excel_tab where parent_id=0 and is_deleted=0";
         List<ExcelTab> query = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(ExcelTab.class));
         //String excelPath="E:\\excel\\";
@@ -57,27 +60,37 @@ public class CheckAndRepairController {
                     String sql2 = "Select * from m_excel_tab where parent_id=" + tab.getId() + " and is_deleted=0 and file_url is not null ";
                     List<ExcelTab> query2 = jdbcTemplate.query(sql2, new BeanPropertyRowMapper<>(ExcelTab.class));
                     for (ExcelTab excelTab1 : query2) {
+                        if(excelTab1.getId()==1800344270830305280L){
+                            continue;
+                        }
                         File html = new File(excelTab1.getHtmlUrl());
                         if(!html.exists()){
                             System.out.println(excelTab.getName() + "--" + tab.getName() + "--" + excelTab1.getName()+"html不存在 id:"+excelTab1.getId());
                             if(excelTab1.getFileUrl() != null&&excelTab1.getFileUrl().endsWith(".xlsx") || excelTab1.getFileUrl().endsWith(".xls")){
-                                long resourceLength = CommonUtil.getResourceLength(excelTab1.getFileUrl());
-                                if(resourceLength>=500){
+                                long resourceLength = CommonUtil.getFileContentLength(excelTab1.getFileUrl());
+                                System.out.println("文件大小:"+resourceLength);
+                                if(resourceLength!=415L&&resourceLength!=0L&&resourceLength!=234L){
                                     //先下载这个文件,再上传
                                     String fileUrl = excelTab1.getFileUrl();
                                     String localPath=excelPath+excelTab1.getId()+".xlsx";
-                                    InputStream inputStreamByUrl = FileUtils.getInputStreamByUrl(fileUrl);
-                                    FileUtils.saveFile(inputStreamByUrl,localPath);
-                                    File htmlFile = new File(localPath);
-                                    if(htmlFile.exists()){
-                                        try {
-                                            MultipartFile file = convert(htmlFile, excelTab1);
-                                            this.putFileAttach(file, excelTab1.getId());
-                                        }catch (Exception e){
-                                            e.printStackTrace();
+                                    System.out.println("开始下载:"+fileUrl);
+                                    try {
+                                        InputStream inputStreamByUrl = FileUtils.getInputStreamByUrl(fileUrl);
+                                        if(inputStreamByUrl!=null){
+                                            FileUtils.saveFile(inputStreamByUrl,localPath);
+                                            File htmlFile = new File(localPath);
+                                            if(htmlFile.exists()){
+                                                this.putFileAttach(new FileInputStream(htmlFile), excelTab1.getId(),excelTab1.getExtension());
+                                            }
+                                        }
+                                    }catch (Exception e){
+                                        System.out.println("下载失败:"+fileUrl);
+                                        if(resourceLength<500){
+                                            deletedExcel(excelTab1.getId());
                                         }
                                     }
                                 }else {
+                                    deletedExcel(excelTab1.getId());
                                     result.append(excelTab.getName() + "--" + tab.getName() + "--" + excelTab1.getName()+"\n");
                                 }
                             }
@@ -86,49 +99,82 @@ public class CheckAndRepairController {
                 }
             }
         } catch (Exception e) {
-            throw new RuntimeException(e);
+            e.printStackTrace();
         }
         System.out.println("完成");
         System.out.println(result);
-        System.out.println(result1);
     }
 
-    public static MultipartFile convert(File file,ExcelTab e) throws IOException {
-        String contentType = Files.probeContentType(file.toPath());
-        byte[] content = Files.readAllBytes(file.toPath());
-        return new MockMultipartFile(
-            "file",
-            e.getName(),
-            contentType,
-            content
-        );
+    public void deletedExcel(Long id){
+        String sql="update m_excel_tab set is_deleted=3 where id="+id;
+        jdbcTemplate.execute(sql);
+        System.out.println("删除成功:"+id);
     }
-    public R putFileAttach(@RequestParam("file") MultipartFile file, Long nodeId) throws Exception {
+
+    public void putFileAttach(InputStream inputStream, Long nodeId,String name) throws Exception {
+        System.out.println("开始上传");
         String file_path = FileUtils.getSysLocalFileUrl();
         String sql="Select * from m_excel_tab where id="+nodeId+" and is_deleted=0";
         ExcelTab detail = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(ExcelTab.class));
         String filecode = SnowFlakeUtil.getId() + "";
         String thmlUrl = file_path + filecode + ".html";
         String exceUrl = file_path + filecode + "123.xlsx";
-        excelTabClient.excelInfo(file.getInputStream(), exceUrl, thmlUrl, "1");
+        ExcelInfoUtils.excelInfo(inputStream, exceUrl, thmlUrl, "1");
         // 上传excel文件
-        BladeFile bladeFile = newIOSSClient.uploadFile(file.getOriginalFilename(), exceUrl);
-        if (bladeFile == null || ObjectUtil.isEmpty(bladeFile)) {
-            return R.fail("oss上传失败,请校验oss配置是否正确");
-        }
+        BladeFile bladeFile = newIOSSClient.uploadFile(name, exceUrl);
         // 解析原始excel
-        detail.setExtension(file.getOriginalFilename());
+        detail.setExtension(name);
         detail.setFileUrl(bladeFile.getLink());
         detail.setFileType(3); // 表示为清表信息  1 表示祖节点  2 表示为节点信息 3 表示清表
         detail.setHtmlUrl(thmlUrl);
-        excelTabClient.saveOrUpdate(detail);
+        String update="update m_excel_tab set file_url ="+"'"+bladeFile.getLink()+"' , html_url="+"'"+thmlUrl+"' where id="+nodeId;
+        System.out.println(update);
+        int i = jdbcTemplate.update(update);
         // 解析html
         excelTabClient.expailHtmlInfo(thmlUrl, detail.getId(), detail.getTabType() + "");
-        return R.success("上传成功");
+        if(i==1){
+            System.out.println("上传成功");
+        }else {
+            System.out.println("上传失败");
+        }
+
     }
 
     /**
-     * 每周天晚上2点定时更新private和contract的html,通过is_private_type_id;
+     * 定时更新private的htmUrl 保证url格式为 /mnt/sdc/Users/hongchuangyanfa/Desktop/privateUrlCopy/
      */
-    public void checkAndRepairPrivateAndContractHtml() {}
+    private void checkAndRepairPrivateAndContractPrivateCopyUrl() {
+
+    }
+
+    /**
+     * 定时更新private和contract的html,通过is_private_type_id;保证一样
+     */
+    @Scheduled(cron = "0 0 1 * * ?")
+    public void checkAndRepairPrivateAndContractHtml() {
+        System.out.println("开始扫描private和contract的html");
+        StringBuilder result=new StringBuilder("");
+        try {
+            String sql="SELECT a.p_key_id,a.is_type_private_pid FROM m_wbs_tree_contract a,m_wbs_tree_private b WHERE a.html_url != b.html_url AND a.is_type_private_pid=b.p_key_id AND a.type=2 AND a.is_deleted=0 AND b.type=2 AND b.html_url is not NULL";
+            List<WbsTreeContract> contractListlist = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(WbsTreeContract.class));
+            for (WbsTreeContract wbsTreeContract : contractListlist) {
+                String update=" update m_wbs_tree_contract set html_url=(select html_url from m_wbs_tree_private where p_key_id="+wbsTreeContract.getIsTypePrivatePid()+") where p_key_id="+wbsTreeContract.getPKeyId();
+                int i = jdbcTemplate.update(update);
+                if(i!=1){
+                    result.append(wbsTreeContract.getPKeyId()+"\n");
+                }
+            }
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+        System.out.println("更新完毕private和contract的html");
+        System.out.println(result);
+    }
+
+    /**
+     * 定时更新private和contract的initTable 通过excelID,保证一样
+     */
+    public void checkAndRepairPrivateAndContractInitTable() {}
+
+
 }

+ 208 - 0
blade-service/blade-repair/src/main/java/org/springblade/repair/util/ExcelInfoUtils.java

@@ -0,0 +1,208 @@
+package org.springblade.repair.util;
+
+import com.spire.xls.CellRange;
+import com.spire.xls.FileFormat;
+import com.spire.xls.Workbook;
+import com.spire.xls.Worksheet;
+import com.spire.xls.core.spreadsheet.HTMLOptions;
+import org.apache.commons.lang.StringUtils;
+import org.jsoup.Jsoup;
+import org.jsoup.nodes.Document;
+import org.jsoup.nodes.Element;
+import org.jsoup.select.Elements;
+
+import org.springblade.common.utils.SnowFlakeUtil;
+import org.springblade.core.tool.utils.FileUtil;
+import org.springblade.core.tool.utils.Func;
+import org.springblade.core.tool.utils.IoUtil;
+import org.springblade.core.tool.utils.ResourceUtil;
+import org.springblade.manager.vo.DateFormat;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.InputStream;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+public class ExcelInfoUtils {
+    public static void excelInfo(InputStream inputStream, String excelURL, String htmlUrl, String type) {
+        try {
+            String file_path = "/Users/hongchuangyanfa/Desktop//pdf/";
+            String filecode = SnowFlakeUtil.getId() + "";
+            String thmlUrl2 = file_path + filecode + "123.html";
+
+            // 解析原始excel
+            Workbook wb = new Workbook();
+            wb.loadFromMHtml(inputStream);
+            Worksheet sheet = wb.getWorksheets().get(0);
+            HTMLOptions options = new HTMLOptions();
+            options.setImageEmbedded(true);
+
+            // 校验excel 是否正常
+            sheet.saveToHtml(htmlUrl, options);
+            File file1 = ResourceUtil.getFile(htmlUrl);
+            String htmlString = IoUtil.readToString(new FileInputStream(file1));
+            Document doc = Jsoup.parse(htmlString);
+            Element table = doc.select("table").first();
+            Elements trs = table.select("tr");
+            Elements cols = table.select("Col");
+            sheet.deleteRow(trs.size()+1,sheet.getRows().length - trs.size());
+            sheet.deleteColumn(cols.size()+1,sheet.getColumns().length - cols.size());
+            file1.delete();
+            //复制一份
+            wb.saveToFile(excelURL, FileFormat.Version2013);
+            // 操作
+            Workbook wb2 = new Workbook();
+            wb2.loadFromMHtml(excelURL);
+            Worksheet sheet2 = wb2.getWorksheets().get(0);
+
+            // 坐标map
+            Map<String, Map<String, Integer>> xyList = new HashMap<>();
+            int j = 0;
+            int maxVal = 0;
+            if(type.equals("2")){
+                CellRange[] sheet2Cells = sheet2.getCells();
+                for (int i = 0; i < sheet2Cells.length; i++) {
+                    CellRange oldcell = sheet2Cells[i];
+                    String data = oldcell.getDataValidation().getErrorMessage();
+                    int k = 0;
+                    if(Func.isNumeric(data)){
+                        k = Func.toInt(data);
+                    }
+                    if (maxVal < k) {
+                        maxVal = k;
+                    }
+                }
+            }else if(type.equals("1")){
+                CellRange[] sheet2Cells = sheet.getCells();
+                for (int i = 0; i < sheet2Cells.length; i++) {
+                    CellRange oldcell = sheet2Cells[i];
+                    oldcell.getDataValidation().setErrorMessage("");
+                    sheet.get(oldcell.getRow(),oldcell.getColumn()).getDataValidation().setErrorMessage("");
+                }
+            }
+
+            //合并单元格操作
+            CellRange[] mergedCells = sheet.getMergedCells();
+            for (int i = 0; i < mergedCells.length; i++) {
+                Map<String, Integer> dataMap = new HashMap<>();
+                CellRange oldcell = mergedCells[i];
+                CellRange mergedCell = sheet.getCellRange(oldcell.getRow(), oldcell.getColumn());
+                if(type.equals("2")){
+                    String data = mergedCell.getDataValidation().getErrorMessage();
+                    if (StringUtils.isEmpty(data)) {
+                        if(maxVal<=0){
+                            j = j + 1;
+                        }else{
+                            maxVal = maxVal+1;
+                            j=maxVal;
+                        }
+                    } else {
+                        j = Func.toInt(data);
+                    }
+                }else{
+                    j = j + 1;
+                }
+                // 目标表添加备注信息
+                mergedCell.getDataValidation().setErrorMessage(j+"");
+                oldcell.getDataValidation().setErrorMessage(j+"");
+                sheet2.getMergedCells()[i].setText(j+"");
+                dataMap.put("y1", oldcell.getRow());
+                dataMap.put("y2", oldcell.getLastRow());
+                dataMap.put("x1", oldcell.getColumn());
+                dataMap.put("x2", oldcell.getLastColumn());
+                xyList.put(j + "", dataMap);
+            }
+
+            //变更最大值
+            if(maxVal<=0){
+                maxVal = j;
+            }
+
+            // 单个
+            CellRange[] onCell = sheet.getCells();
+            // 单个cell
+            for (int i = 0; i < onCell.length; i++) {
+                CellRange oldcell = onCell[i];
+                String data = oldcell.getDataValidation().getErrorMessage();
+                System.out.println(oldcell.getRow()+"---"+oldcell.getColumn()+"---="+oldcell.getText()+"---x="+data);
+                if (StringUtils.isEmpty(data)) {
+                    if(maxVal<=0){
+                        j = j + 1;
+                    }else{
+                        maxVal = maxVal+1;
+                        j=maxVal;
+                    }
+                } else {
+                    j = Func.toInt(data);
+                }
+
+              /*  if(StringUtils.isEmpty(data)){
+                    oldcell.getDataValidation().setErrorMessage(j+"");
+                    sheet2.getCells()[i].setText(j+"");
+                }*/
+
+                if(!xyList.containsKey(j+"")){
+                    Map<String, Integer> dataMap = new HashMap<>();
+                    dataMap.put("y1", oldcell.getRow());
+                    dataMap.put("y2", oldcell.getLastRow());
+                    dataMap.put("x1", oldcell.getColumn());
+                    dataMap.put("x2", oldcell.getLastColumn());
+                    xyList.put(j + "", dataMap);
+                }
+            }
+
+            // 保存上传的excel
+            wb.saveToFile(excelURL,FileFormat.Version2013);
+            sheet.saveToHtml(htmlUrl, options);
+            sheet2.saveToHtml(thmlUrl2);
+
+            // html 转换值
+
+            // 组装坐标
+            File html1 = new File(htmlUrl);  // 原始html
+            File html2 = new File(thmlUrl2); // 坐标html
+            InputStream inputStream1 = new FileInputStream(html1);
+            InputStream inputStream2 = new FileInputStream(html2);
+            String htmlString1 = IoUtil.readToString(inputStream1);
+            String htmlString2 = IoUtil.readToString(inputStream2);
+
+            Document doc1 = Jsoup.parse(htmlString1);
+            Element table1 = doc1.select("table").first();
+            Elements trs1 = table1.select("tr");
+            Document doc2 = Jsoup.parse(htmlString2);
+            Element table2 = doc2.select("table").first();
+            Elements trs2 = table2.select("tr");
+
+            for (int i = 0; i < trs1.size(); i++) {
+                Elements td1 = trs1.get(i).select("td");
+                Elements td2 = trs2.get(i).select("td");
+                for (int x = 0; x < td1.size(); x++) {
+                    Element cell1 = td1.get(x);
+                    Element cell2 = td2.get(x);
+                    String html = cell2.text();
+                    Map<String, Integer> xyMap = xyList.get(html);
+                    if (xyMap != null) {
+                        cell1.attr("x1", xyMap.get("x1") + "");
+                        cell1.attr("x2", xyMap.get("x2") + "");
+                        cell1.attr("y1", xyMap.get("y1") + "");
+                        cell1.attr("y2", xyMap.get("y2") + "");
+                        cell1.attr("exceVal",html);
+                    }
+                }
+            }
+
+            File writeFile = new File(htmlUrl);
+            FileUtil.writeToFile(writeFile, doc1.html(), Boolean.parseBoolean("UTF-8"));
+            if (html2.exists()) {
+                // html2.delete();
+            }
+            wb2.dispose();
+            wb.dispose();
+        }catch (Exception e){
+            e.printStackTrace();
+        }
+    }
+}

+ 1 - 1
blade-service/blade-repair/src/main/resources/application-dev.yml

@@ -1,6 +1,6 @@
 #服务器端口
 server:
-  port: 9876
+  port: 9119
 
 #数据源配置
 spring: