SceneCopyServiceImpl.java 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. package com.fdkankan.scene.service.impl;
  2. import cn.hutool.core.collection.CollUtil;
  3. import cn.hutool.core.io.FileUtil;
  4. import cn.hutool.core.util.StrUtil;
  5. import cn.hutool.extra.qrcode.QrCodeUtil;
  6. import cn.hutool.extra.qrcode.QrConfig;
  7. import com.alibaba.fastjson.JSONObject;
  8. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  9. import com.fdkankan.common.constant.SceneSource;
  10. import com.fdkankan.common.constant.SceneVersionType;
  11. import com.fdkankan.common.exception.BusinessException;
  12. import com.fdkankan.common.util.FileUtils;
  13. import com.fdkankan.common.util.SnowflakeIdGenerator;
  14. import com.fdkankan.fyun.face.FYunFileServiceInterface;
  15. import com.fdkankan.rabbitmq.util.RabbitMqProducer;
  16. import com.fdkankan.scene.entity.*;
  17. import com.fdkankan.scene.mq.consumer.SceneResourcePath;
  18. import com.fdkankan.scene.service.*;
  19. import lombok.extern.slf4j.Slf4j;
  20. import org.apache.commons.lang3.ObjectUtils;
  21. import org.apache.commons.lang3.StringUtils;
  22. import org.springframework.beans.factory.annotation.Autowired;
  23. import org.springframework.beans.factory.annotation.Value;
  24. import org.springframework.stereotype.Service;
  25. import javax.annotation.Resource;
  26. import java.io.File;
  27. import java.util.*;
  28. @Service
  29. @Slf4j
  30. public class SceneCopyServiceImpl implements ISceneCopyService {
  31. @Autowired
  32. ISceneProService sceneProService;
  33. // @Autowired
  34. // ISceneProEditService sceneProEditService;
  35. @Autowired
  36. IScenePlusService scenePlusService;
  37. @Autowired
  38. IScenePlusExtService scenePlusExtService;
  39. @Autowired
  40. ISceneEditInfoService sceneEditInfoService;
  41. @Autowired
  42. ISceneEditInfoExtService sceneEditInfoExtService;
  43. @Autowired
  44. ISceneEditControlsService sceneEditControlsService;
  45. @Autowired
  46. ISurveillanceService surveillanceService;
  47. // @Autowired
  48. // IFolderSceneService folderSceneService;
  49. @Autowired
  50. ILaserService laserService;
  51. @Resource
  52. FYunFileServiceInterface fYunFileServiceInterface;
  53. @Autowired
  54. RabbitMqProducer rabbitMqProducer;
  55. @Autowired
  56. ISceneMarkShapeService sceneMarkShapeService;
  57. @Override
  58. public void copyScene(String oldNum, String newNum) {
  59. ScenePlus scenePlus = scenePlusService.getScenePlusByNum(oldNum);
  60. if(scenePlus != null){
  61. cpV4(scenePlus,oldNum,newNum);
  62. }
  63. }
  64. private void cpV4(ScenePlus scenePlus, String oldNum, String newNum) {
  65. try {
  66. Long plusId = scenePlus.getId();
  67. ScenePlusExt plusExt = scenePlusExtService.getScenePlusExtByPlusId(plusId);
  68. if(plusExt == null){
  69. return;
  70. }
  71. scenePlus.setNum(newNum);
  72. scenePlus.setTitle(scenePlus.getTitle() +"(copy)");
  73. scenePlus.setSceneStatus(0);
  74. scenePlus.setId(null);
  75. scenePlusService.save(scenePlus);
  76. String oldDataSource = plusExt.getDataSource();
  77. String newDataSource = this.getNewDataSource(oldDataSource);
  78. log.info("sceneCopy-V4-oldNum:{},oldDataSource:{},newNum:{},newDataSource:{}", oldNum,oldDataSource,newNum,newDataSource);
  79. String newVideos = plusExt.getVideos();
  80. if(StrUtil.isNotEmpty(newVideos)){
  81. newVideos = plusExt.getVideos().replaceAll("/data/data" + oldNum, "/scene_view_data/" + newNum + "/data").replaceAll(oldNum, newNum);
  82. }
  83. plusExt.setId(null);
  84. plusExt.setPlusId(scenePlus.getId());
  85. plusExt.setDataSource(newDataSource);
  86. plusExt.setWebSite(plusExt.getWebSite().replace(oldNum, newNum));
  87. plusExt.setThumb(plusExt.getThumb().replace(oldNum, newNum));
  88. plusExt.setVideos(newVideos);
  89. plusExt.setViewCount(0);
  90. scenePlusExtService.save(plusExt);
  91. SceneEditInfo sceneEditInfo = sceneEditInfoService.getByScenePlusId(plusId);
  92. Long sceneEditInfoId = sceneEditInfo.getId();
  93. sceneEditInfo.setId(null);
  94. sceneEditInfo.setScenePlusId(scenePlus.getId());
  95. sceneEditInfo.setSceneProId(null);
  96. sceneEditInfo.setTitle(scenePlus.getTitle());
  97. sceneEditInfoService.save(sceneEditInfo);
  98. SceneEditInfoExt sceneEditInfoExt = sceneEditInfoExtService.getByEditInfoId(sceneEditInfoId);
  99. sceneEditInfoExt.setId(null);
  100. sceneEditInfoExt.setEditInfoId(sceneEditInfo.getId());
  101. sceneEditInfoExt.setScenePlusId(scenePlus.getId());
  102. sceneEditInfoExt.setSceneProId(null);
  103. sceneEditInfoExtService.save(sceneEditInfoExt);
  104. SceneEditControls sceneEditControls = sceneEditControlsService.getBySceneEditId(sceneEditInfoId);
  105. sceneEditControls.setId(null);
  106. sceneEditControls.setEditInfoId(sceneEditInfo.getId());
  107. sceneEditControlsService.save(sceneEditControls);
  108. List<Surveillance> list = surveillanceService.list(new LambdaQueryWrapper<Surveillance>().eq(Surveillance::getNum, oldNum));
  109. if (!Objects.isNull(list)) {
  110. list.stream().forEach(item -> {
  111. item.setNum(newNum);
  112. item.setId(null);
  113. surveillanceService.save(item);
  114. });
  115. }
  116. if(scenePlus.getSceneSource() == 4 || scenePlus.getSceneSource() == 5 || scenePlus.getSceneSource() == 7){ //深时复制
  117. laserService.copy(oldNum,newNum,newDataSource,true);
  118. }else {
  119. laserService.cloudPointBuild(oldNum,newNum);
  120. }
  121. //重新生成编辑页基础设置二维码
  122. this.createNewQrCode(SceneVersionType.V4.code(),sceneEditInfoExt.getShareLogoImg(),newNum,plusExt.getWebSite());
  123. //copyDataSource
  124. //cn.hutool.core.io.FileUtil.copyContent(new File(oldDataSource),new File(newDataSource),true);
  125. this.copyOssAndNasV4(oldNum,newNum);
  126. // this.copyMarkShape(oldNum,newNum);
  127. //修改 oss status.json ,nas scene.json
  128. String targetData = String.format(SceneResourcePath.DATA_VIEW_PATH,newNum);
  129. this.updateOssJson(targetData,oldNum,newNum,"status.json");
  130. this.updateNasSceneJson(targetData,oldNum,newNum,scenePlus.getTitle(),"v4","scene.json");
  131. if(scenePlus.getSceneSource() == 4 || scenePlus.getSceneSource() == 5 || scenePlus.getSceneSource() == 7){ //深时复制
  132. laserService.copy(oldNum,newNum,newDataSource,false);
  133. }else {
  134. scenePlus.setSceneStatus(-2);
  135. scenePlusService.updateById(scenePlus);
  136. }
  137. }catch (Exception e){
  138. log.error("copy-V4-error-oldNum:{},newNum:{}",oldNum,newNum,e);
  139. scenePlus.setSceneStatus(-1);
  140. scenePlusService.updateById(scenePlus);
  141. }
  142. }
  143. @Value("${queue.scene.copy.result:ucenter-copy-scene-result}")
  144. private String ucenterCpResultQueue;
  145. private void sendMq(String oldNum, String newNum) {
  146. HashMap<String,Object> map = new HashMap<>();
  147. map.put("oldNum",oldNum);
  148. map.put("newNum",newNum);
  149. rabbitMqProducer.sendByWorkQueue(ucenterCpResultQueue,map);
  150. }
  151. public String getNewDataSource(String oldDataSource){
  152. String newDataSource = null;
  153. if(StringUtils.isBlank(oldDataSource)){
  154. log.info("oldDataSource为空:{}",oldDataSource);
  155. return null;
  156. }
  157. if(!oldDataSource.contains("/")){
  158. log.info("oldDataSource格式错误:{}",oldDataSource);
  159. return null;
  160. }
  161. String time = com.fdkankan.common.util.DateUtil.date2String(new Date(), com.fdkankan.common.util.DateUtil.YYYYMMDDHHMMSSSSS_DATA_FORMAT);
  162. String[] split = oldDataSource.split("/");
  163. if(split.length == 6 ){
  164. String oldFileId = split[4];
  165. Long fileId = new SnowflakeIdGenerator(1,1).nextId();
  166. newDataSource = oldDataSource.replace(oldFileId,fileId.toString());
  167. String snCodeTime = split[5];
  168. if(!snCodeTime.contains("_") || snCodeTime.split("_").length <= 1){
  169. log.info("oldDataSource格式错误:{}",oldDataSource);
  170. }
  171. newDataSource = newDataSource.replace(snCodeTime.split("_")[1],time);
  172. //this.copyFdage(oldDataSource,newDataSource,time);
  173. }
  174. if(newDataSource == null){
  175. log.info("newDataSource格式错误:{}",newDataSource);
  176. }
  177. return newDataSource;
  178. }
  179. public void createNewQrCode(String sceneVersion,String logoPath ,String newNum, String webSite){
  180. String localLogoPath = null;
  181. try {
  182. if(StringUtils.isNotBlank(logoPath)){
  183. if(sceneVersion.equals(SceneVersionType.V3.code())){
  184. localLogoPath = SceneResourcePath.nasBasePath + logoPath;
  185. }else{
  186. localLogoPath = SceneResourcePath.qrCodeBasePath + newNum +"/logo/logo.png";
  187. fYunFileServiceInterface.downloadFile(logoPath,localLogoPath);
  188. }
  189. }
  190. String outPathZh = SceneResourcePath.qrCodeBasePath + newNum + ".png";
  191. String outPathEn = SceneResourcePath.qrCodeBasePath + newNum + "_en.png";
  192. QrConfig qrConfig = QrConfig.create();
  193. qrConfig.setWidth(1024);
  194. qrConfig.setHeight(1024);
  195. if(!ObjectUtils.isEmpty(localLogoPath)){
  196. qrConfig.setImg(localLogoPath);
  197. }
  198. QrCodeUtil.generate(webSite, qrConfig, FileUtil.file(outPathZh));
  199. QrCodeUtil.generate(webSite + "&lang=en", qrConfig, FileUtil.file(outPathEn));
  200. fYunFileServiceInterface.uploadFile(outPathZh, String.format(SceneResourcePath.DOWNLOADS_QRCODE, newNum) + newNum + ".png");
  201. fYunFileServiceInterface.uploadFile(outPathEn, String.format(SceneResourcePath.DOWNLOADS_QRCODE, newNum) + newNum + "_en.png");
  202. }catch (Exception e){
  203. log.info("copy-scene-error:{},newNum:{},error:{}",newNum,e);
  204. }
  205. }
  206. public void updateNasSceneJson(String targetPath, String oldNum, String newNum,String newSceneName,String sceneVersion,String fileName) {
  207. String fileContent = null;
  208. if("v3".equals(sceneVersion)){
  209. String localPath = SceneResourcePath.nasBasePath + targetPath + "/" + fileName;
  210. File file = new File(localPath);
  211. if(!file.exists()){
  212. log.error("sceneCopy-error--localFileExist:localPath:{},oldNum:{},newNum:{}",localPath,oldNum,newNum);
  213. return;
  214. }
  215. fileContent = FileUtil.readUtf8String(file);
  216. }
  217. if("v4".equals(sceneVersion)){
  218. String ossStatusJsonPath = targetPath + "/" + fileName;
  219. if(!fYunFileServiceInterface.fileExist(ossStatusJsonPath)){
  220. log.error("sceneCopy-error--ossFileExist:targetPath:{},oldNum:{},newNum:{}",ossStatusJsonPath,oldNum,newNum);
  221. return;
  222. }
  223. fileContent = fYunFileServiceInterface.getFileContent(ossStatusJsonPath);
  224. }
  225. if(StringUtils.isNotBlank(fileContent)){
  226. //v3编辑器使用
  227. String localPath = SceneResourcePath.nasBasePath + targetPath +"/" + fileName;
  228. File file = new File(localPath);
  229. if(!file.getParentFile().exists()){
  230. file.getParentFile().mkdirs();
  231. }
  232. String newJson = fileContent.replaceAll(oldNum,newNum);
  233. try {
  234. if("v3".equals(sceneVersion)){
  235. if(fileName.contains("scene.json")){
  236. JSONObject jsonObject = JSONObject.parseObject(newJson);
  237. jsonObject.put("sceneName",newSceneName);
  238. FileUtils.writeFile(localPath ,jsonObject.toJSONString());
  239. String sceneJsonPath = String.format(SceneResourcePath.dataPath+"/"+fileName, newNum);
  240. fYunFileServiceInterface.uploadFile(localPath, sceneJsonPath);
  241. }else {
  242. FileUtils.writeFile(localPath ,newJson);
  243. }
  244. }
  245. if("v4".equals(sceneVersion)){
  246. JSONObject jsonObject = JSONObject.parseObject(newJson);
  247. jsonObject.put("title",newSceneName);
  248. jsonObject.put("dynamicPanel",0);
  249. FileUtils.writeFile(localPath, jsonObject.toJSONString());
  250. String sceneJsonPath = String.format(SceneResourcePath.DATA_VIEW_PATH+"/" + fileName, newNum);
  251. fYunFileServiceInterface.uploadFile(localPath, sceneJsonPath);
  252. //修改图片名称
  253. String filePath = String.format(SceneResourcePath.USER_VIEW_PATH, newNum) ;
  254. List<String> files = fYunFileServiceInterface.listRemoteFiles(filePath);
  255. for (String ossFilePath : files) {
  256. if(ossFilePath.contains(oldNum)){
  257. String oldName = ossFilePath;
  258. ossFilePath = ossFilePath.replace(oldNum,newNum);
  259. fYunFileServiceInterface.copyFileInBucket(oldName,ossFilePath);
  260. fYunFileServiceInterface.deleteFile(oldName);
  261. }
  262. }
  263. String dynamicViewPath = String.format(SceneResourcePath.DYNAMIC_VIEW_PATH, newNum);
  264. String dynamicEditPath = String.format(SceneResourcePath.DYNAMIC_EDIT_PATH, newNum);
  265. if(fYunFileServiceInterface.fileExist(dynamicViewPath)){
  266. fYunFileServiceInterface.deleteFile(dynamicViewPath);
  267. }
  268. if(fYunFileServiceInterface.fileExist(dynamicEditPath)){
  269. fYunFileServiceInterface.deleteFile(dynamicEditPath);
  270. }
  271. }
  272. }catch (Exception e){
  273. log.error("writeFile-error:{}",e);
  274. }
  275. }
  276. }
  277. public void updateOssJson(String targetPath,String oldNum, String newNum,String fileName) {
  278. String ossStatusJsonPath = targetPath + "/" + fileName;
  279. if(!fYunFileServiceInterface.fileExist(ossStatusJsonPath)){
  280. log.error("sceneCopy-error--ossFileExist:targetPath:{},oldNum:{},newNum:{}",ossStatusJsonPath,oldNum,newNum);
  281. return;
  282. }
  283. String localPath = SceneResourcePath.nasBasePath + ossStatusJsonPath;
  284. File file = new File(localPath);
  285. if(!file.getParentFile().exists()){
  286. file.getParentFile().mkdirs();
  287. }
  288. String fileContent = fYunFileServiceInterface.getFileContent(ossStatusJsonPath);
  289. if(StringUtils.isNotBlank(fileContent)){
  290. String newJson = fileContent.replaceAll(oldNum,newNum);
  291. try {
  292. FileUtils.writeFile(localPath, newJson);
  293. fYunFileServiceInterface.uploadFile(localPath,ossStatusJsonPath);
  294. }catch (Exception e){
  295. log.error("writeFile-error:{}",e);
  296. }
  297. }
  298. }
  299. private void copyOssAndNasV3(String oldNum ,String newNum ,String sourcePath,String targetPath){
  300. log.info("sceneCopy-ossSource-oldNum:{},newNum:{},sourcePath:{},targetPath:{}",oldNum,newNum,sourcePath,targetPath);
  301. fYunFileServiceInterface.copyFileInBucket(sourcePath,targetPath);
  302. File fileData = new File(SceneResourcePath.nasBasePath + sourcePath);
  303. if(fileData.exists()){
  304. if(targetPath.contains("images")){
  305. this.delLink(fileData.getPath());
  306. }
  307. cn.hutool.core.io.FileUtil.copyContent(fileData,new File(SceneResourcePath.nasBasePath + targetPath),true);
  308. }
  309. }
  310. private void copyOssAndNasV4(String oldNum,String newNum){
  311. // 拷贝场景编辑资源
  312. String oldEditPath = String.format(SceneResourcePath.EDIT_PATH_v4, oldNum);
  313. String newEditPath = String.format(SceneResourcePath.EDIT_PATH_v4, newNum);
  314. fYunFileServiceInterface.copyFileInBucket(oldEditPath, newEditPath);
  315. // 拷贝场景展示资源
  316. String oldViewPath = String.format(SceneResourcePath.VIEW_PATH_v4, oldNum);
  317. String newViewPath = String.format(SceneResourcePath.VIEW_PATH_v4, newNum);
  318. fYunFileServiceInterface.copyFileInBucket(oldViewPath, newViewPath);
  319. //复制计算结果文件
  320. String oldResultPath = String.format(SceneResourcePath.SCENE_RESULT_DATA_PATH, oldNum);
  321. String newResultPath = String.format(SceneResourcePath.SCENE_RESULT_DATA_PATH, newNum);
  322. fYunFileServiceInterface.copyFileInBucket(oldResultPath, newResultPath);
  323. // 拷贝本地资源
  324. String oldPath = SceneResourcePath.nasBasePath + oldNum;
  325. String newPath = SceneResourcePath.nasBasePath + newNum;
  326. if(new File(oldPath).exists()){
  327. FileUtil.copyContent(new File(oldPath), new File(newPath),true);
  328. }
  329. String oldPath_v4 = SceneResourcePath.nasBasePath_v4 + oldNum;
  330. String newPath_v4 = SceneResourcePath.nasBasePath_v4 + newNum;
  331. if(new File(oldPath_v4).exists()){
  332. FileUtil.copyContent(new File(oldPath_v4), new File(newPath_v4),true);
  333. }
  334. }
  335. public void delLink(String path) {
  336. String panPath = path +"/panorama";
  337. File file = new File(panPath);
  338. if(file.exists()){
  339. File[] files = file.listFiles();
  340. if(files == null || files.length == 0){
  341. return;
  342. }
  343. for (File file1 : files) {
  344. String linkPath =file1.getPath() + "/capture";
  345. log.info("delLink--filePath:{}",linkPath);
  346. org.apache.commons.io.FileUtils.deleteQuietly(new File(linkPath));
  347. }
  348. }
  349. }
  350. }