Explorar o código

新建微服务repair:专门用来定时检测数据并修复

chenr hai 3 meses
pai
achega
9e096634ee

+ 11 - 0
blade-service-api/blade-manager-api/src/main/java/org/springblade/manager/feign/ExcelTabClient.java

@@ -7,8 +7,10 @@ import org.springblade.core.tool.api.R;
 import org.springblade.manager.entity.ExcelTab;
 import org.springframework.cloud.openfeign.FeignClient;
 import org.springframework.web.bind.annotation.*;
+import org.springframework.web.multipart.MultipartFile;
 
 import java.io.IOException;
+import java.io.InputStream;
 import java.util.List;
 
 import static org.springblade.core.launch.constant.AppConstant.APPLICATION_NAME_PREFIX;
@@ -73,4 +75,13 @@ public interface ExcelTabClient {
 
     @GetMapping(API_PREFIX + "/pre-buss-pdfs")
     R getPdfS(@RequestParam String nodeId, @RequestParam String classify, @RequestParam String contractId) throws IOException;
+
+    @PostMapping(API_PREFIX + "/saveOrUpdate")
+    void saveOrUpdate(@RequestBody ExcelTab detail);
+
+    @PostMapping(API_PREFIX + "/expailHtmlInfo")
+    void expailHtmlInfo(@RequestParam String thmlUrl, @RequestParam Long id, @RequestParam String s) throws Exception;
+
+    @PostMapping(API_PREFIX + "/excelInfo")
+    void excelInfo(@RequestParam InputStream inputStream, @RequestParam String exceUrl, @RequestParam String thmlUrl, @RequestParam String number);
 }

+ 19 - 0
blade-service-api/blade-manager-api/src/main/java/org/springblade/manager/feign/ExcelTabClientFallBack.java

@@ -10,8 +10,10 @@ import org.springblade.manager.entity.ExcelTab;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.jdbc.core.JdbcTemplate;
 import org.springframework.stereotype.Component;
+import org.springframework.web.multipart.MultipartFile;
 
 import java.io.IOException;
+import java.io.InputStream;
 import java.util.List;
 
 @Component
@@ -73,4 +75,21 @@ public class ExcelTabClientFallBack implements ExcelTabClient {
     public R getPdfS(String nodeId, String classify, String contractId) throws IOException {
         return null;
     }
+
+    @Override
+    public void saveOrUpdate(ExcelTab detail) {
+
+    }
+
+    @Override
+    public void expailHtmlInfo(String thmlUrl, Long id, String s) throws Exception {
+
+    }
+
+    @Override
+    public void excelInfo(InputStream inputStream, String exceUrl, String thmlUrl, String number) {
+
+    }
+
+
 }

+ 19 - 0
blade-service/blade-manager/src/main/java/org/springblade/manager/feign/ExcelTabClientImpl.java

@@ -35,6 +35,7 @@ import org.springblade.manager.service.IContractInfoService;
 import org.springblade.manager.service.IExcelTabService;
 import org.springblade.manager.service.IProjectInfoService;
 import org.springblade.manager.service.IWbsTreePrivateService;
+import org.springblade.manager.utils.ExcelInfoUtils;
 import org.springblade.manager.utils.FileUtils;
 import org.springblade.manager.utils.RandomNumberHolder;
 import org.springblade.manager.vo.TrialSelfDataRecordVO1;
@@ -43,6 +44,7 @@ import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.jdbc.core.BeanPropertyRowMapper;
 import org.springframework.jdbc.core.JdbcTemplate;
 import org.springframework.web.bind.annotation.RestController;
+import org.springframework.web.multipart.MultipartFile;
 
 import javax.imageio.ImageIO;
 import java.awt.*;
