CaseDownService.java 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. package com.fdkankan.fusion.down;
  2. import cn.hutool.core.io.FileUtil;
  3. import com.alibaba.fastjson.JSON;
  4. import com.alibaba.fastjson.JSONArray;
  5. import com.alibaba.fastjson.JSONObject;
  6. import com.deepoove.poi.XWPFTemplate;
  7. import com.fdkankan.fusion.common.FilePath;
  8. import com.fdkankan.fusion.common.ResultData;
  9. import com.fdkankan.fusion.common.util.MinIoUploadService;
  10. import com.fdkankan.fusion.common.util.ShellUtil;
  11. import com.fdkankan.fusion.common.util.UploadToOssUtil;
  12. import com.fdkankan.fusion.entity.*;
  13. import com.fdkankan.fusion.httpClient.LaserService;
  14. import com.fdkankan.fusion.httpClient.response.FdkkResponse;
  15. import com.fdkankan.fusion.request.CaseParam;
  16. import com.fdkankan.fusion.response.DownVo;
  17. import com.fdkankan.fusion.response.DownloadProcessVo;
  18. import com.fdkankan.fusion.response.FusionNumVo;
  19. import com.fdkankan.fusion.response.SceneVo;
  20. import com.fdkankan.fusion.service.*;
  21. import com.fdkankan.fusion.service.impl.DownService;
  22. import com.fdkankan.redis.util.RedisUtil;
  23. import lombok.extern.slf4j.Slf4j;
  24. import org.apache.commons.lang3.StringUtils;
  25. import org.springframework.beans.factory.annotation.Autowired;
  26. import org.springframework.beans.factory.annotation.Value;
  27. import org.springframework.scheduling.annotation.Async;
  28. import org.springframework.stereotype.Service;
  29. import java.io.File;
  30. import java.io.FileOutputStream;
  31. import java.nio.charset.StandardCharsets;
  32. import java.util.List;
  33. @Service
  34. @Slf4j
  35. public class CaseDownService {
  36. @Value("${server.servlet.context-path}")
  37. String basePath;
  38. public static String jsonDataName = "data.json";
  39. public static String caseInfo = "/case/getInfo?caseId=";
  40. public static String caseSettingsInfo = "/caseSettings/info?caseId=";
  41. public static String hostIcon = "/edit/hotIcon/list?caseId=";
  42. public static String caseView = "/caseView/allList?caseId=";
  43. public static String caseFiles = "/caseFiles/allList?caseId=";
  44. public static String caseFilesType = "/caseFilesType/allList?caseId=";
  45. public static String caseScene = "/case/sceneList?caseId=";
  46. public static String caseFusion = "/caseFusion/list?caseId=";
  47. public static String caseVideoFolder = "/caseVideoFolder/allList?caseId=";
  48. public static String caseVideo = "/caseVideo/allList?folderId=";
  49. public static String caseTag = "/caseTag/allList?caseId=";
  50. public static String caseTagPoint = "/caseTagPoint/allList?tagId=";
  51. public static String caseInquest = "/caseInquest/info?caseId=";
  52. public static String caseExtractDetail = "/caseExtractDetail/info?caseId=";
  53. public static String fusionGuide = "/fusionGuide/allList?caseId=";
  54. public static String fusionGuidePath = "/fusionGuidePath/allList?guideId=";
  55. public static String fusionMeter = "/fusionMeter/allList?fusionId=";
  56. public static String model = "/model/getInfo?modelId=";
  57. public static String caseImg = "/caseImg/getFfmpegImage?caseId=";
  58. public static String laserData = "/laser/dataset/%s/getDataSet";
  59. @Autowired
  60. ICaseService caseService;
  61. @Autowired
  62. ICaseSettingsService caseSettingsService;
  63. @Autowired
  64. IFusionNumService fusionNumService;
  65. @Autowired
  66. ICaseViewService caseViewService;
  67. @Autowired
  68. ICaseVideoFolderService caseVideoFolderService;
  69. @Autowired
  70. ICaseVideoService caseVideoService;
  71. @Autowired
  72. ICaseFilesService caseFilesService;
  73. @Autowired
  74. ICaseFilesTypeService caseFilesTypeService;
  75. @Autowired
  76. IHotIconService hotIconService;
  77. @Autowired
  78. ICaseTagService caseTagService;
  79. @Autowired
  80. ICaseTagPointService caseTagPointService;
  81. @Autowired
  82. IFusionGuideService fusionGuideService;
  83. @Autowired
  84. IFusionGuidePathService fusionGuidePathService;
  85. @Autowired
  86. ICaseInquestService caseInquestService;
  87. @Autowired
  88. ICaseExtractDetailService caseExtractDetailService;
  89. @Autowired
  90. IFusionMeterService fusionMeterService;
  91. @Autowired
  92. IModelService modelService;
  93. @Autowired
  94. LaserService laserService;
  95. @Autowired
  96. ICaseOfflineService caseOfflineService;
  97. @Autowired
  98. ICaseImgService caseImgService;
  99. @Autowired
  100. RedisUtil redisUtil;
  101. @Value("${spring.profiles.active}")
  102. private String environment;
  103. public static String downProcessKey = "fusion:down:offline:process:caseId:%s";
  104. public DownVo checkDown(Integer caseId) {
  105. DownVo downVo = new DownVo();
  106. // CaseOffline byCaseId = caseOfflineService.getByCaseId(caseId);
  107. // if(byCaseId != null){
  108. // downVo.setDownloadStatus(3);
  109. // downVo.setDownloadUrl(byCaseId.getOfflineUrl());
  110. // }
  111. return downVo;
  112. }
  113. @Async
  114. public void downOffline(Integer caseId){
  115. try {
  116. String redisKey = String.format(downProcessKey, caseId);
  117. if( redisUtil.hasKey(redisKey)){
  118. String res = redisUtil.get(redisKey);
  119. DownloadProcessVo downloadProcessVo = JSONObject.parseObject(res, DownloadProcessVo.class);
  120. if(downloadProcessVo.getPercent()== null || downloadProcessVo.getPercent() != 100){
  121. return;
  122. }
  123. }
  124. setRedisProcess(caseId,0);
  125. //复制前端资源
  126. cpIndexHtml(caseId);
  127. setRedisProcess(caseId,10);
  128. //创建data.json并下载资源
  129. createDataJson(caseId);
  130. setRedisProcess(caseId,50);
  131. //打包zip
  132. String path = "/mnt/fusion/offline";
  133. String name = "offline_"+ caseId;
  134. String zipName = path+File.separator+name+".zip";
  135. ShellUtil.zipOffline(zipName,name);
  136. setRedisProcess(caseId,70);
  137. //上传oss
  138. String ossUrl = zipName.replace("/mnt/", "");
  139. ShellUtil.yunUpload(zipName,ossUrl);
  140. Thread.sleep(2000L);
  141. FileUtil.del(zipName);
  142. ossUrl = queryPath + ossUrl;
  143. caseOfflineService.saveByCase(caseId,ossUrl);
  144. setRedisProcess(caseId,100,ossUrl);
  145. }catch (Exception e){
  146. log.info("down-offline-error:{}",caseId,e);
  147. setRedisProcess(caseId,0,null,1003);
  148. }
  149. }
  150. public void setRedisProcess(Integer caseId,Integer num){
  151. setRedisProcess(caseId,num,null,1000);
  152. }
  153. public void setRedisProcess(Integer caseId,Integer num,String url){
  154. setRedisProcess(caseId,num,url,1000);
  155. }
  156. public void setRedisProcess(Integer caseId,Integer num,String url,Integer status){
  157. String redisKey = String.format(downProcessKey, caseId);
  158. log.info("down-offline-process:{},{},{}",caseId,num,url);
  159. DownloadProcessVo processVo = new DownloadProcessVo();
  160. processVo.setStatus(status);
  161. processVo.setPercent(num);
  162. processVo.setUrl( url);
  163. redisUtil.set(redisKey,JSONObject.toJSONString(processVo));
  164. }
  165. public DownloadProcessVo process(Integer caseId) {
  166. DownloadProcessVo downVo = new DownloadProcessVo();
  167. String redisKey = String.format(downProcessKey, caseId);
  168. if(redisUtil.hasKey(redisKey)){
  169. return JSONObject.parseObject(redisUtil.get(redisKey),DownloadProcessVo.class);
  170. }
  171. return downVo;
  172. }
  173. public void createDataJson(Integer caseId){
  174. log.info("down-offline-createDataJson:{}",caseId);
  175. JSONObject jsonObject = new JSONObject();
  176. CaseParam param = new CaseParam();
  177. param.setCaseId(caseId);
  178. //设置案件信息
  179. jsonObject.put(basePath+caseInfo+caseId, ResultData.ok(caseService.getInfo(caseId)));
  180. List<CaseSettings> caseSettings = caseSettingsService.getByCaseId(caseId);
  181. jsonObject.put(basePath+caseSettingsInfo+caseId, ResultData.ok(caseSettings));
  182. for (CaseSettings caseSetting : caseSettings) {
  183. downResource(caseId,caseSetting.getBack());
  184. downResource(caseId,caseSetting.getCover());
  185. }
  186. List<FusionNumVo> listByCaseId = fusionNumService.getListByCaseId(caseId,null);
  187. jsonObject.put(basePath+caseFusion+caseId, ResultData.ok(listByCaseId));
  188. for (FusionNumVo fusion : listByCaseId) {
  189. jsonObject.put(basePath+fusionMeter+fusion.getFusionId(), ResultData.ok(fusionMeterService.getListByFusionId(fusion.getFusionId(),null)));
  190. }
  191. List<SceneVo> sceneVos = caseService.sceneList(param);
  192. for (SceneVo sceneData : sceneVos) {
  193. //下载模型
  194. if(StringUtils.isNotBlank(sceneData.getModelGlbUrl())){
  195. downModel(caseId,sceneData.getModelGlbUrl());
  196. }
  197. if(sceneData.getType() != 3){
  198. //下载场景离线包
  199. downSwkk(caseId,sceneData.getNum(),sceneData.getType());
  200. }
  201. if(sceneData.getType() == 2 || sceneData.getType() == 5){
  202. FdkkResponse sceneInfo = laserService.getSceneInfo(sceneData.getNum());
  203. if(sceneInfo != null){
  204. JSONArray jsonArray = JSONArray.parseArray(JSONArray.toJSONString(sceneInfo.getData()));
  205. JSONArray newJsonArray = new JSONArray();
  206. for (Object object : jsonArray) {
  207. JSONObject sceneInfoObj = (JSONObject) object;
  208. String newPath = String.format(FilePath.OFFLINE_LASER_OSS_PATH, sceneData.getNum(), sceneData.getNum());
  209. String oldPath = sceneInfoObj.getString("webBin");
  210. sceneInfoObj.put("oldWebBin",oldPath);
  211. sceneInfoObj.put("webBin",newPath + oldPath);
  212. newJsonArray.add(sceneInfoObj);
  213. }
  214. sceneInfo.setData(newJsonArray);
  215. jsonObject.put(String.format(laserData,sceneData.getNum()),sceneInfo);
  216. }
  217. }
  218. if(sceneData.getModelId() != null){
  219. jsonObject.put(basePath+model+sceneData.getModelId(), ResultData.ok(modelService.getInfo(sceneData.getModelId())));
  220. }
  221. }
  222. jsonObject.put(basePath+caseScene+caseId, ResultData.ok(sceneVos));
  223. List<CaseView> caseViews = caseViewService.allList(caseId, null, null, null, null);
  224. jsonObject.put(basePath+caseView+caseId, ResultData.ok(caseViews));
  225. for (CaseView view : caseViews) {
  226. downResource(caseId,view.getViewImg());
  227. downResource(caseId,view.getViewImgSmall());
  228. }
  229. List<CaseVideoFolder> videoFolders = caseVideoFolderService.getAllList(caseId);
  230. jsonObject.put(basePath+caseVideoFolder+caseId, ResultData.ok(videoFolders));
  231. for (CaseVideoFolder videoFolder : videoFolders) {
  232. downResource(caseId,videoFolder.getVideoFolderCover());
  233. downResource(caseId,videoFolder.getVideoMergeUrl());
  234. List<CaseVideo> allList = caseVideoService.getAllList(videoFolder.getVideoFolderId());
  235. for (CaseVideo video : allList) {
  236. downResource(caseId,video.getVideoCover());
  237. downResource(caseId,video.getVideoPath());
  238. }
  239. jsonObject.put(basePath+caseVideo+videoFolder.getVideoFolderId(), ResultData.ok(allList));
  240. }
  241. List<CaseFiles> caseFilesList = caseFilesService.allList(caseId, null);
  242. for (CaseFiles files : caseFilesList) {
  243. String fileUrl = String.format(FilePath.File_OSS_PATH,environment,files.getFilesId());
  244. downResource(caseId,queryPath +fileUrl);
  245. }
  246. jsonObject.put(basePath+caseFiles+caseId, ResultData.ok(caseFilesList));
  247. jsonObject.put(basePath+caseFilesType+caseId, ResultData.ok(caseFilesTypeService.list()));
  248. List<HotIcon> hotIconList = hotIconService.getListByCaseId(caseId);
  249. for (HotIcon hotIcon : hotIconList) {
  250. downResource(caseId,hotIcon.getIconUrl());
  251. }
  252. jsonObject.put(basePath+hostIcon+caseId, ResultData.ok(hotIconList));
  253. List<CaseTag> caseTagList = caseTagService.allList(caseId, null);
  254. jsonObject.put(basePath+caseTag+caseId, ResultData.ok(caseTagList));
  255. for (CaseTag tag : caseTagList) {
  256. jsonObject.put(basePath+caseTagPoint+tag.getTagId(), ResultData.ok(caseTagPointService.allList(tag.getTagId())));
  257. downResources(caseId,tag.getTagImgUrl());
  258. downResource(caseId,tag.getHotIconUrl());
  259. }
  260. List<FusionGuide> fusionGuides = fusionGuideService.getAllList(caseId);
  261. jsonObject.put(basePath+fusionGuide+caseId, ResultData.ok(fusionGuides));
  262. for (FusionGuide guide : fusionGuides) {
  263. downResource(caseId,guide.getCover());
  264. List<FusionGuidePath> listByGuideId = fusionGuidePathService.getListByGuideId(guide.getFusionGuideId());
  265. for (FusionGuidePath guidePath : listByGuideId) {
  266. downResource(caseId,guidePath.getCover());
  267. }
  268. jsonObject.put(basePath+fusionGuidePath+guide.getFusionGuideId(), ResultData.ok(listByGuideId));
  269. }
  270. CaseInquest caseInquest1 = caseInquestService.getByCaseId(caseId);
  271. if(caseInquest1 != null){
  272. XWPFTemplate inquestTemp = caseInquestService.getWordByTemplate(caseInquest1);
  273. downWordByTemplate(caseId,inquestTemp,"caseInquest.doc");
  274. }
  275. jsonObject.put(basePath+caseInquest+caseId, ResultData.ok(caseInquest1));
  276. CaseExtractDetail caseExtractDetail1 = caseExtractDetailService.getByCaseId(caseId);
  277. if(caseExtractDetail1 != null){
  278. XWPFTemplate detailTemp = caseExtractDetailService.getWordByTemplate(caseExtractDetail1);
  279. downWordByTemplate(caseId,detailTemp,"caseExtractDetail.doc");
  280. }
  281. jsonObject.put(basePath+caseExtractDetail+caseId, ResultData.ok(caseExtractDetail1));
  282. jsonObject.put(basePath+caseImg+caseId, ResultData.ok(caseImgService.getByCaseId(caseId,1)));
  283. FileUtil.writeString(JSON.toJSONString(jsonObject), FilePath.OFFLINE_PACKAGE_PATH+caseId+"/www/package/"+jsonDataName,"UTF-8");
  284. }
  285. //http://127.0.0.1:8080/offline.html?caseId=362&app=1&share=1#/show/summary
  286. static String batName = "start-browser.bat";
  287. public void cpIndexHtml(Integer caseId){
  288. log.info("down-offline-cpIndexHtml:{}",caseId);
  289. String caseOfflinePath = FilePath.OFFLINE_PACKAGE_PATH+caseId;
  290. FileUtil.copyContent(new File(FilePath.OFFLINE_TEMPLATE_PATH),new File(caseOfflinePath),true);
  291. String s = FileUtil.readString(caseOfflinePath + File.separator + batName, StandardCharsets.UTF_8);
  292. String s1 = s.replaceAll("@caseId", String.valueOf(caseId));
  293. FileUtil.writeString(s1, caseOfflinePath + File.separator + batName,"UTF-8");
  294. }
  295. @Autowired
  296. DownService downService;
  297. @Autowired
  298. UploadToOssUtil uploadToOssUtil;
  299. @Autowired
  300. MinIoUploadService minIoUploadService;
  301. public void downSwkk(Integer caseId,String num,Integer type){
  302. String swkkPath = FilePath.OFFLINE_PACKAGE_PATH+caseId + "/www/swkk/"+num;
  303. String swkkZipPath = swkkPath +".zip";
  304. String swssPath = FilePath.OFFLINE_PACKAGE_PATH+caseId + "/www/swss/"+num;
  305. String swssZipPath = swssPath +".zip";
  306. Integer isObj = 0;
  307. if(type == 4 || type == 6){
  308. isObj =1;
  309. }
  310. try {
  311. DownVo downVo = downService.checkDownLoad(num, isObj,"offline");
  312. log.info("down:{}",downVo);
  313. if(downVo.getDownloadStatus() == 3 && StringUtils.isNotBlank(downVo.getDownloadUrl())){
  314. downZip(type,downVo.getDownloadUrl(),swkkZipPath,swkkPath,swssZipPath,swssPath);
  315. }else {
  316. DownVo down = downService.down(num, isObj,"offline");
  317. if(down.getDownloadStatus() == 1){
  318. DownloadProcessVo downloadProcessVo = downService.downloadProcess(num, isObj);
  319. while (downloadProcessVo.getStatus() != 1002 ){
  320. downloadProcessVo = downService.downloadProcess(num, isObj);
  321. Thread.sleep(2000L);
  322. }
  323. downZip(type,downloadProcessVo.getUrl(),swkkZipPath,swkkPath,swssZipPath,swssPath);
  324. }
  325. }
  326. }catch (Exception e){
  327. log.info("下载场景离线包失败:{}",num,e);
  328. }
  329. }
  330. @Value("${upload.query-path}")
  331. private String queryPath;
  332. public void downZip(Integer type ,String uri,String kkzipPath,String kknumPath,String sszipPath,String ssNumPath){
  333. try {
  334. if(type != 2 && type != 5){ //深时点云
  335. if(uri.contains("?")){
  336. uri = uri.split("[?]")[0];
  337. }
  338. ShellUtil.yunDownload(uri.replace(queryPath, ""), kkzipPath);
  339. ShellUtil.unZip(kkzipPath,kknumPath);
  340. FileUtil.del(kkzipPath);
  341. }else {
  342. // InputStream in = minIoUploadService.getObject(uri);
  343. // FileOutputStream out = new FileOutputStream(sszipPath);
  344. // byte[] buffer = new byte[1024];
  345. // int bytesRead;
  346. // while ((bytesRead = in.read(buffer)) != -1) {
  347. // out.write(buffer, 0, bytesRead);
  348. // }
  349. // out.flush();
  350. // out.close();
  351. // in.close();
  352. ShellUtil.yunDownloadSs(uri.replace(queryPath, ""), sszipPath);
  353. ShellUtil.unZip(sszipPath,ssNumPath);
  354. FileUtil.del(sszipPath);
  355. }
  356. }catch (Exception e){
  357. log.info("下载场景离线包失败:{}",uri,e);
  358. }
  359. }
  360. public void downModel(Integer caseId, String modelGlbUrl) {
  361. String path = String.format(FilePath.OFFLINE_OSS_PATH,caseId);
  362. JSONArray jsonArray = JSONArray.parseArray(modelGlbUrl);
  363. for (Object object : jsonArray) {
  364. String res = (String) object;
  365. res = res.replace(queryPath, "");
  366. File file = new File(res);
  367. ShellUtil.yunDownload(file.getParentFile().getPath(), path +queryPath + file.getParentFile().getPath());
  368. }
  369. }
  370. public void downResources(Integer caseId,String urls) {
  371. JSONArray jsonArray = JSONArray.parseArray(urls);
  372. for (Object object : jsonArray) {
  373. String res = (String) object;
  374. downResource(caseId,res);
  375. }
  376. }
  377. public void downResource(Integer caseId,String url) {
  378. if(StringUtils.isBlank(url) ){
  379. return;
  380. }
  381. url = url.replace(queryPath, "");
  382. if( !uploadToOssUtil.existKey(url)){
  383. log.info("downResource文件不存在:{},{}",caseId,url);
  384. return;
  385. }
  386. String path = String.format(FilePath.OFFLINE_OSS_PATH,caseId);
  387. ShellUtil.yunDownload(url,path+queryPath+url);
  388. }
  389. public void downWordByTemplate(Integer caseId,XWPFTemplate template,String name){
  390. // 指定输出文件的路径
  391. String outputPath = String.format(FilePath.OFFLINE_RESOURCE_PACKAGE_PATH,caseId) + name;
  392. try {
  393. FileOutputStream out = new FileOutputStream(outputPath);
  394. template.write(out);
  395. out.close();
  396. log.info("文档已成功写入到: " + outputPath);
  397. } catch (Exception e) {
  398. log.info("写出文档失败:{},{}",caseId,name,e);
  399. }
  400. }
  401. }