FusionDownService.java 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  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.*;
  10. import com.fdkankan.fusion.config.CacheUtil;
  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.HashMap;
  32. import java.util.HashSet;
  33. import java.util.List;
  34. @Service
  35. @Slf4j
  36. public class FusionDownService {
  37. @Value("${server.servlet.context-path}")
  38. String basePath;
  39. public static String jsonDataName = "data.json";
  40. public static String caseFusionInfo = "/caseFusion/info?fusionId=";
  41. public static String caseFusionList= "/caseFusion/list?fusionId=";
  42. public static String mapConfig = "/notAuth/getMapConfig";
  43. public static String caseSettingsInfo = "/caseSettings/info?fusionId=";
  44. public static String hostIconTreeList = "/edit/hotIcon/treeList?fusionId=";
  45. public static String caseTag = "/caseTag/allList?fusionId=";
  46. public static String caseTagPoint = "/caseTagPoint/allList?tagId=";
  47. public static String fusionGuide = "/fusionGuide/allList?fusionId=";
  48. public static String fusionGuidePath = "/fusionGuidePath/allList?guideId=";
  49. public static String fusionMeter = "/fusionMeter/allList?fusionId=";
  50. public static String casePathInfoUrl = "/casePath/info?fusionId=";
  51. public static String caseAnimationUrl = "/caseAnimation/list?fusionId=";
  52. public static String laserData = "/laser/dataset/%s/getDataSet";
  53. public static String laserDataSetAndControlPoint = "/laser/4dage/%s/getDataSetAndControlPoint";
  54. public static String laserDataQuery = "/laser/filter/%s/query?datasetId=%s";
  55. @Autowired
  56. ICaseSettingsService caseSettingsService;
  57. @Autowired
  58. IFusionNumService fusionNumService;
  59. @Autowired
  60. ICaseViewService caseViewService;
  61. @Autowired
  62. ICaseVideoFolderService caseVideoFolderService;
  63. @Autowired
  64. ICaseVideoService caseVideoService;
  65. @Autowired
  66. ICaseFilesService caseFilesService;
  67. @Autowired
  68. ICaseFilesTypeService caseFilesTypeService;
  69. @Autowired
  70. IHotIconService hotIconService;
  71. @Autowired
  72. ICaseTagService caseTagService;
  73. @Autowired
  74. ICaseTagPointService caseTagPointService;
  75. @Autowired
  76. IFusionGuideService fusionGuideService;
  77. @Autowired
  78. IFusionGuidePathService fusionGuidePathService;
  79. @Autowired
  80. ICaseInquestService caseInquestService;
  81. @Autowired
  82. ICaseExtractDetailService caseExtractDetailService;
  83. @Autowired
  84. IFusionMeterService fusionMeterService;
  85. @Autowired
  86. IModelService modelService;
  87. @Autowired
  88. LaserService laserService;
  89. @Autowired
  90. ICaseOfflineService caseOfflineService;
  91. @Autowired
  92. ICaseImgService caseImgService;
  93. @Autowired
  94. ICasePathService casePathService;
  95. @Autowired
  96. ICaseFusionService caseFusionService;
  97. @Autowired
  98. ICaseAnimationService caseAnimationService;
  99. @Autowired
  100. IMapConfigService mapConfigService;
  101. @Autowired
  102. RedisUtil redisUtil;
  103. public static String downProcessKey = "fusion:down:offline:process:fusionId:%s";
  104. @Async
  105. public void downOffline(Integer fusionId){
  106. String caseOffPath = null;
  107. String zipName = null;
  108. try {
  109. String redisKey = String.format(downProcessKey, fusionId);
  110. if(redisUtil.hasKey(redisKey)){
  111. String res = redisUtil.get(redisKey);
  112. DownloadProcessVo downloadProcessVo = JSONObject.parseObject(res, DownloadProcessVo.class);
  113. if(downloadProcessVo.getStatus() != 1003 && (downloadProcessVo.getPercent()== null || downloadProcessVo.getPercent() != 100)){
  114. return;
  115. }
  116. }
  117. String timeKey = DateUtils.dateStr();
  118. caseOffPath =FilePath.OFFLINE_PACKAGE_PATH+timeKey+fusionId ;
  119. setRedisProcess(fusionId,0);
  120. //复制前端资源
  121. cpIndexHtml(fusionId,caseOffPath);
  122. setRedisProcess(fusionId,10);
  123. //创建data.json并下载资源
  124. createDataJson(fusionId,caseOffPath+"/www");
  125. //打包zip
  126. zipName = caseOffPath+".zip";
  127. ShellUtil.zip(zipName,caseOffPath);
  128. setRedisProcess(fusionId,70);
  129. //上传oss
  130. String ossUrl = zipName.replace("/mnt/", "");
  131. ShellUtil.yunUpload(zipName,ossUrl);
  132. ossUrl = queryPath + ossUrl;
  133. setRedisProcess(fusionId,100,ossUrl);
  134. }catch (Exception e){
  135. log.info("down-offline-error:{}",fusionId,e);
  136. setRedisProcess(fusionId,0,null,1003);
  137. }finally {
  138. delFile(caseOffPath);
  139. delFile(zipName);
  140. }
  141. }
  142. private void delFile(String path){
  143. if(StringUtils.isNotBlank(path)){
  144. try {
  145. FileUtil.del(path);
  146. }catch (Exception e){
  147. log.info("删除文件失败:{}",path);
  148. }
  149. }
  150. }
  151. public void setRedisProcess(Integer caseId,Integer num){
  152. setRedisProcess(caseId,num,null,1000);
  153. }
  154. public void setRedisProcess(Integer caseId,Integer num,String url){
  155. setRedisProcess(caseId,num,url,1000);
  156. }
  157. public void setRedisProcess(Integer caseId,Integer num,String url,Integer status){
  158. String redisKey = String.format(downProcessKey, caseId);
  159. log.info("down-offline-process:{},{},{}",caseId,num,url);
  160. DownloadProcessVo processVo = new DownloadProcessVo();
  161. processVo.setStatus(status);
  162. processVo.setPercent(num);
  163. processVo.setUrl( url);
  164. redisUtil.set(redisKey,JSONObject.toJSONString(processVo));
  165. }
  166. public DownloadProcessVo process(Integer caseId) {
  167. DownloadProcessVo downVo = new DownloadProcessVo();
  168. String redisKey = String.format(downProcessKey, caseId);
  169. if(redisUtil.hasKey(redisKey)){
  170. return JSONObject.parseObject(redisUtil.get(redisKey),DownloadProcessVo.class);
  171. }
  172. return downVo;
  173. }
  174. public void createDataJson(Integer fusionId,String caseOffPath){
  175. log.info("down-offline-createDataJson:{}",fusionId);
  176. JSONObject jsonObject = new JSONObject();
  177. CaseFusion caseFusion = caseFusionService.getById(fusionId);
  178. jsonObject.put(basePath+caseFusionInfo+fusionId, ResultData.ok(caseFusion));
  179. List<FusionNumVo> sceneListVo = caseFusionService.getSceneListVo(fusionId);
  180. List<MapConfig> list = mapConfigService.list();
  181. jsonObject.put(basePath+mapConfig, ResultData.ok(list));
  182. List<CaseSettings> caseSettings = caseSettingsService.getByFusionId(fusionId);
  183. jsonObject.put(basePath+caseSettingsInfo+fusionId, ResultData.ok(caseSettings));
  184. for (CaseSettings caseSetting : caseSettings) {
  185. downResource(caseSetting.getBack(),caseOffPath);
  186. downResource(caseSetting.getCover(),caseOffPath);
  187. }
  188. HashMap<String,String> repMap = new HashMap<>();
  189. jsonObject.put(basePath+fusionMeter+fusionId, ResultData.ok(fusionMeterService.getListByFusionId(fusionId,null)));
  190. List<FusionNumVo> fusionNumVos = caseFusionService.getSceneListVo(fusionId);
  191. for (FusionNumVo fusionNumVo : fusionNumVos) {
  192. SceneVo sceneData = fusionNumVo.getSceneData();
  193. //下载模型
  194. if(StringUtils.isNotBlank(sceneData.getModelGlbUrl())){
  195. downModel(sceneData.getModelGlbUrl(),caseOffPath);
  196. }
  197. if(sceneData.getType() != 3){
  198. //下载场景离线包
  199. repMap.put(sceneData.getCutModelPath(),sceneData.getCutModelPath().replace("/"+ CacheUtil.queryPath+"/","/swkk/"+sceneData.getNum() +"/wwwroot/"));
  200. repMap.put(sceneData.getSceneJsonPath(),sceneData.getSceneJsonPath().replace("/"+CacheUtil.queryPath+"/","/swkk/"+sceneData.getNum() +"/wwwroot/"));
  201. repMap.put(sceneData.getSurveillancePath(),sceneData.getSurveillancePath().replace("/"+CacheUtil.queryPath+"/","/swkk/"+sceneData.getNum() +"/wwwroot/"));
  202. downSwkk(caseOffPath,sceneData.getNum(),sceneData.getType());
  203. }
  204. if(NumTypeUtils.isLaser(sceneData.getType())){
  205. FdkkResponse sceneInfo = laserService.getSceneInfo(sceneData.getNum());
  206. HashSet<String> dataSetIds = new HashSet<>();
  207. if(sceneInfo != null){
  208. JSONArray jsonArray = JSONArray.parseArray(JSONArray.toJSONString(sceneInfo.getData()));
  209. JSONArray newJsonArray = new JSONArray();
  210. for (Object object : jsonArray) {
  211. JSONObject sceneInfoObj = (JSONObject) object;
  212. String newPath = String.format(FilePath.OFFLINE_LASER_OSS_PATH, sceneData.getNum(), sceneData.getNum());
  213. String oldPath = sceneInfoObj.getString("webBin");
  214. sceneInfoObj.put("oldWebBin",oldPath);
  215. sceneInfoObj.put("webBin",newPath + oldPath);
  216. newJsonArray.add(sceneInfoObj);
  217. dataSetIds.add( sceneInfoObj.getString("id"));
  218. }
  219. sceneInfo.setData(newJsonArray);
  220. jsonObject.put(String.format(laserData,sceneData.getNum()),sceneInfo);
  221. if(!dataSetIds.isEmpty()){
  222. for (String dataSetId : dataSetIds) {
  223. FdkkResponse sceneInfo2 = laserService.getSceneInfoQuery(sceneData.getNum(),dataSetId);
  224. jsonObject.put(String.format(laserDataQuery,sceneData.getNum(),dataSetId),sceneInfo2);
  225. }
  226. }
  227. }
  228. FdkkResponse dataSetAndControlPoint = laserService.getDataSetAndControlPoint(sceneData.getNum());
  229. if(dataSetAndControlPoint !=null){
  230. jsonObject.put(String.format(laserDataSetAndControlPoint,sceneData.getNum()),dataSetAndControlPoint);
  231. }
  232. }
  233. }
  234. jsonObject.put(basePath+caseFusionList+fusionId, ResultData.ok(sceneListVo));
  235. List<HotIcon> hotIconList = hotIconService.getListByFusionId(fusionId);
  236. for (HotIcon hotIcon : hotIconList) {
  237. downResource(hotIcon.getIconUrl(),caseOffPath);
  238. }
  239. // jsonObject.put(basePath+hostIcon+fusionId, ResultData.ok(hotIconList));
  240. List<HotIcon> treeList = hotIconService.treeList(hotIconList);
  241. jsonObject.put(basePath+hostIconTreeList+fusionId, ResultData.ok(treeList));
  242. List<CaseTag> caseTagList = caseTagService.getListByFusionId(fusionId);
  243. jsonObject.put(basePath+caseTag+fusionId, ResultData.ok(caseTagList));
  244. for (CaseTag tag : caseTagList) {
  245. jsonObject.put(basePath+caseTagPoint+tag.getTagId(), ResultData.ok(caseTagPointService.allList(tag.getTagId())));
  246. downResources(tag.getTagImgUrl(),caseOffPath);
  247. downResource(tag.getHotIconUrl(),caseOffPath);
  248. downResource(tag.getAudio(),caseOffPath);
  249. }
  250. jsonObject.put(basePath+casePathInfoUrl+fusionId, ResultData.ok(casePathService.getByFusionId(fusionId)));
  251. List<FusionGuide> fusionGuides = fusionGuideService.getByFusionId(fusionId);
  252. jsonObject.put(basePath+fusionGuide+fusionId, ResultData.ok(fusionGuides));
  253. for (FusionGuide guide : fusionGuides) {
  254. downResource(guide.getCover(),caseOffPath);
  255. List<FusionGuidePath> listByGuideId = fusionGuidePathService.getListByGuideId(guide.getFusionGuideId());
  256. for (FusionGuidePath guidePath : listByGuideId) {
  257. downResource(guidePath.getCover(),caseOffPath);
  258. }
  259. jsonObject.put(basePath+fusionGuidePath+guide.getFusionGuideId(), ResultData.ok(listByGuideId));
  260. }
  261. List<CaseAnimation> listByFusionId = caseAnimationService.getListByFusionId(fusionId);
  262. for (CaseAnimation caseAnimation : listByFusionId) {
  263. downModel(caseAnimation.getUrl(),caseOffPath);
  264. }
  265. jsonObject.put(basePath+caseAnimationUrl+fusionId, ResultData.ok(listByFusionId));
  266. String jsonString = jsonObject.toJSONString();
  267. if(!repMap.isEmpty()){
  268. for (String key : repMap.keySet()) {
  269. jsonString = jsonString.replace(key,repMap.get(key));
  270. }
  271. }
  272. FileUtil.writeString(jsonString, caseOffPath+"/www/package/"+jsonDataName,"UTF-8");
  273. }
  274. //http://127.0.0.1:8080/offline.html?caseId=362&app=1&share=1#/show/summary
  275. static String batName = "start-browser.bat";
  276. public void cpIndexHtml(Integer caseId,String caseOfflinePath){
  277. log.info("down-offline-cpIndexHtml:{}",caseId);
  278. FileUtil.copyContent(new File(FilePath.OFFLINE_TEMPLATE_PATH_Fusion),new File(caseOfflinePath),true);
  279. String s = FileUtil.readString(caseOfflinePath + File.separator + batName, StandardCharsets.UTF_8);
  280. s = s.replaceAll("@caseId", String.valueOf(caseId));
  281. FileUtil.writeString(s, caseOfflinePath + File.separator + batName,"UTF-8");
  282. }
  283. @Autowired
  284. DownService downService;
  285. @Autowired
  286. UploadToOssUtil uploadToOssUtil;
  287. public void downSwkk(String path,String num,Integer type){
  288. String swkkPath = path + "/swkk/"+num;
  289. String swkkZipPath = swkkPath +".zip";
  290. String swssPath = path + "/swss/"+num;
  291. String swssZipPath = swssPath +".zip";
  292. Integer isObj = 0;
  293. if(type == 4 || type == 6){
  294. isObj =1;
  295. }
  296. try {
  297. DownVo downVo = downService.checkDownLoad(num, isObj,"offline");
  298. log.info("down:{}",downVo);
  299. if(downVo.getDownloadStatus() == 3 && StringUtils.isNotBlank(downVo.getDownloadUrl())){
  300. downZip(type,downVo.getDownloadUrl(),swkkZipPath,swkkPath,swssZipPath,swssPath);
  301. }else {
  302. DownVo down = downService.down(num, isObj,"offline");
  303. if(down.getDownloadStatus() == 1){
  304. DownloadProcessVo downloadProcessVo = downService.downloadProcess(num, isObj);
  305. while (downloadProcessVo.getStatus() != 1002 ){
  306. downloadProcessVo = downService.downloadProcess(num, isObj);
  307. Thread.sleep(2000L);
  308. }
  309. downZip(type,downloadProcessVo.getUrl(),swkkZipPath,swkkPath,swssZipPath,swssPath);
  310. }
  311. }
  312. }catch (Exception e){
  313. log.info("下载场景离线包失败:{}",num,e);
  314. }
  315. }
  316. @Value("${upload.query-path}")
  317. private String queryPath;
  318. public void downZip(Integer type ,String uri,String kkzipPath,String kknumPath,String sszipPath,String ssNumPath){
  319. try {
  320. if(type != 2 && type != 5){ //深时点云
  321. if(uri.contains("?")){
  322. uri = uri.split("[?]")[0];
  323. }
  324. ShellUtil.yunDownload(uri.replace(queryPath, ""), kkzipPath);
  325. ShellUtil.unZip(kkzipPath,kknumPath);
  326. FileUtil.del(kkzipPath);
  327. }else {
  328. ShellUtil.yunDownloadSs(uri.replace(queryPath, ""), sszipPath);
  329. ShellUtil.unZip(sszipPath,ssNumPath);
  330. FileUtil.del(sszipPath);
  331. }
  332. }catch (Exception e){
  333. log.info("下载场景离线包失败:{}",uri,e);
  334. }
  335. }
  336. public void downModel( String modelGlbUrl,String path) {
  337. JSONArray jsonArray = JSONArray.parseArray(modelGlbUrl);
  338. for (Object object : jsonArray) {
  339. String res = (String) object;
  340. log.info("下载模型:{}",res);
  341. res = res.replace(queryPath, "");
  342. if(res.contains(".json") ){
  343. res = new File(res).getParentFile().getPath();
  344. }
  345. ShellUtil.yunDownload(res, path +"/"+ res);
  346. }
  347. }
  348. public void downResources(String urls,String offPath) {
  349. JSONArray jsonArray = JSONArray.parseArray(urls);
  350. for (Object object : jsonArray) {
  351. String res = (String) object;
  352. downResource(res,offPath);
  353. }
  354. }
  355. public void downResource(String url,String path) {
  356. if(StringUtils.isBlank(url) ){
  357. return;
  358. }
  359. url = url.replace(queryPath, "");
  360. if( !uploadToOssUtil.existKey(url)){
  361. log.info("downResource文件不存在:{},{}",url,path);
  362. return;
  363. }
  364. ShellUtil.yunDownload(url,path+"/"+url);
  365. }
  366. }