SceneCommonService.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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.ResultCodeMsg;
  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.ArrayList;
  23. import java.util.Arrays;
  24. import java.util.Date;
  25. import java.util.List;
  26. @Service
  27. @Slf4j
  28. public class SceneCommonService {
  29. @Autowired
  30. FYunFileServiceInterface fYunFileServiceInterface;
  31. @Autowired
  32. IScene3dNumService scene3dNumService;
  33. public String getNewNum(String oldNum ){
  34. String newNum = scene3dNumService.generateSceneNum(1);
  35. if(oldNum.contains("-")){
  36. String pre = oldNum.split("-")[0];
  37. if(!"t".equals(pre) && !"jp".equals(pre)){
  38. return pre +"-"+ newNum;
  39. }
  40. }
  41. return newNum;
  42. }
  43. /**
  44. * 生成新的dataSouce
  45. */
  46. public String getNewDataSource(String oldDataSource){
  47. String newDataSource = null;
  48. if(StringUtils.isBlank(oldDataSource)){
  49. throw new BusinessException(ResultCodeMsg.FAILURE_CODE_400006, ResultCodeMsg.FAILURE_MSG_400006);
  50. }
  51. if(!oldDataSource.contains("/")){
  52. throw new BusinessException(ResultCodeMsg.FAILURE_CODE_400006, ResultCodeMsg.FAILURE_MSG_400006);
  53. }
  54. String time = com.fdkankan.common.util.DateUtil.date2String(new Date(), com.fdkankan.common.util.DateUtil.YYYYMMDDHHMMSSSSS_DATA_FORMAT);
  55. String[] split = oldDataSource.split("/");
  56. if(split.length == 6 ){
  57. String oldFileId = split[4];
  58. Long fileId = new SnowflakeIdGenerator(1,1).nextId();
  59. newDataSource = oldDataSource.replace(oldFileId,fileId.toString());
  60. String snCodeTime = split[5];
  61. if(!snCodeTime.contains("_") || snCodeTime.split("_").length <= 1){
  62. throw new BusinessException(ResultCodeMsg.FAILURE_CODE_400006, ResultCodeMsg.FAILURE_MSG_400006);
  63. }
  64. newDataSource = newDataSource.replace(snCodeTime.split("_")[1],time);
  65. //this.copyFdage(oldDataSource,newDataSource,time);
  66. }
  67. if(newDataSource == null){
  68. throw new BusinessException(ResultCodeMsg.FAILURE_CODE_400006, ResultCodeMsg.FAILURE_MSG_400006);
  69. }
  70. return newDataSource;
  71. }
  72. public void createNewQrCode(String sceneVersion,String logoPath ,String newNum, String webSite){
  73. String localLogoPath = null;
  74. try {
  75. if(StringUtils.isNotBlank(logoPath)){
  76. if(sceneVersion.equals(SceneVersionType.V3.code())){
  77. localLogoPath =SceneResourcePath.nasBasePath + logoPath;
  78. }else{
  79. localLogoPath = SceneResourcePath.qrCodeBasePath + newNum +"/logo/logo.png";
  80. fYunFileServiceInterface.downloadFile(logoPath,localLogoPath);
  81. }
  82. }
  83. String outPathZh = SceneResourcePath.qrCodeBasePath + newNum + ".png";
  84. String outPathEn = SceneResourcePath.qrCodeBasePath + newNum + "_en.png";
  85. QrConfig qrConfig = QrConfig.create();
  86. qrConfig.setWidth(1024);
  87. qrConfig.setHeight(1024);
  88. if(!ObjectUtils.isEmpty(localLogoPath)){
  89. qrConfig.setImg(localLogoPath);
  90. }
  91. QrCodeUtil.generate(webSite, qrConfig, FileUtil.file(outPathZh));
  92. QrCodeUtil.generate(webSite + "&lang=en", qrConfig, FileUtil.file(outPathEn));
  93. fYunFileServiceInterface.uploadFile(outPathZh, String.format(SceneResourcePath.DOWNLOADS_QRCODE, newNum) + newNum + ".png");
  94. fYunFileServiceInterface.uploadFile(outPathEn, String.format(SceneResourcePath.DOWNLOADS_QRCODE, newNum) + newNum + "_en.png");
  95. }catch (Exception e){
  96. log.info("copy-scene-error:{},newNum:{},error:{}",newNum,e);
  97. }
  98. }
  99. public void updateNasSceneJson(String targetPath, String oldNum, String newNum,String newSceneName,String sceneVersion,String fileName) {
  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. try {
  127. if("v3".equals(sceneVersion)){
  128. if(fileName.contains("scene.json")){
  129. JSONObject jsonObject = JSONObject.parseObject(newJson);
  130. jsonObject.put("sceneName",newSceneName);
  131. FileUtils.writeFile(localPath ,jsonObject.toJSONString());
  132. String sceneJsonPath = String.format(SceneResourcePath.dataPath+"/"+fileName, newNum);
  133. fYunFileServiceInterface.uploadFile(localPath, sceneJsonPath);
  134. }else {
  135. FileUtils.writeFile(localPath ,newJson);
  136. }
  137. }
  138. if("v4".equals(sceneVersion)){
  139. JSONObject jsonObject = JSONObject.parseObject(newJson);
  140. jsonObject.put("title",newSceneName);
  141. jsonObject.put("dynamicPanel",0);
  142. FileUtils.writeFile(localPath, jsonObject.toJSONString());
  143. String sceneJsonPath = String.format(SceneResourcePath.DATA_VIEW_PATH+"/" + fileName, newNum);
  144. fYunFileServiceInterface.uploadFile(localPath, sceneJsonPath);
  145. //修改图片名称
  146. String filePath = String.format(SceneResourcePath.USER_VIEW_PATH, newNum) ;
  147. List<String> files = fYunFileServiceInterface.listRemoteFiles(filePath);
  148. for (String ossFilePath : files) {
  149. if(ossFilePath.contains(oldNum)){
  150. String oldName = ossFilePath;
  151. ossFilePath = ossFilePath.replace(oldNum,newNum);
  152. fYunFileServiceInterface.copyFileInBucket(oldName,ossFilePath);
  153. fYunFileServiceInterface.deleteFile(oldName);
  154. }
  155. }
  156. String dynamicViewPath = String.format(SceneResourcePath.DYNAMIC_VIEW_PATH, newNum);
  157. String dynamicEditPath = String.format(SceneResourcePath.DYNAMIC_EDIT_PATH, newNum);
  158. if(fYunFileServiceInterface.fileExist(dynamicViewPath)){
  159. fYunFileServiceInterface.deleteFile(dynamicViewPath);
  160. }
  161. if(fYunFileServiceInterface.fileExist(dynamicEditPath)){
  162. fYunFileServiceInterface.deleteFile(dynamicEditPath);
  163. }
  164. }
  165. }catch (Exception e){
  166. log.error("writeFile-error:{}",e);
  167. }
  168. }
  169. }
  170. public void updateOssJson(String targetPath,String oldNum, String newNum,String fileName) {
  171. String ossStatusJsonPath = targetPath + "/" + fileName;
  172. if(!fYunFileServiceInterface.fileExist(ossStatusJsonPath)){
  173. log.error("sceneCopy-error--ossFileExist:targetPath:{},oldNum:{},newNum:{}",ossStatusJsonPath,oldNum,newNum);
  174. return;
  175. }
  176. String localPath = SceneResourcePath.nasBasePath + ossStatusJsonPath;
  177. File file = new File(localPath);
  178. if(!file.getParentFile().exists()){
  179. file.getParentFile().mkdirs();
  180. }
  181. String fileContent = fYunFileServiceInterface.getFileContent(ossStatusJsonPath);
  182. if(StringUtils.isNotBlank(fileContent)){
  183. String newJson = fileContent.replaceAll(oldNum,newNum);
  184. try {
  185. FileUtils.writeFile(localPath, newJson);
  186. fYunFileServiceInterface.uploadFile(localPath,ossStatusJsonPath);
  187. }catch (Exception e){
  188. log.error("writeFile-error:{}",e);
  189. }
  190. }
  191. }
  192. public void copyFdage(String oldDataSource,String newDataSource,String time){
  193. String ossOldPath = oldDataSource.replace("/mnt/data","home");
  194. String ossNewPath = newDataSource.replace("/mnt/data","home");
  195. fYunFileServiceInterface.copyFileInBucket(ossOldPath,ossNewPath);
  196. String fileName = "/data.fdage";
  197. String fileContent = fYunFileServiceInterface.getFileContent(ossNewPath + fileName);
  198. if(StringUtils.isNotBlank(fileContent)){
  199. JSONObject jsonObject = JSONObject.parseObject(fileContent);
  200. jsonObject.put("uuidtime",time);
  201. FileUtils.writeFile(newDataSource +fileName, jsonObject.toJSONString());
  202. fYunFileServiceInterface.uploadFile(newDataSource +fileName,ossNewPath +fileName);
  203. }
  204. }
  205. public void delLink(String path) {
  206. String panPath = path +"/panorama";
  207. File file = new File(panPath);
  208. if(file.exists()){
  209. File[] files = file.listFiles();
  210. if(files == null || files.length == 0){
  211. return;
  212. }
  213. for (File file1 : files) {
  214. String linkPath =file1.getPath() + "/capture";
  215. log.info("delLink--filePath:{}",linkPath);
  216. org.apache.commons.io.FileUtils.deleteQuietly(new File(linkPath));
  217. }
  218. }
  219. }
  220. @Autowired
  221. ISceneProService sceneProService;
  222. @Autowired
  223. ICameraDetailService cameraDetailService;
  224. @Autowired
  225. ICameraTypeService cameraTypeService;
  226. @Autowired
  227. IScenePlusService scenePlusService;
  228. @Autowired
  229. IScenePlusExtService scenePlusExtService;
  230. public void copyResult(String newNum) {
  231. ScenePro scenePro = sceneProService.getByNum(newNum);
  232. ScenePlus scenePlus = scenePlusService.getByNum(newNum);
  233. if(scenePro == null && scenePlus == null){
  234. log.info("复制失败:{}",newNum);
  235. return;
  236. }
  237. Long cameraId = scenePro == null ? scenePlus.getCameraId() : scenePro.getCameraId();
  238. Integer location = null;
  239. Long space = 0L;
  240. if(scenePlus != null){
  241. ScenePlusExt plusExt = scenePlusExtService.getByPlusId(scenePlus.getId());
  242. if(plusExt == null){
  243. return;
  244. }
  245. location = plusExt.getLocation();
  246. space = plusExt.getSpace();
  247. }
  248. if(scenePro != null){
  249. space = scenePro.getSpace();
  250. }
  251. CameraDetail cameraDetail = cameraDetailService.getByCameraId(cameraId);
  252. CameraType cameraType = cameraTypeService.getByCameraType(cameraDetail.getType());
  253. Boolean checkSpace = cameraDetailService.checkSpace(cameraDetail,cameraType);
  254. if(!checkSpace && (location == null || location != 7)){
  255. sceneProService.lockOrUnLockScenes(new ArrayList<>(), Arrays.asList(newNum),-2,cameraType.getIsLaser(),null);
  256. }
  257. cameraDetailService.addUsedSpace(cameraDetail,space);
  258. if(cameraType.getIsLaser() == 1 && scenePlus != null){
  259. LambdaUpdateWrapper<ScenePlus> wrapper = new LambdaUpdateWrapper<>();
  260. wrapper.eq(ScenePlus::getId,scenePlus.getId());
  261. wrapper.set(ScenePlus::getSceneStatus,-2);
  262. scenePlusService.update(wrapper);
  263. }
  264. if(cameraType.getIsLaser() == 1 && scenePro != null){
  265. LambdaUpdateWrapper<ScenePro> wrapper = new LambdaUpdateWrapper<>();
  266. wrapper.eq(ScenePro::getId,scenePro.getId());
  267. wrapper.set(ScenePro::getStatus,-2);
  268. sceneProService.update(wrapper);
  269. }
  270. }
  271. }