SceneCommonService.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
  7. import com.fdkankan.common.constant.SceneVersionType;
  8. import com.fdkankan.common.exception.BusinessException;
  9. import com.fdkankan.common.util.FileUtils;
  10. import com.fdkankan.common.util.SnowflakeIdGenerator;
  11. import com.fdkankan.fyun.face.FYunFileServiceInterface;
  12. import com.fdkankan.ucenter.common.constants.ResultCode;
  13. import com.fdkankan.ucenter.entity.*;
  14. import com.fdkankan.ucenter.service.*;
  15. import com.fdkankan.ucenter.util.SceneResourcePath;
  16. import lombok.extern.slf4j.Slf4j;
  17. import org.apache.commons.lang3.ObjectUtils;
  18. import org.apache.commons.lang3.StringUtils;
  19. import org.springframework.beans.factory.annotation.Autowired;
  20. import org.springframework.stereotype.Service;
  21. import java.io.File;
  22. import java.util.Date;
  23. import java.util.List;
  24. import java.util.function.ObjDoubleConsumer;
  25. @Service
  26. @Slf4j
  27. public class SceneCommonService {
  28. @Autowired
  29. FYunFileServiceInterface fYunFileServiceInterface;
  30. @Autowired
  31. IScene3dNumService scene3dNumService;
  32. public String getNewNum(String oldNum ){
  33. String newNum = scene3dNumService.generateSceneNum(1);
  34. if(oldNum.contains("-")){
  35. String pre = oldNum.split("-")[0];
  36. if(!"t".equals(pre) && !"jp".equals(pre)){
  37. return pre +"-"+ newNum;
  38. }
  39. }
  40. return newNum;
  41. }
  42. /**
  43. * 生成新的dataSouce
  44. */
  45. public String getNewDataSource(String oldDataSource){
  46. String newDataSource = null;
  47. if(StringUtils.isBlank(oldDataSource)){
  48. throw new BusinessException(ResultCode.FAILURE_CODE_400006,ResultCode.FAILURE_MSG_400006);
  49. }
  50. if(!oldDataSource.contains("/")){
  51. throw new BusinessException(ResultCode.FAILURE_CODE_400006,ResultCode.FAILURE_MSG_400006);
  52. }
  53. String time = com.fdkankan.common.util.DateUtil.date2String(new Date(), com.fdkankan.common.util.DateUtil.YYYYMMDDHHMMSSSSS_DATA_FORMAT);
  54. String[] split = oldDataSource.split("/");
  55. if(split.length == 6 ){
  56. String oldFileId = split[4];
  57. Long fileId = new SnowflakeIdGenerator(1,1).nextId();
  58. newDataSource = oldDataSource.replace(oldFileId,fileId.toString());
  59. String snCodeTime = split[5];
  60. if(!snCodeTime.contains("_") || snCodeTime.split("_").length <= 1){
  61. throw new BusinessException(ResultCode.FAILURE_CODE_400006,ResultCode.FAILURE_MSG_400006);
  62. }
  63. newDataSource = newDataSource.replace(snCodeTime.split("_")[1],time);
  64. //this.copyFdage(oldDataSource,newDataSource,time);
  65. }
  66. if(newDataSource == null){
  67. throw new BusinessException(ResultCode.FAILURE_CODE_400006,ResultCode.FAILURE_MSG_400006);
  68. }
  69. return newDataSource;
  70. }
  71. public void createNewQrCode(String sceneVersion,String logoPath ,String newNum, String webSite){
  72. String localLogoPath = null;
  73. try {
  74. if(StringUtils.isNotBlank(logoPath)){
  75. if(sceneVersion.equals(SceneVersionType.V3.code())){
  76. localLogoPath =SceneResourcePath.nasBasePath + logoPath;
  77. }else{
  78. localLogoPath = SceneResourcePath.qrCodeBasePath + newNum +"/logo/logo.png";
  79. fYunFileServiceInterface.downloadFile(logoPath,localLogoPath);
  80. }
  81. }
  82. String outPathZh = SceneResourcePath.qrCodeBasePath + newNum + ".png";
  83. String outPathEn = SceneResourcePath.qrCodeBasePath + newNum + "_en.png";
  84. QrConfig qrConfig = QrConfig.create();
  85. qrConfig.setWidth(1024);
  86. qrConfig.setHeight(1024);
  87. if(!ObjectUtils.isEmpty(localLogoPath)){
  88. qrConfig.setImg(localLogoPath);
  89. }
  90. QrCodeUtil.generate(webSite, qrConfig, FileUtil.file(outPathZh));
  91. QrCodeUtil.generate(webSite + "&lang=en", qrConfig, FileUtil.file(outPathEn));
  92. fYunFileServiceInterface.uploadFile(outPathZh, String.format(SceneResourcePath.DOWNLOADS_QRCODE, newNum) + newNum + ".png");
  93. fYunFileServiceInterface.uploadFile(outPathEn, String.format(SceneResourcePath.DOWNLOADS_QRCODE, newNum) + newNum + "_en.png");
  94. }catch (Exception e){
  95. log.info("copy-scene-error:{},newNum:{},error:{}",newNum,e);
  96. }
  97. }
  98. public void updateNasSceneJson(String targetPath, String oldNum, String newNum,String newSceneName,String sceneVersion,String fileName) {
  99. try {
  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. if("v3".equals(sceneVersion)){
  127. if(fileName.contains("scene.json")){
  128. JSONObject jsonObject = JSONObject.parseObject(newJson);
  129. jsonObject.put("sceneName",newSceneName);
  130. FileUtils.writeFile(localPath ,jsonObject.toJSONString());
  131. String sceneJsonPath = String.format(SceneResourcePath.dataPath+"/"+fileName, newNum);
  132. fYunFileServiceInterface.uploadFile(localPath, sceneJsonPath);
  133. }else {
  134. FileUtils.writeFile(localPath ,newJson);
  135. }
  136. }
  137. if("v4".equals(sceneVersion)){
  138. JSONObject jsonObject = JSONObject.parseObject(newJson);
  139. jsonObject.put("title",newSceneName);
  140. FileUtils.writeFile(localPath, jsonObject.toJSONString());
  141. String sceneJsonPath = String.format(SceneResourcePath.DATA_VIEW_PATH+"/" + fileName, newNum);
  142. fYunFileServiceInterface.uploadFile(localPath, sceneJsonPath);
  143. //修改图片名称
  144. String filePath = String.format(SceneResourcePath.USER_VIEW_PATH, newNum) ;
  145. List<String> files = fYunFileServiceInterface.listRemoteFiles(filePath);
  146. for (String ossFilePath : files) {
  147. if(ossFilePath.contains(oldNum)){
  148. String oldName = ossFilePath;
  149. ossFilePath = ossFilePath.replace(oldNum,newNum);
  150. fYunFileServiceInterface.copyFileInBucket(oldName,ossFilePath);
  151. fYunFileServiceInterface.deleteFile(oldName);
  152. }
  153. }
  154. }
  155. }
  156. }catch (Exception e){
  157. log.info("writeFile-error:{}",e);
  158. }
  159. }
  160. public void updateOssJson(String targetPath,String oldNum, String newNum,String fileName) {
  161. try {
  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. FileUtils.writeFile(localPath, newJson);
  176. fYunFileServiceInterface.uploadFile(localPath,ossStatusJsonPath);
  177. }
  178. }catch (Exception e){
  179. log.error("writeFile-error:{}",e);
  180. }
  181. }
  182. public void copyFdage(String oldDataSource,String newDataSource,String time){
  183. String ossOldPath = oldDataSource.replace("/mnt/data","home");
  184. String ossNewPath = newDataSource.replace("/mnt/data","home");
  185. fYunFileServiceInterface.copyFileInBucket(ossOldPath,ossNewPath);
  186. String fileName = "/data.fdage";
  187. String fileContent = fYunFileServiceInterface.getFileContent(ossNewPath + fileName);
  188. if(StringUtils.isNotBlank(fileContent)){
  189. JSONObject jsonObject = JSONObject.parseObject(fileContent);
  190. jsonObject.put("uuidtime",time);
  191. FileUtils.writeFile(newDataSource +fileName, jsonObject.toJSONString());
  192. fYunFileServiceInterface.uploadFile(newDataSource +fileName,ossNewPath +fileName);
  193. }
  194. }
  195. public void delLink(String path) {
  196. String panPath = path +"/panorama";
  197. File file = new File(panPath);
  198. if(file.exists()){
  199. File[] files = file.listFiles();
  200. if(files == null || files.length == 0){
  201. return;
  202. }
  203. for (File file1 : files) {
  204. String linkPath =file1.getPath() + "/capture";
  205. log.info("delLink--filePath:{}",linkPath);
  206. org.apache.commons.io.FileUtils.deleteQuietly(new File(linkPath));
  207. }
  208. }
  209. }
  210. @Autowired
  211. ISceneProService sceneProService;
  212. @Autowired
  213. ICameraDetailService cameraDetailService;
  214. @Autowired
  215. ICameraTypeService cameraTypeService;
  216. @Autowired
  217. IScenePlusService scenePlusService;
  218. @Autowired
  219. IScenePlusExtService scenePlusExtService;
  220. public void copyResult(String newNum) {
  221. ScenePro scenePro = sceneProService.getByNum(newNum);
  222. ScenePlus scenePlus = scenePlusService.getByNum(newNum);
  223. if(scenePro == null && scenePlus == null){
  224. log.info("复制失败:{}",newNum);
  225. return;
  226. }
  227. if(scenePlus != null){
  228. LambdaUpdateWrapper<ScenePlus> wrapper = new LambdaUpdateWrapper<>();
  229. wrapper.eq(ScenePlus::getId,scenePlus.getId());
  230. wrapper.set(ScenePlus::getSceneStatus,-2);
  231. scenePlusService.update(wrapper);
  232. }
  233. if(scenePro != null){
  234. LambdaUpdateWrapper<ScenePro> wrapper = new LambdaUpdateWrapper<>();
  235. wrapper.eq(ScenePro::getId,scenePro.getId());
  236. wrapper.set(ScenePro::getStatus,-2);
  237. sceneProService.update(wrapper);
  238. }
  239. }
  240. }