BuildScenePostServiceImpl.java 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904
  1. package com.fdkankan.scene.service.impl;
  2. import static com.fdkankan.push.PushMessageConfig.ANDROID_KEY;
  3. import static com.fdkankan.push.PushMessageConfig.ANDROID_KEY_Z;
  4. import static com.fdkankan.push.PushMessageConfig.ANDROID_SECRET;
  5. import static com.fdkankan.push.PushMessageConfig.ANDROID_SECRET_Z;
  6. import static com.fdkankan.push.PushMessageConfig.IOS_KEY;
  7. import static com.fdkankan.push.PushMessageConfig.IOS_KEY_Z;
  8. import static com.fdkankan.push.PushMessageConfig.IOS_SECRET;
  9. import static com.fdkankan.push.PushMessageConfig.IOS_SECRET_Z;
  10. import cn.hutool.core.bean.BeanUtil;
  11. import cn.hutool.core.collection.CollUtil;
  12. import cn.hutool.core.util.StrUtil;
  13. import com.alibaba.fastjson.JSON;
  14. import com.alibaba.fastjson.JSONArray;
  15. import com.alibaba.fastjson.JSONObject;
  16. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  17. import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
  18. import com.fdkankan.common.constant.BuildStatus;
  19. import com.fdkankan.common.constant.ConstantFilePath;
  20. import com.fdkankan.common.constant.ConstantUrl;
  21. import com.fdkankan.common.constant.ExpiredStatus;
  22. import com.fdkankan.common.constant.PayStatus;
  23. import com.fdkankan.common.constant.SceneFrom;
  24. import com.fdkankan.common.constant.SceneResolution;
  25. import com.fdkankan.common.constant.SceneSource;
  26. import com.fdkankan.common.constant.SceneStatus;
  27. import com.fdkankan.common.constant.ServerCode;
  28. import com.fdkankan.common.constant.UploadFilePath;
  29. import com.fdkankan.common.response.ResultData;
  30. import com.fdkankan.common.util.FileUtil;
  31. import com.fdkankan.common.util.FileUtils;
  32. import com.fdkankan.common.util.MatrixToImageWriterUtil;
  33. import com.fdkankan.common.util.SceneUtil;
  34. import com.fdkankan.fyun.constant.StorageType;
  35. import com.fdkankan.fyun.oss.UploadToOssUtil;
  36. import com.fdkankan.platform.api.dto.CameraDetail;
  37. import com.fdkankan.platform.api.dto.UserIncrement;
  38. import com.fdkankan.platform.api.feign.PlatformGoodsClient;
  39. import com.fdkankan.platform.api.feign.PlatformUserClient;
  40. import com.fdkankan.push.PushMessageConfig;
  41. import com.fdkankan.push.PushMsgUtil;
  42. import com.fdkankan.rabbitmq.bean.BuildSceneResultMqMessage;
  43. import com.fdkankan.redis.constant.RedisKey;
  44. import com.fdkankan.redis.util.RedisUtil;
  45. import com.fdkankan.scene.bean.PointBean;
  46. import com.fdkankan.scene.bean.SceneJsonBean;
  47. import com.fdkankan.scene.bean.SegmentBean;
  48. import com.fdkankan.scene.bean.VertexBean;
  49. import com.fdkankan.scene.bean.WallBean;
  50. import com.fdkankan.scene.entity.SceneEditControls;
  51. import com.fdkankan.scene.entity.SceneEditInfo;
  52. import com.fdkankan.scene.entity.SceneEditInfoExt;
  53. import com.fdkankan.scene.entity.SceneFileBuild;
  54. import com.fdkankan.scene.entity.ScenePlus;
  55. import com.fdkankan.scene.entity.ScenePlusExt;
  56. import com.fdkankan.scene.entity.ScenePro;
  57. import com.fdkankan.scene.entity.SceneProExt;
  58. import com.fdkankan.scene.service.IBuildSceneDTService;
  59. import com.fdkankan.scene.service.IBuildScenePostService;
  60. import com.fdkankan.scene.service.ISceneEditControlsService;
  61. import com.fdkankan.scene.service.ISceneEditInfoExtService;
  62. import com.fdkankan.scene.service.ISceneEditInfoService;
  63. import com.fdkankan.scene.service.ISceneFileBuildService;
  64. import com.fdkankan.scene.service.IScenePlusExtService;
  65. import com.fdkankan.scene.service.IScenePlusService;
  66. import com.fdkankan.scene.service.ISceneProExtService;
  67. import com.fdkankan.scene.service.ISceneProService;
  68. import com.fdkankan.scene.service.ISceneService;
  69. import com.fdkankan.scene.vo.SceneEditControlsVO;
  70. import java.io.File;
  71. import java.io.IOException;
  72. import java.nio.charset.StandardCharsets;
  73. import java.util.Calendar;
  74. import java.util.Collection;
  75. import java.util.HashMap;
  76. import java.util.List;
  77. import java.util.Map;
  78. import java.util.Map.Entry;
  79. import java.util.Objects;
  80. import java.util.stream.Collectors;
  81. import lombok.extern.slf4j.Slf4j;
  82. import org.apache.commons.lang3.StringUtils;
  83. import org.springframework.beans.factory.annotation.Autowired;
  84. import org.springframework.beans.factory.annotation.Value;
  85. import org.springframework.stereotype.Service;
  86. /**
  87. * <p>
  88. * TODO
  89. * </p>
  90. *
  91. * @author dengsixing
  92. * @since 2022/4/20
  93. **/
  94. @Slf4j
  95. @Service
  96. public class BuildScenePostServiceImpl implements IBuildScenePostService {
  97. @Value("${main.url}")
  98. private String mainUrl;
  99. @Value("${scene.pro.url}")
  100. private String sceneProUrl;
  101. @Value("${scene.pro.new.url}")
  102. private String sceneProNewUrl;
  103. @Value("${upload.type}")
  104. private String ossType;
  105. @Value("${oss.prefix.ali}")
  106. private String prefixAli;
  107. @Value("${oss.bucket:4dkankan}")
  108. private String bucket;
  109. @Autowired
  110. ISceneService sceneService;
  111. @Autowired
  112. ISceneFileBuildService sceneFileBuildService;
  113. @Autowired
  114. ISceneProService sceneProService;
  115. @Autowired
  116. ISceneProExtService sceneProExtService;
  117. @Autowired
  118. PlatformGoodsClient platformGoodsClient;
  119. @Autowired
  120. PlatformUserClient platformUserClient;
  121. @Autowired
  122. ISceneEditInfoService sceneEditInfoService;
  123. @Autowired
  124. ISceneEditControlsService sceneEditControlsService;
  125. @Autowired
  126. UploadToOssUtil uploadToOssUtil;
  127. // @Autowired
  128. // IBuildScenePreService buildScenePreService;
  129. @Autowired
  130. RedisUtil redisUtil;
  131. @Autowired
  132. IScenePlusService scenePlusService;
  133. @Autowired
  134. IScenePlusExtService scenePlusExtService;
  135. @Autowired
  136. IBuildSceneDTService buildSceneDTService;
  137. @Autowired
  138. ISceneEditInfoExtService sceneEditInfoExtService;
  139. @Override
  140. public void buildScenePost(BuildSceneResultMqMessage message) {
  141. Integer cameraType = Integer.parseInt(message.getCameraType());
  142. String sceneCode = message.getSceneCode();
  143. String fileId = message.getFileId();
  144. Integer payStatus = message.getPayStatus();
  145. Long computeTime = message.getComputeTime();
  146. Boolean buildSuccess = message.getBuildSuccess();
  147. Integer videoVersion = message.getVideoVersion();
  148. Map<String, String> uploadFiles = message.getUploadFiles();
  149. String path = message.getPath();
  150. Integer pushChannel = message.getPushChannel();
  151. String pushToken = message.getPushToken();
  152. String prefix = message.getPrefix();
  153. try {
  154. if(!buildSuccess){//建模失败,修改状态为失败状态
  155. log.info("建模失败,修改状态为失败状态");
  156. // TODO: 2022/3/21 plus版本稳定后删除------------------------start
  157. sceneService.updateStatus(sceneCode, SceneStatus.FAILD.code());
  158. sceneProService.updateStatus(sceneCode, SceneStatus.FAILD.code());
  159. // TODO: 2022/3/21 plus版本稳定后删除------------------------send
  160. scenePlusService.update(
  161. new LambdaUpdateWrapper<ScenePlus>()
  162. .set(ScenePlus::getSceneStatus, SceneStatus.FAILD.code())
  163. .eq(ScenePlus::getNum, sceneCode));
  164. SceneFileBuild sceneFileBuild = sceneFileBuildService.findByFileId(fileId);
  165. if(Objects.nonNull(sceneFileBuild)){
  166. sceneFileBuild.setBuildStatus(BuildStatus.fail.code());
  167. sceneFileBuildService.updateById(sceneFileBuild);
  168. }
  169. return;
  170. }
  171. //建模成功走以下逻辑
  172. log.info("cameraType:{}",cameraType);
  173. //计算场景消耗磁盘空间
  174. long space = this.calUseSpace(uploadFiles);
  175. if(cameraType < 3){
  176. // TODO: 2022/3/21 plus版本稳定后删除------------------------start
  177. sceneService.updateTime(sceneCode, space, payStatus);
  178. // TODO: 2022/3/21 plus版本稳定后删除------------------------send
  179. this.updateDb4Sm(sceneCode, space);
  180. uploadToOssUtil.uploadMulFiles(uploadFiles);
  181. return;
  182. }
  183. //读取计算结果文件生成videosJson
  184. JSONObject videosJson = this.getVideosJson(path, videoVersion, sceneCode, cameraType);
  185. //写入数据库
  186. // TODO: 2022/3/21 plus版本稳定后删除------------------------start
  187. ScenePro scenePro = this.updateDbPro(sceneCode, space, payStatus, videosJson.toJSONString(), computeTime, fileId);
  188. SceneProExt sceneProExt = sceneProExtService.findBySceneProId(scenePro.getId());
  189. // TODO: 2022/3/21 plus版本稳定后删除------------------------send
  190. ScenePlus scenePlus = this.updateDbPlus(sceneCode, space, payStatus, videosJson.toJSONString(), computeTime, fileId);
  191. ScenePlusExt scenePlusExt = scenePlusExtService.getScenePlusExtByPlusId(scenePlus.getId());
  192. //变更容量,arrearCap为true时,代表容量不足
  193. boolean arrearCap = this.updateSpace(scenePlus.getCameraId(), space, scenePlus.getId(), scenePro.getId());
  194. //如果相机容量不足,需要把场景的paystatus改为容量不足状态
  195. this.sealScene(arrearCap, scenePlus.getId(), scenePro.getId());
  196. Object[] editInfoArr = this.updateEditInfo(scenePro, scenePlus);
  197. SceneEditInfo sceneEditInfo = (SceneEditInfo)editInfoArr[0];
  198. SceneEditInfoExt sceneEditInfoExt = (SceneEditInfoExt)editInfoArr[1];
  199. SceneEditControls sceneEditControls = (SceneEditControls)editInfoArr[2];
  200. String sceneUrl = mainUrl + "/" + sceneProNewUrl;
  201. String outPathZh = ConstantFilePath.BASE_PATH + File.separator + "sceneQRcode/"+sceneCode+".png";
  202. String outPathEn = ConstantFilePath.BASE_PATH + File.separator + "sceneQRcode/"+sceneCode+"_en.png";
  203. //生成新的分享的二维码-中文版本
  204. MatrixToImageWriterUtil.createQRCode(sceneUrl + sceneCode, outPathZh, false,null);
  205. //生成新的分享的二维码-英文版本
  206. MatrixToImageWriterUtil.createQRCode(sceneUrl + sceneCode + "&lang=en", outPathEn, false, null);
  207. //上传二维码
  208. uploadToOssUtil.upload(outPathZh, String.format(UploadFilePath.DOWNLOADS_QRCODE, sceneCode) + sceneCode + ".png");
  209. uploadToOssUtil.upload(outPathEn, String.format(UploadFilePath.DOWNLOADS_QRCODE, sceneCode) + sceneCode + "_en.png");
  210. Map<String, String> newUploadFiles = this.uploadFileMapHandler(sceneCode, cameraType, uploadFiles);
  211. // TODO: 2022/5/26 下载全景图采用打包本地文件的方式,这里应该是不需要上传到oss了,暂时保留代码并注释掉------start
  212. //上传计算结果中的caches/images和caches/videos
  213. // this.uploadCaches(sceneCode, uploadFiles, path);
  214. // TODO: 2022/5/26 下载全景图采用打包本地文件的方式,这里应该是不需要上传到oss了,暂时保留代码并注释掉------end
  215. //上传全景图俯视图
  216. this.uploadFloorCad(path, sceneCode, newUploadFiles);
  217. //上传文件
  218. uploadToOssUtil.uploadMulFiles(newUploadFiles);
  219. // TODO: 2022/3/11 同时上传一份到旧版本的目录,用于过渡期使用,待重构版本稳定后删除
  220. uploadToOssUtil.uploadMulFiles(uploadFiles);
  221. //拷贝部分文件到编辑目录,用于用户编辑
  222. this.copyToEditDir(sceneCode);
  223. //生成houseTypejson并上传
  224. this.uploadHouseTypeJson(sceneCode);
  225. // //上传日志文件
  226. // buildScenePreService.uploadLogFile(sceneCode, sceneProExt.getDataSource());
  227. //写scene.json
  228. this.writeSceneJson(sceneCode, videosJson,
  229. sceneEditInfo, sceneEditInfoExt, sceneEditControls, scenePlus, scenePlusExt, arrearCap);
  230. //计算成功,发短信
  231. this.sendSms(pushChannel,pushToken, cameraType, scenePro.getSceneName(), scenePro.getWebSite());
  232. log.info("场景计算结果处理结束,场景码:{}", message.getSceneCode());
  233. }catch (Exception e){
  234. log.error("场景计算结果处理出错", e);
  235. //发送钉钉
  236. String serverPath = message.getPath().substring(0,message.getPath().lastIndexOf("/")+1).concat(prefix);
  237. // buildSceneDTService.handFail("场景计算结果处理出错", serverPath, sceneCode, BuildScenePreServiceImpl.hostName, BuildScenePreServiceImpl.contentExt);
  238. }
  239. }
  240. private void uploadFloorCad(String path, String num, Map<String, String> uploadFiles){
  241. //户型图上传
  242. String userEditPath = UploadFilePath.USER_EDIT_PATH + "floor-cad-%s.%s";
  243. String userViewPath = UploadFilePath.USER_VIEW_PATH + "floor-cad-%s.%s";
  244. String floorCadPath = path + "/results/floorplan_cad";
  245. List<String> floorCadList = FileUtil.getFileList(floorCadPath);
  246. if(CollUtil.isNotEmpty(floorCadList)){
  247. floorCadList.stream().forEach(str->{
  248. String substring = str.substring(str.lastIndexOf(File.separator) + 1);
  249. String[] arr = substring.split("floor");
  250. String[] arr2 = arr[1].split("\\.");
  251. uploadFiles.put(str, String.format(userEditPath, num, arr2[0], arr2[1]));
  252. uploadFiles.put(str, String.format(userViewPath, num, arr2[0], arr2[1]));
  253. });
  254. }
  255. }
  256. private void writeSceneJson(String num, JSONObject videosJson, SceneEditInfo sceneEditInfo,SceneEditInfoExt sceneEditInfoExt,
  257. SceneEditControls sceneEditControls, ScenePlus scenePlus, ScenePlusExt scenePlusExt, boolean arrearCap) throws Exception{
  258. String sceneJsonKey = String.format(UploadFilePath.DATA_VIEW_PATH, num) + "scene.json";
  259. boolean exist = uploadToOssUtil.existKey(sceneJsonKey);
  260. //如果云端没有scene.json文件,生成一份
  261. if(!exist){
  262. SceneJsonBean sceneJson = new SceneJsonBean();
  263. BeanUtil.copyProperties(sceneEditInfoExt, sceneJson);
  264. BeanUtil.copyProperties(sceneEditInfo, sceneJson);
  265. SceneEditControlsVO sceneEditControlsVO = BeanUtil.copyProperties(sceneEditControls, SceneEditControlsVO.class);
  266. sceneJson.setControls(sceneEditControlsVO);
  267. sceneJson.setNum(num);
  268. sceneJson.setCreateTime(scenePlus.getCreateTime());
  269. sceneJson.setSceneResolution(scenePlusExt.getSceneResolution());
  270. sceneJson.setSceneFrom(scenePlusExt.getSceneFrom());
  271. sceneJson.setVideos(JSON.toJSONString(videosJson));
  272. if(arrearCap) {
  273. sceneJson.setPayStatus(PayStatus.NO_CAPACITY.code());
  274. }
  275. String sceneJsonStr = JSON.toJSONString(sceneJson);
  276. //上传sceneJson文件
  277. uploadToOssUtil.upload(sceneJsonStr.getBytes(), sceneJsonKey);
  278. //scenejson写入缓存
  279. redisUtil.set(String.format(RedisKey.SCENE_JSON, num), sceneJsonStr);
  280. }
  281. // TODO: 2022/4/21 plus版本稳定后删除------------------------------------------start
  282. String localSceneJsonPath = String.format(ConstantFilePath.SCENE_PATH_FORMAT, num);
  283. String localSceneJson = FileUtils.readFile(localSceneJsonPath);
  284. if(StrUtil.isNotEmpty(localSceneJson)){
  285. JSONObject sceneJsonObject = JSON.parseObject(localSceneJson);
  286. sceneJsonObject.put("videos", JSON.toJSONString(videosJson));
  287. if(arrearCap) {
  288. sceneJsonObject.put("payStatus", PayStatus.NO_CAPACITY.code());
  289. }
  290. FileUtils.writeFile(localSceneJsonPath, sceneJsonObject.toJSONString());
  291. }
  292. // TODO: 2022/4/21 plus版本稳定后删除------------------------------------------end
  293. }
  294. private void sendSms(Integer pushChannel, String pushToken, int cameraType, String sceneName, String webSite){
  295. log.info("推送消息,渠道是 {}, 手机token是 {}", pushChannel, pushToken);
  296. if(Objects.isNull(pushChannel) && StrUtil.isBlank(pushToken))
  297. return;
  298. try{
  299. if(StorageType.AWS.code().equals(ossType)){
  300. PushMsgUtil
  301. .googlePushMsg(ConstantFilePath.BASE_PATH + "/refreshToken.json", pushToken,
  302. sceneName + "计算完成", "您上传的" + sceneName + "计算完成,点击查看", webSite);
  303. return;
  304. }
  305. PushMessageConfig demo = null;
  306. if(pushChannel == 0){
  307. if(cameraType == 10 || cameraType == 13){
  308. //ios
  309. log.info("IOS_KEY:{}, IOS_SECRET:{}", IOS_KEY_Z, IOS_SECRET_Z);
  310. demo = new PushMessageConfig(IOS_KEY_Z, IOS_SECRET_Z);
  311. demo.sendIOSUnicast(pushToken, "四维看看Minion",
  312. sceneName + "计算完成", "您上传的" + sceneName + "计算完成,点击查看", webSite);
  313. }else {
  314. //ios
  315. log.info("IOS_KEY:{}, IOS_SECRET:{}", IOS_KEY, IOS_SECRET);
  316. demo = new PushMessageConfig(IOS_KEY, IOS_SECRET);
  317. demo.sendIOSUnicast(pushToken, "四维看看Pro",
  318. sceneName + "计算完成", "您上传的" + sceneName + "计算完成,点击查看", webSite);
  319. }
  320. }else {
  321. if(cameraType == 10 || cameraType == 13){
  322. //ios
  323. //安卓
  324. log.info("ANDROID_KEY:{}, ANDROID_SECRET:{}", ANDROID_KEY_Z, ANDROID_SECRET_Z);
  325. demo = new PushMessageConfig(ANDROID_KEY_Z, ANDROID_SECRET_Z);
  326. demo.sendAndroidUnicast2(pushToken, "四维看看Minion",
  327. sceneName + "计算完成", "您上传的" + sceneName + "计算完成,点击查看", webSite);
  328. }else {
  329. //安卓
  330. log.info("ANDROID_KEY:{}, ANDROID_SECRET:{}", ANDROID_KEY, ANDROID_SECRET);
  331. demo = new PushMessageConfig(ANDROID_KEY, ANDROID_SECRET);
  332. demo.sendAndroidUnicast(pushToken, "四维看看Pro",
  333. sceneName + "计算完成", "您上传的" + sceneName + "计算完成,点击查看", webSite);
  334. }
  335. }
  336. log.info("消息推送结束!");
  337. }catch (Exception e){
  338. log.error("推送消息失败:", e);
  339. }
  340. }
  341. private void copyToEditDir(String num) throws IOException {
  342. String editImagesPath = String.format(UploadFilePath.IMG_EDIT_PATH, num);
  343. String viewImagesPath = String.format(UploadFilePath.IMG_VIEW_PATH, num);
  344. String editDataPath = String.format(UploadFilePath.DATA_EDIT_PATH, num);
  345. String viewDataPath = String.format(UploadFilePath.DATA_VIEW_PATH, num);
  346. Map<String, String> map = new HashMap<>();
  347. map.put(editImagesPath + "vision.modeldata", viewImagesPath + "vision.modeldata");
  348. map.put(editImagesPath + "vision2.modeldata", viewImagesPath + "vision2.modeldata");
  349. map.put(editDataPath + "floorplan_cad.json", viewDataPath + "floorplan_cad.json");
  350. for (Entry<String, String> entry : map.entrySet()) {
  351. uploadToOssUtil.copyFiles(entry.getValue(), entry.getKey());
  352. }
  353. }
  354. private void uploadCaches(String num, Map<String,String> map, String path){
  355. String sceneNumPath = String.format("scene/%s", num);
  356. List<String> ossImagesList = uploadToOssUtil.listKeys(String.format(UploadFilePath.IMG_CACHES_PATH, num));
  357. if(CollUtil.isNotEmpty(ossImagesList)){
  358. ossImagesList.parallelStream().forEach(key->{
  359. try {
  360. uploadToOssUtil.delete(key);
  361. } catch (IOException e) {
  362. log.error("删除全景图缓存失败,num:{},key:{}", num, key);
  363. }
  364. });
  365. }
  366. String visionPath = path + "/results/vision.txt";
  367. List<String> panoramaImageList = SceneUtil.getPanoramaImageList(visionPath);
  368. List<String> imagesList = FileUtil.getFileList(path + "/caches/images");
  369. log.info("caches/images_path:{}", path + "/caches/images");
  370. if(CollUtil.isNotEmpty(imagesList)){
  371. log.info("上传的caches/images文件列表:{}", imagesList.toString());
  372. imagesList.stream().forEach(str -> {
  373. String filePath = str.substring(str.lastIndexOf(File.separator) + 1);
  374. if(panoramaImageList.contains(filePath)){
  375. map.put(str, str.replace(path, sceneNumPath));
  376. }
  377. });
  378. }
  379. List<String> ossVideosList = uploadToOssUtil.listKeys(String.format(UploadFilePath.VIDEOS_CACHES_PATH, num));
  380. if(CollUtil.isNotEmpty(ossVideosList)){
  381. ossVideosList.parallelStream().forEach(key->{
  382. try {
  383. uploadToOssUtil.delete(key);
  384. } catch (IOException e) {
  385. log.error("删除球幕视频缓存失败,num:{},key:{}", num, key);
  386. }
  387. });
  388. }
  389. List<String> videosList = FileUtil.getFileList(path + "/caches/videos");
  390. if(CollUtil.isNotEmpty(videosList)){
  391. log.info("上传的caches/videos文件列表:{}", videosList.toString());
  392. videosList.stream().forEach(str -> map.put(str, str.replace(path, sceneNumPath)));
  393. }
  394. }
  395. private Map<String, String> uploadFileMapHandler(String num, int cameraType, Map<String, String> map) {
  396. if(CollUtil.isEmpty(map)){
  397. return null;
  398. }
  399. String dataPath = String.format("data/data%s/", num);
  400. String imagePath = String.format("images/images%s/", num);
  401. String videoPath = String.format("video/video%s/", num);
  402. String dataViewPath = String.format(UploadFilePath.DATA_VIEW_PATH, num);
  403. String imgViewPath = String.format(UploadFilePath.IMG_VIEW_PATH, num);
  404. String videosViewPath = String.format(UploadFilePath.VIDEOS_VIEW_PATH, num);
  405. Map<String, String> newMap = new HashMap<>();
  406. for (String key : map.keySet()) {
  407. String value = map.get(key);
  408. if(value.contains(dataPath)){
  409. newMap.put(key, value.replace(dataPath, dataViewPath));
  410. }
  411. if(value.contains(imagePath)){
  412. newMap.put(key, value.replace(imagePath, imgViewPath));
  413. }
  414. if(value.contains(videoPath)){
  415. newMap.put(key, value.replace(videoPath, videosViewPath));
  416. }
  417. }
  418. return newMap;
  419. }
  420. private JSONObject getVideosJson(String path, Integer videoVersion, String projectNum, int cameraType) throws Exception {
  421. //读取videos_hdr_param.json, 保存点位视频的value
  422. Map<String, Object> videoMap = new HashMap<>();
  423. String videosHdr = FileUtils.readFile(path + File.separator + "results/videos/videos_hdr_param.json");
  424. JSONArray videoArray = null;
  425. if(StringUtils.isNotEmpty(videosHdr)){
  426. videoArray = JSONObject.parseObject(videosHdr).getJSONArray("hdr_param");
  427. }
  428. if(videoArray != null){
  429. for(int i = 0, len = videoArray.size(); i < len; i++) {
  430. videoMap.put(videoArray.getJSONObject(i).getString("name"), videoArray.getJSONObject(i).getString("value"));
  431. if(videoArray.getJSONObject(i).containsKey("fov")){
  432. videoMap.put(videoArray.getJSONObject(i).getString("name") + "_fov", videoArray.getJSONObject(i).getString("fov"));
  433. }
  434. }
  435. }
  436. //获取upload中的video视频名称
  437. String uploadData = FileUtils.readFile(path + File.separator + "results" +File.separator+"upload.json");
  438. JSONObject uploadJson = null;
  439. JSONArray array = null;
  440. if(uploadData!=null) {
  441. uploadJson = JSONObject.parseObject(uploadData);
  442. array = uploadJson.getJSONArray("upload");
  443. }
  444. JSONObject fileJson = null;
  445. String fileName = "";
  446. //计算ts文件的大小,并拼接成json格式
  447. JSONArray jsonArray = new JSONArray();
  448. JSONObject videoJson = null;
  449. JSONObject videosJson = new JSONObject();
  450. long videoSize = 0L;
  451. for(int i = 0, len = array.size(); i < len; i++) {
  452. fileJson = array.getJSONObject(i);
  453. fileName = fileJson.getString("file");
  454. if(fileJson.getIntValue("clazz") == 11 && fileName.contains(".mp4") && !fileName.contains("-ios.mp4")){
  455. videoJson = new JSONObject();
  456. videoJson.put("id", fileName.substring(
  457. 0, fileName.lastIndexOf(".")).replace("videos/", ""));
  458. //如果ts文件存在,就计算ts大小
  459. if(new File(path + File.separator + "results" +File.separator+ fileName.replace(".mp4", ".ts")).exists()){
  460. videoSize = new File(path + File.separator + "results" +File.separator+ fileName.replace(".mp4", ".ts")).length();
  461. videoJson.put("tsSize", videoSize);
  462. }
  463. if(videoMap.containsKey(videoJson.get("id"))){
  464. videoJson.put("value", videoMap.get(videoJson.get("id")));
  465. }
  466. if(videoMap.containsKey(videoJson.get("id") + "_fov")){
  467. videoJson.put("blend_fov", videoMap.get(videoJson.get("id") + "_fov"));
  468. }else {
  469. videoJson.put("blend_fov", 7);
  470. }
  471. jsonArray.add(videoJson);
  472. }
  473. }
  474. videosJson.put("data", jsonArray);
  475. if(Objects.nonNull(videoVersion) && videoVersion >= 4){
  476. videosJson.put("version", 3);
  477. if(StorageType.OSS.code().equals(ossType)){
  478. videosJson.put("upPath", prefixAli + "data/data" + projectNum + "/Up.xml");
  479. }
  480. if(StorageType.AWS.code().equals(ossType)){
  481. videosJson.put("upPath", ConstantUrl.PREFIX_AWS + "data/data" + projectNum + "/Up.xml");
  482. }
  483. if(cameraType == 13){
  484. //转台相机
  485. videosJson.put("upPath", videosJson.getString("upPath").replace(".xml", ".txt"));
  486. }
  487. }else {
  488. videosJson.put("version", 1);
  489. if("oss".equals(ossType)){
  490. videosJson.put("upPath", prefixAli + "data/data" + projectNum + "/Up2.xml");
  491. }
  492. if("aws".equals(ossType)){
  493. videosJson.put("upPath", ConstantUrl.PREFIX_AWS + "data/data" + projectNum + "/Up2.xml");
  494. }
  495. if(cameraType == 13){
  496. //转台相机
  497. videosJson.put("upPath", videosJson.getString("upPath").replace(".xml", ".txt"));
  498. }
  499. }
  500. if(cameraType == 5 || cameraType == 6){
  501. videosJson.put("version", 1);
  502. if("oss".equals(ossType)){
  503. videosJson.put("upPath", prefixAli + "data/data" + projectNum + "/stitch_params.txt");
  504. }
  505. if("aws".equals(ossType)){
  506. videosJson.put("upPath", ConstantUrl.PREFIX_AWS + "data/data" + projectNum + "/stitch_params.txt");
  507. }
  508. }
  509. return videosJson;
  510. }
  511. private Long calUseSpace(Map<String, String> uploadFile) {
  512. File spaceFile = null;
  513. long space = 0L;
  514. for (String key : uploadFile.keySet()) {
  515. spaceFile = new File(key);
  516. if(spaceFile.exists()){
  517. space += spaceFile.length();
  518. }
  519. }
  520. return space;
  521. }
  522. private void sealScene(boolean arrearCap, Long scenePlusId, Long sceneProId){
  523. if(!arrearCap){
  524. return;
  525. }
  526. scenePlusService.update(
  527. new LambdaUpdateWrapper<ScenePlus>()
  528. .set(ScenePlus::getPayStatus, PayStatus.NO_CAPACITY.code())
  529. .eq(ScenePlus::getId, scenePlusId));
  530. // TODO: 2022/3/22 plus版本稳定后删除---------------------------start
  531. sceneProService.update(
  532. new LambdaUpdateWrapper<ScenePro>()
  533. .set(ScenePro::getPayStatus, PayStatus.NO_CAPACITY.code())
  534. .eq(ScenePro::getId, sceneProId));
  535. // TODO: 2022/3/22 plus版本稳定后删除---------------------------end
  536. }
  537. /**
  538. * <p>
  539. 双目场景更新数据库
  540. * </p>
  541. * @author dengsixing
  542. * @date 2022/3/21
  543. * @param num
  544. * @param space
  545. **/
  546. private void updateDb4Sm(String num, long space){
  547. List<ScenePlus> ScenePlusList = scenePlusService.list(
  548. new LambdaQueryWrapper<ScenePlus>()
  549. .select(ScenePlus::getId)
  550. .eq(ScenePlus::getNum, num));
  551. if(CollUtil.isNotEmpty(ScenePlusList))
  552. return ;
  553. List<Long> sceneIds = ScenePlusList.stream().map(scene -> {
  554. return scene.getId();
  555. }).collect(Collectors.toList());
  556. //更新场景创建时间
  557. scenePlusService.update(new LambdaUpdateWrapper<ScenePlus>()
  558. .in(ScenePlus::getId, sceneIds)
  559. .set(ScenePlus::getCreateTime, Calendar.getInstance().getTime())
  560. .set(ScenePlus::getSceneStatus, SceneStatus.NO_DISPLAY.code()));
  561. //更新使用容量
  562. scenePlusExtService.update(
  563. new LambdaUpdateWrapper<ScenePlusExt>()
  564. .in(ScenePlusExt::getPlusId, sceneIds)
  565. .set(ScenePlusExt::getSpace, space));
  566. }
  567. private ScenePro updateDbPro(String sceneCode, Long space, Integer payStatus, String videosJson, Long computeTime, String fileId) throws Exception{
  568. ScenePro scenePro = sceneProService.findBySceneNum(sceneCode);
  569. sceneProService.updateTime(sceneCode, space, payStatus, videosJson, computeTime);
  570. SceneFileBuild sceneFileBuild = sceneFileBuildService.findByFileId(fileId);
  571. if(sceneFileBuild != null){
  572. sceneFileBuild.setBuildStatus(BuildStatus.success.code());
  573. sceneFileBuildService.updateById(sceneFileBuild);
  574. }
  575. if(Objects.nonNull(scenePro)){
  576. SceneProExt sceneProExt = sceneProExtService.findBySceneProId(scenePro.getId());
  577. SceneSource sceneSource = SceneSource.get(sceneProExt.getSceneSource());
  578. switch (sceneSource){
  579. case BM:
  580. sceneProExt.setSceneResolution(SceneResolution.TILES_2K.code());
  581. sceneProExt.setSceneFrom(SceneFrom.PRO.code());
  582. break;
  583. case SM:
  584. sceneProExt.setSceneResolution(SceneResolution.TILES_1K.code());
  585. sceneProExt.setSceneFrom(SceneFrom.LITE.code());
  586. break;
  587. case ZT:
  588. sceneProExt.setSceneResolution(SceneResolution.TILES_4K.code());
  589. sceneProExt.setSceneFrom(SceneFrom.MINION.code());
  590. break;
  591. case JG:
  592. sceneProExt.setSceneResolution(SceneResolution.TILES_4K.code());
  593. sceneProExt.setSceneFrom(SceneFrom.LASER.code());
  594. break;
  595. }
  596. sceneProExtService.updateById(sceneProExt);
  597. }
  598. return scenePro;
  599. }
  600. private ScenePlus updateDbPlus(String num, Long space, Integer payStatus, String videosJson, Long computeTime, String fileId) throws Exception{
  601. ScenePlus scenePlus = scenePlusService.getScenePlusByNum(num);
  602. ScenePlusExt scenePlusExt = scenePlusExtService.getScenePlusExtByPlusId(scenePlus.getId());
  603. List<ScenePlus> list = scenePlusService.list(
  604. new LambdaQueryWrapper<ScenePlus>()
  605. .select(ScenePlus::getId)
  606. .eq(ScenePlus::getNum, num));
  607. if(scenePlus == null || CollUtil.isEmpty(list))
  608. return null;
  609. //修改场景状态 空间 支付状态 计算时间
  610. List<Long> scenePlusIds = list.stream().map(plus -> {
  611. return plus.getId();
  612. }).collect(Collectors.toList());
  613. scenePlusService.update(new LambdaUpdateWrapper<ScenePlus>()
  614. .in(ScenePlus::getId, scenePlusIds)
  615. .set(ScenePlus::getSceneStatus, SceneStatus.NO_DISPLAY.code())
  616. .set(ScenePlus::getCreateTime, Calendar.getInstance().getTime())
  617. .set(ScenePlus::getPayStatus, payStatus));
  618. scenePlusExtService.update(new LambdaUpdateWrapper<ScenePlusExt>()
  619. .in(ScenePlusExt::getPlusId, scenePlusIds)
  620. .set(ScenePlusExt::getSpace, space)
  621. .set(ScenePlusExt::getComputeTime, computeTime)
  622. .set(ScenePlusExt::getVideos, videosJson));
  623. SceneFileBuild sceneFileBuild = sceneFileBuildService.findByFileId(fileId);
  624. if(sceneFileBuild != null){
  625. sceneFileBuild.setBuildStatus(BuildStatus.success.code());
  626. sceneFileBuildService.updateById(sceneFileBuild);
  627. }
  628. SceneSource sceneSource = SceneSource.get(scenePlus.getSceneSource());
  629. switch (sceneSource){
  630. case BM:
  631. scenePlusExt.setSceneResolution(SceneResolution.TILES_2K.code());
  632. scenePlusExt.setSceneFrom(SceneFrom.PRO.code());
  633. break;
  634. case SM:
  635. scenePlusExt.setSceneResolution(SceneResolution.TILES_1K.code());
  636. scenePlusExt.setSceneFrom(SceneFrom.LITE.code());
  637. break;
  638. case ZT:
  639. scenePlusExt.setSceneResolution(SceneResolution.TILES_4K.code());
  640. scenePlusExt.setSceneFrom(SceneFrom.MINION.code());
  641. break;
  642. case JG:
  643. scenePlusExt.setSceneResolution(SceneResolution.TILES_4K.code());
  644. scenePlusExt.setSceneFrom(SceneFrom.LASER.code());
  645. break;
  646. }
  647. scenePlusExtService.updateById(scenePlusExt);
  648. return scenePlus;
  649. }
  650. private boolean updateSpace(Long cameraId, Long space, Long scenePlusId, Long sceneProId) throws Exception{
  651. //更新相机使用用量
  652. ResultData<CameraDetail> resultData = platformGoodsClient.updateCameraDetailByCameraIdAndSpace(cameraId, space);
  653. if(resultData.getCode() != ServerCode.SUCCESS.code()){
  654. log.error("调用platform服务updateCameraDetailByCameraIdAndSpace失败,参数{},{}" ,cameraId, space);
  655. throw new Exception(ServerCode.FEIGN_REQUEST_FAILD.message());
  656. }
  657. CameraDetail cameraDetail = JSONObject.parseObject(JSONObject.toJSONString(resultData.getData()), CameraDetail.class);
  658. ResultData<UserIncrement> resultData1 = platformUserClient.getUserIncrementByCameraId(cameraId);
  659. if(resultData1.getCode() != ServerCode.SUCCESS.code()){
  660. log.error("调用platform服务getUserIncrementByCameraId失败,参数{}" ,cameraId);
  661. throw new Exception(ServerCode.FEIGN_REQUEST_FAILD.message());
  662. }
  663. if( resultData1.getData() == null || resultData1.getData().getIsExpired() == ExpiredStatus.Expired.code()) {
  664. // 新上传的场景,如果总容量小于使用容量,则该大场景保留在临时存储空间30天
  665. if (cameraDetail.getTotalSpace().compareTo(cameraDetail.getUsedSpace()) == -1){
  666. return true;
  667. }
  668. }
  669. return false;
  670. }
  671. private Object[] updateEditInfo(ScenePro scenePro, ScenePlus scenePlus){
  672. SceneEditInfo sceneEditInfo = sceneEditInfoService.getByScenePlusId(scenePlus.getId());
  673. SceneEditControls sceneEditControls = null;
  674. SceneEditInfoExt sceneEditInfoExt = null;
  675. if(sceneEditInfo == null){
  676. sceneEditInfo = new SceneEditInfo();
  677. sceneEditInfo.setScenePlusId(scenePlus.getId());
  678. sceneEditInfo.setSceneProId(scenePro.getId());
  679. sceneEditInfo.setDescription(scenePlus.getDescription());
  680. sceneEditInfo.setTitle(scenePlus.getTitle());
  681. sceneEditInfoService.save(sceneEditInfo);
  682. }else{
  683. sceneEditControls = sceneEditControlsService.getBySceneEditId(sceneEditInfo.getId());
  684. sceneEditInfoExt = sceneEditInfoExtService.getByEditInfoId(sceneEditInfo.getId());
  685. sceneEditInfo.setVersion(sceneEditInfo.getVersion() + 1);
  686. sceneEditInfoService.updateById(sceneEditInfo);
  687. }
  688. if(sceneEditControls == null){
  689. sceneEditControls = new SceneEditControls();
  690. sceneEditControls.setEditInfoId(sceneEditInfo.getId());
  691. sceneEditControlsService.save(sceneEditControls);
  692. }
  693. if(sceneEditInfoExt == null){
  694. sceneEditInfoExt = new SceneEditInfoExt();
  695. sceneEditInfoExt.setScenePlusId(scenePlus.getId());
  696. sceneEditInfoExt.setSceneProId(scenePro.getId());
  697. sceneEditInfoExt.setEditInfoId(sceneEditInfo.getId());
  698. sceneEditInfoExtService.save(sceneEditInfoExt);
  699. }
  700. return new Object[]{sceneEditInfo, sceneEditInfoExt, sceneEditControls};
  701. }
  702. public void uploadHouseTypeJson(String num) throws IOException {
  703. JSONObject result = new JSONObject();
  704. result.put("name", "houseType.json");
  705. result.put("version", "2.1");
  706. String floorplanCadPath = String.format(UploadFilePath.DATA_VIEW_PATH, num) + "floorplan_cad.json";
  707. String floorcadStr = uploadToOssUtil.getObjectContent(this.bucket, floorplanCadPath);
  708. JSONObject floorcadObj = JSON.parseObject(floorcadStr);
  709. JSONArray floors = floorcadObj.getJSONArray("floors");
  710. JSONArray targetFloors = new JSONArray();
  711. result.put("floors", targetFloors);
  712. for(int i = 0; i < floors.size(); i++){
  713. JSONObject floor = (JSONObject)floors.get(i);
  714. JSONArray[] pointsAndWalls = this.createHouseTypeJsonHandler(floor);
  715. JSONArray points = pointsAndWalls[0];
  716. JSONArray walls = pointsAndWalls[1];
  717. JSONObject targetFloor = new JSONObject();
  718. targetFloor.put("points", points);
  719. targetFloor.put("walls", walls);
  720. targetFloors.add(targetFloor);
  721. }
  722. String hourseTypeJsonPath = String.format(UploadFilePath.DATA_VIEW_PATH, num) + "houseType.json";
  723. uploadToOssUtil.upload(result.toJSONString().getBytes(), hourseTypeJsonPath);
  724. cn.hutool.core.io.FileUtil.writeString(result.toJSONString(), "F:\\test\\hourseType.json", StandardCharsets.UTF_8);
  725. }
  726. private JSONArray[] createHouseTypeJsonHandler(JSONObject floor){
  727. JSONArray[] result = new JSONArray[2];
  728. //处理点
  729. Map<Integer, VertexBean> vertexMap = new HashMap<>();
  730. Map<String, PointBean> pointMap = new HashMap<>();
  731. Map<Integer, String> vpMap = new HashMap<>();
  732. JSONArray vertexArr = floor.getJSONArray("vertex-xy");
  733. for(int i = 0; i < vertexArr.size(); i++){
  734. Object o = vertexArr.get(i);
  735. VertexBean vertexBean = JSON.parseObject(JSON.toJSONString(o), VertexBean.class);
  736. Integer vertexId = vertexBean.getId();
  737. vertexMap.put(vertexId, vertexBean);
  738. String pointId = "Point" + i;
  739. pointMap.put(pointId, PointBean.builder().vectorId(pointId).x(vertexBean.getX()).y(vertexBean.getY()).build());
  740. vpMap.put(vertexId, pointId);
  741. }
  742. //处理墙
  743. Map<Integer, SegmentBean> segmentMap = new HashMap<>();
  744. Map<String, WallBean> wallMap = new HashMap<>();
  745. Map<Integer, String> swMap = new HashMap<>();
  746. JSONArray segmentArr = floor.getJSONArray("segment");
  747. Map<String, String> startMap = new HashMap<>();
  748. Map<String, String> endMap = new HashMap<>();
  749. for(int i = 0; i < segmentArr.size(); i++){
  750. Object o = segmentArr.get(i);
  751. SegmentBean segmentBean = JSON.parseObject(JSON.toJSONString(o), SegmentBean.class);
  752. String startPointId = vpMap.get(segmentBean.getA());
  753. String endPointId = vpMap.get(segmentBean.getB());
  754. segmentBean.setStartPointId(startPointId);
  755. segmentBean.setEndPointId(endPointId);
  756. Integer segmentId = segmentBean.getId();
  757. segmentMap.put(segmentId, segmentBean);
  758. String wallId = "Wall" + i;
  759. WallBean wallBean = WallBean.builder()
  760. .vectorId(wallId)
  761. .start(segmentBean.getStartPointId())
  762. .end(segmentBean.getEndPointId())
  763. .children(new String[]{})
  764. .width(0.2d)
  765. .build();
  766. wallMap.put(wallId, wallBean);
  767. startMap.put(wallBean.getStart(), wallBean.getVectorId());
  768. endMap.put(wallBean.getEnd(), wallBean.getVectorId());
  769. swMap.put(segmentId, wallId);
  770. }
  771. Collection<PointBean> pointBeans = pointMap.values();
  772. for (PointBean pointBean : pointBeans) {
  773. Map<String, String> parent = new HashMap<>();
  774. String startParent = startMap.get(pointBean.getVectorId());
  775. String endParent = endMap.get(pointBean.getVectorId());
  776. parent.put(startParent, "start");
  777. parent.put(endParent, "end");
  778. pointBean.setParent(parent);
  779. }
  780. JSONArray pointArr = JSON.parseArray(JSON.toJSONString(pointBeans));
  781. result[0] = pointArr;
  782. Collection<WallBean> wallBeans = wallMap.values();
  783. JSONArray wallArr = JSON.parseArray(JSON.toJSONString(wallBeans));
  784. result[1] = wallArr;
  785. return result;
  786. }
  787. public static void main(String[] args) {
  788. // BuildScenePostServiceImpl.createHouseTypeJson();
  789. }
  790. }