@@ -71,6 +73,7 @@ public class ExcelTabClientImpl implements ExcelTabClient {
     private final IProjectInfoService projectInfoService;
     private final IContractInfoService contractInfoService;
     private final EntrustInfoServiceClient entrustInfoServiceClient;
+
     @Override
     public ExcelTab getById(String id) {
         return this.excelTabService.getById(id);
@@ -477,6 +480,22 @@ public class ExcelTabClientImpl implements ExcelTabClient {
         return excelTabController.getPdfS(nodeId, classify, contractId);
     }
 
+    @Override
+    public void saveOrUpdate(ExcelTab detail) {
+        excelTabService.saveOrUpdate(detail);
+    }
+
+    @Override
+    public void expailHtmlInfo(String thmlUrl, Long id, String s) throws Exception {
+        excelTabController.expailHtmlInfo(thmlUrl, id, s);
+    }
+
+    @Override
+    public void excelInfo(InputStream inputStream, String exceUrl, String thmlUrl, String number) {
+        ExcelInfoUtils.excelInfo(inputStream, exceUrl, thmlUrl, number);
+    }
+
+
     private String getHtmlString(String pkeyId) throws Exception {
         WbsTreePrivate wbsTreePrivate = wbsTreePrivateMapper.selectOne(Wrappers.<WbsTreePrivate>query().lambda()
                 .select(WbsTreePrivate::getHtmlUrl)

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

@@ -0,0 +1,69 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+    <modelVersion>4.0.0</modelVersion>
+    <parent>
+        <groupId>org.springblade</groupId>
+        <artifactId>BladeX</artifactId>
+        <version>2.9.1.RELEASE</version>
+        <relativePath>../../pom.xml</relativePath>
+    </parent>
+
+    <artifactId>blade-repair</artifactId>
+
+    <properties>
+        <maven.compiler.source>8</maven.compiler.source>
+        <maven.compiler.target>8</maven.compiler.target>
+        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+    </properties>
+    <dependencies>
+        <dependency>
+            <groupId>org.springblade</groupId>
+            <artifactId>blade-core-launch</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.springblade</groupId>
+            <artifactId>blade-core-boot</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.springblade</groupId>
+            <artifactId>blade-common</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.springblade</groupId>
+            <artifactId>blade-core-cloud</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.springblade</groupId>
+            <artifactId>blade-manager-api</artifactId>
+            <version>2.9.1.RELEASE</version>
+        </dependency>
+        <dependency>
+            <groupId>org.springblade</groupId>
+            <artifactId>blade-manager-api</artifactId>
+            <version>2.9.1.RELEASE</version>
+            <scope>compile</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.springblade</groupId>
+            <artifactId>blade-starter-oss</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.springblade</groupId>
+            <artifactId>blade-resource-api</artifactId>
+            <version>2.9.1.RELEASE</version>
+            <scope>compile</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.springblade</groupId>
+            <artifactId>blade-system-api</artifactId>
+            <version>2.9.1.RELEASE</version>
+            <scope>compile</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.springframework</groupId>
+            <artifactId>spring-test</artifactId>
+        </dependency>
+    </dependencies>
+</project>

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

@@ -0,0 +1,18 @@
+package org.springblade;
+
+import org.springblade.common.constant.LauncherConstant;
+import org.springblade.core.cloud.feign.EnableBladeFeign;
+import org.springblade.core.launch.BladeApplication;
+import org.springframework.cache.annotation.EnableCaching;
+import org.springframework.cloud.client.SpringCloudApplication;
+import org.springframework.scheduling.annotation.EnableAsync;
+
+@EnableBladeFeign
+@SpringCloudApplication
+@EnableAsync
+@EnableCaching
+public class RepairApplication {
+    public static void main(String[] args) {
+        BladeApplication.run(LauncherConstant.APPLICATION_METER_NAME, RepairApplication.class, args);
+    }
+}

+ 134 - 0
blade-service/blade-repair/src/main/java/org/springblade/repair/controller/CheckAndRepairController.java

@@ -0,0 +1,134 @@
+package org.springblade.repair.controller;
+
+import io.swagger.annotations.Api;
+import io.swagger.annotations.ApiOperation;
+import lombok.AllArgsConstructor;
+import org.springblade.common.utils.CommonUtil;
+import org.springblade.common.utils.SnowFlakeUtil;
+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.repair.util.FileUtils;
+import org.springblade.resource.feign.NewIOSSClient;
+import org.springframework.jdbc.core.BeanPropertyRowMapper;
+import org.springframework.jdbc.core.JdbcTemplate;
+import org.springframework.mock.web.MockMultipartFile;
+import org.springframework.scheduling.annotation.Scheduled;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RequestParam;
+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.IOException;
+import java.io.InputStream;
+import java.nio.file.Files;
+import java.util.List;
+
+
+@RestController
+@AllArgsConstructor
+@RequestMapping("/checkAndRepair")
+@Api(value = "checkAndRepair", tags = "定时自动检测修复")
+public class CheckAndRepairController {
+   private final JdbcTemplate jdbcTemplate;
+    private final ExcelTabClient excelTabClient;
+    private final NewIOSSClient newIOSSClient;
+
+    /**
+     * 每天晚上1点检查excelHtml是否存在,如果不存在就下载excel表格然后重新上传
+     */
+    @RequestMapping("/checkAndRepairExcelHtml")
+    @ApiOperation("定时检测修复ExcelHtml")
+    @Scheduled(cron = "0  15 16 * * ?")
+    public void checkAndRepairExcelHtml() {
+        StringBuilder result=new StringBuilder("");
+        StringBuilder result1=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\\";
+        String excelPath="/mnt/sdc/Users/hongchuangyanfa/Desktop/excel/";
+        try {
+            for (ExcelTab excelTab : query) {
+                String sql1 = "Select * from m_excel_tab where parent_id=" + excelTab.getId() + " and is_deleted=0";
+                List<ExcelTab> query1 = jdbcTemplate.query(sql1, new BeanPropertyRowMapper<>(ExcelTab.class));
+                for (ExcelTab tab : query1) {
+                    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) {
+                        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){
+                                    //先下载这个文件,再上传
+                                    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();
+                                        }
+                                    }
+                                }else {
+                                    result.append(excelTab.getName() + "--" + tab.getName() + "--" + excelTab1.getName()+"\n");
+                                }
+                            }
+                        }
+                    }
+                }
+            }
+        } catch (Exception e) {
+            throw new RuntimeException(e);
+        }
+        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 R putFileAttach(@RequestParam("file") MultipartFile file, Long nodeId) throws Exception {
+        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");
+        // 上传excel文件
+        BladeFile bladeFile = newIOSSClient.uploadFile(file.getOriginalFilename(), exceUrl);
+        if (bladeFile == null || ObjectUtil.isEmpty(bladeFile)) {
+            return R.fail("oss上传失败,请校验oss配置是否正确");
+        }
+        // 解析原始excel
+        detail.setExtension(file.getOriginalFilename());
+        detail.setFileUrl(bladeFile.getLink());
+        detail.setFileType(3); // 表示为清表信息  1 表示祖节点  2 表示为节点信息 3 表示清表
+        detail.setHtmlUrl(thmlUrl);
+        excelTabClient.saveOrUpdate(detail);
+        // 解析html
+        excelTabClient.expailHtmlInfo(thmlUrl, detail.getId(), detail.getTabType() + "");
+        return R.success("上传成功");
+    }
+
+    /**
+     * 每周天晚上2点定时更新private和contract的html,通过is_private_type_id;
+     */
+    public void checkAndRepairPrivateAndContractHtml() {}
+}

