SceneCommonService.java 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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 newSceneName,String sceneVersion,String fileName) {
  98. String fileContent = null;
  99. if("v3".equals(sceneVersion)){
  100. String localPath = SceneResourcePath.nasBasePath + targetPath + "/" + fileName;
  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 + "/" + fileName;
  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 +"/" + fileName;
  119. File file = new File(localPath);
  120. if(!file.getParentFile().exists()){
  121. file.getParentFile().mkdirs();
  122. }
  123. String newJson = fileContent.replaceAll(oldNum,newNum);
  124. try {
  125. if("v3".equals(sceneVersion)){
  126. if(fileName.contains("scene.json")){
  127. JSONObject jsonObject = JSONObject.parseObject(newJson);
  128. jsonObject.put("sceneName",newSceneName);
  129. com.fdkankan.ucenter.util.FileUtil.writeFile(localPath,jsonObject.toJSONString());
  130. String sceneJsonPath = String.format(SceneResourcePath.dataPath+"/"+fileName, newNum);
  131. fYunFileServiceInterface.uploadFile(localPath, sceneJsonPath);
  132. }else {
  133. com.fdkankan.ucenter.util.FileUtil.writeFile(localPath,newJson);
  134. }
  135. }
  136. if("v4".equals(sceneVersion)){
  137. JSONObject jsonObject = JSONObject.parseObject(newJson);
  138. jsonObject.put("title",newSceneName);
  139. com.fdkankan.ucenter.util.FileUtil.writeFile(localPath,jsonObject.toJSONString());
  140. String sceneJsonPath = String.format(SceneResourcePath.DATA_VIEW_PATH+"/" + fileName, newNum);
  141. fYunFileServiceInterface.uploadFile(localPath, sceneJsonPath);
  142. //修改图片名称
  143. String filePath = String.format(SceneResourcePath.USER_VIEW_PATH, newNum) ;
  144. List<String> files = fYunFileServiceInterface.listRemoteFiles(filePath);
  145. for (String ossFilePath : files) {
  146. if(ossFilePath.contains(oldNum)){
  147. String oldName = ossFilePath;
  148. ossFilePath = ossFilePath.replace(oldNum,newNum);
  149. fYunFileServiceInterface.copyFileInBucket(oldName,ossFilePath);
  150. fYunFileServiceInterface.deleteFile(oldName);
  151. }
  152. }
  153. }
  154. }catch (Exception e){
  155. log.error("writeFile-error:{}",e);
  156. }
  157. }
  158. }
  159. public void updateOssJson(String targetPath,String oldNum, String newNum,String fileName) {
  160. String ossStatusJsonPath = targetPath + "/" + fileName;
  161. if(!fYunFileServiceInterface.fileExist(ossStatusJsonPath)){
  162. log.error("sceneCopy-error--ossFileExist:targetPath:{},oldNum:{},newNum:{}",ossStatusJsonPath,oldNum,newNum);
  163. return;
  164. }
  165. String localPath = SceneResourcePath.nasBasePath + ossStatusJsonPath;
  166. File file = new File(localPath);
  167. if(!file.getParentFile().exists()){
  168. file.getParentFile().mkdirs();
  169. }
  170. String fileContent = fYunFileServiceInterface.getFileContent(ossStatusJsonPath);
  171. if(StringUtils.isNotBlank(fileContent)){
  172. String newJson = fileContent.replaceAll(oldNum,newNum);
  173. try {
  174. com.fdkankan.ucenter.util.FileUtil.writeFile(localPath,newJson);
  175. fYunFileServiceInterface.uploadFile(localPath,ossStatusJsonPath);
  176. }catch (Exception e){
  177. log.error("writeFile-error:{}",e);
  178. }
  179. }
  180. }
  181. public void copyFdage(String oldDataSource,String newDataSource,String time){
  182. String ossOldPath = oldDataSource.replace("/mnt/data","home");
  183. String ossNewPath = newDataSource.replace("/mnt/data","home");
  184. fYunFileServiceInterface.copyFileInBucket(ossOldPath,ossNewPath);
  185. String fileName = "/data.fdage";
  186. String fileContent = fYunFileServiceInterface.getFileContent(ossNewPath + fileName);
  187. if(StringUtils.isNotBlank(fileContent)){
  188. JSONObject jsonObject = JSONObject.parseObject(fileContent);
  189. jsonObject.put("uuidtime",time);
  190. FileUtils.writeFile(newDataSource +fileName, jsonObject.toJSONString());
  191. fYunFileServiceInterface.uploadFile(newDataSource +fileName,ossNewPath +fileName);
  192. }
  193. }
  194. }