CaseDownService.java 21 KB

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