From 84d5ac6676d471073cc1054536f9ca12801ca86d Mon Sep 17 00:00:00 2001
From: 13693261870 <252740454@qq.com>
Date: 星期三, 17 四月 2024 17:32:22 +0800
Subject: [PATCH] 集成CAD预览功能

---
 src/main/java/com/lf/server/controller/data/MetaController.java |   36 ++++-
 src/main/java/com/lf/server/helper/AsyncHelper.java             |   63 ++++++++++
 src/main/java/com/lf/server/entity/all/StaticData.java          |    4 
 src/main/java/com/lf/server/service/show/CadService.java        |  201 +++++++++++++++++++++++++++++++++
 src/main/resources/application.yml                              |    3 
 5 files changed, 299 insertions(+), 8 deletions(-)

diff --git a/src/main/java/com/lf/server/controller/data/MetaController.java b/src/main/java/com/lf/server/controller/data/MetaController.java
index 2bb573d..4f97cef 100644
--- a/src/main/java/com/lf/server/controller/data/MetaController.java
+++ b/src/main/java/com/lf/server/controller/data/MetaController.java
@@ -22,13 +22,14 @@
 import com.lf.server.service.data.DownloadService;
 import com.lf.server.service.data.MetaService;
 import com.lf.server.service.data.VerService;
+import com.lf.server.service.show.CadService;
 import com.lf.server.service.sys.DepService;
 import com.lf.server.service.sys.DownlogService;
 import com.lf.server.service.sys.TokenService;
 import io.swagger.annotations.*;
-import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.web.bind.annotation.*;
 
+import javax.annotation.Resource;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 import java.io.File;
@@ -45,26 +46,29 @@
 @RestController
 @RequestMapping("/meta")
 public class MetaController extends BaseController {
-    @Autowired
+    @Resource
     MetaService metaService;
 
-    @Autowired
+    @Resource
     TokenService tokenService;
 
-    @Autowired
+    @Resource
     BaseQueryService baseQueryService;
 
-    @Autowired
+    @Resource
     DownlogService downlogService;
 
-    @Autowired
+    @Resource
     DownloadService downloadService;
 
-    @Autowired
+    @Resource
     VerService verService;
 
-    @Autowired
+    @Resource
     DepService depService;
+
+    @Resource
+    CadService cadService;
 
     @SysLog()
     @ApiOperation(value = "鍒嗛〉鏌ヨ骞惰繑鍥炶褰曟暟")
@@ -247,6 +251,22 @@
     }
 
     @SysLog()
