SceneEditInfoServiceImpl.java 82 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883
  1. package com.fdkankan.scene.service.impl;
  2. import cn.hutool.core.bean.BeanUtil;
  3. import cn.hutool.core.collection.CollUtil;
  4. import cn.hutool.core.net.multipart.UploadFile;
  5. import cn.hutool.core.util.StrUtil;
  6. import cn.hutool.core.util.ZipUtil;
  7. import com.alibaba.fastjson.JSON;
  8. import com.alibaba.fastjson.JSONArray;
  9. import com.alibaba.fastjson.JSONObject;
  10. import com.alibaba.fastjson.serializer.SerializerFeature;
  11. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  12. import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
  13. import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
  14. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  15. import com.dtflys.forest.exceptions.ForestRuntimeException;
  16. import com.dtflys.forest.http.ForestRequest;
  17. import com.dtflys.forest.http.ForestResponse;
  18. import com.fdkankan.common.constant.*;
  19. import com.fdkankan.common.exception.BusinessException;
  20. import com.fdkankan.common.response.Result;
  21. import com.fdkankan.common.response.ResultData;
  22. import com.fdkankan.common.util.ComputerUtil;
  23. import com.fdkankan.common.util.CreateObjUtil;
  24. import com.fdkankan.common.util.FileMd5Util;
  25. import com.fdkankan.common.util.FileUtil;
  26. import com.fdkankan.common.util.FileUtils;
  27. import com.fdkankan.common.util.SceneUtil;
  28. import com.fdkankan.fyun.constant.StorageType;
  29. import com.fdkankan.fyun.oss.UploadToOssUtil;
  30. import com.fdkankan.platform.api.feign.PlatformGoodsClient;
  31. import com.fdkankan.platform.api.feign.PlatformUserClient;
  32. import com.fdkankan.platform.api.dto.CameraDetail;
  33. import com.fdkankan.platform.api.dto.Company;
  34. import com.fdkankan.redis.constant.RedisKey;
  35. import com.fdkankan.redis.constant.RedisLockKey;
  36. import com.fdkankan.redis.util.RedisLockUtil;
  37. import com.fdkankan.redis.util.RedisUtil;
  38. import com.fdkankan.scene.api.dto.SceneInfoDTO;
  39. import com.fdkankan.scene.bean.BoxPhotoBean;
  40. import com.fdkankan.scene.bean.RequestSceneProV4;
  41. import com.fdkankan.scene.bean.SceneJsonBean;
  42. import com.fdkankan.scene.bean.TagBean;
  43. import com.fdkankan.scene.callback.FdkkMiniReqErrorCallback;
  44. import com.fdkankan.scene.callback.FdkkMiniReqSuccessCallback;
  45. import com.fdkankan.scene.entity.SceneDataDownload;
  46. import com.fdkankan.scene.entity.SceneEditControls;
  47. import com.fdkankan.scene.entity.SceneEditInfo;
  48. import com.fdkankan.scene.entity.SceneEditInfoExt;
  49. import com.fdkankan.scene.entity.ScenePlus;
  50. import com.fdkankan.scene.entity.ScenePlusExt;
  51. import com.fdkankan.scene.entity.ScenePro;
  52. import com.fdkankan.scene.entity.SceneProV3;
  53. import com.fdkankan.scene.factory.FloorLogoHandlerFactory;
  54. import com.fdkankan.scene.factory.MusicHandlerFactory;
  55. import com.fdkankan.scene.factory.ScreenshotHandlerFactory;
  56. import com.fdkankan.scene.httpclient.FdkankanMiniClient;
  57. import com.fdkankan.scene.mapper.ISceneEditInfoMapper;
  58. import com.fdkankan.scene.service.ISceneDataDownloadService;
  59. import com.fdkankan.scene.service.ISceneEditControlsService;
  60. import com.fdkankan.scene.service.ISceneEditInfoExtService;
  61. import com.fdkankan.scene.service.ISceneEditInfoService;
  62. import com.fdkankan.scene.service.IScenePlusExtService;
  63. import com.fdkankan.scene.service.IScenePlusService;
  64. import com.fdkankan.scene.service.ISceneProExtService;
  65. import com.fdkankan.scene.service.ISceneProService;
  66. import com.fdkankan.scene.service.ISceneUploadService;
  67. import com.fdkankan.scene.vo.*;
  68. import com.google.common.collect.Lists;
  69. import com.google.errorprone.annotations.Var;
  70. import java.io.File;
  71. import java.nio.charset.Charset;
  72. import java.util.ArrayList;
  73. import java.util.Arrays;
  74. import java.util.Comparator;
  75. import java.util.Date;
  76. import java.util.HashMap;
  77. import java.util.HashSet;
  78. import java.util.List;
  79. import java.util.Map;
  80. import java.util.Map.Entry;
  81. import java.util.Set;
  82. import java.util.UUID;
  83. import java.util.stream.Collectors;
  84. import lombok.extern.slf4j.Slf4j;
  85. import org.apache.commons.lang3.StringUtils;
  86. import org.springframework.beans.factory.annotation.Autowired;
  87. import org.springframework.beans.factory.annotation.Value;
  88. import org.springframework.cloud.context.config.annotation.RefreshScope;
  89. import org.springframework.stereotype.Service;
  90. import java.io.IOException;
  91. import java.util.Calendar;
  92. import java.util.Objects;
  93. import org.springframework.transaction.annotation.Transactional;
  94. import org.springframework.util.ObjectUtils;
  95. import org.springframework.web.multipart.MultipartFile;
  96. /**
  97. * <p>
  98. * 服务实现类
  99. * </p>
  100. *
  101. * @author
  102. * @since 2022-01-18
  103. */
  104. @Slf4j
  105. @RefreshScope
  106. @Service
  107. public class SceneEditInfoServiceImpl extends ServiceImpl<ISceneEditInfoMapper, SceneEditInfo> implements ISceneEditInfoService {
  108. @Value("${oss.prefix.ali}")
  109. private String prefixAli;
  110. @Value("${oss.prefix.url}")
  111. private String ossUrlPrefix;
  112. @Value("${upload.type}")
  113. private String type;
  114. @Value("${oss.bucket:4dkankan}")
  115. private String bucket;
  116. @Value("${main.url}")
  117. private String mainUrl;
  118. @Value("${ecs.checkFile.maxTimes:5}")
  119. private int maxCheckTimes;
  120. @Value("${ecs.checkFile.waitTime:5000}")
  121. private int waitTime;
  122. @Value("${http.host.4dkankanMini}")
  123. private String fkankanMiniHost;
  124. @Value("${http.api-v4.getDataSyncByCameraId}")
  125. private String URL_GET_DATASYNC_BY_CAMERAID;
  126. @Value("${http.api-v4.getSceneProByNum}")
  127. private String URL_GET_SCENEPRO_BY_NUM;
  128. @Value("${http.api-v4.upgradeToV4ResultSync}")
  129. private String URL_UPGRADE_TO_V4_RESULT_SYNC;
  130. @Autowired
  131. private ISceneEditControlsService sceneEditControlsService;
  132. @Autowired
  133. private ISceneProService sceneProService;
  134. @Autowired
  135. private UploadToOssUtil uploadToOssUtil;
  136. @Autowired
  137. RedisUtil redisUtil;
  138. @Autowired
  139. private ISceneProExtService sceneProExtService;
  140. @Autowired
  141. private RedisLockUtil redisLockUtil;
  142. @Autowired
  143. ISceneDataDownloadService sceneDataDownloadService;
  144. @Autowired
  145. PlatformGoodsClient platformGoodsClient;
  146. @Autowired
  147. PlatformUserClient platformUserClient;
  148. @Autowired
  149. private ISceneEditInfoExtService sceneEditInfoExtService;
  150. @Autowired
  151. IScenePlusService scenePlusService;
  152. @Autowired
  153. IScenePlusExtService scenePlusExtService;
  154. @Autowired
  155. private FdkankanMiniClient fdkankanMiniClient;
  156. @Autowired
  157. private ISceneUploadService sceneUploadService;
  158. @Transactional
  159. @Override
  160. public SceneEditInfoVO saveScene(SceneEditInfoParamVO param) {
  161. ScenePlus scenePlus = scenePlusService.getScenePlusByNum(param.getNum());
  162. // ScenePro scenePro = sceneProService.findBySceneNum(param.getNum());
  163. if(Objects.isNull(scenePlus)){
  164. throw new BusinessException(ErrorCode.FAILURE_CODE_5005);
  165. }
  166. SceneEditInfo sceneEditInfoDb = this.getByScenePlusId(scenePlus.getId());
  167. SceneEditControls sceneEditControlsDb = null;
  168. if(Objects.nonNull(sceneEditInfoDb)){
  169. sceneEditControlsDb = sceneEditControlsService.getBySceneEditId(sceneEditInfoDb.getId());
  170. }
  171. SceneEditInfo sceneEditInfo = BeanUtil.copyProperties(param, SceneEditInfo.class);
  172. sceneEditInfo.setScenePlusId(scenePlus.getId());
  173. if(Objects.isNull(sceneEditInfoDb)){
  174. this.save(sceneEditInfo);
  175. }else{
  176. sceneEditInfo.setId(sceneEditInfoDb.getId());
  177. sceneEditInfo.setVersion(sceneEditInfoDb.getVersion() + 1);
  178. this.updateById(sceneEditInfo);
  179. }
  180. if(Objects.nonNull(param.getControls())){
  181. SceneEditControls sceneEditControls = BeanUtil.copyProperties(param.getControls(), SceneEditControls.class);
  182. sceneEditControls.setEditInfoId(sceneEditInfo.getId());
  183. if(Objects.isNull(sceneEditControlsDb)){
  184. sceneEditControlsService.save(sceneEditControls);
  185. }else{
  186. sceneEditControls.setId(sceneEditControlsDb.getId());
  187. sceneEditControlsService.updateById(sceneEditControls);
  188. }
  189. }
  190. SceneEditInfoVO result = BeanUtil.copyProperties(param, SceneEditInfoVO.class);
  191. result.setCreateTime(sceneEditInfo.getCreateTime());
  192. return result;
  193. }
  194. @Override
  195. public SceneEditInfo getBySceneProId(long sceneProId) {
  196. return this.getOne(new LambdaQueryWrapper<SceneEditInfo>()
  197. .eq(SceneEditInfo::getSceneProId, sceneProId));
  198. }
  199. @Override
  200. public SceneEditInfo getByScenePlusId(long scenePlusId) {
  201. return this.getOne(new LambdaQueryWrapper<SceneEditInfo>()
  202. .eq(SceneEditInfo::getScenePlusId, scenePlusId));
  203. }
  204. @Transactional
  205. @Override
  206. public ResultData publicScene(SceneEditInfoParamVO param) throws Exception{
  207. String num = param.getNum();
  208. //根据场景码查询数据库
  209. ScenePro scenePro = sceneProService.findBySceneNum(num);
  210. ScenePlus scenePlus = scenePlusService.getScenePlusByNum(num);
  211. ScenePlusExt scenePlusExt = scenePlusExtService.getScenePlusExtByPlusId(scenePlus.getId());
  212. SceneEditInfo sceneEditInfo = this.getByScenePlusId(scenePlus.getId());
  213. SceneEditInfoExt sceneEditInfoExt = null;
  214. SceneEditControls sceneEditControls = null;
  215. if(sceneEditInfo == null){
  216. sceneEditInfo = new SceneEditInfo();
  217. sceneEditInfo.setSceneProId(scenePlus.getId());
  218. }else{
  219. sceneEditControls = sceneEditControlsService.getBySceneEditId(sceneEditInfo.getId());
  220. sceneEditInfoExt = sceneEditInfoExtService.getByEditInfoId(sceneEditInfo.getId());
  221. }
  222. if(sceneEditInfoExt == null){
  223. sceneEditInfoExt = new SceneEditInfoExt();
  224. sceneEditInfoExt.setFloorPlanAngle(0f);
  225. sceneEditInfoExt.setFloorPlanCompass(0f);
  226. }
  227. // if(sceneEditControls == null){
  228. // sceneEditControls = new SceneEditControls();
  229. // }
  230. //生成sceneJson
  231. SceneJsonBean sceneJson = new SceneJsonBean();
  232. BeanUtil.copyProperties(sceneEditInfoExt, sceneJson);
  233. BeanUtil.copyProperties(sceneEditInfo, sceneJson);
  234. SceneEditControlsVO sceneEditControlsVO = BeanUtil.copyProperties(sceneEditControls, SceneEditControlsVO.class);
  235. sceneJson.setControls(sceneEditControlsVO);
  236. sceneJson.setNum(num);
  237. // if(StrUtil.isNotEmpty(sceneEditInfo.getFloorPlanPath())){
  238. // sceneJson.setFloorPlanPaths(sceneEditInfo.getFloorPlanPath().split(","));
  239. // }
  240. sceneJson.setCreateTime(scenePlus.getCreateTime());
  241. sceneJson.setSceneResolution(scenePlusExt.getSceneResolution());
  242. sceneJson.setSceneFrom(scenePlusExt.getSceneFrom());
  243. if(StrUtil.isNotEmpty(scenePlusExt.getVideos())){
  244. sceneJson.setVideos(scenePlusExt.getVideos());
  245. }
  246. //处理热点数据,生成hot.json
  247. this.publicHotData(num, sceneEditInfo);
  248. // TODO: 2022/3/2 这里的逻辑放在上传球幕视频接口中做了,这里先暂时注释掉,以后要删除
  249. //处理球幕视频
  250. // this.buildVideo(sceneEditInfo, sceneProExt.getDataSource(), sceneNum);
  251. //本地写sceneJson文件
  252. String localSceneJsonPath = String.format(ConstantFilePath.SCENE_DATA_PATH_V4, num) + "scene.json";
  253. FileUtils.writeFile(localSceneJsonPath, JSON.toJSONString(sceneJson));
  254. //上传sceneJson文件
  255. String sceneJsonPath = String.format(UploadFilePath.DATA_VIEW_PATH+"scene.json", num);
  256. uploadToOssUtil.upload(JSON.toJSONBytes(sceneJson), sceneJsonPath);
  257. //sceneJson放入缓存
  258. String key = String.format(RedisKey.SCENE_JSON, num);
  259. redisUtil.set(key, JSON.toJSONString(sceneJson));
  260. //删除发布数据中的user目录
  261. String publicUserPath = String.format(UploadFilePath.USER_VIEW_PATH, num);
  262. uploadToOssUtil.deleteFile(publicUserPath);
  263. // String editUserPath = String.format(UploadFilePath.USER_EDIT_PATH, sceneNum);
  264. //复制编辑目录到发布目录
  265. String editPath = String.format(UploadFilePath.USER_EDIT_PATH, param.getNum());
  266. String viewPath = String.format(UploadFilePath.USER_VIEW_PATH, param.getNum());
  267. uploadToOssUtil.copyFiles(editPath,viewPath);
  268. //入库
  269. if(sceneEditInfo.getId() == null){
  270. this.save(sceneEditInfo);
  271. }else{
  272. this.updateById(sceneEditInfo);
  273. }
  274. sceneEditInfoExt.setSceneProId(scenePro.getId());
  275. sceneEditInfoExt.setScenePlusId(scenePlus.getId());
  276. sceneEditInfoExt.setEditInfoId(sceneEditInfo.getId());
  277. sceneEditInfoExtService.saveOrUpdate(sceneEditInfoExt);
  278. // sceneEditControls.setEditInfoId(sceneEditInfo.getId());
  279. // if(sceneEditControls.getId() == null){
  280. // sceneEditControlsService.save(sceneEditControls);
  281. // }else{
  282. // sceneEditControlsService.updateById(sceneEditControls);
  283. // }
  284. // todo 调用v3接口同步场景缩略图url---------------------------------start
  285. String url = fkankanMiniHost + URL_UPGRADE_TO_V4_RESULT_SYNC;
  286. fdkankanMiniClient.upgradeToV4ResultSync(url,
  287. RequestSceneProV4.builder().id(scenePlus.getId()).thumb(scenePlusExt.getThumb()).sceneName(sceneEditInfo.getTitle()).build(),
  288. new FdkkMiniReqSuccessCallback(), new FdkkMiniReqErrorCallback()
  289. );
  290. // todo 调用v3接口同步场景缩略图url---------------------------------end
  291. return ResultData.ok();
  292. }
  293. private void buildVideo(SceneEditInfo sceneEditInfo, String path, String num) throws Exception{
  294. if(CommonStatus.NO.equals(sceneEditInfo.getBuildVideoStatus())){
  295. return;
  296. }
  297. if(path != null && !"".equals(path) && path.startsWith("http")){
  298. path = ConstantFilePath.BUILD_MODEL_PATH + File.separator + path.split("/")[path.split("/").length - 2];
  299. }
  300. String target = path + "_images";
  301. log.info("球幕视频路径:{}", target + File.separator + "extras/video");
  302. File video = new File(target + File.separator + "extras/video");
  303. //如果文件夹或者文件不存在,跳出
  304. if(!video.exists() || video.listFiles() == null || video.listFiles().length == 0){
  305. return;
  306. }
  307. String userEditPath = String.format(UploadFilePath.USER_EDIT_PATH, num);
  308. for(String videoName : video.list()){
  309. log.info("球幕视频名称:{}", videoName);
  310. uploadToOssUtil.upload(target + File.separator + "extras/video/" + videoName,userEditPath + videoName);
  311. CreateObjUtil.mp4ToFlv(target + File.separator + "extras/video/" + videoName,
  312. target + File.separator + "extras/video/" + videoName.replace("mp4", "flv"));
  313. uploadToOssUtil.upload(target + File.separator + "extras/video/" + videoName.replace("mp4", "flv"),userEditPath + videoName.replace("mp4", "flv"));
  314. //覆盖原始视频资源
  315. FileUtils.copyFile(target + File.separator + "extras/video/" + videoName,
  316. path + File.separator + "caches/videos/" + videoName, true);
  317. }
  318. FileUtils.deleteDirectory(target + File.separator + "extras/video/");
  319. //重置状态
  320. sceneEditInfo.setBuildVideoStatus(CommonStatus.NO.code());
  321. }
  322. private void publicHotData(String sceneNum, SceneEditInfo sceneEditInfo) throws IOException {
  323. String hotDataKey = String.format(RedisKey.SCENE_HOT_DATA, sceneNum);
  324. Map<String, String> hotMap = redisUtil.hmget(hotDataKey);
  325. List<String> hotList = Lists.newArrayList();
  326. hotMap.entrySet().stream().forEach(entry->{
  327. if(StrUtil.isNotEmpty(entry.getValue())){
  328. hotList.add(entry.getValue());
  329. }
  330. });
  331. if(CollUtil.isNotEmpty(hotList)){
  332. JSONArray jsonhots = new JSONArray();
  333. hotList.stream().forEach(hot->{
  334. jsonhots.add(JSONObject.parseObject(hot));
  335. });
  336. String hotJsonPath = String.format(UploadFilePath.USER_EDIT_PATH, sceneNum) + "hot.json";
  337. uploadToOssUtil.upload(jsonhots.toString().getBytes(), hotJsonPath);
  338. //修改tags状态为是,标识有热点数据
  339. this.saveTagsToSceneEditInfo(sceneNum, sceneEditInfo);
  340. }
  341. }
  342. @Override
  343. public SceneInfoVO getSceneInfo(@Var SceneInfoParamVO param) throws Exception{
  344. SceneInfoReqType sceneInfoReqType = SceneInfoReqType.get(param.getReqType());
  345. switch (sceneInfoReqType){
  346. //如果是编辑页面请求,查数据库
  347. case EDIT:
  348. return this.getSceneInfo4Edit(param.getNum());
  349. //如果是查看页面请求,查redis
  350. case VIEW:
  351. return this.getSceneInfo4View(param.getNum());
  352. }
  353. return null;
  354. }
  355. @Override
  356. public SceneInfoDTO getSceneInfo4Download(SceneInfoParamVO param) throws Exception{
  357. SceneInfoVO sceneInfo4View = this.getSceneInfo4View(param.getNum());
  358. if(Objects.isNull(sceneInfo4View)){
  359. return null;
  360. }
  361. SceneInfoDTO sceneInfoDTO = BeanUtil.copyProperties(sceneInfo4View, SceneInfoDTO.class);
  362. ScenePlus scenePlus = scenePlusService.getScenePlusByNum(param.getNum());
  363. sceneInfoDTO.setSceneSource(scenePlus.getSceneSource());
  364. return sceneInfoDTO;
  365. }
  366. /**
  367. * <p>
  368. 编辑页面获取场景详情信息
  369. * </p>
  370. * @author dengsixing
  371. * @date 2022/1/21
  372. * @param num
  373. * @return com.fdkankan.scene.vo.SceneInfoVO
  374. **/
  375. private SceneInfoVO getSceneInfo4Edit(String num) throws Exception{
  376. // ScenePro scenePro = sceneProService.findBySceneNum(num);
  377. ScenePlus scenePlus = scenePlusService.getScenePlusByNum(num);
  378. if(Objects.isNull(scenePlus)){
  379. throw new BusinessException(ErrorCode.FAILURE_CODE_5005);
  380. }
  381. // SceneProExt sceneProExt = sceneProExtService.findBySceneProId(scenePro.getId());
  382. ScenePlusExt scenePlusExt = scenePlusExtService.getScenePlusExtByPlusId(scenePlus.getId());
  383. SceneEditInfo sceneEditInfo = this.getByScenePlusId(scenePlus.getId());
  384. if(Objects.isNull(sceneEditInfo)){
  385. throw new BusinessException(ErrorCode.FAILURE_CODE_5005);
  386. }
  387. SceneEditInfoExt sceneEditInfoExt = sceneEditInfoExtService.getByEditInfoId(sceneEditInfo.getId());
  388. SceneEditControls sceneEditControls = sceneEditControlsService.getBySceneEditId(sceneEditInfo.getId());
  389. SceneInfoVO sceneInfoVO = new SceneInfoVO();
  390. BeanUtil.copyProperties(sceneEditInfoExt, sceneInfoVO);
  391. BeanUtil.copyProperties(sceneEditInfo, sceneInfoVO);
  392. if(Objects.isNull(sceneInfoVO.getFloorPlanAngle())){
  393. sceneInfoVO.setFloorPlanAngle(0f);
  394. }
  395. if(Objects.isNull(sceneInfoVO.getFloorPlanCompass())){
  396. sceneInfoVO.setFloorPlanCompass(0f);
  397. }
  398. sceneInfoVO.setControls(BeanUtil.copyProperties(sceneEditControls, SceneEditControlsVO.class));
  399. sceneInfoVO.setNum(num);
  400. //生成sceneJson
  401. // if(StrUtil.isNotEmpty(sceneEditInfo.getFloorPlanPath())){
  402. // sceneInfoVO.setFloorPlanPaths(sceneEditInfo.getFloorPlanPath().split(","));
  403. // }
  404. sceneInfoVO.setCreateTime(scenePlus.getCreateTime());
  405. sceneInfoVO.setSceneResolution(scenePlusExt.getSceneResolution());
  406. sceneInfoVO.setSceneFrom(scenePlusExt.getSceneFrom());
  407. sceneInfoVO.setVideos(scenePlusExt.getVideos());
  408. // TODO: 2022/4/24 v3版本停机要切换---------------------------start
  409. // this.setExtData(sceneInfoVO, scenePlus.getCameraId());
  410. this.setExtDataFromV3(sceneInfoVO, scenePlus.getCameraId());
  411. // TODO: 2022/4/24 v3版本停机要切换---------------------------end
  412. return sceneInfoVO;
  413. }
  414. /**
  415. * <p>
  416. getInfo接口返回字段扩展
  417. * </p>
  418. * @author dengsixing
  419. * @date 2022/3/9
  420. **/
  421. private void setExtDataFromV3(SceneInfoVO sceneInfoVO, Long cameraId) throws Exception{
  422. if(Objects.isNull(cameraId)){
  423. return;
  424. }
  425. String url = fkankanMiniHost + URL_GET_DATASYNC_BY_CAMERAID + cameraId;
  426. Result<String> dataSyncResult = fdkankanMiniClient
  427. .getDataSyncType(url, new FdkkMiniReqSuccessCallback(), new FdkkMiniReqErrorCallback());
  428. sceneInfoVO.setDataSync(dataSyncResult.getMsg());
  429. }
  430. /**
  431. * <p>
  432. getInfo接口返回字段扩展
  433. * </p>
  434. * @author dengsixing
  435. * @date 2022/3/9
  436. **/
  437. private void setExtData(SceneInfoVO sceneInfoVO, Long cameraId) throws Exception{
  438. ResultData<CameraDetail> cameraResultData = platformGoodsClient.getCameraDetailByCameraId(cameraId);
  439. if(!cameraResultData.getSuccess()){
  440. throw new Exception(ServerCode.FEIGN_REQUEST_FAILD.message());
  441. }
  442. CameraDetail camera = cameraResultData.getData();
  443. if(Objects.isNull(camera)){
  444. throw new BusinessException(ErrorCode.FAILURE_CODE_7010);
  445. }
  446. if(Objects.isNull(camera.getCompanyId())){
  447. return;
  448. }
  449. ResultData<Company> companyResultData = platformUserClient.getCompanyById(camera.getCompanyId());
  450. if(!companyResultData.getSuccess()){
  451. throw new Exception(ServerCode.FEIGN_REQUEST_FAILD.message());
  452. }
  453. Company company = companyResultData.getData();
  454. if(Objects.isNull(company)){
  455. return;
  456. }
  457. sceneInfoVO.setDataSync(company.getDataSync());
  458. }
  459. /**
  460. * <p>
  461. 查看页面获取场景详情信息
  462. * </p>
  463. * @author dengsixing
  464. * @date 2022/1/21
  465. * @param num
  466. * @return com.fdkankan.scene.vo.SceneInfoVO
  467. **/
  468. private SceneInfoVO getSceneInfo4View(String num) throws Exception{
  469. // TODO: 2022/5/19 请求v3的接口,获取场景的状态 待v3停机后要换成v4d的逻辑-------------------------start
  470. String url = fkankanMiniHost + URL_GET_SCENEPRO_BY_NUM + num;
  471. Result<SceneProV3> result = fdkankanMiniClient
  472. .getSceneProByNum(url, new FdkkMiniReqSuccessCallback(),
  473. new FdkkMiniReqErrorCallback());
  474. SceneProV3 sceneProV3 = result.getData();
  475. if(sceneProV3 == null || sceneProV3.getId() == null){
  476. throw new BusinessException(ErrorCode.FAILURE_CODE_5005);
  477. }
  478. if(sceneProV3.getRecStatus().equals("I")){
  479. throw new BusinessException(ErrorCode.FAILURE_CODE_5009);
  480. }
  481. if(SceneStatus.SUCCESS.code() != sceneProV3.getStatus()
  482. && SceneStatus.NO_DISPLAY.code() != sceneProV3.getStatus()){
  483. throw new BusinessException(ErrorCode.FAILURE_CODE_5033);
  484. }
  485. if(PayStatus.PAY.code() != sceneProV3.getPayStatus()){
  486. throw new BusinessException(ErrorCode.FAILURE_CODE_5034);
  487. }
  488. // TODO: 2022/5/19 请求v3的接口,获取场景的状态 待v3停机后要换成v4d的逻辑-------------------------send
  489. // ScenePro scenePro = sceneProService.findBySceneNum(num);
  490. ScenePlus scenePlus = scenePlusService.getScenePlusByNum(num);
  491. if(Objects.isNull(scenePlus)){
  492. throw new BusinessException(ErrorCode.FAILURE_CODE_5005);
  493. }
  494. String key = String.format(RedisKey.SCENE_JSON, num);
  495. String sceneJson = redisUtil.get(key);
  496. SceneInfoVO sceneInfoVO = null;
  497. //先查询redis
  498. if(StrUtil.isNotEmpty(sceneJson)) {
  499. sceneInfoVO = JSON.parseObject(sceneJson, SceneInfoVO.class);
  500. // TODO: 2022/4/24 v3版本停机要切换---------------------------start
  501. // this.setExtData(sceneInfoVO, scenePlus.getCameraId());
  502. this.setExtDataFromV3(sceneInfoVO, scenePlus.getCameraId());
  503. // TODO: 2022/4/24 v3版本停机要切换---------------------------end
  504. sceneInfoVO.setScenePassword(null);
  505. if(Objects.isNull(sceneInfoVO.getFloorPlanAngle())){
  506. sceneInfoVO.setFloorPlanAngle(0f);
  507. }
  508. if(Objects.isNull(sceneInfoVO.getFloorPlanCompass())){
  509. sceneInfoVO.setFloorPlanCompass(0f);
  510. }
  511. return sceneInfoVO;
  512. }
  513. //如果redis没找到,从scene.json中获取
  514. String objectName = String.format(ConstantFilePath.SCENE_VIEW_DATA_DATA_SCENEJSON, num);
  515. String objectContent = uploadToOssUtil.getObjectContent(bucket, objectName);
  516. if(StrUtil.isEmpty(objectContent))
  517. return null;
  518. sceneInfoVO = JSON.parseObject(objectContent, SceneInfoVO.class);
  519. // TODO: 2022/4/24 v3版本停机要切换---------------------------start
  520. // this.setExtData(sceneInfoVO, scenePlus.getCameraId());
  521. this.setExtDataFromV3(sceneInfoVO, scenePlus.getCameraId());
  522. // TODO: 2022/4/24 v3版本停机要切换---------------------------end
  523. if(Objects.isNull(sceneInfoVO.getFloorPlanAngle())){
  524. sceneInfoVO.setFloorPlanAngle(0f);
  525. }
  526. if(Objects.isNull(sceneInfoVO.getFloorPlanCompass())){
  527. sceneInfoVO.setFloorPlanCompass(0f);
  528. }
  529. redisUtil.set(key, JSON.toJSONString(sceneInfoVO));
  530. sceneInfoVO.setScenePassword(null);
  531. return sceneInfoVO;
  532. }
  533. @Override
  534. // @Transactional
  535. public ResultData saveUpload(SaveUploadParamVO param) throws Exception {
  536. FileBizType fileBizType = FileBizType.get(param.getBizType());
  537. String num = param.getNum();
  538. String fileInfo = param.getFileInfo();
  539. if(Objects.isNull(fileBizType)){
  540. throw new BusinessException(ErrorCode.FAILURE_CODE_7003);
  541. }
  542. switch (fileBizType){
  543. case MUSIC:
  544. return new MusicHandlerFactory().getHandler().save(num, fileInfo);
  545. case FLOOR_LOGO:
  546. return new FloorLogoHandlerFactory().getHandler().save(num, fileInfo);
  547. // case FLOOR_PLAN:
  548. // return new FloorPlanHandlerFactory().getHandler().save(num, fileInfo);
  549. case SCREENSHOT:
  550. return new ScreenshotHandlerFactory().getHandler().save(num, fileInfo);
  551. }
  552. return ResultData.ok();
  553. }
  554. @Override
  555. public ResultData saveCad(BaseDataParamVO param) throws Exception {
  556. String num = param.getNum();
  557. // ScenePro scenePro = sceneProService.findBySceneNum(num);
  558. ScenePlus scenePlus = scenePlusService.getScenePlusByNum(num);
  559. if(Objects.isNull(scenePlus))
  560. throw new BusinessException(ErrorCode.FAILURE_CODE_5005);
  561. String editUserPath = String.format(UploadFilePath.USER_EDIT_PATH, num);
  562. String localDataPath = String.format(ConstantFilePath.SCENE_USER_PATH_V4, num);
  563. JSONObject fileInfoJson = JSON.parseObject(param.getData());
  564. Float angel = null;
  565. Float compass = null;
  566. String floorJsonData = fileInfoJson.getString("floorJsonData");
  567. if(StrUtil.isNotBlank(floorJsonData)){
  568. JSONObject jsonObject = JSON.parseObject(floorJsonData);
  569. angel = jsonObject.getFloat("angle");
  570. compass = jsonObject.getFloat("compass");
  571. }
  572. //处理户型图数据
  573. //上传floorplan_user.json文件
  574. FileUtils.writeFile(localDataPath + "floorplan_user.json", floorJsonData);
  575. uploadToOssUtil.upload(localDataPath + "floorplan_user.json", editUserPath + "floorplan_user.json");
  576. //写入数据库
  577. Byte floorPlanUser = null;
  578. if(StrUtil.isNotEmpty(floorJsonData)){
  579. floorPlanUser = CommonStatus.YES.code();
  580. }
  581. SceneEditInfo sceneEditInfoDb = this.getByScenePlusId(scenePlus.getId());
  582. if(Objects.nonNull(sceneEditInfoDb)){
  583. LambdaUpdateWrapper<SceneEditInfo> updateWrapper = new LambdaUpdateWrapper<SceneEditInfo>()
  584. .setSql("version=version+" + 1)
  585. .eq(SceneEditInfo::getId, sceneEditInfoDb.getId());
  586. if(floorPlanUser != null){
  587. updateWrapper.set(SceneEditInfo::getFloorPlanUser, floorPlanUser);
  588. }
  589. this.update(updateWrapper);
  590. }else{
  591. sceneEditInfoDb = new SceneEditInfo();
  592. sceneEditInfoDb.setScenePlusId(scenePlus.getId());
  593. sceneEditInfoDb.setFloorPlanUser(floorPlanUser);
  594. this.save(sceneEditInfoDb);
  595. }
  596. SceneEditInfoExt sceneEditInfoExt = sceneEditInfoExtService.getByEditInfoId(sceneEditInfoDb.getId());
  597. if(Objects.isNull(sceneEditInfoExt)){
  598. sceneEditInfoExt = new SceneEditInfoExt();
  599. sceneEditInfoExt.setEditInfoId(sceneEditInfoDb.getId());
  600. }
  601. if(Objects.nonNull(angel)){
  602. sceneEditInfoExt.setFloorPlanAngle(angel);
  603. }
  604. if(Objects.nonNull(compass)){
  605. sceneEditInfoExt.setFloorPlanCompass(compass);
  606. }
  607. sceneEditInfoExtService.saveOrUpdate(sceneEditInfoExt);
  608. return ResultData.ok();
  609. }
  610. @Override
  611. public ResultData resetCad(String num){
  612. ScenePlus scenePlus = scenePlusService.getScenePlusByNum(num);
  613. if(Objects.isNull(scenePlus))
  614. throw new BusinessException(ErrorCode.FAILURE_CODE_5005);
  615. ScenePlusExt scenePlusExt = scenePlusExtService
  616. .getScenePlusExtByPlusId(scenePlus.getId());
  617. String path = scenePlusExt.getDataSource();
  618. //户型图上传
  619. String userEditPath = UploadFilePath.USER_EDIT_PATH + "floor-cad-%s.%s";
  620. String floorCadPath = path + "/results/floorplan_cad";
  621. List<String> floorCadList = FileUtil.getFileList(floorCadPath);
  622. if(CollUtil.isNotEmpty(floorCadList)){
  623. floorCadList.stream().forEach(str->{
  624. String substring = str.substring(str.lastIndexOf(File.separator) + 1);
  625. String[] arr = substring.split("floor");
  626. String[] arr2 = arr[1].split("\\.");
  627. uploadToOssUtil.upload(str, String.format(userEditPath, num, arr2[0], arr2[1]));
  628. });
  629. }
  630. ScenePro scenePro = sceneProService.findBySceneNum(num);
  631. SceneEditInfo sceneEditInfoDb = this.getByScenePlusId(scenePlus.getId());
  632. SceneEditInfoExt sceneEditInfoExt = null;
  633. if(Objects.nonNull(sceneEditInfoDb)){
  634. LambdaUpdateWrapper<SceneEditInfo> updateWrapper = new LambdaUpdateWrapper<SceneEditInfo>()
  635. .setSql("version=version+" + 1)
  636. .set(SceneEditInfo::getFloorPlanUser, CommonStatus.NO.code())
  637. .eq(SceneEditInfo::getId, sceneEditInfoDb.getId());
  638. this.update(updateWrapper);
  639. sceneEditInfoExt = sceneEditInfoExtService.getByEditInfoId(sceneEditInfoDb.getId());
  640. }else{
  641. sceneEditInfoDb = new SceneEditInfo();
  642. sceneEditInfoDb.setSceneProId(scenePro.getId());
  643. sceneEditInfoDb.setScenePlusId(scenePlus.getId());
  644. sceneEditInfoDb.setFloorPlanUser(CommonStatus.NO.code());
  645. this.save(sceneEditInfoDb);
  646. }
  647. if(Objects.isNull(sceneEditInfoExt)){
  648. sceneEditInfoExt = new SceneEditInfoExt();
  649. sceneEditInfoExt.setSceneProId(scenePro.getId());
  650. sceneEditInfoDb.setScenePlusId(scenePlus.getId());
  651. sceneEditInfoExt.setEditInfoId(sceneEditInfoDb.getId());
  652. }
  653. sceneEditInfoExt.setFloorPlanAngle(0f);
  654. sceneEditInfoExt.setFloorPlanCompass(0f);
  655. sceneEditInfoExtService.saveOrUpdate(sceneEditInfoExt);
  656. return ResultData.ok();
  657. }
  658. @Override
  659. public ResultData renameCad(RenameCadParamVO param) throws IOException {
  660. String key = String.format(UploadFilePath.DATA_VIEW_PATH, param.getNum()) + "floorplan_cad.json";
  661. String floorplanCadJson = uploadToOssUtil.getObjectContent(this.bucket, key);
  662. if(StrUtil.isEmpty(floorplanCadJson)){
  663. return ResultData.ok();
  664. }
  665. Map<Integer, String> renameMap = new HashMap<>();
  666. param.getFloors().stream().forEach(floor->{
  667. renameMap.put(floor.getSubgroup(), floor.getName());
  668. });
  669. JSONObject jsonObject = JSON.parseObject(floorplanCadJson);
  670. if(Objects.isNull(jsonObject)){
  671. return ResultData.ok();
  672. }
  673. JSONArray floorArr = jsonObject.getJSONArray("floors");
  674. if(CollUtil.isEmpty(floorArr)){
  675. return ResultData.ok();
  676. }
  677. for (Object o : floorArr) {
  678. JSONObject item = (JSONObject)o;
  679. int subgroup = item.getIntValue("subgroup");
  680. String name = renameMap.get(subgroup);
  681. if(StrUtil.isEmpty(name)){
  682. continue;
  683. }
  684. item.put("name", name);
  685. }
  686. uploadToOssUtil.upload(jsonObject.toJSONString().getBytes(), key);
  687. return ResultData.ok();
  688. }
  689. private JSONObject updateFloorName(String sourceFloors, String targeFloors){
  690. JSONArray sourceFloorsJson = JSON.parseArray(sourceFloors);
  691. JSONObject targeFloorsJson = JSONObject.parseObject(targeFloors);
  692. JSONArray array = targeFloorsJson.getJSONArray("floors");
  693. for (int i = 0; i < array.size(); ++i) {
  694. JSONObject targetFloor = array.getJSONObject(i);
  695. int targetId = targetFloor.getIntValue("id");
  696. for (int j = 0; j < sourceFloorsJson.size(); ++j) {
  697. JSONObject floor = sourceFloorsJson.getJSONObject(j);
  698. int id = floor.getIntValue("id");
  699. String name = floor.getString("name");
  700. if (targetId != id)
  701. continue;
  702. targetFloor.put("name", name);
  703. }
  704. }
  705. return targeFloorsJson;
  706. }
  707. @Override
  708. public void upgradeVersionById(Long id) {
  709. this.update(new LambdaUpdateWrapper<SceneEditInfo>()
  710. .setSql("version=version+" + 1)
  711. .eq(SceneEditInfo::getId, id));
  712. }
  713. @Override
  714. public ResultData uploadPanorama(String num, MultipartFile file) throws Exception {
  715. //校验压缩包格式
  716. // log.info("全景图压缩包文件名:" + file.getOriginalFilename());
  717. // if(!file.getOriginalFilename().endsWith(".zip")){
  718. // throw new BusinessException(ErrorCode.FAILURE_CODE_7015);
  719. // }
  720. if(!file.getOriginalFilename().endsWith(".zip") && !file.getOriginalFilename().endsWith(".jpg")){
  721. throw new BusinessException(ErrorCode.FAILURE_CODE_7007, "jpg或者zip");
  722. }
  723. // ScenePro scenePro = sceneProService.findBySceneNum(num);
  724. ScenePlus scenePlus = scenePlusService.getScenePlusByNum(num);
  725. if(scenePlus == null){
  726. throw new BusinessException(ErrorCode.FAILURE_CODE_5005);
  727. }
  728. // SceneProExt sceneProExt = sceneProExtService.findBySceneProId(scenePro.getId());
  729. ScenePlusExt scenePlusExt = scenePlusExtService
  730. .getScenePlusExtByPlusId(scenePlus.getId());
  731. //原始计算根目录
  732. // String path = "F:\\test";
  733. String path = scenePlusExt.getDataSource();
  734. //全景图计算根目录
  735. String target = path + "_images";
  736. //解压缩文件存放目录
  737. String targetImagesPath = target + "/extras/images/";
  738. //压缩文件保存目录
  739. String zipTargetFilePath = targetImagesPath + file.getOriginalFilename();
  740. //压缩包保存到本地
  741. // String cachePath = String.format(ConstantFilePath.SCENE_CACHE_IMAGES, num);
  742. //先删除本地文件
  743. FileUtils.deleteDirectory(targetImagesPath);
  744. File targetFile = new File(zipTargetFilePath);
  745. if(!targetFile.getParentFile().exists()){
  746. targetFile.getParentFile().mkdirs();
  747. }
  748. file.transferTo(targetFile);
  749. //如果是压缩包上传,需要解压缩
  750. if(file.getOriginalFilename().endsWith(".zip")){
  751. //解压zip包
  752. FileUtil.unZip(zipTargetFilePath, targetImagesPath);
  753. // FileUtils.decompress(zipTargetFilePath, targetImagesPath);
  754. //删除压缩包
  755. FileUtil.delFile(zipTargetFilePath);
  756. }
  757. //判断文件夹目录结构,图片必须放在压缩包根目录下,不支持空文件夹或其他格式文件上传
  758. File[] files = new File(targetImagesPath).listFiles();
  759. Arrays.stream(files).forEach(item->{
  760. if(item.isDirectory()){
  761. throw new BusinessException(ErrorCode.FAILURE_CODE_7018);
  762. }
  763. });
  764. //获取解压后的文件列表
  765. List<String> uploadFileList = FileUtil.getFileList(targetImagesPath);
  766. if(CollUtil.isEmpty(uploadFileList)){
  767. throw new BusinessException(ErrorCode.FAILURE_CODE_5062);
  768. }
  769. //判断是否有可用的jpg文件
  770. boolean existJpg = false;
  771. if(CollUtil.isNotEmpty(uploadFileList)){
  772. existJpg = uploadFileList.stream().anyMatch(str -> {
  773. if(str.endsWith(".jpg")){
  774. return true;
  775. }
  776. return false;
  777. });
  778. }
  779. if(!existJpg){
  780. throw new BusinessException(ErrorCode.FAILURE_CODE_5062);
  781. }
  782. //列出caches/images中的文件列表
  783. String imgCachePath = String.format(UploadFilePath.IMG_CACHES_PATH, num);
  784. //比对图片列表,不存在的要返回名称集合
  785. String visionPath = path + "/results/vision.txt";
  786. List<String> panoramaImageList = SceneUtil.getPanoramaImageList(visionPath);
  787. List<String> notExistFileList = uploadFileList.stream().filter(filePath -> {
  788. filePath = filePath.substring(filePath.lastIndexOf(File.separator) + 1);
  789. if(panoramaImageList.contains(filePath)){
  790. return false;
  791. }
  792. return true;
  793. }).collect(Collectors.toList());
  794. if(CollUtil.isNotEmpty(notExistFileList)){
  795. //删除错误文件
  796. notExistFileList.parallelStream().forEach(filePath->{
  797. FileUtils.deleteFile(filePath);
  798. });
  799. }
  800. //判断成功的图片,如果成功图片为0,就直接返回,不需要执行算法
  801. uploadFileList = FileUtil.getFileList(targetImagesPath);
  802. if(CollUtil.isEmpty(uploadFileList)){
  803. if(CollUtil.isNotEmpty(notExistFileList)){
  804. notExistFileList = notExistFileList.stream().map(filePath -> {
  805. return filePath.substring(filePath.lastIndexOf(File.separator) + 1);
  806. }).collect(Collectors.toList());
  807. }
  808. return ResultData.ok(new UploadPanoramaVO(0, notExistFileList));
  809. }
  810. //上传
  811. Map<String, String> map = new HashMap<>();
  812. // uploadFileList.stream().forEach(filePath->{
  813. // map.put(filePath, filePath.replace(targetImagesPath, imgCachePath));
  814. // });
  815. String imgViewPath = String.format(UploadFilePath.IMG_VIEW_PATH, num);
  816. FileUtils.downLoadFromUrl(ossUrlPrefix + imgViewPath + "vision.modeldata" + "?m="+new Date().getTime(),
  817. "vision.modeldata", target + File.separator + "extras" + File.separator);
  818. CreateObjUtil.convertVisionmodeldataToTxt(target + File.separator + "extras" + File.separator + "vision.modeldata",
  819. target + File.separator + "extras" + File.separator + "vision.txt");
  820. FileUtils.copyFile(path + File.separator + "data.json", target + File.separator+"data.json", true);
  821. FileUtils.copyFile(path + File.separator + "project.json", target + File.separator+"project.json", true);
  822. //data.json增加extras为执行重建算法
  823. String data = FileUtils.readFile(target + File.separator+"data.json");
  824. if(data != null){
  825. JSONObject floorplanJson = new JSONObject();
  826. floorplanJson.put("has_source_images", true);
  827. floorplanJson.put("has_vision_txt", true);
  828. JSONObject dataJson = JSONObject.parseObject(data);
  829. dataJson.put("extras", floorplanJson);
  830. //V5表示不需要生成high,low文件
  831. dataJson.put("skybox_type", "SKYBOX_V6");
  832. if(scenePlusExt.getSceneScheme() == 11){
  833. dataJson.put("skybox_type", "SKYBOX_V7");
  834. }
  835. dataJson.put("split_type", "SPLIT_V8");
  836. //sceneScheme为3切成瓦片图
  837. if(scenePlusExt.getSceneScheme() == 3){
  838. dataJson.put("skybox_type", "SKYBOX_V4");
  839. }
  840. FileUtils.writeFile(target + File.separator+"data.json", new String(dataJson.toString().getBytes(), "UTF-8"));
  841. }
  842. if(new File(target + File.separator + "capture").exists()){
  843. new File(target + File.separator + "capture").delete();
  844. }
  845. if(new File(target + File.separator + "results").exists()){
  846. FileUtils.delAllFile(target + File.separator + "results");
  847. }
  848. if(StorageType.AWS.code().equals(this.type)){
  849. //亚马逊保持旧方式,超链接capture
  850. CreateObjUtil.createSoftConnection(path + File.separator + "capture", target + File.separator + "capture");
  851. }
  852. CreateObjUtil.ossUtilCp(ConstantFilePath.OSS_PREFIX + path.replace(ConstantFilePath.BUILD_MODEL_PATH, "") + "/data.fdage", target + File.separator + "capture/");
  853. CreateObjUtil.build3dModel(target , "1");
  854. String uploadJsonPath= target + File.separator + "results" +File.separator+"upload.json";
  855. Thread.sleep(2000);
  856. boolean exist = ComputerUtil.checkComputeCompleted(uploadJsonPath, maxCheckTimes, waitTime);
  857. if(!exist){
  858. throw new BusinessException(ErrorCode.FAILURE_CODE_7013);
  859. }
  860. String uploadData = FileUtils.readFile(uploadJsonPath);
  861. JSONObject uploadJson = null;
  862. JSONArray array = null;
  863. if(uploadData!=null) {
  864. uploadJson = JSONObject.parseObject(uploadData);
  865. array = uploadJson.getJSONArray("upload");
  866. }
  867. if(array == null){
  868. throw new BusinessException(ErrorCode.FAILURE_CODE_7013);
  869. }
  870. JSONObject fileJson = null;
  871. String fileName = "";
  872. for(int i = 0, len = array.size(); i < len; i++) {
  873. fileJson = array.getJSONObject(i);
  874. fileName = fileJson.getString("file");
  875. //文件不存在抛出异常
  876. if (!new File(target + File.separator + "results" + File.separator + fileName)
  877. .exists()) {
  878. throw new Exception(
  879. target + File.separator + "results" + File.separator + fileName + "文件不存在");
  880. }
  881. //high文件夹
  882. if (fileJson.getIntValue("clazz") == 3) {
  883. map.put(target + File.separator + "results" + File.separator + fileName,
  884. imgViewPath + "pan/high/" + fileName.replace("high/", ""));
  885. continue;
  886. }
  887. //low文件夹
  888. if (fileJson.getIntValue("clazz") == 4) {
  889. map.put(target + File.separator + "results" + File.separator + fileName,
  890. imgViewPath + "pan/low/" + fileName.replace("low/", ""));
  891. continue;
  892. }
  893. //tiles文件夹,亚马逊没有裁剪图片api,不需要上传4k图
  894. if (fileJson.getIntValue("clazz") == 5) {
  895. map.put(target + File.separator + "results" + File.separator + fileName,
  896. imgViewPath + fileName);
  897. continue;
  898. }
  899. //tiles文件夹,亚马逊瓦片图
  900. if (fileJson.getIntValue("clazz") == 7) {
  901. map.put(target + File.separator + "results" + File.separator + fileName,
  902. imgViewPath + fileName);
  903. continue;
  904. }
  905. }
  906. if(map.size()>0) {
  907. uploadToOssUtil.uploadMulFiles(map);
  908. }
  909. //拷贝修改后的全景图到缓存目录
  910. String cachedImagesPath = String.format(ConstantFilePath.SCENE_CACHE_IMAGES, num);
  911. uploadFileList.stream().forEach(srcPath->{
  912. cn.hutool.core.io.FileUtil.copy(srcPath, srcPath.replace(targetImagesPath, cachedImagesPath), true);
  913. });
  914. //更新版本号
  915. SceneEditInfo sceneEditInfo = this.getByScenePlusId(scenePlus.getId());
  916. this.upgradeVersionById(sceneEditInfo.getId());
  917. //如果部分成功,则需要返回成功数量和失败列表
  918. if(CollUtil.isNotEmpty(notExistFileList)){
  919. notExistFileList = notExistFileList.stream().map(filePath -> {
  920. return filePath.substring(filePath.lastIndexOf(File.separator) + 1);
  921. }).collect(Collectors.toList());
  922. return ResultData.ok(new UploadPanoramaVO(uploadFileList.size(), notExistFileList));
  923. }
  924. return ResultData.ok();
  925. }
  926. @Override
  927. public ResultData downloadPanorama(FileParamVO param) throws Exception {
  928. String num = param.getNum();
  929. String fileName = param.getFileName();
  930. ScenePlus scenePlus = scenePlusService.getScenePlusByNum(num);
  931. if(Objects.isNull(scenePlus)){
  932. throw new BusinessException(ErrorCode.FAILURE_CODE_5005);
  933. }
  934. ScenePlusExt scenePlusExt = scenePlusExtService.getScenePlusExtByPlusId(scenePlus.getId());
  935. String path = scenePlusExt.getDataSource();
  936. String cachePath = String.format(ConstantFilePath.SCENE_CACHE, num);
  937. String imgCachePath = String.format(UploadFilePath.IMG_CACHES_PATH, num);
  938. String localImagesPath = String.format(ConstantFilePath.SCENE_CACHE_IMAGES, num);
  939. String cacheFormat = "downloads/scene/%s/caches/";
  940. String cacheImageFormat = "downloads/scene/%s/caches/images/";
  941. String url = null;
  942. String downloadName = null;
  943. //如果入参文件名不为空,则是单个文件下载,不需要打包
  944. if(StrUtil.isNotEmpty(fileName)){
  945. //如果是单张图片,直接提供oss url
  946. String localFilePath = localImagesPath + fileName;
  947. String ossFilePath = imgCachePath + fileName;
  948. if(!cn.hutool.core.io.FileUtil.exist(localFilePath)){
  949. throw new BusinessException(ErrorCode.FAILURE_CODE_5063);
  950. }
  951. uploadToOssUtil.upload(localFilePath, ossFilePath);
  952. url = ossUrlPrefix + ossFilePath;
  953. // FileUtils.downLoadFromUrl(imageUrl, fileName, localImagesPath);
  954. downloadName = fileName;
  955. }else{
  956. //从vision.txt中读取有效全景图名称列表
  957. // String visionPath = path + "/results/vision.txt";
  958. // List<String> imageList = SceneUtil.getPanoramaImageList(visionPath);
  959. //
  960. // //先下载到本地
  961. // List<String> keyList = cn.hutool.core.io.FileUtil.listFileNames(imgCachePath);
  962. // if (!StorageType.LOCAL.code().equals(this.type)) {// TODO: 2022/2/15 这里有可能有问题,可能还需要考虑本地部署的情况
  963. //// System.setProperty("java.util.concurrent.ForkJoinPool.common.parallelism", "8");
  964. // keyList.stream().forEach(key->{
  965. // String file = key.substring(key.lastIndexOf("/") + 1);
  966. // if(imageList.contains(file)){
  967. // String imageUrl = ossUrlPrefix + imgCachePath + file + "?t=" + System.currentTimeMillis();
  968. // FileUtils.downLoadFromUrl(imageUrl, file, localImagesPath);
  969. // }
  970. // });
  971. // }
  972. if(!cn.hutool.core.io.FileUtil.exist(localImagesPath)
  973. || cn.hutool.core.io.FileUtil.isDirEmpty(new File(localImagesPath))){
  974. throw new BusinessException(ErrorCode.FAILURE_CODE_5063);
  975. }
  976. downloadName = num + "_images.zip";
  977. //打包
  978. String zipPath = cachePath + downloadName;
  979. ZipUtil.zip(localImagesPath, zipPath);
  980. //上传压缩包
  981. // uploadToOssUtil.upload(zipPath, String.format(cacheFormat, num) + downloadName);
  982. uploadToOssUtil.uploadBySh(zipPath, String.format(cacheFormat, num) + downloadName);
  983. url = ossUrlPrefix + String.format(cacheFormat, num) + downloadName;
  984. //删除本地压缩包
  985. FileUtils.deleteFile(zipPath);
  986. // //删除本地目录
  987. // FileUtils.deleteDirectory(localImagesPath);
  988. }
  989. Map<String, Object> map = new HashMap<>();
  990. map.put("fileUrl", url + "?t=" + System.currentTimeMillis());
  991. map.put("fileName", downloadName);
  992. return ResultData.ok(map);
  993. }
  994. @Override
  995. public void saveTagsToSceneEditInfo(String num, SceneEditInfo sceneEditInfo){
  996. //查询缓存是否包含热点数据
  997. String key = String.format(RedisKey.SCENE_HOT_DATA, num);
  998. Map<String, String> allTagsMap = redisUtil.hmget(key);
  999. boolean hashTags = false;
  1000. for (Entry<String, String> tagMap : allTagsMap.entrySet()) {
  1001. if(StrUtil.isEmpty(tagMap.getValue())){
  1002. continue;
  1003. }
  1004. hashTags = true;
  1005. break;
  1006. }
  1007. //更改热点状态
  1008. sceneEditInfo.setTags(hashTags ? CommonStatus.YES.code() : CommonStatus.NO.code());
  1009. //version 是空的代表 sceneEditInfo记录集不存在
  1010. if(sceneEditInfo.getVersion() != null){
  1011. sceneEditInfo.setVersion(sceneEditInfo.getVersion() + 1);
  1012. }
  1013. }
  1014. @Override
  1015. public ResultData saveVideoBox(FileNameAndDataParamVO param) throws Exception {
  1016. JSONObject boxVideo = JSONObject.parseObject(param.getData());
  1017. String sid = boxVideo.getString("sid");
  1018. if(StrUtil.isEmpty(sid)){
  1019. throw new BusinessException(ErrorCode.PARAM_REQUIRED);
  1020. }
  1021. // ScenePro scenePro = sceneProService.findBySceneNum(param.getNum());
  1022. ScenePlus scenePlus = scenePlusService.getScenePlusByNum(param.getNum());
  1023. if(Objects.isNull(scenePlus))
  1024. throw new BusinessException(ErrorCode.FAILURE_CODE_5005);
  1025. SceneEditInfo sceneEditInfo = this.getByScenePlusId(scenePlus.getId());
  1026. //转换视频格式
  1027. this.transferToFlv(param.getNum(), param.getFileName());
  1028. //生成boxVideos数据
  1029. String boxVideos = this.createBoxVideos(param.getNum(), sid, boxVideo, sceneEditInfo, OperationType.ADDORUPDATE.code());
  1030. //更新数据库
  1031. this.updateBoxVideos(sceneEditInfo, scenePlus.getId(), boxVideos);
  1032. return ResultData.ok();
  1033. }
  1034. @Override
  1035. public ResultData deleteVideoBox(DeleteSidParamVO param) throws Exception {
  1036. // ScenePro scenePro = sceneProService.findBySceneNum(param.getNum());
  1037. ScenePlus scenePlus = scenePlusService.getScenePlusByNum(param.getNum());
  1038. if(Objects.isNull(scenePlus))
  1039. throw new BusinessException(ErrorCode.FAILURE_CODE_5005);
  1040. SceneEditInfo sceneEditInfo = this.getByScenePlusId(scenePlus.getId());
  1041. //根据sid移除json
  1042. String boxVideos = this.createBoxVideos(param.getNum(), param.getSid(), null, sceneEditInfo, OperationType.DELETE.code());
  1043. //写数据库
  1044. this.updateBoxVideos(sceneEditInfo,scenePlus.getId(),boxVideos);
  1045. return ResultData.ok();
  1046. }
  1047. @Override
  1048. public ResultData saveBoxPhoto(BaseDataParamVO param) throws Exception {
  1049. JSONObject boxPhoto = JSONObject.parseObject(param.getData());
  1050. String sid = boxPhoto.getString("sid");
  1051. if(StrUtil.isEmpty(sid)){
  1052. throw new BusinessException(ServerCode.PARAM_REQUIRED, sid);
  1053. }
  1054. ScenePlus scenePlus = scenePlusService.getScenePlusByNum(param.getNum());
  1055. if(Objects.isNull(scenePlus))
  1056. throw new BusinessException(ErrorCode.FAILURE_CODE_5005);
  1057. SceneEditInfo sceneEditInfo = this.getByScenePlusId(scenePlus.getId());
  1058. //生成boxVideos数据
  1059. String boxPhotos = this.createBoxPhotos(param.getNum(), sid, boxPhoto, sceneEditInfo, OperationType.ADDORUPDATE.code());
  1060. //更新数据库
  1061. this.updateBoxPhotos(sceneEditInfo, boxPhotos);
  1062. return ResultData.ok();
  1063. }
  1064. @Override
  1065. public ResultData deleteBoxPhoto(DeleteSidParamVO param) throws Exception {
  1066. ScenePlus scenePlus = scenePlusService.getScenePlusByNum(param.getNum());
  1067. if(Objects.isNull(scenePlus))
  1068. throw new BusinessException(ErrorCode.FAILURE_CODE_5005);
  1069. SceneEditInfo sceneEditInfo = this.getByScenePlusId(scenePlus.getId());
  1070. //根据sid移除json
  1071. String boxPhotos = this.createBoxPhotos(param.getNum(), param.getSid(), null, sceneEditInfo, OperationType.DELETE.code());
  1072. //写数据库
  1073. this.updateBoxPhotos(sceneEditInfo, boxPhotos);
  1074. return ResultData.ok();
  1075. }
  1076. @Override
  1077. public DownloadVO downloadBallScreenVideo(BallScreenVideoParamVO param) {
  1078. String videoPath = String.format(UploadFilePath.VIDEOS_VIEW_PATH, param.getNum()) + param.getFileName();
  1079. String url = ossUrlPrefix + videoPath + "?t=" + System.currentTimeMillis();
  1080. return DownloadVO.builder()
  1081. .fileName(param.getFileName())
  1082. .url(url)
  1083. .build();
  1084. }
  1085. @Override
  1086. public ResultData uploadBallScreenVideo(String num, String fileName, MultipartFile file) throws Exception {
  1087. //校验格式
  1088. if(!fileName.endsWith(".mp4")){
  1089. throw new BusinessException(ErrorCode.FAILURE_CODE_7016);
  1090. }
  1091. String videosViewPath = String.format(UploadFilePath.VIDEOS_VIEW_PATH, num);
  1092. // ScenePro scenePro = sceneProService.findBySceneNum(num);
  1093. ScenePlus scenePlus = scenePlusService.getScenePlusByNum(num);
  1094. if(scenePlus == null){
  1095. throw new BusinessException(ErrorCode.FAILURE_CODE_5005);
  1096. }
  1097. // SceneProExt sceneProExt = sceneProExtService.findBySceneProId(scenePro.getId());
  1098. ScenePlusExt scenePlusExt = scenePlusExtService.getScenePlusExtByPlusId(scenePlus.getId());
  1099. String path = scenePlusExt.getDataSource();
  1100. //校验文件名
  1101. String videos = scenePlusExt.getVideos();
  1102. if(StrUtil.isEmpty(videos)){
  1103. throw new BusinessException(ErrorCode.FAILURE_CODE_7012);
  1104. }
  1105. JSONObject videosJson = JSON.parseObject(videos);
  1106. JSONArray dataArr = videosJson.getJSONArray("data");
  1107. boolean exists = false;
  1108. for (Object o : dataArr) {
  1109. JSONObject jsonObject = (JSONObject)o;
  1110. if(jsonObject.getString("id").equals(fileName.replace(".mp4", ""))){
  1111. exists = true;
  1112. break;
  1113. }
  1114. }
  1115. if(!exists){
  1116. throw new BusinessException(ErrorCode.FAILURE_CODE_7012);
  1117. }
  1118. if(path != null && !"".equals(path) && path.startsWith("http")){
  1119. path = ConstantFilePath.BUILD_MODEL_PATH + File.separator + path.split("/")[path.split("/").length - 2];
  1120. }
  1121. String target = path + "_images";
  1122. String filePath = target + File.separator + "extras/video" + File.separator + fileName;
  1123. File targetFile = new File(filePath);
  1124. if(!targetFile.getParentFile().exists()){
  1125. targetFile.getParentFile().mkdirs();
  1126. }
  1127. //保存视频到本地
  1128. file.transferTo(targetFile);
  1129. File video = new File(target + File.separator + "extras/video");
  1130. //如果文件夹或者文件不存在,跳出
  1131. if(!video.exists() || video.listFiles() == null || video.listFiles().length == 0){
  1132. throw new BusinessException(ErrorCode.FAILURE_CODE_7009);
  1133. }
  1134. for(String videoName : video.list()){
  1135. log.info("球幕视频名称:{}", videoName);
  1136. uploadToOssUtil.upload(target + File.separator + "extras/video/" + videoName,videosViewPath + videoName);
  1137. CreateObjUtil.mp4ToFlv(target + File.separator + "extras/video/" + videoName,
  1138. target + File.separator + "extras/video/" + videoName.replace("mp4", "flv"));
  1139. uploadToOssUtil.upload(target + File.separator + "extras/video/" + videoName.replace("mp4", "flv"),videosViewPath + videoName.replace("mp4", "flv"));
  1140. //覆盖原始视频资源
  1141. FileUtils.copyFile(target + File.separator + "extras/video/" + videoName,
  1142. path + File.separator + "caches/videos/" + videoName, true);
  1143. }
  1144. FileUtils.deleteDirectory(target + File.separator + "extras/video/");
  1145. SceneEditInfo sceneEditInfo = this.getByScenePlusId(scenePlus.getId());
  1146. this.upgradeVersionById(sceneEditInfo.getId());
  1147. //更新scene.json版本号
  1148. this.upgradeVersionToSceneJson(num);
  1149. return ResultData.ok();
  1150. }
  1151. @Override
  1152. public void upgradeVersionToSceneJson(String num) throws Exception{
  1153. String key = String.format(RedisKey.SCENE_JSON, num);
  1154. String objectName = String.format(ConstantFilePath.SCENE_VIEW_DATA_DATA_SCENEJSON, num);
  1155. String objectContent = uploadToOssUtil.getObjectContent(bucket, objectName);
  1156. SceneJsonBean sceneJsonBean = JSON.parseObject(objectContent, SceneJsonBean.class);
  1157. sceneJsonBean.setVersion(sceneJsonBean.getVersion() +1);
  1158. uploadToOssUtil.upload(JSON.toJSONString(sceneJsonBean).getBytes(), objectName);
  1159. redisUtil.set(key, JSON.toJSONString(sceneJsonBean));
  1160. }
  1161. @Override
  1162. public ResultData sceneSync(String num, String type, String floorPlanJson, String ajkJson, String cameraJson,
  1163. MultipartFile[] files) throws Exception {
  1164. String lockKey = String.format(RedisLockKey.LOCK_SCENE_SYNC, num);
  1165. Boolean lock = redisLockUtil.lock(lockKey, RedisKey.EXPIRE_TIME_2_HOUR);
  1166. if(!lock){
  1167. throw new BusinessException(ErrorCode.SYSTEM_BUSY);
  1168. }
  1169. try {
  1170. // ScenePro scenePro = sceneProService.findBySceneNum(num);
  1171. ScenePlus scenePlus = scenePlusService.getScenePlusByNum(num);
  1172. if(scenePlus == null){
  1173. throw new BusinessException(ErrorCode.FAILURE_CODE_5005);
  1174. }
  1175. // SceneProExt sceneProExt = sceneProExtService.findBySceneProId(scenePro.getId());
  1176. ScenePlusExt scenePlusExt = scenePlusExtService.getScenePlusExtByPlusId(scenePlus.getId());
  1177. //更新scene.json文件
  1178. String strsceneInfos = FileUtils.readFile(ConstantFilePath.SCENE_PATH + "data" + File.separator + "data" + num + File.separator + "scene.json");
  1179. if(strsceneInfos == null)
  1180. new File(ConstantFilePath.SCENE_PATH + "data" + File.separator + "data" + num + File.separator + "scene.json").createNewFile();
  1181. String path = scenePlusExt.getDataSource();// /mnt/data/0662c5389/831989883441512448/4898cab04f8c_202104141602356060/
  1182. if(path != null && !"".equals(path) && path.startsWith("http")){
  1183. path = ConstantFilePath.BUILD_MODEL_PATH + File.separator + path.split("/")[path.split("/").length - 2];
  1184. }
  1185. String target = path + "_ajk"; // /mnt/data/0662c5389/831989883441512448/4898cab04f8c_202104141602356060_ajk
  1186. File editPath = new File(target);
  1187. if(!editPath.exists()){
  1188. editPath.mkdirs();
  1189. }
  1190. //创建文件夹软连接并且复制data.json和project.json
  1191. if(new File(target + File.separator + "capture").exists()){
  1192. new File(target + File.separator + "capture").delete();
  1193. }
  1194. if(new File(target + File.separator + "caches").exists()){
  1195. //删除link
  1196. new File(target + File.separator + "caches" + File.separator + "images").delete();
  1197. //删除所有文件
  1198. FileUtils.delAllFile(target + File.separator + "caches");
  1199. }
  1200. if(new File(target + File.separator + "results").exists()){
  1201. FileUtils.delAllFile(target + File.separator + "results");
  1202. }
  1203. //创建文件夹,并link文件夹
  1204. new File(target + File.separator + "caches").mkdirs();
  1205. CreateObjUtil.createSoftConnection(path + File.separator + "capture", target + File.separator + "capture");
  1206. if(new File(path + File.separator + "caches" + File.separator + "images").exists()){
  1207. CreateObjUtil.createSoftConnection(path + File.separator + "caches" + File.separator + "images", target + File.separator + "caches" + File.separator + "images");
  1208. }
  1209. FileUtils.copyFile(path + File.separator + "data.json", target + File.separator+"data.json", true);
  1210. FileUtils.copyFile(path + File.separator + "project.json", target + File.separator+"project.json", true);
  1211. //data.json增加extras为执行重建算法
  1212. String project = FileUtils.readFile(target + File.separator+"project.json");
  1213. if(project != null){
  1214. JSONObject projectJson = JSONObject.parseObject(project);
  1215. projectJson.put("parent", projectJson.get("uuid"));
  1216. projectJson.put("uuid", UUID.randomUUID().toString());
  1217. projectJson.put("time", System.currentTimeMillis());
  1218. FileUtils.writeFile(path + File.separator + "project.json", projectJson.toString());
  1219. }
  1220. String data = FileUtils.readFile(target + File.separator+"data.json");
  1221. if(data != null){
  1222. JSONObject floorplanJson = new JSONObject();
  1223. floorplanJson.put("has_floor_ajk_json", true);
  1224. floorplanJson.put("has_vision_txt", true);
  1225. floorplanJson.put("has_floorplan_json", true);
  1226. JSONObject dataJson = JSONObject.parseObject(data);
  1227. dataJson.put("extras", floorplanJson);
  1228. //V5表示不需要生成high,low文件
  1229. dataJson.put("skybox_type", "SKYBOX_V8");
  1230. dataJson.put("split_type", "SPLIT_V10");
  1231. FileUtils.writeFile(target + File.separator+"data.json", new String(dataJson.toString().getBytes(), "UTF-8"));
  1232. }
  1233. //文件上传的位置可以自定义
  1234. log.info("画墙重建模型开始");
  1235. File targetFile = new File(target + File.separator + "extras" + File.separator + "floor_ajk.json");
  1236. if(!targetFile.getParentFile().exists()){
  1237. targetFile.getParentFile().mkdirs();
  1238. }
  1239. if(targetFile.exists()){
  1240. FileUtils.deleteFile(target + File.separator + "extras" + File.separator + "floor_ajk.json");
  1241. }
  1242. // 保存
  1243. FileUtils.writeFile(target + File.separator + "extras" + File.separator + "floorplan.json", new String(floorPlanJson.getBytes(), "UTF-8"));
  1244. FileUtils.writeFile(target + File.separator + "extras" + File.separator + "floor_ajk.json", new String(ajkJson.getBytes(), "UTF-8"));
  1245. FileUtils.writeFile(ConstantFilePath.SCENE_PATH+"data"+File.separator+"data"+num + File.separator + "floor_ajk.json", new String(ajkJson.getBytes(), "UTF-8"));
  1246. FileUtils.writeFile(target + File.separator + "extras" + File.separator + "vision.txt", new String(cameraJson.getBytes(), "UTF-8"));
  1247. FileUtils.writeFile(ConstantFilePath.SCENE_PATH+"data"+File.separator+"data"+num + File.separator + "camera.json", new String(cameraJson.getBytes(), "UTF-8"));
  1248. for(int i = 0; i < files.length; i ++){
  1249. File cadImg = new File(target + File.separator + "extras" + File.separator + "Floorplans/" + files[i].getOriginalFilename());
  1250. if(!cadImg.getParentFile().exists()){
  1251. cadImg.getParentFile().mkdirs();
  1252. }
  1253. if(cadImg.exists())
  1254. {
  1255. cadImg.delete();
  1256. }
  1257. files[i].transferTo(cadImg);
  1258. }
  1259. //下载封面图
  1260. FileUtils.downLoadFromUrl(scenePlusExt.getThumb() + "?t=" + System.currentTimeMillis(),
  1261. "Cover.png", target + File.separator + "extras" + File.separator + "CoverImage");
  1262. //转换成jpg
  1263. FileUtils.pngToJpg(target + File.separator + "extras" + File.separator + "CoverImage/Cover.png",
  1264. target + File.separator + "extras" + File.separator + "CoverImage/Cover.jpg");
  1265. //安居客算法运行
  1266. log.info("安居客算法:开始建模——"+num);
  1267. CreateObjUtil.build3dModel(target , "1");
  1268. String uploadJsonPath = target + File.separator + "results" + File.separator + "upload.json";
  1269. log.info("uploadJsonPath=" + uploadJsonPath);
  1270. //因为共享目录有延迟,这里循环检测算法是否计算完毕3次,每次隔五秒
  1271. boolean exist = ComputerUtil.checkComputeCompleted(uploadJsonPath, maxCheckTimes, waitTime);
  1272. if(!exist){
  1273. throw new BusinessException(ErrorCode.FAILURE_CODE_7017);
  1274. }
  1275. String zipPath = target + File.separator + "results/" + num + ".zip";
  1276. new File(zipPath).delete();
  1277. ZipUtil.zip(target + File.separator + "results/ajk/", zipPath);
  1278. // String command = "bash /opt/ossutil/gzip.sh " + zipPath.replace(".zip", "") + " " + target + File.separator + "results/ajk/";
  1279. // log.info("压缩文件:" + command);
  1280. // CreateObjUtil.callshell(command);
  1281. if(!new File(zipPath).exists()){
  1282. return ResultData.error(ErrorCode.FAILURE_CODE_5043);
  1283. }
  1284. String fileMD5 = FileMd5Util.getFileMD5(new File(zipPath));
  1285. uploadToOssUtil.upload(zipPath, "data_download/" + num + ".zip");
  1286. SceneDataDownload sceneDataDownload = sceneDataDownloadService.findBySceneNum(num);
  1287. if(sceneDataDownload == null){
  1288. sceneDataDownload = new SceneDataDownload();
  1289. sceneDataDownload.setNum(num);
  1290. sceneDataDownload.setDownloadPath(ossUrlPrefix + "data_download/" + num + ".zip");
  1291. sceneDataDownload.setFileMd5(fileMD5);
  1292. sceneDataDownloadService.save(sceneDataDownload);
  1293. return ResultData.ok();
  1294. }
  1295. sceneDataDownload.setFileMd5(fileMD5);
  1296. sceneDataDownload.setUpdateTime(Calendar.getInstance().getTime());
  1297. sceneDataDownloadService.updateById(sceneDataDownload);
  1298. }catch (Exception e){
  1299. log.error("画墙重建模型失败...", e);
  1300. throw new BusinessException(ErrorCode.FAILURE_CODE_5039);
  1301. }finally {
  1302. redisLockUtil.unlockLua(lockKey);
  1303. }
  1304. return ResultData.ok();
  1305. }
  1306. @Override
  1307. public ResultData checkKey(SceneCheckKeyParamVO param) throws Exception {
  1308. SceneJsonBean sceneJsonBean = null;
  1309. String sceneJson = redisUtil.get(String.format(RedisKey.SCENE_JSON, param.getNum()));
  1310. if(StrUtil.isEmpty(sceneJson)){
  1311. sceneJson = uploadToOssUtil.getObjectContent(bucket,
  1312. String.format(UploadFilePath.DATA_VIEW_PATH, param.getNum()) + "scene.json");
  1313. }
  1314. sceneJsonBean = JSON.parseObject(sceneJson, SceneJsonBean.class);
  1315. if(!param.getPassword().equals(sceneJsonBean.getScenePassword())){
  1316. throw new BusinessException(ErrorCode.FAILURE_CODE_5021);
  1317. }
  1318. return ResultData.ok();
  1319. }
  1320. @Override
  1321. public ResultData addMosaics(BaseDataParamVO param) throws Exception {
  1322. JSONArray mosaicArr = JSONArray.parseArray(param.getData());
  1323. if (CollUtil.isEmpty(mosaicArr) || mosaicArr.size() > 1) {
  1324. throw new BusinessException(ErrorCode.FAILURE_CODE_5012);
  1325. }
  1326. JSONObject mosicObj= mosaicArr.getJSONObject(0);
  1327. ScenePlus scenePlus = scenePlusService.getScenePlusByNum(param.getNum());
  1328. if(Objects.isNull(scenePlus)){
  1329. throw new BusinessException(ErrorCode.FAILURE_CODE_5005);
  1330. }
  1331. ScenePlusExt scenePlusExt = scenePlusExtService.getScenePlusExtByPlusId(scenePlus.getId());
  1332. SceneEditInfo sceneEditInfo = this.getByScenePlusId(scenePlus.getId());
  1333. SceneEditInfoExt sceneEditInfoExt = sceneEditInfoExtService.getByEditInfoId(sceneEditInfo.getId());
  1334. String dataViewPath = String.format(UploadFilePath.DATA_VIEW_PATH, param.getNum());
  1335. String imgViewPath = String.format(UploadFilePath.IMG_VIEW_PATH, param.getNum());
  1336. String path = scenePlusExt.getDataSource();
  1337. if(StrUtil.isNotEmpty(path) && path.startsWith("http")){
  1338. path = ConstantFilePath.BUILD_MODEL_PATH + File.separator + path.split("/")[path.split("/").length - 2];
  1339. }
  1340. String target = path + "_roi";
  1341. cn.hutool.core.io.FileUtil.del(target);
  1342. //文件上传的位置可以自定义
  1343. String filePath = target + File.separator + "extras" + File.separator + "images/";
  1344. File imageFile = new File(filePath);
  1345. if(!imageFile.getParentFile().exists()){
  1346. imageFile.getParentFile().mkdirs();
  1347. }
  1348. FileUtils.copyFolderAllFiles(path + "/caches/images/", filePath, true);
  1349. String mosaicData = sceneEditInfoExt.getMosaics();
  1350. JSONArray jsonArray = new JSONArray();
  1351. if(StrUtil.isNotEmpty(mosaicData)){
  1352. jsonArray = JSONArray.parseArray(mosaicData);
  1353. }
  1354. jsonArray.add(mosicObj);
  1355. // 准备算法数据
  1356. List<JSONObject> mosaicsList = new ArrayList<>();
  1357. for(int i = 0, len = jsonArray.size(); i < len; i++){
  1358. JSONObject jsonObject = jsonArray.getJSONObject(i);
  1359. boolean contains = false;
  1360. for (JSONObject item : mosaicsList) {
  1361. if(StringUtils.equals(item.getString("panoId"),jsonObject.getString("panoId"))){
  1362. contains = true;
  1363. item.getJSONArray("rect").addAll(jsonObject.getJSONArray("rect"));
  1364. }
  1365. }
  1366. if(!contains){
  1367. mosaicsList.add(jsonObject);
  1368. }
  1369. }
  1370. JSONObject roiImageJSon = new JSONObject();
  1371. roiImageJSon.put("batch",mosaicsList);
  1372. FileUtils.writeFile(target + File.separator + "extras" + File.separator + "image-ROI.json", JSON.toJSONString(roiImageJSon,SerializerFeature.DisableCircularReferenceDetect));
  1373. FileUtils.copyFile(path + File.separator + "results/vision.txt", target + File.separator+"extras/vision.txt", true);
  1374. FileUtils.copyFile(path + File.separator + "results/vision2.txt", target + File.separator+"extras/vision2.txt", true);
  1375. FileUtils.copyFile(path + File.separator + "data.json", target + File.separator+"data.json", true);
  1376. String data = FileUtils.readFile(target + File.separator+"data.json");
  1377. if(data != null){
  1378. JSONObject floorplanJson = new JSONObject();
  1379. floorplanJson.put("has_source_images", true);
  1380. floorplanJson.put("has_vision_txt", true);
  1381. JSONObject dataJson = JSONObject.parseObject(data);
  1382. dataJson.put("extras", floorplanJson);
  1383. //V5表示不需要生成high,low文件
  1384. dataJson.put("skybox_type", "SKYBOX_V6");
  1385. if(scenePlusExt.getSceneScheme() == 11){
  1386. dataJson.put("skybox_type", "SKYBOX_V7");
  1387. }
  1388. dataJson.put("split_type", "SPLIT_V18");
  1389. //sceneScheme为3切成瓦片图
  1390. if(scenePlusExt.getSceneScheme() == 3){
  1391. dataJson.put("skybox_type", "SKYBOX_V4");
  1392. }
  1393. FileUtils.writeFile(target + File.separator+"data.json", new String(dataJson.toString().getBytes(), "UTF-8"));
  1394. }
  1395. FileUtils.copyFile(path + File.separator + "project.json", target + File.separator+"project.json", true);
  1396. //进行计算
  1397. log.info("马赛克建模开始, num:{}, path:{}" + param.getNum(), target);
  1398. CreateObjUtil.build3dModel2(target , "1");
  1399. log.info("马赛克建模结束, num:{}, path:{}" + param.getNum(), target);
  1400. //读取upload文件,检验需要上传的文件是否存在
  1401. String uploadJsonPath = target + File.separator + "results" + File.separator + "upload.json";
  1402. boolean exist = ComputerUtil.checkComputeCompleted(uploadJsonPath, maxCheckTimes, waitTime);
  1403. if(!exist){
  1404. throw new BusinessException(ErrorCode.FAILURE_CODE_5042);
  1405. }
  1406. String uploadData = FileUtils.readFile(target + File.separator + "results" + File.separator + "upload.json");
  1407. JSONObject uploadJson = JSONObject.parseObject(uploadData);
  1408. JSONArray array = uploadJson.getJSONArray("upload");
  1409. Map<String,String> map = new HashMap<>();
  1410. JSONObject fileJson = null;
  1411. String fileName = "";
  1412. for(int i = 0, len = array.size(); i < len; i++){
  1413. fileJson = array.getJSONObject(i);
  1414. fileName = fileJson.getString("file");
  1415. //文件不存在抛出异常
  1416. if(!new File(target + File.separator + "results" +File.separator + fileName).exists()){
  1417. throw new Exception(target + File.separator + "results" +File.separator + fileName+"文件不存在");
  1418. }
  1419. //high文件夹
  1420. if(fileJson.getIntValue("clazz") == 3){
  1421. map.put(target + File.separator + "results" +File.separator+ fileName,
  1422. imgViewPath + "pan/high/"+ fileName.replace("high/", ""));
  1423. }
  1424. //low文件夹
  1425. if(fileJson.getIntValue("clazz") == 4){
  1426. map.put(target + File.separator + "results" +File.separator+ fileName,
  1427. imgViewPath + "pan/low/"+ fileName.replace("low/", ""));
  1428. }
  1429. //tiles文件夹,亚马逊没有裁剪图片api,不需要上传4k图
  1430. if(fileJson.getIntValue("clazz") == 5){
  1431. map.put(target + File.separator + "results" + File.separator+ fileName,
  1432. imgViewPath + fileName);
  1433. }
  1434. //tiles文件夹,亚马逊瓦片图
  1435. if(fileJson.getIntValue("clazz") == 7 ){
  1436. map.put(target + File.separator + "results" + File.separator+ fileName,
  1437. imgViewPath + fileName);
  1438. }
  1439. }
  1440. for(String imagesName : imageFile.list()){
  1441. //覆盖原始图片资源
  1442. FileUtils.copyFile(target + File.separator + "extras/images/" + imagesName,
  1443. path + File.separator + "caches/images/" + imagesName, true);
  1444. FileUtils.deleteFile(target + File.separator + "extras/images/" + imagesName);
  1445. }
  1446. uploadToOssUtil.uploadMulFiles(map);
  1447. this.upgradeVersionById(sceneEditInfo.getId());
  1448. sceneEditInfoExt.setMosaics(JSON.toJSONString(jsonArray,SerializerFeature.DisableCircularReferenceDetect));
  1449. sceneEditInfoExtService.updateById(sceneEditInfoExt);
  1450. // 更新 scene.json
  1451. String sceneJsonPath = dataViewPath + "scene.json";
  1452. String sceneJson = uploadToOssUtil.getObjectContent(this.bucket, sceneJsonPath);
  1453. JSONObject scenejson = new JSONObject();
  1454. if(StrUtil.isNotEmpty(sceneJson)) {
  1455. scenejson = JSONObject.parseObject(sceneJson);
  1456. }
  1457. scenejson.put("mosaics",JSON.toJSONString(jsonArray, SerializerFeature.DisableCircularReferenceDetect));
  1458. Integer version = scenejson.getIntValue("version");
  1459. version = Objects.isNull(version) ? 1 : version + 1;
  1460. scenejson.put("version", version);
  1461. uploadToOssUtil.upload(scenejson.toJSONString().getBytes(), sceneJsonPath);
  1462. Map<String,Object> result = new HashMap<>(1);
  1463. result.put("mosaics",jsonArray);
  1464. return ResultData.ok(result);
  1465. }
  1466. private void updateBoxVideos(SceneEditInfo sceneEditInfo, Long scenePlusId, String boxVideos){
  1467. if(Objects.isNull(sceneEditInfo)){
  1468. sceneEditInfo = new SceneEditInfo();
  1469. sceneEditInfo.setScenePlusId(scenePlusId);
  1470. sceneEditInfo.setBoxVideos(boxVideos);
  1471. this.save(sceneEditInfo);
  1472. }else{
  1473. this.update(new UpdateWrapper<SceneEditInfo>()
  1474. .setSql("version = version + 1")
  1475. .set("box_videos", boxVideos)
  1476. .eq("id", sceneEditInfo.getId()));
  1477. }
  1478. }
  1479. private void updateBoxPhotos(SceneEditInfo sceneEditInfo, String boxPhotos){
  1480. this.update(new LambdaUpdateWrapper<SceneEditInfo>()
  1481. .set(SceneEditInfo::getBoxPhotos, boxPhotos)
  1482. .setSql("version = version + 1")
  1483. .eq(SceneEditInfo::getId, sceneEditInfo.getId()));
  1484. }
  1485. private String createBoxVideos(
  1486. String num, String sid, JSONObject boxVideo,
  1487. SceneEditInfo sceneEditInfo, int type) throws Exception{
  1488. String boxVideos = null;
  1489. if(sceneEditInfo != null){
  1490. boxVideos = sceneEditInfo.getBoxVideos();
  1491. }
  1492. JSONArray boxVideosJson = null;
  1493. if (StrUtil.isNotEmpty(boxVideos)) {
  1494. boxVideosJson = JSONArray.parseArray(boxVideos);
  1495. }else {
  1496. boxVideosJson = new JSONArray();
  1497. }
  1498. String result = null;
  1499. //删除
  1500. if(type == OperationType.DELETE.code()){
  1501. Set<String> deleteFile = new HashSet<>();
  1502. if(boxVideosJson.size() == 0)
  1503. return null;
  1504. for(int i=0;i<boxVideosJson.size();++i){
  1505. JSONObject ele = boxVideosJson.getJSONObject(i);
  1506. if(ele.getString("sid").equals(sid)){
  1507. String poster = ele.getString("poster");
  1508. if(StrUtil.isNotEmpty(poster))
  1509. deleteFile.add(poster);
  1510. String url = ele.getString("url");
  1511. if(StrUtil.isNotEmpty(url)){
  1512. deleteFile.add(url);
  1513. deleteFile.add(url.replace(".mp4",".flv"));
  1514. }
  1515. boxVideosJson.remove(i);
  1516. }
  1517. }
  1518. //删除资源文件
  1519. if(CollUtil.isNotEmpty(deleteFile))
  1520. sceneUploadService.delete(
  1521. DeleteFileParamVO.builder().num(num)
  1522. .bizType(FileBizType.BOX_VIDEO.code())
  1523. .fileNames(new ArrayList<>(deleteFile)).build());
  1524. }else{
  1525. // TODO: 2022/3/1 目前需求只保留一个,所以这里先注释掉,sid不同时,不追加
  1526. //更新
  1527. // boolean exist = false;
  1528. // for(int i=0;i<boxVideosJson.size();++i){
  1529. // JSONObject ele = boxVideosJson.getJSONObject(i);
  1530. // if(ele.getString("sid").equals(sid)){
  1531. // boxVideosJson.set(i, boxVideo);
  1532. // exist = true;
  1533. // }
  1534. // }
  1535. // //新增
  1536. // if(!exist){
  1537. // boxVideosJson.add(boxVideo);
  1538. // }
  1539. boxVideosJson.clear();
  1540. boxVideosJson.add(boxVideo);
  1541. }
  1542. if(boxVideosJson.size() != 0){
  1543. result = boxVideosJson.toJSONString();
  1544. }
  1545. return result;
  1546. }
  1547. private String createBoxPhotos(String num, String sid, JSONObject boxPhoto, SceneEditInfo sceneEditInfo, int type) throws Exception{
  1548. String boxPhotos = null;
  1549. if(sceneEditInfo != null){
  1550. boxPhotos = sceneEditInfo.getBoxPhotos();
  1551. }
  1552. JSONArray boxPhotosJson = null;
  1553. if (StrUtil.isNotEmpty(boxPhotos)) {
  1554. boxPhotosJson = JSONArray.parseArray(boxPhotos);
  1555. }else {
  1556. boxPhotosJson = new JSONArray();
  1557. }
  1558. String result = null;
  1559. //删除
  1560. if(type == OperationType.DELETE.code()){
  1561. Set<String> deleteFile = new HashSet<>();
  1562. if(boxPhotosJson.size() == 0)
  1563. return null;
  1564. for(int i=0;i<boxPhotosJson.size();++i){
  1565. JSONObject ele = boxPhotosJson.getJSONObject(i);
  1566. if(ele.getString("sid").equals(sid)){
  1567. String poster = ele.getString("poster");
  1568. if(StrUtil.isNotEmpty(poster))
  1569. deleteFile.add(poster);
  1570. String url = ele.getString("url");
  1571. if(StrUtil.isNotEmpty(url))
  1572. deleteFile.add(url);
  1573. boxPhotosJson.remove(i);
  1574. }
  1575. }
  1576. //删除资源文件
  1577. if(CollUtil.isNotEmpty(deleteFile))
  1578. sceneUploadService.delete(
  1579. DeleteFileParamVO.builder().num(num)
  1580. .bizType(FileBizType.BOX_VIDEO.code())
  1581. .fileNames(new ArrayList<>(deleteFile)).build());
  1582. }else{
  1583. //更新
  1584. boolean exist = false;
  1585. for(int i=0;i<boxPhotosJson.size();++i){
  1586. JSONObject ele = boxPhotosJson.getJSONObject(i);
  1587. if(ele.getString("sid").equals(sid)){
  1588. boxPhoto.put("createTime", ele.getLong("createTime"));
  1589. boxPhotosJson.set(i, boxPhoto);
  1590. exist = true;
  1591. }
  1592. }
  1593. //新增
  1594. if(!exist){
  1595. boxPhoto.put("createTime", Calendar.getInstance().getTimeInMillis());
  1596. boxPhotosJson.add(boxPhoto);
  1597. }
  1598. }
  1599. if(boxPhotosJson.size() != 0){
  1600. List<BoxPhotoBean> list = Lists.newArrayList();
  1601. for (Object o : boxPhotosJson) {
  1602. JSONObject jsonObject = (JSONObject)o;
  1603. list.add(BoxPhotoBean.builder().createTime(jsonObject.getLong("createTime")).boxPhoto(jsonObject).build());
  1604. }
  1605. //按创建时间倒叙排序
  1606. list.sort(Comparator.comparingLong(BoxPhotoBean::getCreateTime).reversed());
  1607. // list转JSONArray
  1608. JSONArray array = new JSONArray();
  1609. list.stream().forEach(bean->{
  1610. array.add(bean.getBoxPhoto());
  1611. });
  1612. result = array.toJSONString();
  1613. }
  1614. return result;
  1615. }
  1616. private void transferToFlv(String num, String fileName) throws Exception {
  1617. String userEditPath = String.format(UploadFilePath.USER_EDIT_PATH, num);
  1618. String localImagesPath = String.format(ConstantFilePath.SCENE_USER_PATH_V4, num);
  1619. String localFilePath = localImagesPath + fileName;
  1620. File targetFile = new File(localImagesPath);
  1621. if (!targetFile.exists()){
  1622. targetFile.mkdirs();
  1623. }
  1624. targetFile = new File(localFilePath);
  1625. if (targetFile.exists()){
  1626. FileUtils.deleteFile(localFilePath);
  1627. }
  1628. //从用户编辑目录中下载视频到本地
  1629. if (!StorageType.LOCAL.code().equals(this.type)) {
  1630. String filePath = userEditPath + fileName;
  1631. String imageUrl = ossUrlPrefix + filePath + "?t=" + System.currentTimeMillis();
  1632. FileUtils.downLoadFromUrl(imageUrl, fileName, localImagesPath);
  1633. }
  1634. //视频格式转换
  1635. CreateObjUtil.mp4ToFlv(localFilePath, localFilePath.replace("mp4", "flv"));
  1636. //上传
  1637. String flvFileName = fileName.replace("mp4", "flv");
  1638. // uploadToOssUtil.upload(localFilePath, userEditPath + fileName);
  1639. uploadToOssUtil.upload(localFilePath.replace("mp4", "flv"), userEditPath+flvFileName);
  1640. }
  1641. }