SceneCommonService.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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.RedisKeyUtil;
  13. import com.fdkankan.ucenter.common.constants.ResultCode;
  14. import com.fdkankan.ucenter.entity.*;
  15. import com.fdkankan.ucenter.service.*;
  16. import com.fdkankan.ucenter.util.SceneResourcePath;
  17. import lombok.extern.slf4j.Slf4j;
  18. import org.apache.commons.lang3.ObjectUtils;
  19. import org.apache.commons.lang3.StringUtils;
  20. import org.springframework.beans.factory.annotation.Autowired;
  21. import org.springframework.stereotype.Service;
  22. import java.io.File;
  23. import java.util.ArrayList;
  24. import java.util.Arrays;
  25. import java.util.Date;
  26. import java.util.List;
  27. import java.util.function.ObjDoubleConsumer;
  28. @Service
  29. @Slf4j
  30. public class SceneCommonService {
  31. @Autowired
  32. FYunFileServiceInterface fYunFileServiceInterface;
  33. @Autowired
  34. IScene3dNumService scene3dNumService;
  35. public String getNewNum(String oldNum ){
  36. String newNum = scene3dNumService.generateSceneNum(1);
  37. if(oldNum.contains("-")){
  38. String pre = oldNum.split("-")[0];
  39. if(!"t".equals(pre) && !"jp".equals(pre)){
  40. return pre +"-"+ newNum;
  41. }
  42. }
  43. return newNum;
  44. }
  45. /**
  46. * 生成新的dataSouce
  47. */
  48. public String getNewDataSource(String oldDataSource){
  49. String newDataSource = null;
  50. if(StringUtils.isBlank(oldDataSource)){
  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 newSceneName,String sceneVersion,String fileName) {
  102. String fileContent = null;
  103. if("v3".equals(sceneVersion)){
  104. String localPath = SceneResourcePath.nasBasePath + targetPath + "/" + fileName;
  105. File file = new File(localPath);
  106. if(!file.exists()){
  107. log.error("sceneCopy-error--localFileExist:localPath:{},oldNum:{},newNum:{}",localPath,oldNum,newNum);
  108. return;
  109. }
  110. fileContent = FileUtil.readUtf8String(file);
  111. }
  112. if("v4".equals(sceneVersion)){
  113. String ossStatusJsonPath = targetPath + "/" + fileName;
  114. if(!fYunFileServiceInterface.fileExist(ossStatusJsonPath)){
  115. log.error("sceneCopy-error--ossFileExist:targetPath:{},oldNum:{},newNum:{}",ossStatusJsonPath,oldNum,newNum);
  116. return;
  117. }
  118. fileContent = fYunFileServiceInterface.getFileContent(ossStatusJsonPath);
  119. }
  120. if(StringUtils.isNotBlank(fileContent)){
  121. //v3编辑器使用
  122. String localPath = SceneResourcePath.nasBasePath + targetPath +"/" + fileName;
  123. File file = new File(localPath);
  124. if(!file.getParentFile().exists()){
  125. file.getParentFile().mkdirs();
  126. }
  127. String newJson = fileContent.replaceAll(oldNum,newNum);
  128. try {
  129. if("v3".equals(sceneVersion)){
  130. if(fileName.contains("scene.json")){
  131. JSONObject jsonObject = JSONObject.parseObject(newJson);
  132. jsonObject.put("sceneName",newSceneName);
  133. FileUtils.writeFile(localPath ,jsonObject.toJSONString());
  134. String sceneJsonPath = String.format(SceneResourcePath.dataPath+"/"+fileName, newNum);
  135. fYunFileServiceInterface.uploadFile(localPath, sceneJsonPath);
  136. }else {
  137. FileUtils.writeFile(localPath ,newJson);
  138. }
  139. }
  140. if("v4".equals(sceneVersion)){
  141. JSONObject jsonObject = JSONObject.parseObject(newJson);
  142. jsonObject.put("title",newSceneName);
  143. jsonObject.put("dynamicPanel",0);
  144. FileUtils.writeFile(localPath, jsonObject.toJSONString());
  145. String sceneJsonPath = String.format(SceneResourcePath.DATA_VIEW_PATH+"/" + fileName, newNum);
  146. fYunFileServiceInterface.uploadFile(localPath, sceneJsonPath);
  147. //修改图片名称
  148. String filePath = String.format(SceneResourcePath.USER_VIEW_PATH, newNum) ;
  149. List<String> files = fYunFileServiceInterface.listRemoteFiles(filePath);
  150. for (String ossFilePath : files) {
  151. if(ossFilePath.contains(oldNum)){
  152. String oldName = ossFilePath;
  153. ossFilePath = ossFilePath.replace(oldNum,newNum);
  154. fYunFileServiceInterface.copyFileInBucket(oldName,ossFilePath);
  155. fYunFileServiceInterface.deleteFile(oldName);
  156. }
  157. }
  158. String dynamicViewPath = String.format(SceneResourcePath.DYNAMIC_VIEW_PATH, newNum);
  159. String dynamicEditPath = String.format(SceneResourcePath.DYNAMIC_EDIT_PATH, newNum);
  160. if(fYunFileServiceInterface.fileExist(dynamicViewPath)){
  161. fYunFileServiceInterface.deleteFile(dynamicViewPath);
  162. }
  163. if(fYunFileServiceInterface.fileExist(dynamicEditPath)){
  164. fYunFileServiceInterface.deleteFile(dynamicEditPath);
  165. }
  166. }
  167. }catch (Exception e){
  168. log.error("writeFile-error:{}",e);
  169. }
  170. }
  171. }
  172. public void updateOssJson(String targetPath,String oldNum, String newNum,String fileName) {
  173. String ossStatusJsonPath = targetPath + "/" + fileName;
  174. if(!fYunFileServiceInterface.fileExist(ossStatusJsonPath)){
  175. log.error("sceneCopy-error--ossFileExist:targetPath:{},oldNum:{},newNum:{}",ossStatusJsonPath,oldNum,newNum);
  176. return;
  177. }
  178. String localPath = SceneResourcePath.nasBasePath + ossStatusJsonPath;
  179. File file = new File(localPath);
  180. if(!file.getParentFile().exists()){
  181. file.getParentFile().mkdirs();
  182. }
  183. String fileContent = fYunFileServiceInterface.getFileContent(ossStatusJsonPath);
  184. if(StringUtils.isNotBlank(fileContent)){
  185. String newJson = fileContent.replaceAll(oldNum,newNum);
  186. try {
  187. FileUtils.writeFile(localPath, newJson);
  188. fYunFileServiceInterface.uploadFile(localPath,ossStatusJsonPath);
  189. }catch (Exception e){
  190. log.error("writeFile-error:{}",e);
  191. }
  192. }
  193. }
  194. public void copyFdage(String oldDataSource,String newDataSource,String time){
  195. String ossOldPath = oldDataSource.replace("/mnt/data","home");
  196. String ossNewPath = newDataSource.replace("/mnt/data","home");
  197. fYunFileServiceInterface.copyFileInBucket(ossOldPath,ossNewPath);
  198. String fileName = "/data.fdage";
  199. String fileContent = fYunFileServiceInterface.getFileContent(ossNewPath + fileName);
  200. if(StringUtils.isNotBlank(fileContent)){
  201. JSONObject jsonObject = JSONObject.parseObject(fileContent);
  202. jsonObject.put("uuidtime",time);
  203. FileUtils.writeFile(newDataSource +fileName, jsonObject.toJSONString());
  204. fYunFileServiceInterface.uploadFile(newDataSource +fileName,ossNewPath +fileName);
  205. }
  206. }
  207. public void delLink(String path) {
  208. String panPath = path +"/panorama";
  209. File file = new File(panPath);
  210. if(file.exists()){
  211. File[] files = file.listFiles();
  212. if(files == null || files.length == 0){
  213. return;
  214. }
  215. for (File file1 : files) {
  216. String linkPath =file1.getPath() + "/capture";
  217. log.info("delLink--filePath:{}",linkPath);
  218. org.apache.commons.io.FileUtils.deleteQuietly(new File(linkPath));
  219. }
  220. }
  221. }
  222. @Autowired
  223. ISceneProService sceneProService;
  224. @Autowired
  225. ICameraDetailService cameraDetailService;
  226. @Autowired
  227. ICameraTypeService cameraTypeService;
  228. @Autowired
  229. IScenePlusService scenePlusService;
  230. @Autowired
  231. IScenePlusExtService scenePlusExtService;
  232. public void copyResult(String newNum) {
  233. ScenePro scenePro = sceneProService.getByNum(newNum);
  234. ScenePlus scenePlus = scenePlusService.getByNum(newNum);
  235. if(scenePro == null && scenePlus == null){
  236. log.info("复制失败:{}",newNum);
  237. return;
  238. }
  239. Long cameraId = scenePro == null ? scenePlus.getCameraId() : scenePro.getCameraId();
  240. Integer location = null;
  241. Long space = 0L;
  242. if(scenePlus != null){
  243. ScenePlusExt plusExt = scenePlusExtService.getByPlusId(scenePlus.getId());
  244. if(plusExt == null){
  245. return;
  246. }
  247. location = plusExt.getLocation();
  248. space = plusExt.getSpace();
  249. }
  250. if(scenePro != null){
  251. space = scenePro.getSpace();
  252. }
  253. CameraDetail cameraDetail = cameraDetailService.getByCameraId(cameraId);
  254. CameraType cameraType = cameraTypeService.getByCameraType(cameraDetail.getType());
  255. Boolean checkSpace = cameraDetailService.checkSpace(cameraDetail,cameraType);
  256. if(!checkSpace){
  257. List<String> panoNumList = new ArrayList<>();
  258. if(location != null && location == 7){
  259. panoNumList.add(scenePlus.getNum());
  260. }
  261. sceneProService.lockOrUnLockScenes(new ArrayList<>(), Arrays.asList(newNum),-2,cameraType.getIsLaser(),panoNumList);
  262. }
  263. cameraDetailService.addUsedSpace(cameraDetail,space);
  264. if(cameraType.getIsLaser() == 1 && scenePlus != null){
  265. LambdaUpdateWrapper<ScenePlus> wrapper = new LambdaUpdateWrapper<>();
  266. wrapper.eq(ScenePlus::getId,scenePlus.getId());
  267. wrapper.set(ScenePlus::getSceneStatus,-2);
  268. scenePlusService.update(wrapper);
  269. }
  270. if(cameraType.getIsLaser() == 1 && scenePro != null){
  271. LambdaUpdateWrapper<ScenePro> wrapper = new LambdaUpdateWrapper<>();
  272. wrapper.eq(ScenePro::getId,scenePro.getId());
  273. wrapper.set(ScenePro::getStatus,-2);
  274. sceneProService.update(wrapper);
  275. }
  276. }
  277. }