SceneCopyServiceImpl.java 19 KB

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