+ 107 - 0
blade-service/blade-repair/src/main/java/org/springblade/repair/util/FileUtils.java

@@ -0,0 +1,107 @@
+package org.springblade.repair.util;
+
+import org.springblade.common.constant.CommonConstant;
+import org.springblade.common.utils.CommonUtil;
+import org.springblade.common.utils.SystemUtils;
+import org.springblade.system.cache.ParamCache;
+
+import java.io.*;
+
+public class FileUtils {
+    // 获取本地 或 远程工作流ParamCache
+    public static InputStream getInputStreamByUrl(String fileUrl) throws Exception {
+        File file1 = new File(fileUrl);
+        InputStream fileInputStream = null;
+        if (file1.exists()) {
+            fileInputStream = new FileInputStream(file1);
+        } else {
+            String path = getNetUrl(fileUrl);
+            fileInputStream = CommonUtil.getOSSInputStream(path);
+        }
+        return fileInputStream;
+    }
+
+    //根据流存储文件到本地
+    public static void saveFile(InputStream inputStream, String filePath) {
+
+        String localFilePath = filePath; // 本地文件路径
+
+        try (
+            FileOutputStream outputStream = new FileOutputStream(localFilePath)) {
+
+            byte[] buffer = new byte[1024];
+            int length;
+
+            while ((length = inputStream.read(buffer)) > 0) {
+                outputStream.write(buffer, 0, length);
+            }
+
+            System.out.println("文件下载成功,保存路径: " + localFilePath);
+
+        } catch (IOException e) {
+            e.printStackTrace();
+            System.err.println("文件下载失败: " + e.getMessage());
+        }
+
+    }
+
+    public static String getNetUrl(String fileUrl) {
+        String file_path = ParamCache.getValue(CommonConstant.SYS_LOCAL_URL);
+        String file_path2 = getSysLocalFileUrl();
+        String sys_file_net_url = ParamCache.getValue(CommonConstant.SYS_FILE_NET_URL);
+        String sys_isonline = ParamCache.getValue(CommonConstant.SYS_ISONLINE);
+        if(fileUrl.indexOf("aliyuncs.com") >= 0 || fileUrl.indexOf("xinan1.zos.ctyun.cn")>=0 || fileUrl.indexOf("/mnt/sdc/Users/hongchuangyanfa/Desktop/")>=0){
+            if(fileUrl.indexOf("/mnt/sdc/Users/hongchuangyanfa/Desktop/")>=0){
+                if(SystemUtils.isWindows() || SystemUtils.isMacOs()){
+                    file_path2 = file_path;
+                }else{
+                    return sys_file_net_url + fileUrl.replaceAll("//", "/").replaceAll(file_path2, "");
+                }
+            }else{
+                return fileUrl;
+            }
+        } else {
+            if("20".equals(sys_isonline)){
+                file_path2 = file_path;
+            }
+        }
+        String s1 = fileUrl.replaceAll("//", "/").replace("///","/");
+        file_path2= file_path2.replaceAll("//","/").replaceAll("///","/");
+        String s2= s1.replaceAll(file_path2, "");
+        String path = sys_file_net_url + s2;
+        return path;
+    }
+    public static String getSysLocalFileUrl() {
+        String file_path = ParamCache.getValue(CommonConstant.SYS_LOCAL_URL);
+        String sys_isonline = ParamCache.getValue(CommonConstant.SYS_ISONLINE);
+
+        if (sys_isonline.equals("1")) { //正式环境
+            if (SystemUtils.isMacOs()) {
+                file_path = "/Users/hongchuangyanfa/Desktop/";
+            } else if (SystemUtils.isWindows()) {
+                file_path = "C://upload//";
+            }
+        } else if (sys_isonline.equals("2")) { //109测试环境
+            if (SystemUtils.isMacOs()) {
+                file_path = "/www/wwwroot/Users/hongchuangyanfa/Desktop/";
+            } else if (SystemUtils.isWindows()) {
+                file_path = "C://upload//";
+            }
+        } else if (sys_isonline.equals("20")) { //183
+            if (SystemUtils.isLinux()) {
+                file_path = "/home/www/wwwroot/Users/hongchuangyanfa/Desktop/";
+            } else if (SystemUtils.isMacOs()) {
+                file_path = "/Users/hongchuangyanfa/Desktop/";
+            } else if (SystemUtils.isWindows()) {
+                file_path = "C://upload//";
+            }
+        } else {  //本地环境
+            if (SystemUtils.isMacOs()) {
+                file_path = "/Users/hongchuangyanfa/Desktop/";
+            } else if (SystemUtils.isWindows()) {
+                file_path = "C://upload//";
+            }
+        }
+        return file_path;
+    }
+}

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

