SceneCommonService.java 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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.service.IScene3dNumService;
  13. import com.fdkankan.ucenter.util.SceneResourcePath;
  14. import lombok.extern.slf4j.Slf4j;
  15. import org.apache.commons.lang3.ObjectUtils;
  16. import org.apache.commons.lang3.StringUtils;
  17. import org.springframework.beans.factory.annotation.Autowired;
  18. import org.springframework.stereotype.Service;
  19. import java.io.File;
  20. import java.util.Date;
  21. @Service
  22. @Slf4j
  23. public class SceneCommonService {
  24. @Autowired
  25. FYunFileServiceInterface fYunFileServiceInterface;
  26. @Autowired
  27. IScene3dNumService scene3dNumService;
  28. public String getNewNum(String oldNum ){
  29. String newNum = scene3dNumService.generateSceneNum(1);
  30. if(oldNum.contains("-")){
  31. String pre = oldNum.split("-")[0];
  32. if(!"t".equals(pre) && !"jp".equals(pre)){
  33. return pre +"-"+ newNum;
  34. }
  35. }
  36. return newNum;
  37. }
  38. /**
  39. * 生成新的dataSouce
  40. */
  41. public String getNewDataSource(String oldDataSource){
  42. if(true){
  43. return oldDataSource;
  44. }
  45. String newDataSource = null;
  46. if(StringUtils.isBlank(oldDataSource)){
  47. throw new BusinessException(ResultCode.FAILURE_CODE_400006,ResultCode.FAILURE_MSG_400006);
  48. }
  49. File oldDataSouceFile = new File(oldDataSource);
  50. if(!oldDataSouceFile.exists()){
  51. throw new BusinessException(ResultCode.FAILURE_CODE_400006,ResultCode.FAILURE_MSG_400006);
  52. }
  53. if(!oldDataSource.contains("/")){
  54. throw new BusinessException(ResultCode.FAILURE_CODE_400006,ResultCode.FAILURE_MSG_400006);
  55. }
  56. String time = com.fdkankan.common.util.DateUtil.date2String(new Date(), com.fdkankan.common.util.DateUtil.YYYYMMDDHHMMSSSSS_DATA_FORMAT);
  57. String[] split = oldDataSource.split("/");
  58. if(split.length == 6 ){
  59. String oldFileId = split[4];
  60. Long fileId = new SnowflakeIdGenerator(1,1).nextId();
  61. newDataSource = oldDataSource.replace(oldFileId,fileId.toString());
  62. String snCodeTime = split[5];
  63. if(!snCodeTime.contains("_") || snCodeTime.split("_").length <= 1){
  64. throw new BusinessException(ResultCode.FAILURE_CODE_400006,ResultCode.FAILURE_MSG_400006);
  65. }
  66. newDataSource = newDataSource.replace(snCodeTime.split("_")[1],time);
  67. this.copyFdage(oldDataSource,newDataSource,time);
  68. }
  69. if(newDataSource == null){
  70. throw new BusinessException(ResultCode.FAILURE_CODE_400006,ResultCode.FAILURE_MSG_400006);
  71. }
  72. return newDataSource;
  73. }
  74. public void createNewQrCode(String sceneVersion,String logoPath ,String newNum, String webSite){
  75. String localLogoPath = null;
  76. try {
  77. if(StringUtils.isNotBlank(logoPath)){
  78. if(sceneVersion.equals(SceneVersionType.V3.code())){
  79. localLogoPath =SceneResourcePath.nasBasePath + logoPath;
  80. }else{
  81. localLogoPath = SceneResourcePath.qrCodeBasePath + newNum +"/logo/logo.png";
  82. fYunFileServiceInterface.downloadFile(logoPath,localLogoPath);
  83. }
  84. }
  85. String outPathZh = SceneResourcePath.qrCodeBasePath + newNum + ".png";
  86. String outPathEn = SceneResourcePath.qrCodeBasePath + newNum + "_en.png";
  87. QrConfig qrConfig = QrConfig.create();
  88. qrConfig.setWidth(1024);
  89. qrConfig.setHeight(1024);
  90. if(!ObjectUtils.isEmpty(localLogoPath)){
  91. qrConfig.setImg(localLogoPath);
  92. }
  93. QrCodeUtil.generate(webSite, qrConfig, FileUtil.file(outPathZh));
  94. QrCodeUtil.generate(webSite + "&lang=en", qrConfig, FileUtil.file(outPathEn));
  95. fYunFileServiceInterface.uploadFile(outPathZh, String.format(SceneResourcePath.DOWNLOADS_QRCODE, newNum) + newNum + ".png");
  96. fYunFileServiceInterface.uploadFile(outPathEn, String.format(SceneResourcePath.DOWNLOADS_QRCODE, newNum) + newNum + "_en.png");
  97. }catch (Exception e){
  98. log.info("copy-scene-error:{},newNum:{},error:{}",newNum,e);
  99. }
  100. }
  101. public void updateNasSceneJson(String targetPath, String oldNum, String newNum,String oldSceneName,String newSceneName,String sceneVersion) {
  102. String localPath = SceneResourcePath.nasBasePath + targetPath + "/" + "scene.json";
  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. String fileContent = FileUtil.readUtf8String(file);
  109. if(StringUtils.isNotBlank(fileContent)){
  110. String newJson = fileContent.replaceAll(oldNum,newNum);
  111. newJson = newJson.replaceAll(oldSceneName,newSceneName);
  112. try {
  113. com.fdkankan.ucenter.util.FileUtil.writeFile(localPath,newJson);
  114. }catch (Exception e){
  115. log.error("writeFile-error:{}",e);
  116. }
  117. }
  118. if("v3".equals(sceneVersion)){
  119. String sceneJsonPath = String.format(SceneResourcePath.dataPath+"/scene.json", newNum);
  120. fYunFileServiceInterface.uploadFile(localPath, sceneJsonPath);
  121. }
  122. if("v4".equals(sceneVersion)){
  123. String sceneJsonPath = String.format(SceneResourcePath.DATA_VIEW_PATH+"scene.json", newNum);
  124. fYunFileServiceInterface.uploadFile(localPath, sceneJsonPath);
  125. }
  126. }
  127. public void updateOssJson(String targetPath,String oldNum, String newNum,String sceneVersion) {
  128. String ossStatusJsonPath = targetPath + "/" + "status.json";
  129. if(!fYunFileServiceInterface.fileExist(ossStatusJsonPath)){
  130. log.error("sceneCopy-error--ossFileExist:targetPath:{},oldNum:{},newNum:{}",ossStatusJsonPath,oldNum,newNum);
  131. return;
  132. }
  133. String localPath = SceneResourcePath.nasBasePath + ossStatusJsonPath;
  134. File file = new File(localPath);
  135. if(!file.getParentFile().exists()){
  136. file.mkdirs();
  137. }
  138. String fileContent = fYunFileServiceInterface.getFileContent(ossStatusJsonPath);
  139. if(StringUtils.isNotBlank(fileContent)){
  140. String newJson = fileContent.replaceAll(oldNum,newNum);
  141. try {
  142. com.fdkankan.ucenter.util.FileUtil.writeFile(localPath,newJson);
  143. fYunFileServiceInterface.uploadFile(localPath,ossStatusJsonPath);
  144. }catch (Exception e){
  145. log.error("writeFile-error:{}",e);
  146. }
  147. }
  148. }
  149. public void copyFdage(String oldDataSource,String newDataSource,String time){
  150. String ossOldPath = oldDataSource.replace("/mnt/data","home");
  151. String ossNewPath = newDataSource.replace("/mnt/data","home");
  152. fYunFileServiceInterface.copyFileInBucket(ossOldPath,ossNewPath);
  153. String fileName = "/data.fdage";
  154. String fileContent = fYunFileServiceInterface.getFileContent(ossNewPath + fileName);
  155. if(StringUtils.isNotBlank(fileContent)){
  156. JSONObject jsonObject = JSONObject.parseObject(fileContent);
  157. jsonObject.put("uuidtime",time);
  158. FileUtils.writeFile(newDataSource +fileName, jsonObject.toJSONString());
  159. fYunFileServiceInterface.uploadFile(newDataSource +fileName,ossNewPath +fileName);
  160. }
  161. }
  162. }