CaseDownService.java 19 KB

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