@@ -0,0 +1,25 @@
+#服务器端口
+server:
+  port: 9876
+
+#数据源配置
+spring:
+  datasource:
+    url: ${blade.datasource.dev.url}
+    username: ${blade.datasource.dev.username}
+    password: ${blade.datasource.dev.password}
+
+
+#oss:
+#  enabled: true
+#  name: alioss
+#  tenant-mode: true
+#  endpoint: https://oss-cn-chengdu.aliyuncs.com/
+#  access-key: LTAI4FmSV1pWZAJ9xSvHg5rP
+#  secret-key: 5D3XQj4pBe8VbOAVFNqdioJA8riH4S
+#  bucket-name: bladex-test-info
+
+#Mybatis-plus配置
+#mybatis-plus:
+#  configuration:
+#    cache-enabled: true

+ 11 - 0
blade-service/blade-repair/src/main/resources/application-prod.yml

@@ -0,0 +1,11 @@
+#服务器端口
+server:
+  port: 9876
+
+#数据源配置
+spring:
+  datasource:
+    url: ${blade.datasource.prod.url}
+    username: ${blade.datasource.prod.username}
+    password: ${blade.datasource.prod.password}
+

+ 10 - 0
blade-service/blade-repair/src/main/resources/application-test.yml

@@ -0,0 +1,10 @@
+#服务器端口
+server:
+  port: 9876
+
+#数据源配置
+spring:
+  datasource:
+    url: ${blade.datasource.test.url}
+    username: ${blade.datasource.test.username}
+    password: ${blade.datasource.test.password}

+ 1 - 0
pom.xml

@@ -40,6 +40,7 @@
         <module>blade-plugin-api</module>
         <module>blade-service</module>
         <module>blade-service-api</module>
+        <module>blade-service/blade-repair</module>
     </modules>
 
     <dependencyManagement>