CaseDownService.java 20 KB

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