package com.fdkankan.fusion.down; import cn.hutool.core.io.FileUtil; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.deepoove.poi.XWPFTemplate; import com.fdkankan.fusion.common.FilePath; import com.fdkankan.fusion.common.ResultCode; import com.fdkankan.fusion.common.ResultData; import com.fdkankan.fusion.common.util.DateUtils; import com.fdkankan.fusion.common.util.ShellUtil; import com.fdkankan.fusion.common.util.UploadToOssUtil; import com.fdkankan.fusion.config.CacheUtil; import com.fdkankan.fusion.entity.*; import com.fdkankan.fusion.exception.BusinessException; import com.fdkankan.fusion.httpClient.LaserService; import com.fdkankan.fusion.httpClient.response.FdkkResponse; import com.fdkankan.fusion.request.CaseParam; import com.fdkankan.fusion.response.*; import com.fdkankan.fusion.service.*; import com.fdkankan.fusion.service.impl.DownService; import com.fdkankan.redis.util.RedisUtil; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.scheduling.annotation.Async; import org.springframework.stereotype.Service; import java.io.File; import java.io.FileOutputStream; import java.net.URLDecoder; import java.net.URLEncoder; import java.nio.charset.StandardCharsets; import java.util.HashMap; import java.util.HashSet; import java.util.List; @Service @Slf4j public class CaseDownService { @Value("${server.servlet.context-path}") String basePath; public static String jsonDataName = "data.json"; public static String caseInfoUrl = "/case/getInfo?caseId="; public static String caseFilesTypeTreeUrl = "/caseFilesType/getByTree?caseId="; public static String sceneListUrl = "/case/sceneList?caseId="; public static String caseImgListUrl = "/caseImg/getFfmpegImage?caseId="; public static String caseFileOtherUrl = "/caseFiles/allList?caseId=%s&filesTypeId=6"; public static String caseVideoFolderUrl = "/caseVideoFolder/allList?caseId="; public static String caseFilesInfoUrl = "/fusion/caseFiles/info?filesId="; public static String fusionAndSceneTypeSceneUrl = "/case/getFusionAndScene?caseId=%s&type=scene"; public static String fusionAndSceneTypeFusionUrl = "/case/getFusionAndScene?caseId=%s&type=fusion"; public static String systemSettingListUrl = "/systemSetting/list"; public static String laserData = "/laser/dataset/%s/getDataSet"; public static String laserDataSetAndControlPoint = "/laser/4dage/%s/getDataSetAndControlPoint"; public static String laserDataQuery = "/laser/filter/%s/query?datasetId=%s"; @Autowired ICaseService caseService; @Autowired ICaseFilesTypeService caseFilesTypeService; @Autowired LaserService laserService; @Autowired FusionDownService fusionDownService; @Autowired IMapConfigService mapConfigService; @Autowired ICaseImgService caseImgService; @Autowired ICaseFilesService caseFilesService; @Autowired ICaseVideoFolderService caseVideoFolderService; @Autowired ISystemSettingService systemSettingService; @Autowired RedisUtil redisUtil; @Value("${spring.profiles.active}") private String environment; public static String downProcessKey = "fusion:down:offline:process:caseId:%s"; public DownVo checkDown(Integer caseId) { DownVo downVo = new DownVo(); // CaseOffline byCaseId = caseOfflineService.getByCaseId(caseId); // if(byCaseId != null){ // downVo.setDownloadStatus(3); // downVo.setDownloadUrl(byCaseId.getOfflineUrl()); // } return downVo; } @Async public void downOffline(Integer caseId,String fromRoute){ String caseOffPath = null; String zipName = null; try { String redisKey = String.format(downProcessKey, caseId); if( redisUtil.hasKey(redisKey)){ String res = redisUtil.get(redisKey); DownloadProcessVo downloadProcessVo = JSONObject.parseObject(res, DownloadProcessVo.class); if(downloadProcessVo.getStatus() != 1003 && (downloadProcessVo.getPercent()== null || downloadProcessVo.getPercent() != 100)){ return; } } String timeKey = DateUtils.dateStr(); caseOffPath =FilePath.OFFLINE_PACKAGE_PATH_CASE+timeKey+caseId ; setRedisProcess(caseId,0); //复制前端资源 cpIndexHtml(caseId,caseOffPath,fromRoute); setRedisProcess(caseId,10); //创建data.json并下载资源 createDataJson(caseId,caseOffPath+"/www"); setRedisProcess(caseId,50); //打包zip zipName = caseOffPath+".zip"; ShellUtil.zip(zipName,caseOffPath); setRedisProcess(caseId,70); //上传oss String ossUrl = zipName.replace("/mnt/fusion", "fusion/"+CacheUtil.environment); ShellUtil.yunUpload(zipName,ossUrl); ossUrl = queryPath + ossUrl; setRedisProcess(caseId,100,ossUrl); }catch (Exception e){ setRedisProcess(caseId,0,null,1003); log.info("down-offline-error:{}",caseId,e); }finally { delFile(caseOffPath); delFile(zipName); } } private void delFile(String path){ if(StringUtils.isNotBlank(path)){ try { Thread.sleep(2000L); FileUtil.del(path); }catch (Exception e){ log.info("删除文件失败:{}",path); } } } public void setRedisProcess(Integer caseId,Integer num){ setRedisProcess(caseId,num,null,1000); } public void setRedisProcess(Integer caseId,Integer num,String url){ setRedisProcess(caseId,num,url,1000); } public void setRedisProcess(Integer caseId,Integer num,String url,Integer status){ String redisKey = String.format(downProcessKey, caseId); log.info("down-offline-process:{},{},{}",caseId,num,url); DownloadProcessVo processVo = new DownloadProcessVo(); processVo.setStatus(status); processVo.setPercent(num); processVo.setUrl( url); redisUtil.set(redisKey,JSONObject.toJSONString(processVo)); } public DownloadProcessVo process(Integer caseId) { DownloadProcessVo downVo = new DownloadProcessVo(); String redisKey = String.format(downProcessKey, caseId); if(redisUtil.hasKey(redisKey)){ return JSONObject.parseObject(redisUtil.get(redisKey),DownloadProcessVo.class); } return downVo; } public void createDataJson(Integer caseId,String casePath){ log.info("down-offline-createDataJson:{}",caseId); JSONObject jsonObject = new JSONObject(); //设置案件信息 jsonObject.put(basePath+caseInfoUrl+caseId, ResultData.ok(caseService.getInfo(caseId,false))); List byTree = caseFilesTypeService.getByTree(caseId); for (CaseFilesTypeTreeVo caseFilesTypeTreeVo : byTree) { downTreeResource(caseFilesTypeTreeVo,casePath,jsonObject); } jsonObject.put(basePath+caseFilesTypeTreeUrl+caseId, ResultData.ok(byTree)); List caseImgList = caseImgService.getByCaseId(caseId, 1); for (CaseImg caseImg : caseImgList) { downResource(caseImg.getImgUrl(),casePath); } jsonObject.put(basePath+caseImgListUrl+caseId, ResultData.ok(caseImgList)); List files = caseFilesService.allList(caseId, 6); for (CaseFiles file : files) { //downResource(file.getFilesUrl(),casePath); downCaseFiles(file,casePath); } jsonObject.put(basePath+String.format(caseFileOtherUrl,caseId), ResultData.ok(files)); List allList = caseVideoFolderService.getAllList(caseId); for (CaseVideoFolder caseVideoFolder : allList) { downResource(caseVideoFolder.getVideoFolderCover(),casePath); downResource(caseVideoFolder.getVideoMergeUrl(),casePath); } jsonObject.put(basePath+caseVideoFolderUrl+caseId, ResultData.ok(allList)); jsonObject.put(basePath+systemSettingListUrl, ResultData.ok(systemSettingService.list())); CaseParam param = new CaseParam(); param.setCaseId(caseId); jsonObject.put(basePath+sceneListUrl+caseId, ResultData.ok(caseService.sceneList(param))); List sceneList = caseService.getFusionAndScene(caseId, "scene"); jsonObject.put(basePath+String.format(fusionAndSceneTypeSceneUrl,caseId), ResultData.ok(sceneList)); List fusionList = caseService.getFusionAndScene(caseId, "fusion"); jsonObject.put(basePath+String.format(fusionAndSceneTypeFusionUrl,caseId), ResultData.ok(fusionList)); for (FusionAndSceneVo fusionAndSceneVo : sceneList) { //下载场景 downSwkk(casePath,fusionAndSceneVo.getNum(),fusionAndSceneVo.getSceneType()); if(fusionAndSceneVo.getSceneType() == 2 || fusionAndSceneVo.getSceneType() == 5){ FdkkResponse sceneInfo = laserService.getSceneInfo(fusionAndSceneVo.getNum()); HashSet dataSetIds = new HashSet<>(); if(sceneInfo != null){ JSONArray jsonArray = JSONArray.parseArray(JSONArray.toJSONString(sceneInfo.getData())); JSONArray newJsonArray = new JSONArray(); for (Object object : jsonArray) { JSONObject sceneInfoObj = (JSONObject) object; String newPath = String.format(FilePath.OFFLINE_LASER_OSS_PATH, fusionAndSceneVo.getNum(), fusionAndSceneVo.getNum()); String oldPath = sceneInfoObj.getString("webBin"); sceneInfoObj.put("oldWebBin",oldPath); sceneInfoObj.put("webBin",newPath + oldPath); newJsonArray.add(sceneInfoObj); dataSetIds.add( sceneInfoObj.getString("id")); } sceneInfo.setData(newJsonArray); jsonObject.put(String.format(laserData,fusionAndSceneVo.getNum()),sceneInfo); if(!dataSetIds.isEmpty()){ for (String dataSetId : dataSetIds) { FdkkResponse sceneInfo2 = laserService.getSceneInfoQuery(fusionAndSceneVo.getNum(),dataSetId); jsonObject.put(String.format(laserDataQuery,fusionAndSceneVo.getNum(),dataSetId),sceneInfo2); } } } FdkkResponse dataSetAndControlPoint = laserService.getDataSetAndControlPoint(fusionAndSceneVo.getNum()); if(dataSetAndControlPoint !=null){ jsonObject.put(String.format(laserDataSetAndControlPoint,fusionAndSceneVo.getNum()),dataSetAndControlPoint); } } } for (FusionAndSceneVo fusionAndSceneVo : fusionList) { downFusion(casePath,fusionAndSceneVo.getFusionId()); } String jsonString = JSON.toJSONString(jsonObject); FileUtil.writeString(jsonString, casePath+"/package/"+jsonDataName,"UTF-8"); } private void downTreeResource(CaseFilesTypeTreeVo caseFilesTypeTreeVo,String casePath,JSONObject jsonObject) { if(caseFilesTypeTreeVo.getCaseFilesList() != null && !caseFilesTypeTreeVo.getCaseFilesList().isEmpty()){ for (CaseFiles caseFiles : caseFilesTypeTreeVo.getCaseFilesList()) { downCaseFiles(caseFiles,casePath); CaseFiles info = caseFilesService.info(caseFiles.getFilesId()); jsonObject.put(basePath+caseFilesInfoUrl+caseFiles.getFilesId(), ResultData.ok(info)); } } if(caseFilesTypeTreeVo.getChildrenList() != null && !caseFilesTypeTreeVo.getChildrenList().isEmpty()){ List childrenList = caseFilesTypeTreeVo.getChildrenList(); for (CaseFilesTypeTreeVo filesTypeTreeVo : childrenList) { downTreeResource(filesTypeTreeVo,casePath,jsonObject); } } } //http://127.0.0.1:8080/offline.html?caseId=362&app=1&share=1#/show/summary static String batName = "start-browser.bat"; public void cpIndexHtml(Integer caseId,String casePath,String fromRoute){ log.info("down-offline-cpIndexHtml:{}",caseId); FileUtil.copyContent(new File(FilePath.OFFLINE_TEMPLATE_PATH_CASE),new File(casePath),true); String s = FileUtil.readString(casePath + File.separator + batName, StandardCharsets.UTF_8); String s1 = s.replaceAll("@caseId", String.valueOf(caseId)); if(StringUtils.isBlank(fromRoute)){ fromRoute = "fire"; } s1 = s1.replaceAll("@fromRoute", fromRoute); FileUtil.writeString(s1, casePath + File.separator + batName,"UTF-8"); } @Autowired DownService downService; @Autowired UploadToOssUtil uploadToOssUtil; private void downFusion(String casePath,Integer fusionId) { String path = casePath +"/fusion_offline/"+fusionId; try { log.info("下载多元融合:{},{}",casePath,fusionId); fusionDownService.downOffline(fusionId,path,null); }catch (Exception e){ log.info("下载多元融合失败:{}",fusionId,e); throw new BusinessException(ResultCode.SCENE_DOWN_ERROR); } } public void downSwkk(String path,String num,Integer type){ String swkkPath = path + "/swkk/"+num; String swkkZipPath = swkkPath +".zip"; String swssPath = path + "/swss/"+num; String swssZipPath = swssPath +".zip"; Integer isObj = 0; if(type == 4 || type == 6){ isObj =1; } try { DownVo downVo = downService.checkDownLoad(num, isObj,"offline"); log.info("down:{}",downVo); if(downVo.getDownloadStatus() == 3 && StringUtils.isNotBlank(downVo.getDownloadUrl())){ downZip(type,downVo.getDownloadUrl(),swkkZipPath,swkkPath,swssZipPath,swssPath); }else { DownVo down = downService.down(num, isObj,"offline"); if(down.getDownloadStatus() == 1){ DownloadProcessVo downloadProcessVo = downService.downloadProcess(num, isObj); while (downloadProcessVo.getStatus() == 1000 || downloadProcessVo.getStatus() == 1001 ){ downloadProcessVo = downService.downloadProcess(num, isObj); Thread.sleep(2000L); } if(downloadProcessVo.getStatus() == 1003 && StringUtils.isBlank(downVo.getDownloadUrl())){ throw new BusinessException(ResultCode.SCENE_DOWN_ERROR); } downZip(type,downloadProcessVo.getUrl(),swkkZipPath,swkkPath,swssZipPath,swssPath); } } }catch (Exception e){ log.info("下载场景离线包失败:{}",num,e); throw new BusinessException(ResultCode.SCENE_DOWN_ERROR); } } @Value("${upload.query-path}") private String queryPath; @Value("${upload.query-path-ss}") private String ssQueryPath; public void downZip(Integer type ,String uri,String kkzipPath,String kknumPath,String sszipPath,String ssNumPath){ try { if(type != 2 && type != 5){ //深时点云 if(uri.contains("?")){ uri = uri.split("[?]")[0]; } ShellUtil.yunDownload(uri.replace(queryPath, ""), kkzipPath); ShellUtil.unZip(kkzipPath,kknumPath); FileUtil.del(kkzipPath); }else { ShellUtil.yunDownloadSs(uri.replace(ssQueryPath, ""), sszipPath); ShellUtil.unZip(sszipPath,ssNumPath); FileUtil.del(sszipPath); } }catch (Exception e){ log.info("下载场景离线包失败:{}",uri,e); } } public void downModel(Integer caseId, String modelGlbUrl) { String path = String.format(FilePath.OFFLINE_OSS_PATH,caseId); JSONArray jsonArray = JSONArray.parseArray(modelGlbUrl); for (Object object : jsonArray) { String res = (String) object; log.info("下载模型:{}",res); res = res.replace(queryPath, ""); if(res.contains(".json") ){ res = new File(res).getParentFile().getPath(); } if(!res.contains(".")){ res += "/"; } ShellUtil.yunDownload(res, path + "/" + res); } } public void downResources(String urls,String path) { JSONArray jsonArray = JSONArray.parseArray(urls); for (Object object : jsonArray) { String res = (String) object; downResource(res,path); } } public void downResource(String url,String path) { if(StringUtils.isBlank(url) ){ return; } url = url.replace(queryPath, ""); String desPath = path + "/" + url; log.info("downResource:{}",url); if(!desPath.contains(".")){ ShellUtil.yunDownload(url, new File(desPath).getParentFile().getPath()); return; } ShellUtil.yunDownload(url,desPath); } public void downCaseFiles(CaseFiles caseFiles,String path){ if(StringUtils.isBlank(caseFiles.getFilesUrl()) ){ return; } String url = caseFiles.getFilesUrl().replace(queryPath, ""); String desPath = path + "/" + url; log.info("downResource:{}",url); if(!desPath.contains(".")){ ShellUtil.yunDownload(url, new File(desPath).getParentFile().getPath()); return; } String filePath = String.format(FilePath.File_OSS_PATH, environment, caseFiles.getFilesId() + "/"); if(url.contains(filePath)){ ShellUtil.yunDownload(filePath, path +"/" + filePath); return; } ShellUtil.yunDownload(url,desPath); } }