package com.fdkankan.scene.service.impl; import cn.hutool.core.collection.CollUtil; import cn.hutool.core.io.FileUtil; import cn.hutool.core.util.StrUtil; import cn.hutool.extra.qrcode.QrCodeUtil; import cn.hutool.extra.qrcode.QrConfig; import com.alibaba.fastjson.JSONObject; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.fdkankan.common.constant.SceneVersionType; import com.fdkankan.common.exception.BusinessException; import com.fdkankan.common.util.FileUtils; import com.fdkankan.common.util.SnowflakeIdGenerator; import com.fdkankan.fyun.face.FYunFileServiceInterface; import com.fdkankan.rabbitmq.util.RabbitMqProducer; import com.fdkankan.scene.entity.*; import com.fdkankan.scene.mq.consumer.SceneResourcePath; import com.fdkankan.scene.service.*; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.ObjectUtils; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; import javax.annotation.Resource; import java.io.File; import java.util.*; @Service @Slf4j public class SceneCopyServiceImpl implements ISceneCopyService { @Autowired ISceneProService sceneProService; @Autowired ISceneProEditService sceneProEditService; @Autowired IScenePlusService scenePlusService; @Autowired IScenePlusExtService scenePlusExtService; @Autowired ISceneEditInfoService sceneEditInfoService; @Autowired ISceneEditInfoExtService sceneEditInfoExtService; @Autowired ISceneEditControlsService sceneEditControlsService; @Autowired ISurveillanceService surveillanceService; @Autowired IFolderSceneService folderSceneService; @Autowired ILaserService laserService; @Resource FYunFileServiceInterface fYunFileServiceInterface; @Autowired RabbitMqProducer rabbitMqProducer; @Autowired ISceneMarkShapeService sceneMarkShapeService; @Override public void copyScene(String oldNum, String newNum) { ScenePro scenePro = sceneProService.getByNum(oldNum); if(scenePro != null && scenePro.getIsUpgrade() == 0){ cpV3(scenePro,oldNum,newNum); } ScenePlus scenePlus = scenePlusService.getScenePlusByNum(oldNum); if(scenePlus != null){ cpV4(scenePlus,oldNum,newNum); } } private void cpV4(ScenePlus scenePlus, String oldNum, String newNum) { try { Long plusId = scenePlus.getId(); ScenePlusExt plusExt = scenePlusExtService.getScenePlusExtByPlusId(plusId); if(plusExt == null){ return; } scenePlus.setNum(newNum); scenePlus.setTitle(scenePlus.getTitle() +"(copy)"); scenePlus.setSceneStatus(0); scenePlus.setId(null); scenePlusService.save(scenePlus); this.saveFolder(plusId,scenePlus.getId()); String oldDataSource = plusExt.getDataSource(); String newDataSource = this.getNewDataSource(oldDataSource); log.info("sceneCopy-V4-oldNum:{},oldDataSource:{},newNum:{},newDataSource:{}", oldNum,oldDataSource,newNum,newDataSource); String newVideos = plusExt.getVideos(); if(StrUtil.isNotEmpty(newVideos)){ newVideos = plusExt.getVideos().replaceAll("/data/data" + oldNum, "/scene_view_data/" + newNum + "/data").replaceAll(oldNum, newNum); } plusExt.setId(null); plusExt.setPlusId(scenePlus.getId()); plusExt.setDataSource(newDataSource); plusExt.setWebSite(plusExt.getWebSite().replace(oldNum, newNum)); plusExt.setThumb(plusExt.getThumb().replace(oldNum, newNum)); plusExt.setVideos(newVideos); plusExt.setViewCount(0); scenePlusExtService.save(plusExt); SceneEditInfo sceneEditInfo = sceneEditInfoService.getByScenePlusId(plusId); Long sceneEditInfoId = sceneEditInfo.getId(); sceneEditInfo.setId(null); sceneEditInfo.setScenePlusId(scenePlus.getId()); sceneEditInfo.setSceneProId(null); sceneEditInfo.setTitle(scenePlus.getTitle()); sceneEditInfoService.save(sceneEditInfo); SceneEditInfoExt sceneEditInfoExt = sceneEditInfoExtService.getByEditInfoId(sceneEditInfoId); sceneEditInfoExt.setId(null); sceneEditInfoExt.setEditInfoId(sceneEditInfo.getId()); sceneEditInfoExt.setScenePlusId(scenePlus.getId()); sceneEditInfoExt.setSceneProId(null); sceneEditInfoExtService.save(sceneEditInfoExt); SceneEditControls sceneEditControls = sceneEditControlsService.getBySceneEditId(sceneEditInfoId); sceneEditControls.setId(null); sceneEditControls.setEditInfoId(sceneEditInfo.getId()); sceneEditControlsService.save(sceneEditControls); List list = surveillanceService.list(new LambdaQueryWrapper().eq(Surveillance::getNum, oldNum)); if (!Objects.isNull(list)) { list.stream().forEach(item -> { item.setNum(newNum); item.setId(null); surveillanceService.save(item); }); } if(scenePlus.getSceneSource() == 4 || scenePlus.getSceneSource() == 5){ //深时复制 laserService.copy(oldNum,newNum,newDataSource,true); }else { laserService.cloudPointBuild(oldNum,newNum); } //重新生成编辑页基础设置二维码 this.createNewQrCode(SceneVersionType.V4.code(),sceneEditInfoExt.getShareLogoImg(),newNum,plusExt.getWebSite()); //copyDataSource //cn.hutool.core.io.FileUtil.copyContent(new File(oldDataSource),new File(newDataSource),true); this.copyOssAndNasV4(oldNum,newNum); this.copyMarkShape(oldNum,newNum); //修改 oss status.json ,nas scene.json String targetData = String.format(SceneResourcePath.DATA_VIEW_PATH,newNum); this.updateOssJson(targetData,oldNum,newNum,"status.json"); this.updateNasSceneJson(targetData,oldNum,newNum,scenePlus.getTitle(),"v4","scene.json"); if(scenePlus.getSceneSource() == 4 || scenePlus.getSceneSource() == 5){ //深时复制 laserService.copy(oldNum,newNum,newDataSource,false); }else { scenePlus.setSceneStatus(-2); scenePlusService.updateById(scenePlus); sendMq(oldNum,newNum); } }catch (Exception e){ log.error("copy-V4-error-oldNum:{},newNum:{}",oldNum,newNum,e); scenePlus.setSceneStatus(-1); scenePlusService.updateById(scenePlus); } } private void copyMarkShape(String oldNum, String newNum){ List sceneMarkShapes = sceneMarkShapeService.findByNum(oldNum); if(CollUtil.isEmpty(sceneMarkShapes)){ return; } sceneMarkShapes.stream().forEach(v->{ v.setId(null); v.setNum(newNum); v.setCreateTime(new Date()); v.setUpdateTime(null); }); sceneMarkShapeService.saveBatch(sceneMarkShapes); } private void cpV3(ScenePro scenePro ,String oldNum,String newNum) { try { Long sceneProId = scenePro.getId(); scenePro.setNum(newNum); scenePro.setId(null); scenePro.setSceneName(scenePro.getSceneName() +"(copy)"); scenePro.setViewCount(0); scenePro.setThumb(scenePro.getThumb().replaceAll(oldNum,scenePro.getNum())); scenePro.setWebSite(scenePro.getWebSite().replaceAll(oldNum,scenePro.getNum())); scenePro.setStatus(0); sceneProService.save(scenePro); this.saveFolder(sceneProId,scenePro.getId()); String oldDataSource = scenePro.getDataSource(); String newDataSource = this.getNewDataSource(oldDataSource); if(StringUtils.isBlank(newDataSource)){ log.info("cpv3-error-newDataSource为空:{}",newDataSource); return; } log.info("sceneCopy-v3-oldNum:{},oldDataSource:{},newNum:{},newDataSource:{}", oldNum,oldDataSource,newNum,newDataSource); scenePro.setDataSource(newDataSource); SceneProEdit oldEditScene = sceneProEditService.getByProId(sceneProId); oldEditScene.setId(null); oldEditScene.setProId(scenePro.getId()); oldEditScene.setScreencapVoiceSrc(oldEditScene.getScreencapVoiceSrc() == null ? null : oldEditScene.getScreencapVoiceSrc().replace(oldNum, scenePro.getNum())); oldEditScene.setScreencapVoiceSound(oldEditScene.getScreencapVoiceSound() == null ? null : oldEditScene.getScreencapVoiceSound().replace(oldNum, scenePro.getNum())); oldEditScene.setScreencapVoiceSoundsync(oldEditScene.getScreencapVoiceSoundsync() == null ? null : oldEditScene.getScreencapVoiceSoundsync().replace(oldNum, scenePro.getNum())); oldEditScene.setPlayData(oldEditScene.getPlayData() == null ? null : oldEditScene.getPlayData().replace(oldNum, scenePro.getNum())); oldEditScene.setScreencapThumb(oldEditScene.getScreencapThumb() == null ? null : oldEditScene.getScreencapThumb().replace(oldNum, scenePro.getNum())); oldEditScene.setFloorPlanPng(oldEditScene.getFloorPlanPng() == null ? null : oldEditScene.getFloorPlanPng().replace(oldNum, scenePro.getNum())); sceneProEditService.save(oldEditScene); if(scenePro.getSceneSource() == 4 || scenePro.getSceneSource() == 5) { //深时复制 laserService.copy(oldNum,newNum,newDataSource,true); } //重新生成编辑页基础设置二维码 this.createNewQrCode(SceneVersionType.V3.code(),oldEditScene.getShareLogo(),newNum,scenePro.getWebSite()); //copyDataSource // cn.hutool.core.io.FileUtil.copyContent(new File(oldDataSource),new File(newDataSource),true); String sourceData = String.format(SceneResourcePath.dataPath, oldNum); String targetData = String.format(SceneResourcePath.dataPath, scenePro.getNum()); this.copyOssAndNasV3(oldNum,scenePro.getNum(),sourceData,targetData); //修改 oss status.json ,nas scene.json this.updateOssJson(targetData,oldNum,newNum,"status.json"); this.updateOssJson(targetData,oldNum,newNum,"hot.json"); this.updateNasSceneJson(targetData,oldNum,newNum,scenePro.getSceneName(),"v3","scene.json"); this.updateNasSceneJson(targetData,oldNum,newNum,scenePro.getSceneName(),"v3","hot.json"); String sourceImages = String.format(SceneResourcePath.imagesPath, oldNum); String targetImages = String.format(SceneResourcePath.imagesPath, scenePro.getNum()); this.copyOssAndNasV3(oldNum,scenePro.getNum(),sourceImages,targetImages); String sourceVideo = String.format(SceneResourcePath.videoPath, oldNum); String targetVideo = String.format(SceneResourcePath.videoPath, scenePro.getNum()); this.copyOssAndNasV3(oldNum,scenePro.getNum(),sourceVideo,targetVideo); String sourceVoice = String.format(SceneResourcePath.voicePath, oldNum); String targetVoice = String.format(SceneResourcePath.voicePath, scenePro.getNum()); this.copyOssAndNasV3(oldNum,scenePro.getNum(),sourceVoice,targetVoice); if(scenePro.getSceneSource() == 4 || scenePro.getSceneSource() == 5){ //深时复制 laserService.copy(oldNum,newNum,newDataSource,false); }else { scenePro.setStatus(-2); sceneProService.updateById(scenePro); sendMq(oldNum,newNum); } }catch (Exception e){ log.info("cpv3-error:{},{}",oldNum,newNum,e); scenePro.setStatus(-1); sceneProService.updateById(scenePro); } } private void saveFolder(Long oldSceneId,Long newSceneId) { FolderScene folderScene = folderSceneService.getByType(oldSceneId, null); if(folderScene!= null){ folderScene.setId(null); folderScene.setSceneId(newSceneId); folderSceneService.save(folderScene); } } @Value("${queue.scene.copy.result:ucenter-copy-scene-result}") private String ucenterCpResultQueue; private void sendMq(String oldNum, String newNum) { HashMap map = new HashMap<>(); map.put("oldNum",oldNum); map.put("newNum",newNum); rabbitMqProducer.sendByWorkQueue(ucenterCpResultQueue,map); } public String getNewDataSource(String oldDataSource){ String newDataSource = null; if(StringUtils.isBlank(oldDataSource)){ log.info("oldDataSource为空:{}",oldDataSource); return null; } if(!oldDataSource.contains("/")){ log.info("oldDataSource格式错误:{}",oldDataSource); return null; } String time = com.fdkankan.common.util.DateUtil.date2String(new Date(), com.fdkankan.common.util.DateUtil.YYYYMMDDHHMMSSSSS_DATA_FORMAT); String[] split = oldDataSource.split("/"); if(split.length == 6 ){ String oldFileId = split[4]; Long fileId = new SnowflakeIdGenerator(1,1).nextId(); newDataSource = oldDataSource.replace(oldFileId,fileId.toString()); String snCodeTime = split[5]; if(!snCodeTime.contains("_") || snCodeTime.split("_").length <= 1){ log.info("oldDataSource格式错误:{}",oldDataSource); } newDataSource = newDataSource.replace(snCodeTime.split("_")[1],time); //this.copyFdage(oldDataSource,newDataSource,time); } if(newDataSource == null){ log.info("newDataSource格式错误:{}",newDataSource); } return newDataSource; } public void createNewQrCode(String sceneVersion,String logoPath ,String newNum, String webSite){ String localLogoPath = null; try { if(StringUtils.isNotBlank(logoPath)){ if(sceneVersion.equals(SceneVersionType.V3.code())){ localLogoPath = SceneResourcePath.nasBasePath + logoPath; }else{ localLogoPath = SceneResourcePath.qrCodeBasePath + newNum +"/logo/logo.png"; fYunFileServiceInterface.downloadFile(logoPath,localLogoPath); } } String outPathZh = SceneResourcePath.qrCodeBasePath + newNum + ".png"; String outPathEn = SceneResourcePath.qrCodeBasePath + newNum + "_en.png"; QrConfig qrConfig = QrConfig.create(); qrConfig.setWidth(1024); qrConfig.setHeight(1024); if(!ObjectUtils.isEmpty(localLogoPath)){ qrConfig.setImg(localLogoPath); } QrCodeUtil.generate(webSite, qrConfig, FileUtil.file(outPathZh)); QrCodeUtil.generate(webSite + "&lang=en", qrConfig, FileUtil.file(outPathEn)); fYunFileServiceInterface.uploadFile(outPathZh, String.format(SceneResourcePath.DOWNLOADS_QRCODE, newNum) + newNum + ".png"); fYunFileServiceInterface.uploadFile(outPathEn, String.format(SceneResourcePath.DOWNLOADS_QRCODE, newNum) + newNum + "_en.png"); }catch (Exception e){ log.info("copy-scene-error:{},newNum:{},error:{}",newNum,e); } } public void updateNasSceneJson(String targetPath, String oldNum, String newNum,String newSceneName,String sceneVersion,String fileName) { String fileContent = null; if("v3".equals(sceneVersion)){ String localPath = SceneResourcePath.nasBasePath + targetPath + "/" + fileName; File file = new File(localPath); if(!file.exists()){ log.error("sceneCopy-error--localFileExist:localPath:{},oldNum:{},newNum:{}",localPath,oldNum,newNum); return; } fileContent = FileUtil.readUtf8String(file); } if("v4".equals(sceneVersion)){ String ossStatusJsonPath = targetPath + "/" + fileName; if(!fYunFileServiceInterface.fileExist(ossStatusJsonPath)){ log.error("sceneCopy-error--ossFileExist:targetPath:{},oldNum:{},newNum:{}",ossStatusJsonPath,oldNum,newNum); return; } fileContent = fYunFileServiceInterface.getFileContent(ossStatusJsonPath); } if(StringUtils.isNotBlank(fileContent)){ //v3编辑器使用 String localPath = SceneResourcePath.nasBasePath + targetPath +"/" + fileName; File file = new File(localPath); if(!file.getParentFile().exists()){ file.getParentFile().mkdirs(); } String newJson = fileContent.replaceAll(oldNum,newNum); try { if("v3".equals(sceneVersion)){ if(fileName.contains("scene.json")){ JSONObject jsonObject = JSONObject.parseObject(newJson); jsonObject.put("sceneName",newSceneName); FileUtils.writeFile(localPath ,jsonObject.toJSONString()); String sceneJsonPath = String.format(SceneResourcePath.dataPath+"/"+fileName, newNum); fYunFileServiceInterface.uploadFile(localPath, sceneJsonPath); }else { FileUtils.writeFile(localPath ,newJson); } } if("v4".equals(sceneVersion)){ JSONObject jsonObject = JSONObject.parseObject(newJson); jsonObject.put("title",newSceneName); jsonObject.put("dynamicPanel",0); FileUtils.writeFile(localPath, jsonObject.toJSONString()); String sceneJsonPath = String.format(SceneResourcePath.DATA_VIEW_PATH+"/" + fileName, newNum); fYunFileServiceInterface.uploadFile(localPath, sceneJsonPath); //修改图片名称 String filePath = String.format(SceneResourcePath.USER_VIEW_PATH, newNum) ; List files = fYunFileServiceInterface.listRemoteFiles(filePath); for (String ossFilePath : files) { if(ossFilePath.contains(oldNum)){ String oldName = ossFilePath; ossFilePath = ossFilePath.replace(oldNum,newNum); fYunFileServiceInterface.copyFileInBucket(oldName,ossFilePath); fYunFileServiceInterface.deleteFile(oldName); } } String dynamicViewPath = String.format(SceneResourcePath.DYNAMIC_VIEW_PATH, newNum); String dynamicEditPath = String.format(SceneResourcePath.DYNAMIC_EDIT_PATH, newNum); if(fYunFileServiceInterface.fileExist(dynamicViewPath)){ fYunFileServiceInterface.deleteFile(dynamicViewPath); } if(fYunFileServiceInterface.fileExist(dynamicEditPath)){ fYunFileServiceInterface.deleteFile(dynamicEditPath); } } }catch (Exception e){ log.error("writeFile-error:{}",e); } } } public void updateOssJson(String targetPath,String oldNum, String newNum,String fileName) { String ossStatusJsonPath = targetPath + "/" + fileName; if(!fYunFileServiceInterface.fileExist(ossStatusJsonPath)){ log.error("sceneCopy-error--ossFileExist:targetPath:{},oldNum:{},newNum:{}",ossStatusJsonPath,oldNum,newNum); return; } String localPath = SceneResourcePath.nasBasePath + ossStatusJsonPath; File file = new File(localPath); if(!file.getParentFile().exists()){ file.getParentFile().mkdirs(); } String fileContent = fYunFileServiceInterface.getFileContent(ossStatusJsonPath); if(StringUtils.isNotBlank(fileContent)){ String newJson = fileContent.replaceAll(oldNum,newNum); try { FileUtils.writeFile(localPath, newJson); fYunFileServiceInterface.uploadFile(localPath,ossStatusJsonPath); }catch (Exception e){ log.error("writeFile-error:{}",e); } } } private void copyOssAndNasV3(String oldNum ,String newNum ,String sourcePath,String targetPath){ log.info("sceneCopy-ossSource-oldNum:{},newNum:{},sourcePath:{},targetPath:{}",oldNum,newNum,sourcePath,targetPath); fYunFileServiceInterface.copyFileInBucket(sourcePath,targetPath); File fileData = new File(SceneResourcePath.nasBasePath + sourcePath); if(fileData.exists()){ if(targetPath.contains("images")){ this.delLink(fileData.getPath()); } cn.hutool.core.io.FileUtil.copyContent(fileData,new File(SceneResourcePath.nasBasePath + targetPath),true); } } private void copyOssAndNasV4(String oldNum,String newNum){ // 拷贝场景编辑资源 String oldEditPath = String.format(SceneResourcePath.EDIT_PATH_v4, oldNum); String newEditPath = String.format(SceneResourcePath.EDIT_PATH_v4, newNum); fYunFileServiceInterface.copyFileInBucket(oldEditPath, newEditPath); // 拷贝场景展示资源 String oldViewPath = String.format(SceneResourcePath.VIEW_PATH_v4, oldNum); String newViewPath = String.format(SceneResourcePath.VIEW_PATH_v4, newNum); fYunFileServiceInterface.copyFileInBucket(oldViewPath, newViewPath); //复制计算结果文件 String oldResultPath = String.format(SceneResourcePath.SCENE_RESULT_DATA_PATH, oldNum); String newResultPath = String.format(SceneResourcePath.SCENE_RESULT_DATA_PATH, newNum); fYunFileServiceInterface.copyFileInBucket(oldResultPath, newResultPath); // String oldPath = SceneResourcePath.nasBasePath + oldNum; // String newPath = SceneResourcePath.nasBasePath + newNum; // if(new File(oldPath).exists()){ // FileUtil.copyContent(new File(oldPath), new File(newPath),true); // } String oldPath_v4 = SceneResourcePath.nasBasePath_v4 + oldNum; String newPath_v4 = SceneResourcePath.nasBasePath_v4 + newNum; if(new File(oldPath_v4).exists()){ FileUtil.copyContent(new File(oldPath_v4), new File(newPath_v4),true); } } public void delLink(String path) { String panPath = path +"/panorama"; File file = new File(panPath); if(file.exists()){ File[] files = file.listFiles(); if(files == null || files.length == 0){ return; } for (File file1 : files) { String linkPath =file1.getPath() + "/capture"; log.info("delLink--filePath:{}",linkPath); org.apache.commons.io.FileUtils.deleteQuietly(new File(linkPath)); } } } }