SceneCommonService.java 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. package com.fdkankan.manage_jp.service.impl;
  2. import cn.hutool.core.io.FileUtil;
  3. import com.alibaba.fastjson.JSON;
  4. import com.alibaba.fastjson.JSONObject;
  5. import com.fdkankan.common.util.FileUtils;
  6. import com.fdkankan.fyun.face.FYunFileServiceInterface;
  7. import com.fdkankan.image.MatrixToImageWriterUtil;
  8. import com.fdkankan.manage_jp.common.ResultCode;
  9. import com.fdkankan.manage_jp.exception.BusinessException;
  10. import com.fdkankan.manage_jp.service.IScene3dNumService;
  11. import com.fdkankan.manage_jp.util.SceneResourcePath;
  12. import com.fdkankan.manage_jp.util.SnowflakeIdGenerator;
  13. import lombok.extern.slf4j.Slf4j;
  14. import org.apache.commons.lang3.StringUtils;
  15. import org.springframework.beans.factory.annotation.Autowired;
  16. import org.springframework.stereotype.Service;
  17. import java.io.File;
  18. import java.util.Date;
  19. @Service
  20. @Slf4j
  21. public class SceneCommonService {
  22. @Autowired
  23. FYunFileServiceInterface fYunFileServiceInterface;
  24. @Autowired
  25. IScene3dNumService scene3dNumService;
  26. public String getNewNum(String oldNum ){
  27. String newNum = scene3dNumService.generateNum();
  28. if(oldNum.contains("-")){
  29. String pre = oldNum.split("-")[0];
  30. if(!"t".equals(pre)){
  31. return pre +"-"+ newNum;
  32. }
  33. }
  34. return newNum;
  35. }
  36. /**
  37. * 生成新的dataSouce
  38. */
  39. public String getNewDataSource(String oldDataSource){
  40. String newDataSource = null;
  41. if(StringUtils.isBlank(oldDataSource)){
  42. throw new BusinessException(ResultCode.SCENE_DATA_ERROR);
  43. }
  44. File oldDataSouceFile = new File(oldDataSource);
  45. if(!oldDataSouceFile.exists()){
  46. throw new BusinessException(ResultCode.SCENE_DATA_ERROR);
  47. }
  48. if(!oldDataSource.contains("/")){
  49. throw new BusinessException(ResultCode.SCENE_DATA_ERROR);
  50. }
  51. String time = com.fdkankan.common.util.DateUtil.date2String(new Date(), com.fdkankan.common.util.DateUtil.YYYYMMDDHHMMSSSSS_DATA_FORMAT);
  52. String[] split = oldDataSource.split("/");
  53. if(split.length == 6 ){
  54. String oldFileId = split[4];
  55. Long fileId = SnowflakeIdGenerator.snowflakeIdGenerator.nextId();
  56. newDataSource = oldDataSource.replace(oldFileId,fileId.toString());
  57. String snCodeTime = split[5];
  58. if(!snCodeTime.contains("_") || snCodeTime.split("_").length <= 1){
  59. throw new BusinessException(ResultCode.SCENE_DATA_ERROR);
  60. }
  61. newDataSource = newDataSource.replace(snCodeTime.split("_")[1],time);
  62. this.copyFdage(oldDataSource,newDataSource,time);
  63. }
  64. if(newDataSource == null){
  65. throw new BusinessException(ResultCode.SCENE_DATA_ERROR);
  66. }
  67. return newDataSource;
  68. }
  69. public void createNewQrCode(String newNum, String webSite){
  70. try {
  71. String outPathZh = SceneResourcePath.qrCodeBasePath + newNum + ".png";
  72. String outPathEn = SceneResourcePath.qrCodeBasePath + newNum + "_en.png";
  73. MatrixToImageWriterUtil.createQRCode(webSite, outPathZh, false,null);
  74. MatrixToImageWriterUtil.createQRCode(webSite + "&lang=en", outPathEn, false, null);
  75. fYunFileServiceInterface.uploadFile(outPathZh, String.format(SceneResourcePath.DOWNLOADS_QRCODE, newNum) + newNum + ".png");
  76. fYunFileServiceInterface.uploadFile(outPathEn, String.format(SceneResourcePath.DOWNLOADS_QRCODE, newNum) + newNum + "_en.png");
  77. }catch (Exception e){
  78. log.info("copy-scene-error:{},newNum:{},error:{}",newNum,e);
  79. }
  80. }
  81. public void updateNasSceneJson(String targetPath, String oldNum, String newNum,String oldSceneName,String newSceneName,String sceneVersion) {
  82. String localPath = SceneResourcePath.nasBasePath + targetPath + "/" + "scene.json";
  83. File file = new File(localPath);
  84. if(!file.exists()){
  85. log.error("sceneCopy-error--localFileExist:localPath:{},oldNum:{},newNum:{}",localPath,oldNum,newNum);
  86. return;
  87. }
  88. String fileContent = cn.hutool.core.io.FileUtil.readUtf8String(file);
  89. if(StringUtils.isNotBlank(fileContent)){
  90. String newJson = fileContent.replaceAll(oldNum,newNum);
  91. newJson = newJson.replaceAll(oldSceneName,newSceneName);
  92. try {
  93. com.fdkankan.manage_jp.util.FileUtil.writeFile(localPath,newJson);
  94. }catch (Exception e){
  95. log.error("writeFile-error:{}",e);
  96. }
  97. }
  98. if("v3".equals(sceneVersion)){
  99. String sceneJsonPath = String.format(SceneResourcePath.dataPath+"/scene.json", newNum);
  100. fYunFileServiceInterface.uploadFile(localPath, sceneJsonPath);
  101. }
  102. if("v4".equals(sceneVersion)){
  103. String sceneJsonPath = String.format(SceneResourcePath.DATA_VIEW_PATH+"scene.json", newNum);
  104. fYunFileServiceInterface.uploadFile(localPath, sceneJsonPath);
  105. }
  106. }
  107. public void updateOssJson(String targetPath,String oldNum, String newNum,String sceneVersion) {
  108. String ossStatusJsonPath = targetPath + "/" + "status.json";
  109. if(!fYunFileServiceInterface.fileExist(ossStatusJsonPath)){
  110. log.error("sceneCopy-error--ossFileExist:targetPath:{},oldNum:{},newNum:{}",ossStatusJsonPath,oldNum,newNum);
  111. return;
  112. }
  113. String localPath = SceneResourcePath.nasBasePath + ossStatusJsonPath;
  114. File file = new File(localPath);
  115. if(!file.getParentFile().exists()){
  116. file.mkdirs();
  117. }
  118. String fileContent = fYunFileServiceInterface.getFileContent(ossStatusJsonPath);
  119. if(StringUtils.isNotBlank(fileContent)){
  120. String newJson = fileContent.replaceAll(oldNum,newNum);
  121. try {
  122. com.fdkankan.manage_jp.util.FileUtil.writeFile(localPath,newJson);
  123. fYunFileServiceInterface.uploadFile(localPath,ossStatusJsonPath);
  124. }catch (Exception e){
  125. log.error("writeFile-error:{}",e);
  126. }
  127. }
  128. }
  129. public void copyFdage(String oldDataSource,String newDataSource,String time){
  130. String ossOldPath = oldDataSource.replace("/mnt/data","home");
  131. String ossNewPath = newDataSource.replace("/mnt/data","home");
  132. fYunFileServiceInterface.copyFileInBucket(ossOldPath,ossNewPath);
  133. String fileName = "/data.fdage";
  134. String fileContent = fYunFileServiceInterface.getFileContent(ossNewPath + fileName);
  135. if(StringUtils.isNotBlank(fileContent)){
  136. JSONObject jsonObject = JSONObject.parseObject(fileContent);
  137. jsonObject.put("uuidtime",time);
  138. FileUtils.writeFile(newDataSource +fileName, jsonObject.toJSONString());
  139. fYunFileServiceInterface.uploadFile(newDataSource +fileName,ossNewPath +fileName);
  140. }
  141. }
  142. }