SceneCommonService.java 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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. public String getNewNum(String oldNum ){
  32. String newNum = scene3dNumService.generateSceneNum(1);
  33. if(oldNum.contains("-")){
  34. String pre = oldNum.split("-")[0];
  35. if(!"t".equals(pre) && !"jp".equals(pre)){
  36. return pre +"-"+ newNum;
  37. }
  38. }
  39. return newNum;
  40. }
  41. /**
  42. * 生成新的dataSouce
  43. */
  44. public String getNewDataSource(String oldDataSource){
  45. String newDataSource = null;
  46. if(StringUtils.isBlank(oldDataSource)){
  47. throw new BusinessException(ResultCode.FAILURE_CODE_400006,ResultCode.FAILURE_MSG_400006);
  48. }
  49. if(!oldDataSource.contains("/")){
  50. throw new BusinessException(ResultCode.FAILURE_CODE_400006,ResultCode.FAILURE_MSG_400006);
  51. }
  52. String time = com.fdkankan.common.util.DateUtil.date2String(new Date(), com.fdkankan.common.util.DateUtil.YYYYMMDDHHMMSSSSS_DATA_FORMAT);
  53. String[] split = oldDataSource.split("/");
  54. if(split.length == 6 ){
  55. String oldFileId = split[4];
  56. Long fileId = new SnowflakeIdGenerator(1,1).nextId();
  57. newDataSource = oldDataSource.replace(oldFileId,fileId.toString());
  58. String snCodeTime = split[5];
  59. if(!snCodeTime.contains("_") || snCodeTime.split("_").length <= 1){
  60. throw new BusinessException(ResultCode.FAILURE_CODE_400006,ResultCode.FAILURE_MSG_400006);
  61. }
  62. newDataSource = newDataSource.replace(snCodeTime.split("_")[1],time);
  63. //this.copyFdage(oldDataSource,newDataSource,time);
  64. }
  65. if(newDataSource == null){
  66. throw new BusinessException(ResultCode.FAILURE_CODE_400006,ResultCode.FAILURE_MSG_400006);
  67. }
  68. return newDataSource;
  69. }
  70. public void createNewQrCode(String sceneVersion,String logoPath ,String newNum, String webSite){
  71. String localLogoPath = null;
  72. try {
  73. if(StringUtils.isNotBlank(logoPath)){
  74. if(sceneVersion.equals(SceneVersionType.V3.code())){
  75. localLogoPath =SceneResourcePath.nasBasePath + logoPath;
  76. }else{
  77. localLogoPath = SceneResourcePath.qrCodeBasePath + newNum +"/logo/logo.png";
  78. fYunFileServiceInterface.downloadFile(logoPath,localLogoPath);
  79. }
  80. }
  81. String outPathZh = SceneResourcePath.qrCodeBasePath + newNum + ".png";
  82. String outPathEn = SceneResourcePath.qrCodeBasePath + newNum + "_en.png";
  83. QrConfig qrConfig = QrConfig.create();
  84. qrConfig.setWidth(1024);
  85. qrConfig.setHeight(1024);
  86. if(!ObjectUtils.isEmpty(localLogoPath)){
  87. qrConfig.setImg(localLogoPath);
  88. }
  89. QrCodeUtil.generate(webSite, qrConfig, FileUtil.file(outPathZh));
  90. QrCodeUtil.generate(webSite + "&lang=en", qrConfig, FileUtil.file(outPathEn));
  91. fYunFileServiceInterface.uploadFile(outPathZh, String.format(SceneResourcePath.DOWNLOADS_QRCODE, newNum) + newNum + ".png");
  92. fYunFileServiceInterface.uploadFile(outPathEn, String.format(SceneResourcePath.DOWNLOADS_QRCODE, newNum) + newNum + "_en.png");
  93. }catch (Exception e){
  94. log.info("copy-scene-error:{},newNum:{},error:{}",newNum,e);
  95. }
  96. }
  97. public void updateNasSceneJson(String targetPath, String oldNum, String newNum,String oldSceneName,String newSceneName,String sceneVersion) {
  98. String fileContent = null;
  99. if("v3".equals(sceneVersion)){
  100. String localPath = SceneResourcePath.nasBasePath + targetPath + "/" + "scene.json";
  101. File file = new File(localPath);
  102. if(!file.exists()){
  103. log.error("sceneCopy-error--localFileExist:localPath:{},oldNum:{},newNum:{}",localPath,oldNum,newNum);
  104. return;
  105. }
  106. fileContent = FileUtil.readUtf8String(file);
  107. }
  108. if("v4".equals(sceneVersion)){
  109. String ossStatusJsonPath = targetPath + "/" + "scene.json";
  110. if(!fYunFileServiceInterface.fileExist(ossStatusJsonPath)){
  111. log.error("sceneCopy-error--ossFileExist:targetPath:{},oldNum:{},newNum:{}",ossStatusJsonPath,oldNum,newNum);
  112. return;
  113. }
  114. fileContent = fYunFileServiceInterface.getFileContent(ossStatusJsonPath);
  115. }
  116. if(StringUtils.isNotBlank(fileContent)){
  117. //v3编辑器使用
  118. String localPath = SceneResourcePath.nasBasePath + targetPath +"/scene.json";
  119. File file = new File(localPath);
  120. if(!file.getParentFile().exists()){
  121. file.getParentFile().mkdirs();
  122. }
  123. String newJson = fileContent.replaceAll(oldNum,newNum);
  124. newJson = newJson.replace(oldSceneName,newSceneName);
  125. try {
  126. com.fdkankan.ucenter.util.FileUtil.writeFile(localPath,newJson);
  127. if("v3".equals(sceneVersion)){
  128. String sceneJsonPath = String.format(SceneResourcePath.dataPath+"/scene.json", newNum);
  129. fYunFileServiceInterface.uploadFile(localPath, sceneJsonPath);
  130. }
  131. if("v4".equals(sceneVersion)){
  132. String sceneJsonPath = String.format(SceneResourcePath.DATA_VIEW_PATH+"/scene.json", newNum);
  133. fYunFileServiceInterface.uploadFile(localPath, sceneJsonPath);
  134. //修改图片名称
  135. String filePath = String.format(SceneResourcePath.USER_VIEW_PATH, newNum) ;
  136. List<String> files = fYunFileServiceInterface.listRemoteFiles(filePath);
  137. for (String fileName : files) {
  138. if(fileName.contains(oldNum)){
  139. String oldName = fileName;
  140. fileName = fileName.replace(oldNum,newNum);
  141. fYunFileServiceInterface.copyFileInBucket(oldName,fileName);
  142. fYunFileServiceInterface.deleteFile(oldName);
  143. }
  144. }
  145. }
  146. }catch (Exception e){
  147. log.error("writeFile-error:{}",e);
  148. }
  149. }
  150. }
  151. public void updateOssJson(String targetPath,String oldNum, String newNum,String fileName) {
  152. String ossStatusJsonPath = targetPath + "/" + fileName;
  153. if(!fYunFileServiceInterface.fileExist(ossStatusJsonPath)){
  154. log.error("sceneCopy-error--ossFileExist:targetPath:{},oldNum:{},newNum:{}",ossStatusJsonPath,oldNum,newNum);
  155. return;
  156. }
  157. String localPath = SceneResourcePath.nasBasePath + ossStatusJsonPath;
  158. File file = new File(localPath);
  159. if(!file.getParentFile().exists()){
  160. file.getParentFile().mkdirs();
  161. }
  162. String fileContent = fYunFileServiceInterface.getFileContent(ossStatusJsonPath);
  163. if(StringUtils.isNotBlank(fileContent)){
  164. String newJson = fileContent.replaceAll(oldNum,newNum);
  165. try {
  166. com.fdkankan.ucenter.util.FileUtil.writeFile(localPath,newJson);
  167. fYunFileServiceInterface.uploadFile(localPath,ossStatusJsonPath);
  168. }catch (Exception e){
  169. log.error("writeFile-error:{}",e);
  170. }finally {
  171. FileUtil.del(localPath);
  172. }
  173. }
  174. }
  175. public void copyFdage(String oldDataSource,String newDataSource,String time){
  176. String ossOldPath = oldDataSource.replace("/mnt/data","home");
  177. String ossNewPath = newDataSource.replace("/mnt/data","home");
  178. fYunFileServiceInterface.copyFileInBucket(ossOldPath,ossNewPath);
  179. String fileName = "/data.fdage";
  180. String fileContent = fYunFileServiceInterface.getFileContent(ossNewPath + fileName);
  181. if(StringUtils.isNotBlank(fileContent)){
  182. JSONObject jsonObject = JSONObject.parseObject(fileContent);
  183. jsonObject.put("uuidtime",time);
  184. FileUtils.writeFile(newDataSource +fileName, jsonObject.toJSONString());
  185. fYunFileServiceInterface.uploadFile(newDataSource +fileName,ossNewPath +fileName);
  186. }
  187. }
  188. }