+    @ApiOperation(value = "鏌ヨDwg杞崲")
+    @ApiImplicitParams({
+            @ApiImplicitParam(name = "id", value = "鍏冩暟鎹甀D", dataType = "int", paramType = "query", example = "7715")
+    })
+    @GetMapping(value = "/selectConvertToDwg")
+    public ResponseMsg<Object> selectConvertToDwg(int id) {
+        try {
+            String rs = cadService.convert(id);
+
+            return success(rs);
+        } catch (Exception ex) {
+            return fail(ex, null);
+        }
+    }
+
+    @SysLog()
     @ApiOperation(value = "鎻掑叆涓�鏉�")
     @ApiImplicitParams({
             @ApiImplicitParam(name = "entity", value = "瀹炰綋绫�", dataType = "MetaEntity", paramType = "body")
diff --git a/src/main/java/com/lf/server/entity/all/StaticData.java b/src/main/java/com/lf/server/entity/all/StaticData.java
index 9889518..e7db131 100644
--- a/src/main/java/com/lf/server/entity/all/StaticData.java
+++ b/src/main/java/com/lf/server/entity/all/StaticData.java
@@ -62,6 +62,8 @@
 
     public final static int I100 = 100;
 
+    public final static int I120 = 120;
+
     public static final int I180 = 180;
 
     public static final int I180_NEG = -180;
@@ -92,6 +94,8 @@
 
     public final static String IN = "in";
 
+    public final static String DWG = "dwg";
+
     public final static String ZIP = ".zip";
 
     public final static String XLS = ".xls";
diff --git a/src/main/java/com/lf/server/helper/AsyncHelper.java b/src/main/java/com/lf/server/helper/AsyncHelper.java
new file mode 100644
index 0000000..e50befd
--- /dev/null
+++ b/src/main/java/com/lf/server/helper/AsyncHelper.java
@@ -0,0 +1,63 @@
+package com.lf.server.helper;
+
+
+import com.lf.server.entity.all.StaticData;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
+import java.util.TimerTask;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.TimeUnit;
+
+/**
+ * 寮傛甯姪绫�
+ * @author WWW
+ * @date 2023-07-10
+ */
+public class AsyncHelper {
+    /**
+     * 鎿嶄綔寤惰繜10姣
+     */
+    private final static int OPERATE_DELAY_TIME = 10;
+
+    private final static Log log = LogFactory.getLog(AsyncHelper.class);
+
+    private ScheduledExecutorService executor = SpringContextHelper.getBean("scheduledExecutorService");
+
+    public AsyncHelper() {
+    }
+
+    /**
+     * 鎵ц浠诲姟
+     */
+    public void execute(TimerTask task) {
+        executor.schedule(task, OPERATE_DELAY_TIME, TimeUnit.MILLISECONDS);
+    }
+
+    /**
+     * 鍏抽棴浠诲姟
+     */
+    public void shutdown() {
+        shutdownAndAwaitTermination(executor);
+    }
+
+    /**
+     * 鍋滄浠诲姟绾跨▼姹�
+     */
+    public static void shutdownAndAwaitTermination(ExecutorService pool) {
+        try {
+            if (null == pool || pool.isShutdown()) {
+                return;
+            }
+
+            pool.shutdown();
+            if (!pool.awaitTermination(StaticData.I120, TimeUnit.SECONDS)) {
+                pool.shutdownNow();
+            }
+        } catch (InterruptedException ie) {
+            pool.shutdownNow();
+            Thread.currentThread().interrupt();
+        }
+    }
+}
diff --git a/src/main/java/com/lf/server/service/show/CadService.java b/src/main/java/com/lf/server/service/show/CadService.java
new file mode 100644
index 0000000..684d504
--- /dev/null
+++ b/src/main/java/com/lf/server/service/show/CadService.java
@@ -0,0 +1,201 @@
+package com.lf.server.service.show;
+
+import com.lf.server.entity.all.StaticData;
+import com.lf.server.entity.data.MetaEntity;
+import com.lf.server.helper.PathHelper;
+import com.lf.server.service.data.MetaService;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.stereotype.Service;
+import org.springframework.util.StringUtils;
+
+import javax.annotation.Resource;
+import java.io.*;
+
+/**
+ * CAD鏈嶅姟绫�
+ * @author WWW
+ * @date 2024-04-17
+ */
+@SuppressWarnings("ALL")
+@Service
+public class CadService {
+    @Value("${sys.cad.exePath}")
+    private String exePath;
+
+    @Value("${sys.cad.targetPath}")
+    private String targetPath;
+
+    @Resource
+    MetaService metaService;
+
+    @Resource
+    PathHelper pathHelper;
+
+    private final static Log log = LogFactory.getLog(CadService.class);
+
+    /**
+     * 鏍煎紡杞寲
+     */
+    public String convert(int metaId) {
+        MetaEntity me = metaService.selectById(metaId);
+        if (null == me || StringUtils.isEmpty(me.getPath()) || !StaticData.DWG.equals(me.getType())) {
+            return null;
+        }
+
+        String dwg = pathHelper.getConfig().getUploadPath() + File.separator + me.getPath();
+        File file = new File(dwg);
+        if (!file.exists() || file.isDirectory()) {
+            return null;
+        }
+
+        String[] strs = me.getPath().replace("\\", "/").split("/");
+        String outPath = targetPath + "/" + strs[0];
+        String outName = strs[1].replace(".dwg", ".mxweb");
+        file = new File(outPath + "/" + outName);
+        if (file.exists()) {
+            return strs[0] + "/" + outName;
+        }
+
+        if (callConvert(dwg, outPath, outName)) {
+            return strs[0] + "/" + outName;
+        }
+
+        return null;
+    }
+
+    public boolean callConvert(String dwgFile, String outPath, String outName) {
+        try {
+            String jsonParam = "{'srcpath':'" + dwgFile + "','outpath':'" + outPath + "','outname':'" + outName + "'}";
+            File f = new File(outPath);
+            if (!f.exists() || !f.isDirectory()) {
+                f.mkdirs();
+            }
+
+            StringBuilder sb = new StringBuilder();
+            Process process = Runtime.getRuntime().exec(new String[]{exePath, jsonParam});
+            BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
+
+            String line;
+            while ((line = reader.readLine()) != null) {
+                sb.append(line).append("\n");
+            }
+            reader.close();
+
+            return true;
+        } catch (Exception ex) {
+            log.error(ex.getMessage(), ex);
+            return false;
+        }
+    }
+
+    public boolean callConvert(String dwgFile) {
+        try {
+            StringBuilder sb = new StringBuilder();
+            String jsonParam = "{\"srcpath\":\"" + dwgFile + "\"}";
+            Process process = Runtime.getRuntime().exec(new String[]{exePath, jsonParam});
+            BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
+
+            String line;
+            while ((line = reader.readLine()) != null) {
+                sb.append(line).append("\n");
+            }
+            reader.close();
+
+            return true;
+        } catch (Exception ex) {
+            log.error(ex.getMessage(), ex);
+            return false;
+        }
+    }
+
+    @SuppressWarnings("AlibabaAvoidManuallyCreateThread")
+    public String callMxFileConvert(String sDwgFile) {
+        Runtime rn = Runtime.getRuntime();
+        Process process = null;
+
+        // 杞崲鍙傛暟銆�
+        String sJsonParam = "{\"srcpath\":\"" + sDwgFile + "\"}";
+        String[] sRetJson = new String[1];
+
+        try {
+            // 鍚姩涓�涓繘绋嬪簭锛岃皟鐢ㄨ浆鎹㈢▼搴忋��
+            process = rn.exec(new String[]{exePath, sJsonParam});
+            final InputStream ins = process.getInputStream();
+            final InputStream errs = process.getErrorStream();
+            //纭繚瀛愯繘绋嬩笌涓昏繘绋嬩箣闂磇nputStream涓嶉樆濉�
+            new Thread() {
+                @Override
+                public void run() {
+                    BufferedReader inb = null;
+                    String line = null;
+                    try {
+                        inb = new BufferedReader(new InputStreamReader(ins, "gbk"));
+                        while ((line = inb.readLine()) != null) {
+                            sRetJson[0] = line;
+                        }
+                    } catch (IOException e) {
+                        e.printStackTrace();
+                    } finally {
+                        try {
+                            if (null != inb) {
+                                inb.close();
+                            }
+                            if (null != ins) {
+                                ins.close();
+                            }
+                        } catch (IOException e) {
+                            e.printStackTrace();
+                        }
+                    }
+                }
+            }.start();
+
+            new Thread() {
+                @Override
+                public void run() {
+                    BufferedReader errb = null;
+                    String line = null;
+                    try {
+                        errb = new BufferedReader(new InputStreamReader(errs, "gbk"));
+                        while ((line = errb.readLine()) != null) {
+                            System.out.println("executeMxExe - ErrorStream : " + line);
+                        }
+                    } catch (IOException e) {
+                        e.printStackTrace();
+                    } finally {
+                        try {
+                            if (null != errb) {
+                                errb.close();
+                            }
+                            if (null != errs) {
+                                errs.close();
+                            }
+                        } catch (IOException e) {
+                            e.printStackTrace();
+                        }
+                    }
+                }
+            }.start();
+
+            process.waitFor();
+        } catch (Exception e) {
+            log.error(e.getMessage(), e);
+        } finally {
+            if (null != process) {
+                OutputStream out = process.getOutputStream();
+                if (null != out) {
+                    try {
+                        out.close();
+                    } catch (IOException e) {
+                        log.error(e.getMessage(), e);
+                    }
+                }
+                process.destroy();
+            }
+        }
+
+        return sRetJson[0];
+    }
+}
diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml
index 9d903ea..f0a7368 100644
--- a/src/main/resources/application.yml
+++ b/src/main/resources/application.yml
@@ -153,5 +153,8 @@
     upload: D:\LF\upload
     # 涓存椂鐩綍
     temp: D:\LF\temp
+  cad:
+    exePath: C:/360/MxDrawCloudServer1.0TryVersion/MxDrawCloudServer/Bin/MxCAD/Release/mxcadassembly.exe
+    targetPath: C:/360/MxDrawCloudServer1.0TryVersion/MxDrawCloudServer/SRC/TsWeb/public/data
   # 涓婁紶闄勪欢琛�
   attachTabs: bd.b_pac_hydrogeology,bd.b_pac_frozensoil,bd.b_pac_geologic_hazard,bd.b_pac_marine_meteorological,bd.b_pac_meteorological,bs.m_equipment_nameplate,bs.m_hydraulic_protection,bs.m_marker,bs.s_explorationpoint,bs.u_sectionline
\ No newline at end of file

--
Gitblit v1.9.3