SceneCommonService.java 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. package com.fdkankan.ucenter.service.impl;
  2. import cn.hutool.core.io.FileUtil;
  3. import cn.hutool.extra.qrcode.QrCodeUtil;
  4. import cn.hutool.extra.qrcode.QrConfig;
  5. import com.alibaba.fastjson.JSONObject;
  6. import com.fdkankan.common.constant.SceneVersionType;
  7. import com.fdkankan.common.exception.BusinessException;
  8. import com.fdkankan.common.util.FileUtils;
  9. import com.fdkankan.common.util.SnowflakeIdGenerator;
  10. import com.fdkankan.fyun.face.FYunFileServiceInterface;
  11. import com.fdkankan.ucenter.common.constants.ResultCode;
  12. import com.fdkankan.ucenter.entity.SceneResource;
  13. import com.fdkankan.ucenter.service.IScene3dNumService;
  14. import com.fdkankan.ucenter.util.SceneResourcePath;
  15. import lombok.extern.slf4j.Slf4j;
  16. import org.apache.commons.lang3.ObjectUtils;
  17. import org.apache.commons.lang3.StringUtils;
  18. import org.springframework.beans.factory.annotation.Autowired;
  19. import org.springframework.stereotype.Service;
  20. import java.io.File;
  21. import java.util.Date;
  22. import java.util.List;
  23. import java.util.function.ObjDoubleConsumer;
  24. @Service
  25. @Slf4j
  26. public class SceneCommonService {
  27. @Autowired
  28. FYunFileServiceInterface fYunFileServiceInterface;
  29. @Autowired
  30. IScene3dNumService scene3dNumService;
  31. @Autowired
  32. com.fdkankan.ucenter.util.FileUtil fileUtil;
  33. public String getNewNum(String oldNum ){
  34. String newNum = scene3dNumService.generateSceneNum(1);
  35. if(oldNum.contains("-")){
  36. String pre = oldNum.split("-")[0];
  37. if(!"t".equals(pre) && !"jp".equals(pre)){
  38. return pre +"-"+ newNum;
  39. }
  40. }
  41. return newNum;
  42. }
  43. /**
  44. * 生成新的dataSouce
  45. */
  46. public String getNewDataSource(String oldDataSource){
  47. String newDataSource = null;
  48. if(StringUtils.isBlank(oldDataSource)){
  49. throw new BusinessException(ResultCode.FAILURE_CODE_400006,ResultCode.FAILURE_MSG_400006);
  50. }
  51. if(!oldDataSource.contains("/")){
  52. throw new BusinessException(ResultCode.FAILURE_CODE_400006,ResultCode.FAILURE_MSG_400006);
  53. }
  54. String time = com.fdkankan.common.util.DateUtil.date2String(new Date(), com.fdkankan.common.util.DateUtil.YYYYMMDDHHMMSSSSS_DATA_FORMAT);
  55. String[] split = oldDataSource.split("/");
  56. if(split.length == 6 ){
  57. String oldFileId = split[4];
  58. Long fileId = new SnowflakeIdGenerator(1,1).nextId();
  59. newDataSource = oldDataSource.replace(oldFileId,fileId.toString());
  60. String snCodeTime = split[5];
  61. if(!snCodeTime.contains("_") || snCodeTime.split("_").length <= 1){
  62. throw new BusinessException(ResultCode.FAILURE_CODE_400006,ResultCode.FAILURE_MSG_400006);
  63. }
  64. newDataSource = newDataSource.replace(snCodeTime.split("_")[1],time);
  65. //this.copyFdage(oldDataSource,newDataSource,time);
  66. }
  67. if(newDataSource == null){
  68. throw new BusinessException(ResultCode.FAILURE_CODE_400006,ResultCode.FAILURE_MSG_400006);
  69. }
  70. return newDataSource;
  71. }
  72. public void createNewQrCode(String sceneVersion,String logoPath ,String newNum, String webSite){
  73. String localLogoPath = null;
  74. try {
  75. if(StringUtils.isNotBlank(logoPath)){
  76. if(sceneVersion.equals(SceneVersionType.V3.code())){
  77. localLogoPath =SceneResourcePath.nasBasePath + logoPath;
  78. }else{
  79. localLogoPath = SceneResourcePath.qrCodeBasePath + newNum +"/logo/logo.png";
  80. fYunFileServiceInterface.downloadFile(logoPath,localLogoPath);
  81. }
  82. }
  83. String outPathZh = SceneResourcePath.qrCodeBasePath + newNum + ".png";
  84. String outPathEn = SceneResourcePath.qrCodeBasePath + newNum + "_en.png";
  85. QrConfig qrConfig = QrConfig.create();
  86. qrConfig.setWidth(1024);
  87. qrConfig.setHeight(1024);
  88. if(!ObjectUtils.isEmpty(localLogoPath)){
  89. qrConfig.setImg(localLogoPath);
  90. }
  91. QrCodeUtil.generate(webSite, qrConfig, FileUtil.file(outPathZh));
  92. QrCodeUtil.generate(webSite + "&lang=en", qrConfig, FileUtil.file(outPathEn));
  93. fYunFileServiceInterface.uploadFile(outPathZh, String.format(SceneResourcePath.DOWNLOADS_QRCODE, newNum) + newNum + ".png");
  94. fYunFileServiceInterface.uploadFile(outPathEn, String.format(SceneResourcePath.DOWNLOADS_QRCODE, newNum) + newNum + "_en.png");
  95. }catch (Exception e){
  96. log.info("copy-scene-error:{},newNum:{},error:{}",newNum,e);
  97. }
  98. }
  99. public void updateNasSceneJson(String targetPath, String oldNum, String newNum,String newSceneName,String sceneVersion,String fileName) {
  100. String fileContent = null;
  101. if("v3".equals(sceneVersion)){
  102. String localPath = SceneResourcePath.nasBasePath + targetPath + "/" + fileName;
  103. File file = new File(localPath);
  104. if(!file.exists()){
  105. log.error("sceneCopy-error--localFileExist:localPath:{},oldNum:{},newNum:{}",localPath,oldNum,newNum);
  106. return;
  107. }
  108. fileContent = FileUtil.readUtf8String(file);
  109. }
  110. if("v4".equals(sceneVersion)){
  111. String ossStatusJsonPath = targetPath + "/" + fileName;
  112. if(!fYunFileServiceInterface.fileExist(ossStatusJsonPath)){
  113. log.error("sceneCopy-error--ossFileExist:targetPath:{},oldNum:{},newNum:{}",ossStatusJsonPath,oldNum,newNum);
  114. return;
  115. }
  116. fileContent = fYunFileServiceInterface.getFileContent(ossStatusJsonPath);
  117. }
  118. if(StringUtils.isNotBlank(fileContent)){
  119. //v3编辑器使用
  120. String localPath = SceneResourcePath.nasBasePath + targetPath +"/" + fileName;
  121. File file = new File(localPath);
  122. if(!file.getParentFile().exists()){
  123. file.getParentFile().mkdirs();
  124. }
  125. String newJson = fileContent.replaceAll(oldNum,newNum);
  126. try {
  127. if("v3".equals(sceneVersion)){
  128. if(fileName.contains("scene.json")){
  129. JSONObject jsonObject = JSONObject.parseObject(newJson);
  130. jsonObject.put("sceneName",newSceneName);
  131. fileUtil.writeFile(localPath,jsonObject.toJSONString());
  132. String sceneJsonPath = String.format(SceneResourcePath.dataPath+"/"+fileName, newNum);
  133. fYunFileServiceInterface.uploadFile(localPath, sceneJsonPath);
  134. }else {
  135. fileUtil.writeFile(localPath,newJson);
  136. }
  137. }
  138. if("v4".equals(sceneVersion)){
  139. JSONObject jsonObject = JSONObject.parseObject(newJson);
  140. jsonObject.put("title",newSceneName);
  141. fileUtil.writeFile(localPath,jsonObject.toJSONString());
  142. String sceneJsonPath = String.format(SceneResourcePath.DATA_VIEW_PATH+"/" + fileName, newNum);
  143. fYunFileServiceInterface.uploadFile(localPath, sceneJsonPath);
  144. //修改图片名称
  145. String filePath = String.format(SceneResourcePath.USER_VIEW_PATH, newNum) ;
  146. List<String> files = fYunFileServiceInterface.listRemoteFiles(filePath);
  147. for (String ossFilePath : files) {
  148. if(ossFilePath.contains(oldNum)){
  149. String oldName = ossFilePath;
  150. ossFilePath = ossFilePath.replace(oldNum,newNum);
  151. fYunFileServiceInterface.copyFileInBucket(oldName,ossFilePath);
  152. fYunFileServiceInterface.deleteFile(oldName);
  153. }
  154. }
  155. }
  156. }catch (Exception e){
  157. log.error("writeFile-error:{}",e);
  158. }
  159. }
  160. }
  161. public void updateOssJson(String targetPath,String oldNum, String newNum,String fileName) {
  162. String ossStatusJsonPath = targetPath + "/" + fileName;
  163. if(!fYunFileServiceInterface.fileExist(ossStatusJsonPath)){
  164. log.error("sceneCopy-error--ossFileExist:targetPath:{},oldNum:{},newNum:{}",ossStatusJsonPath,oldNum,newNum);
  165. return;
  166. }
  167. String localPath = SceneResourcePath.nasBasePath + ossStatusJsonPath;
  168. File file = new File(localPath);
  169. if(!file.getParentFile().exists()){
  170. file.getParentFile().mkdirs();
  171. }
  172. String fileContent = fYunFileServiceInterface.getFileContent(ossStatusJsonPath);
  173. if(StringUtils.isNotBlank(fileContent)){
  174. String newJson = fileContent.replaceAll(oldNum,newNum);
  175. try {
  176. fileUtil.writeFile(localPath,newJson);
  177. fYunFileServiceInterface.uploadFile(localPath,ossStatusJsonPath);
  178. }catch (Exception e){
  179. log.error("writeFile-error:{}",e);
  180. }
  181. }
  182. }
  183. public void copyFdage(String oldDataSource,String newDataSource,String time){
  184. String ossOldPath = oldDataSource.replace("/mnt/data","home");
  185. String ossNewPath = newDataSource.replace("/mnt/data","home");
  186. fYunFileServiceInterface.copyFileInBucket(ossOldPath,ossNewPath);
  187. String fileName = "/data.fdage";
  188. String fileContent = fYunFileServiceInterface.getFileContent(ossNewPath + fileName);
  189. if(StringUtils.isNotBlank(fileContent)){
  190. JSONObject jsonObject = JSONObject.parseObject(fileContent);
  191. jsonObject.put("uuidtime",time);
  192. FileUtils.writeFile(newDataSource +fileName, jsonObject.toJSONString());
  193. fYunFileServiceInterface.uploadFile(newDataSource +fileName,ossNewPath +fileName);
  194. }
  195. }
  196. public void delLink(String path) {
  197. String panPath = path +"/panorama";
  198. File file = new File(panPath);
  199. if(file.exists()){
  200. File[] files = file.listFiles();
  201. if(files == null || files.length == 0){
  202. return;
  203. }
  204. for (File file1 : files) {
  205. String linkPath =file1.getPath() + "/capture";
  206. log.info("delLink--filePath:{}",linkPath);
  207. org.apache.commons.io.FileUtils.deleteQuietly(new File(linkPath));
  208. }
  209. }
  210. }
  211. }