SceneEditInfoServiceImpl.java 108 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630
  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.util.StrUtil;
  5. import cn.hutool.core.util.ZipUtil;
  6. import com.alibaba.fastjson.JSON;
  7. import com.alibaba.fastjson.JSONArray;
  8. import com.alibaba.fastjson.JSONObject;
  9. import com.alibaba.fastjson.serializer.SerializerFeature;
  10. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  11. import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
  12. import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
  13. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  14. import com.fdkankan.common.constant.*;
  15. import com.fdkankan.common.exception.BusinessException;
  16. import com.fdkankan.common.response.Result;
  17. import com.fdkankan.common.response.ResultData;
  18. import com.fdkankan.common.util.ComputerUtil;
  19. import com.fdkankan.common.util.CreateHouseJsonUtil;
  20. import com.fdkankan.common.util.CreateObjUtil;
  21. import com.fdkankan.common.util.FileMd5Util;
  22. import com.fdkankan.common.util.FileUtil;
  23. import com.fdkankan.common.util.FileUtils;
  24. import com.fdkankan.common.util.SceneUtil;
  25. import com.fdkankan.fyun.constant.StorageType;
  26. import com.fdkankan.fyun.oss.UploadToOssUtil;
  27. import com.fdkankan.platform.api.feign.PlatformGoodsClient;
  28. import com.fdkankan.platform.api.feign.PlatformUserClient;
  29. import com.fdkankan.platform.api.dto.CameraDetail;
  30. import com.fdkankan.platform.api.dto.Company;
  31. import com.fdkankan.redis.constant.RedisKey;
  32. import com.fdkankan.redis.constant.RedisLockKey;
  33. import com.fdkankan.redis.util.RedisLockUtil;
  34. import com.fdkankan.redis.util.RedisUtil;
  35. import com.fdkankan.scene.api.dto.SceneInfoDTO;
  36. import com.fdkankan.scene.bean.BoxPhotoBean;
  37. import com.fdkankan.scene.bean.IconBean;
  38. import com.fdkankan.scene.bean.PointBean;
  39. import com.fdkankan.scene.bean.RequestSceneProV4;
  40. import com.fdkankan.scene.bean.SceneJsonBean;
  41. import com.fdkankan.scene.bean.SegmentBean;
  42. import com.fdkankan.scene.bean.StyleBean;
  43. import com.fdkankan.scene.bean.TagBean;
  44. import com.fdkankan.scene.bean.VertexBean;
  45. import com.fdkankan.scene.bean.WallBean;
  46. import com.fdkankan.scene.callback.FdkkMiniReqErrorCallback;
  47. import com.fdkankan.scene.callback.FdkkMiniReqSuccessCallback;
  48. import com.fdkankan.scene.entity.SceneDataDownload;
  49. import com.fdkankan.scene.entity.SceneEditControls;
  50. import com.fdkankan.scene.entity.SceneEditInfo;
  51. import com.fdkankan.scene.entity.SceneEditInfoExt;
  52. import com.fdkankan.scene.entity.ScenePlus;
  53. import com.fdkankan.scene.entity.ScenePlusExt;
  54. import com.fdkankan.scene.entity.ScenePro;
  55. import com.fdkankan.scene.entity.SceneProV3;
  56. import com.fdkankan.scene.factory.FloorLogoHandlerFactory;
  57. import com.fdkankan.scene.factory.MusicHandlerFactory;
  58. import com.fdkankan.scene.factory.ScreenshotHandlerFactory;
  59. import com.fdkankan.scene.httpclient.FdkankanMiniClient;
  60. import com.fdkankan.scene.mapper.ISceneEditInfoMapper;
  61. import com.fdkankan.scene.service.ISceneDataDownloadService;
  62. import com.fdkankan.scene.service.ISceneEditControlsService;
  63. import com.fdkankan.scene.service.ISceneEditInfoExtService;
  64. import com.fdkankan.scene.service.ISceneEditInfoService;
  65. import com.fdkankan.scene.service.IScenePlusExtService;
  66. import com.fdkankan.scene.service.IScenePlusService;
  67. import com.fdkankan.scene.service.ISceneProExtService;
  68. import com.fdkankan.scene.service.ISceneProService;
  69. import com.fdkankan.scene.service.ISceneUploadService;
  70. import com.fdkankan.scene.vo.*;
  71. import com.google.common.collect.Lists;
  72. import com.google.errorprone.annotations.Var;
  73. import java.io.File;
  74. import java.nio.charset.StandardCharsets;
  75. import java.util.ArrayList;
  76. import java.util.Arrays;
  77. import java.util.Collection;
  78. import java.util.Comparator;
  79. import java.util.Date;
  80. import java.util.HashMap;
  81. import java.util.HashSet;
  82. import java.util.List;
  83. import java.util.Map;
  84. import java.util.Map.Entry;
  85. import java.util.Set;
  86. import java.util.UUID;
  87. import java.util.concurrent.atomic.AtomicInteger;
  88. import java.util.stream.Collectors;
  89. import javax.annotation.Resource;
  90. import lombok.extern.slf4j.Slf4j;
  91. import org.apache.commons.lang3.StringUtils;
  92. import org.springframework.beans.factory.annotation.Autowired;
  93. import org.springframework.beans.factory.annotation.Value;
  94. import org.springframework.cloud.context.config.annotation.RefreshScope;
  95. import org.springframework.stereotype.Service;
  96. import java.io.IOException;
  97. import java.util.Calendar;
  98. import java.util.Objects;
  99. import org.springframework.transaction.annotation.Transactional;
  100. import org.springframework.web.multipart.MultipartFile;
  101. /**
  102. * <p>
  103. * 服务实现类
  104. * </p>
  105. *
  106. * @author
  107. * @since 2022-01-18
  108. */
  109. @Slf4j
  110. @RefreshScope
  111. @Service
  112. public class SceneEditInfoServiceImpl extends ServiceImpl<ISceneEditInfoMapper, SceneEditInfo> implements ISceneEditInfoService {
  113. @Value("${oss.prefix.ali}")
  114. private String prefixAli;
  115. @Value("${oss.prefix.url}")
  116. private String ossUrlPrefix;
  117. @Value("${upload.type}")
  118. private String type;
  119. @Value("${oss.bucket:4dkankan}")
  120. private String bucket;
  121. @Value("${main.url}")
  122. private String mainUrl;
  123. @Value("${ecs.checkFile.maxTimes:5}")
  124. private int maxCheckTimes;
  125. @Value("${ecs.checkFile.waitTime:5000}")
  126. private int waitTime;
  127. @Value("${http.host.4dkankanMini}")
  128. private String fkankanMiniHost;
  129. @Value("${http.api-v4.getDataSyncByCameraId}")
  130. private String URL_GET_DATASYNC_BY_CAMERAID;
  131. @Value("${http.api-v4.getSceneProByNum}")
  132. private String URL_GET_SCENEPRO_BY_NUM;
  133. @Value("${http.api-v4.upgradeToV4ResultSync}")
  134. private String URL_UPGRADE_TO_V4_RESULT_SYNC;
  135. @Autowired
  136. private ISceneEditControlsService sceneEditControlsService;
  137. @Autowired
  138. private ISceneProService sceneProService;
  139. @Autowired
  140. private UploadToOssUtil uploadToOssUtil;
  141. @Autowired
  142. RedisUtil redisUtil;
  143. @Autowired
  144. private ISceneProExtService sceneProExtService;
  145. @Autowired
  146. private RedisLockUtil redisLockUtil;
  147. @Autowired
  148. ISceneDataDownloadService sceneDataDownloadService;
  149. @Autowired
  150. PlatformGoodsClient platformGoodsClient;
  151. @Autowired
  152. PlatformUserClient platformUserClient;
  153. @Autowired
  154. private ISceneEditInfoExtService sceneEditInfoExtService;
  155. @Autowired
  156. IScenePlusService scenePlusService;
  157. @Autowired
  158. IScenePlusExtService scenePlusExtService;
  159. @Resource
  160. private FdkankanMiniClient fdkankanMiniClient;
  161. @Autowired
  162. private ISceneUploadService sceneUploadService;
  163. @Transactional
  164. @Override
  165. public SceneEditInfoVO saveScene(SceneEditInfoParamVO param) {
  166. ScenePlus scenePlus = scenePlusService.getScenePlusByNum(param.getNum());
  167. // ScenePro scenePro = sceneProService.findBySceneNum(param.getNum());
  168. if(Objects.isNull(scenePlus)){
  169. throw new BusinessException(ErrorCode.FAILURE_CODE_5005);
  170. }
  171. SceneEditInfo sceneEditInfoDb = this.getByScenePlusId(scenePlus.getId());
  172. SceneEditControls sceneEditControlsDb = null;
  173. if(Objects.nonNull(sceneEditInfoDb)){
  174. sceneEditControlsDb = sceneEditControlsService.getBySceneEditId(sceneEditInfoDb.getId());
  175. }
  176. //用户上传的文件后缀名转小写
  177. this.lowercaseExtName(param);
  178. SceneEditInfo sceneEditInfo = BeanUtil.copyProperties(param, SceneEditInfo.class);
  179. sceneEditInfo.setScenePlusId(scenePlus.getId());
  180. if(Objects.isNull(sceneEditInfoDb)){
  181. this.save(sceneEditInfo);
  182. }else{
  183. sceneEditInfo.setId(sceneEditInfoDb.getId());
  184. sceneEditInfo.setVersion(sceneEditInfoDb.getVersion() + 1);
  185. this.updateById(sceneEditInfo);
  186. }
  187. if(Objects.nonNull(param.getControls())){
  188. SceneEditControls sceneEditControls = BeanUtil.copyProperties(param.getControls(), SceneEditControls.class);
  189. sceneEditControls.setEditInfoId(sceneEditInfo.getId());
  190. if(Objects.isNull(sceneEditControlsDb)){
  191. sceneEditControlsService.save(sceneEditControls);
  192. }else{
  193. sceneEditControls.setId(sceneEditControlsDb.getId());
  194. sceneEditControlsService.updateById(sceneEditControls);
  195. }
  196. }
  197. SceneEditInfoVO result = BeanUtil.copyProperties(param, SceneEditInfoVO.class);
  198. result.setCreateTime(sceneEditInfo.getCreateTime());
  199. return result;
  200. }
  201. private void lowercaseExtName(SceneEditInfoParamVO sceneEditInfo){
  202. if(StrUtil.isNotEmpty(sceneEditInfo.getMusic())){
  203. String extName = cn.hutool.core.io.FileUtil.extName(sceneEditInfo.getMusic());
  204. if(StrUtil.isNotEmpty(extName)){
  205. sceneEditInfo.setMusic(sceneEditInfo.getMusic().replace(extName, extName.toLowerCase()));
  206. }
  207. }
  208. if(StrUtil.isNotEmpty(sceneEditInfo.getMusicFile())){
  209. String extName = cn.hutool.core.io.FileUtil.extName(sceneEditInfo.getMusicFile());
  210. if(StrUtil.isNotEmpty(extName)){
  211. sceneEditInfo.setMusicFile(sceneEditInfo.getMusicFile().replace(extName, extName.toLowerCase()));
  212. }
  213. }
  214. }
  215. @Override
  216. public SceneEditInfo getBySceneProId(long sceneProId) {
  217. return this.getOne(new LambdaQueryWrapper<SceneEditInfo>()
  218. .eq(SceneEditInfo::getSceneProId, sceneProId));
  219. }
  220. @Override
  221. public SceneEditInfo getByScenePlusId(long scenePlusId) {
  222. return this.getOne(new LambdaQueryWrapper<SceneEditInfo>()
  223. .eq(SceneEditInfo::getScenePlusId, scenePlusId));
  224. }
  225. @Transactional
  226. @Override
  227. public ResultData publicScene(SceneEditInfoParamVO param) throws Exception{
  228. String num = param.getNum();
  229. //根据场景码查询数据库
  230. ScenePro scenePro = sceneProService.findBySceneNum(num);
  231. ScenePlus scenePlus = scenePlusService.getScenePlusByNum(num);
  232. ScenePlusExt scenePlusExt = scenePlusExtService.getScenePlusExtByPlusId(scenePlus.getId());
  233. SceneEditInfo sceneEditInfo = this.getByScenePlusId(scenePlus.getId());
  234. SceneEditInfoExt sceneEditInfoExt = null;
  235. SceneEditControls sceneEditControls = null;
  236. if(sceneEditInfo == null){
  237. sceneEditInfo = new SceneEditInfo();
  238. sceneEditInfo.setSceneProId(scenePlus.getId());
  239. }else{
  240. sceneEditControls = sceneEditControlsService.getBySceneEditId(sceneEditInfo.getId());
  241. sceneEditInfoExt = sceneEditInfoExtService.getByEditInfoId(sceneEditInfo.getId());
  242. }
  243. if(sceneEditInfoExt == null){
  244. sceneEditInfoExt = new SceneEditInfoExt();
  245. sceneEditInfoExt.setFloorPlanAngle(0f);
  246. sceneEditInfoExt.setFloorPlanCompass(0f);
  247. }
  248. //生成sceneJson
  249. SceneJsonBean sceneJson = new SceneJsonBean();
  250. BeanUtil.copyProperties(sceneEditInfoExt, sceneJson);
  251. BeanUtil.copyProperties(sceneEditInfo, sceneJson);
  252. SceneEditControlsVO sceneEditControlsVO = BeanUtil.copyProperties(sceneEditControls, SceneEditControlsVO.class);
  253. sceneJson.setControls(sceneEditControlsVO);
  254. sceneJson.setNum(num);
  255. sceneJson.setCreateTime(scenePlus.getCreateTime());
  256. sceneJson.setSceneResolution(scenePlusExt.getSceneResolution());
  257. sceneJson.setSceneFrom(scenePlusExt.getSceneFrom());
  258. sceneJson.setSceneKind(scenePlusExt.getSceneKind());
  259. if(StrUtil.isNotEmpty(scenePlusExt.getVideos())){
  260. sceneJson.setVideos(scenePlusExt.getVideos());
  261. }
  262. //发布马赛克列表
  263. sceneJson.setMosaicList(this.getMosaicList(num));
  264. //处理热点数据,生成hot.json
  265. this.publicHotData(num, sceneEditInfo);
  266. //发布场景关联相关数据
  267. this.publicLinkSceneData(num);
  268. //本地写sceneJson文件
  269. String localSceneJsonPath = String.format(ConstantFilePath.SCENE_DATA_PATH_V4, num) + "scene.json";
  270. FileUtils.writeFile(localSceneJsonPath, JSON.toJSONString(sceneJson));
  271. //上传sceneJson文件
  272. String sceneJsonPath = String.format(UploadFilePath.DATA_VIEW_PATH+"scene.json", num);
  273. uploadToOssUtil.upload(JSON.toJSONBytes(sceneJson), sceneJsonPath);
  274. //sceneJson放入缓存
  275. String key = String.format(RedisKey.SCENE_JSON, num);
  276. redisUtil.set(key, JSON.toJSONString(sceneJson));
  277. //删除发布数据中的user目录
  278. String publicUserPath = String.format(UploadFilePath.USER_VIEW_PATH, num);
  279. uploadToOssUtil.deleteFile(publicUserPath);
  280. // String editUserPath = String.format(UploadFilePath.USER_EDIT_PATH, sceneNum);
  281. //复制编辑目录到发布目录
  282. String editPath = String.format(UploadFilePath.USER_EDIT_PATH, param.getNum());
  283. String viewPath = String.format(UploadFilePath.USER_VIEW_PATH, param.getNum());
  284. uploadToOssUtil.copyFiles(editPath,viewPath);
  285. //入库
  286. if(sceneEditInfo.getId() == null){
  287. this.save(sceneEditInfo);
  288. }else{
  289. this.updateById(sceneEditInfo);
  290. }
  291. sceneEditInfoExt.setSceneProId(scenePro.getId());
  292. sceneEditInfoExt.setScenePlusId(scenePlus.getId());
  293. sceneEditInfoExt.setEditInfoId(sceneEditInfo.getId());
  294. sceneEditInfoExtService.saveOrUpdate(sceneEditInfoExt);
  295. // todo 调用v3接口同步场景缩略图url---------------------------------start
  296. String url = fkankanMiniHost + URL_UPGRADE_TO_V4_RESULT_SYNC;
  297. fdkankanMiniClient.upgradeToV4ResultSync(url,
  298. RequestSceneProV4.builder()
  299. .id(scenePlus.getId())
  300. .thumb(scenePlusExt.getThumb())
  301. .sceneName(sceneEditInfo.getTitle())
  302. .sceneDec(sceneEditInfo.getDescription())
  303. .build(),
  304. new FdkkMiniReqSuccessCallback(), new FdkkMiniReqErrorCallback()
  305. );
  306. // todo 调用v3接口同步场景缩略图url---------------------------------end
  307. return ResultData.ok();
  308. }
  309. public void publicLinkSceneData(String num) throws IOException {
  310. String imgEditPath = String.format(UploadFilePath.IMG_EDIT_PATH, num);
  311. String userEditPath = String.format(UploadFilePath.USER_EDIT_PATH, num);
  312. String imgViewPath = String.format(UploadFilePath.IMG_VIEW_PATH, num);
  313. //生成links.json并上传到发布目录
  314. String linkPanKey = String.format(RedisKey.SCENE_LINKPAN_DATA, num);
  315. Map<String, String> linkPanMap = redisUtil.hmget(linkPanKey);
  316. if(CollUtil.isEmpty(linkPanMap)){
  317. return;
  318. }
  319. JSONArray linkPanArr = new JSONArray();
  320. linkPanMap.values().stream().forEach(linkPan->{
  321. linkPanArr.add(JSON.parseObject(linkPan));
  322. });
  323. String linkScenePath = userEditPath + "links.json";
  324. uploadToOssUtil.upload(linkPanArr.toString().getBytes(), linkScenePath);
  325. //拷贝编辑目录到发布目录
  326. uploadToOssUtil.deleteFile(imgViewPath + "panorama");
  327. uploadToOssUtil.copyFiles(imgEditPath + "panorama",imgViewPath + "panorama");
  328. }
  329. private void buildVideo(SceneEditInfo sceneEditInfo, String path, String num) throws Exception{
  330. if(CommonStatus.NO.equals(sceneEditInfo.getBuildVideoStatus())){
  331. return;
  332. }
  333. if(path != null && !"".equals(path) && path.startsWith("http")){
  334. path = ConstantFilePath.BUILD_MODEL_PATH + File.separator + path.split("/")[path.split("/").length - 2];
  335. }
  336. String target = path + "_images";
  337. log.info("球幕视频路径:{}", target + File.separator + "extras/video");
  338. File video = new File(target + File.separator + "extras/video");
  339. //如果文件夹或者文件不存在,跳出
  340. if(!video.exists() || video.listFiles() == null || video.listFiles().length == 0){
  341. return;
  342. }
  343. String userEditPath = String.format(UploadFilePath.USER_EDIT_PATH, num);
  344. for(String videoName : video.list()){
  345. log.info("球幕视频名称:{}", videoName);
  346. uploadToOssUtil.upload(target + File.separator + "extras/video/" + videoName,userEditPath + videoName);
  347. CreateObjUtil.mp4ToFlv(target + File.separator + "extras/video/" + videoName,
  348. target + File.separator + "extras/video/" + videoName.replace("mp4", "flv"));
  349. uploadToOssUtil.upload(target + File.separator + "extras/video/" + videoName.replace("mp4", "flv"),userEditPath + videoName.replace("mp4", "flv"));
  350. //覆盖原始视频资源
  351. FileUtils.copyFile(target + File.separator + "extras/video/" + videoName,
  352. path + File.separator + "caches/videos/" + videoName, true);
  353. }
  354. FileUtils.deleteDirectory(target + File.separator + "extras/video/");
  355. //重置状态
  356. sceneEditInfo.setBuildVideoStatus(CommonStatus.NO.code());
  357. }
  358. private void publicHotData(String sceneNum, SceneEditInfo sceneEditInfo) throws IOException {
  359. String hotDataKey = String.format(RedisKey.SCENE_HOT_DATA, sceneNum);
  360. Map<String, String> hotMap = redisUtil.hmget(hotDataKey);
  361. List<String> hotList = Lists.newArrayList();
  362. hotMap.entrySet().stream().forEach(entry->{
  363. if(StrUtil.isNotEmpty(entry.getValue())){
  364. hotList.add(entry.getValue());
  365. }
  366. });
  367. if(CollUtil.isNotEmpty(hotList)){
  368. JSONArray jsonhots = new JSONArray();
  369. hotList.stream().forEach(hot->{
  370. jsonhots.add(JSONObject.parseObject(hot));
  371. });
  372. String hotJsonPath = String.format(UploadFilePath.USER_EDIT_PATH, sceneNum) + "hot.json";
  373. uploadToOssUtil.upload(jsonhots.toString().getBytes(), hotJsonPath);
  374. //修改tags状态为是,标识有热点数据
  375. this.saveTagsToSceneEditInfo(sceneNum, sceneEditInfo);
  376. }
  377. }
  378. @Override
  379. public SceneInfoVO getSceneInfo(@Var SceneInfoParamVO param) throws Exception{
  380. SceneInfoReqType sceneInfoReqType = SceneInfoReqType.get(param.getReqType());
  381. switch (sceneInfoReqType){
  382. //如果是编辑页面请求,查数据库
  383. case EDIT:
  384. return this.getSceneInfo4Edit(param.getNum());
  385. //如果是查看页面请求,查redis
  386. case VIEW:
  387. return this.getSceneInfo4View(param.getNum());
  388. }
  389. return null;
  390. }
  391. @Override
  392. public SceneInfoDTO getSceneInfo4Download(SceneInfoParamVO param) throws Exception{
  393. SceneInfoVO sceneInfo4View = this.getSceneInfo4View(param.getNum());
  394. if(Objects.isNull(sceneInfo4View)){
  395. return null;
  396. }
  397. SceneInfoDTO sceneInfoDTO = BeanUtil.copyProperties(sceneInfo4View, SceneInfoDTO.class);
  398. ScenePlus scenePlus = scenePlusService.getScenePlusByNum(param.getNum());
  399. sceneInfoDTO.setSceneSource(scenePlus.getSceneSource());
  400. return sceneInfoDTO;
  401. }
  402. /**
  403. * <p>
  404. 编辑页面获取场景详情信息
  405. * </p>
  406. * @author dengsixing
  407. * @date 2022/1/21
  408. * @param num
  409. * @return com.fdkankan.scene.vo.SceneInfoVO
  410. **/
  411. private SceneInfoVO getSceneInfo4Edit(String num) throws Exception{
  412. // ScenePro scenePro = sceneProService.findBySceneNum(num);
  413. ScenePlus scenePlus = scenePlusService.getScenePlusByNum(num);
  414. if(Objects.isNull(scenePlus)){
  415. throw new BusinessException(ErrorCode.FAILURE_CODE_5005);
  416. }
  417. // SceneProExt sceneProExt = sceneProExtService.findBySceneProId(scenePro.getId());
  418. ScenePlusExt scenePlusExt = scenePlusExtService.getScenePlusExtByPlusId(scenePlus.getId());
  419. SceneEditInfo sceneEditInfo = this.getByScenePlusId(scenePlus.getId());
  420. if(Objects.isNull(sceneEditInfo)){
  421. throw new BusinessException(ErrorCode.FAILURE_CODE_5005);
  422. }
  423. SceneEditInfoExt sceneEditInfoExt = sceneEditInfoExtService.getByEditInfoId(sceneEditInfo.getId());
  424. SceneEditControls sceneEditControls = sceneEditControlsService.getBySceneEditId(sceneEditInfo.getId());
  425. SceneInfoVO sceneInfoVO = new SceneInfoVO();
  426. BeanUtil.copyProperties(sceneEditInfoExt, sceneInfoVO);
  427. BeanUtil.copyProperties(sceneEditInfo, sceneInfoVO);
  428. if(Objects.isNull(sceneInfoVO.getFloorPlanAngle())){
  429. sceneInfoVO.setFloorPlanAngle(0f);
  430. }
  431. if(Objects.isNull(sceneInfoVO.getFloorPlanCompass())){
  432. sceneInfoVO.setFloorPlanCompass(0f);
  433. }
  434. sceneInfoVO.setControls(BeanUtil.copyProperties(sceneEditControls, SceneEditControlsVO.class));
  435. sceneInfoVO.setNum(num);
  436. //生成sceneJson
  437. // if(StrUtil.isNotEmpty(sceneEditInfo.getFloorPlanPath())){
  438. // sceneInfoVO.setFloorPlanPaths(sceneEditInfo.getFloorPlanPath().split(","));
  439. // }
  440. sceneInfoVO.setCreateTime(scenePlus.getCreateTime());
  441. sceneInfoVO.setSceneResolution(scenePlusExt.getSceneResolution());
  442. sceneInfoVO.setSceneFrom(scenePlusExt.getSceneFrom());
  443. sceneInfoVO.setSceneKind(scenePlusExt.getSceneKind());
  444. sceneInfoVO.setVideos(scenePlusExt.getVideos());
  445. sceneInfoVO.setMosaicList(this.getMosaicList(num));
  446. // TODO: 2022/4/24 v3版本停机要切换---------------------------start
  447. // this.setExtData(sceneInfoVO, scenePlus.getCameraId());
  448. this.setExtDataFromV3(sceneInfoVO, scenePlus.getCameraId());
  449. // TODO: 2022/4/24 v3版本停机要切换---------------------------end
  450. return sceneInfoVO;
  451. }
  452. /**
  453. * <p>
  454. getInfo接口返回字段扩展
  455. * </p>
  456. * @author dengsixing
  457. * @date 2022/3/9
  458. **/
  459. private void setExtDataFromV3(SceneInfoVO sceneInfoVO, Long cameraId) throws Exception{
  460. if(Objects.isNull(cameraId)){
  461. return;
  462. }
  463. String url = fkankanMiniHost + URL_GET_DATASYNC_BY_CAMERAID + cameraId;
  464. Result<String> dataSyncResult = fdkankanMiniClient
  465. .getDataSyncType(url, new FdkkMiniReqSuccessCallback(), new FdkkMiniReqErrorCallback());
  466. sceneInfoVO.setDataSync(dataSyncResult.getMsg());
  467. }
  468. /**
  469. * <p>
  470. getInfo接口返回字段扩展
  471. * </p>
  472. * @author dengsixing
  473. * @date 2022/3/9
  474. **/
  475. private void setExtData(SceneInfoVO sceneInfoVO, Long cameraId) throws Exception{
  476. ResultData<CameraDetail> cameraResultData = platformGoodsClient.getCameraDetailByCameraId(cameraId);
  477. if(!cameraResultData.getSuccess()){
  478. throw new Exception(ServerCode.FEIGN_REQUEST_FAILD.message());
  479. }
  480. CameraDetail camera = cameraResultData.getData();
  481. if(Objects.isNull(camera)){
  482. throw new BusinessException(ErrorCode.FAILURE_CODE_7010);
  483. }
  484. if(Objects.isNull(camera.getCompanyId())){
  485. return;
  486. }
  487. ResultData<Company> companyResultData = platformUserClient.getCompanyById(camera.getCompanyId());
  488. if(!companyResultData.getSuccess()){
  489. throw new Exception(ServerCode.FEIGN_REQUEST_FAILD.message());
  490. }
  491. Company company = companyResultData.getData();
  492. if(Objects.isNull(company)){
  493. return;
  494. }
  495. sceneInfoVO.setDataSync(company.getDataSync());
  496. }
  497. /**
  498. * <p>
  499. 查看页面获取场景详情信息
  500. * </p>
  501. * @author dengsixing
  502. * @date 2022/1/21
  503. * @param num
  504. * @return com.fdkankan.scene.vo.SceneInfoVO
  505. **/
  506. private SceneInfoVO getSceneInfo4View(String num) throws Exception{
  507. // TODO: 2022/5/19 请求v3的接口,获取场景的状态 待v3停机后要换成v4d的逻辑-------------------------start
  508. String url = fkankanMiniHost + URL_GET_SCENEPRO_BY_NUM + num + "&addViewCount=yes";
  509. Result<SceneProV3> result = fdkankanMiniClient
  510. .getSceneProByNum(url, new FdkkMiniReqSuccessCallback(),
  511. new FdkkMiniReqErrorCallback());
  512. SceneProV3 sceneProV3 = result.getData();
  513. if(sceneProV3 == null || sceneProV3.getId() == null){
  514. throw new BusinessException(ErrorCode.FAILURE_CODE_5005);
  515. }
  516. if(sceneProV3.getRecStatus().equals("I")){
  517. throw new BusinessException(ErrorCode.FAILURE_CODE_5009);
  518. }
  519. if(SceneStatus.SUCCESS.code() != sceneProV3.getStatus()
  520. && SceneStatus.NO_DISPLAY.code() != sceneProV3.getStatus()){
  521. throw new BusinessException(ErrorCode.FAILURE_CODE_5033);
  522. }
  523. if(PayStatus.PAY.code() != sceneProV3.getPayStatus()){
  524. throw new BusinessException(ErrorCode.FAILURE_CODE_5034);
  525. }
  526. // TODO: 2022/5/19 请求v3的接口,获取场景的状态 待v3停机后要换成v4d的逻辑-------------------------send
  527. // ScenePro scenePro = sceneProService.findBySceneNum(num);
  528. ScenePlus scenePlus = scenePlusService.getScenePlusByNum(num);
  529. if(Objects.isNull(scenePlus)){
  530. throw new BusinessException(ErrorCode.FAILURE_CODE_5005);
  531. }
  532. String key = String.format(RedisKey.SCENE_JSON, num);
  533. String sceneJson = redisUtil.get(key);
  534. SceneInfoVO sceneInfoVO = null;
  535. //先查询redis
  536. if(StrUtil.isNotEmpty(sceneJson)) {
  537. sceneInfoVO = JSON.parseObject(sceneJson, SceneInfoVO.class);
  538. // TODO: 2022/4/24 v3版本停机要切换---------------------------start
  539. // this.setExtData(sceneInfoVO, scenePlus.getCameraId());
  540. this.setExtDataFromV3(sceneInfoVO, scenePlus.getCameraId());
  541. // TODO: 2022/4/24 v3版本停机要切换---------------------------end
  542. sceneInfoVO.setScenePassword(null);
  543. if(Objects.isNull(sceneInfoVO.getFloorPlanAngle())){
  544. sceneInfoVO.setFloorPlanAngle(0f);
  545. }
  546. if(Objects.isNull(sceneInfoVO.getFloorPlanCompass())){
  547. sceneInfoVO.setFloorPlanCompass(0f);
  548. }
  549. return sceneInfoVO;
  550. }
  551. //如果redis没找到,从scene.json中获取
  552. String objectName = String.format(ConstantFilePath.SCENE_VIEW_DATA_DATA_SCENEJSON, num);
  553. String objectContent = uploadToOssUtil.getObjectContent(bucket, objectName);
  554. if(StrUtil.isEmpty(objectContent))
  555. return null;
  556. sceneInfoVO = JSON.parseObject(objectContent, SceneInfoVO.class);
  557. // TODO: 2022/4/24 v3版本停机要切换---------------------------start
  558. // this.setExtData(sceneInfoVO, scenePlus.getCameraId());
  559. this.setExtDataFromV3(sceneInfoVO, scenePlus.getCameraId());
  560. // TODO: 2022/4/24 v3版本停机要切换---------------------------end
  561. if(Objects.isNull(sceneInfoVO.getFloorPlanAngle())){
  562. sceneInfoVO.setFloorPlanAngle(0f);
  563. }
  564. if(Objects.isNull(sceneInfoVO.getFloorPlanCompass())){
  565. sceneInfoVO.setFloorPlanCompass(0f);
  566. }
  567. redisUtil.set(key, JSON.toJSONString(sceneInfoVO));
  568. sceneInfoVO.setScenePassword(null);
  569. return sceneInfoVO;
  570. }
  571. @Override
  572. // @Transactional
  573. public ResultData saveUpload(SaveUploadParamVO param) throws Exception {
  574. FileBizType fileBizType = FileBizType.get(param.getBizType());
  575. String num = param.getNum();
  576. String fileInfo = param.getFileInfo();
  577. if(Objects.isNull(fileBizType)){
  578. throw new BusinessException(ErrorCode.FAILURE_CODE_7003);
  579. }
  580. switch (fileBizType){
  581. case MUSIC:
  582. return new MusicHandlerFactory().getHandler().save(num, fileInfo);
  583. case FLOOR_LOGO:
  584. return new FloorLogoHandlerFactory().getHandler().save(num, fileInfo);
  585. // case FLOOR_PLAN:
  586. // return new FloorPlanHandlerFactory().getHandler().save(num, fileInfo);
  587. case SCREENSHOT:
  588. return new ScreenshotHandlerFactory().getHandler().save(num, fileInfo);
  589. }
  590. return ResultData.ok();
  591. }
  592. @Override
  593. public ResultData saveCad(BaseDataParamVO param) throws Exception {
  594. String num = param.getNum();
  595. // ScenePro scenePro = sceneProService.findBySceneNum(num);
  596. ScenePlus scenePlus = scenePlusService.getScenePlusByNum(num);
  597. if(Objects.isNull(scenePlus))
  598. throw new BusinessException(ErrorCode.FAILURE_CODE_5005);
  599. String editUserPath = String.format(UploadFilePath.USER_EDIT_PATH, num);
  600. String localDataPath = String.format(ConstantFilePath.SCENE_USER_PATH_V4, num);
  601. JSONObject fileInfoJson = JSON.parseObject(param.getData());
  602. Float angel = null;
  603. Float compass = null;
  604. String floorJsonData = fileInfoJson.getString("floorJsonData");
  605. if(StrUtil.isNotBlank(floorJsonData)){
  606. JSONObject jsonObject = JSON.parseObject(floorJsonData);
  607. angel = jsonObject.getFloat("angle");
  608. compass = jsonObject.getFloat("compass");
  609. }
  610. //处理户型图数据
  611. //上传floorplan_user.json文件
  612. FileUtils.writeFile(localDataPath + "floorplan_user.json", floorJsonData);
  613. uploadToOssUtil.upload(localDataPath + "floorplan_user.json", editUserPath + "floorplan_user.json");
  614. //写入数据库
  615. Byte floorPlanUser = null;
  616. if(StrUtil.isNotEmpty(floorJsonData)){
  617. // TODO: 2022/7/26 生成hostType
  618. JSONObject houseTypeJson = CreateHouseJsonUtil
  619. .createHouseTypeJsonByUser(localDataPath + "floorplan_user.json");
  620. uploadToOssUtil.upload(houseTypeJson.toJSONString().getBytes(), editUserPath + "houseType.json");
  621. floorPlanUser = CommonStatus.YES.code();
  622. }
  623. SceneEditInfo sceneEditInfoDb = this.getByScenePlusId(scenePlus.getId());
  624. if(Objects.nonNull(sceneEditInfoDb)){
  625. LambdaUpdateWrapper<SceneEditInfo> updateWrapper = new LambdaUpdateWrapper<SceneEditInfo>()
  626. .setSql("version=version+" + 1)
  627. .eq(SceneEditInfo::getId, sceneEditInfoDb.getId());
  628. if(floorPlanUser != null){
  629. updateWrapper.set(SceneEditInfo::getFloorPlanUser, floorPlanUser);
  630. }
  631. this.update(updateWrapper);
  632. }else{
  633. sceneEditInfoDb = new SceneEditInfo();
  634. sceneEditInfoDb.setScenePlusId(scenePlus.getId());
  635. sceneEditInfoDb.setFloorPlanUser(floorPlanUser);
  636. this.save(sceneEditInfoDb);
  637. }
  638. SceneEditInfoExt sceneEditInfoExt = sceneEditInfoExtService.getByEditInfoId(sceneEditInfoDb.getId());
  639. if(Objects.isNull(sceneEditInfoExt)){
  640. sceneEditInfoExt = new SceneEditInfoExt();
  641. sceneEditInfoExt.setEditInfoId(sceneEditInfoDb.getId());
  642. }
  643. if(Objects.nonNull(angel)){
  644. sceneEditInfoExt.setFloorPlanAngle(angel);
  645. }
  646. if(Objects.nonNull(compass)){
  647. sceneEditInfoExt.setFloorPlanCompass(compass);
  648. }
  649. sceneEditInfoExtService.saveOrUpdate(sceneEditInfoExt);
  650. return ResultData.ok();
  651. }
  652. @Override
  653. public ResultData resetCad(String num) throws IOException {
  654. ScenePlus scenePlus = scenePlusService.getScenePlusByNum(num);
  655. if(Objects.isNull(scenePlus))
  656. throw new BusinessException(ErrorCode.FAILURE_CODE_5005);
  657. ScenePlusExt scenePlusExt = scenePlusExtService
  658. .getScenePlusExtByPlusId(scenePlus.getId());
  659. String path = scenePlusExt.getDataSource();
  660. //户型图上传
  661. String userEditPath = UploadFilePath.USER_EDIT_PATH + "floor-cad-%s.%s";
  662. String floorCadPath = path + "/results/floorplan_cad";
  663. List<String> floorCadList = FileUtil.getFileList(floorCadPath);
  664. if(CollUtil.isNotEmpty(floorCadList)){
  665. floorCadList.stream().forEach(str->{
  666. String substring = str.substring(str.lastIndexOf(File.separator) + 1);
  667. String[] arr = substring.split("floor");
  668. String[] arr2 = arr[1].split("\\.");
  669. uploadToOssUtil.upload(str, String.format(userEditPath, num, arr2[0], arr2[1]));
  670. });
  671. }
  672. //根据floorplan_cad.json生成houseType.json
  673. this.uploadHouseTypeJson(num);
  674. ScenePro scenePro = sceneProService.findBySceneNum(num);
  675. SceneEditInfo sceneEditInfoDb = this.getByScenePlusId(scenePlus.getId());
  676. SceneEditInfoExt sceneEditInfoExt = null;
  677. if(Objects.nonNull(sceneEditInfoDb)){
  678. LambdaUpdateWrapper<SceneEditInfo> updateWrapper = new LambdaUpdateWrapper<SceneEditInfo>()
  679. .setSql("version=version+" + 1)
  680. .set(SceneEditInfo::getFloorPlanUser, CommonStatus.NO.code())
  681. .eq(SceneEditInfo::getId, sceneEditInfoDb.getId());
  682. this.update(updateWrapper);
  683. sceneEditInfoExt = sceneEditInfoExtService.getByEditInfoId(sceneEditInfoDb.getId());
  684. }else{
  685. sceneEditInfoDb = new SceneEditInfo();
  686. sceneEditInfoDb.setSceneProId(scenePro.getId());
  687. sceneEditInfoDb.setScenePlusId(scenePlus.getId());
  688. sceneEditInfoDb.setFloorPlanUser(CommonStatus.NO.code());
  689. this.save(sceneEditInfoDb);
  690. }
  691. if(Objects.isNull(sceneEditInfoExt)){
  692. sceneEditInfoExt = new SceneEditInfoExt();
  693. sceneEditInfoExt.setSceneProId(scenePro.getId());
  694. sceneEditInfoDb.setScenePlusId(scenePlus.getId());
  695. sceneEditInfoExt.setEditInfoId(sceneEditInfoDb.getId());
  696. }
  697. sceneEditInfoExt.setFloorPlanAngle(0f);
  698. sceneEditInfoExt.setFloorPlanCompass(0f);
  699. sceneEditInfoExtService.saveOrUpdate(sceneEditInfoExt);
  700. return ResultData.ok();
  701. }
  702. private 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.USER_EDIT_PATH, num) + "houseType.json";
  723. uploadToOssUtil.upload(result.toJSONString().getBytes(), hourseTypeJsonPath);
  724. }
  725. private JSONArray[] createHouseTypeJsonHandler(JSONObject floor){
  726. JSONArray[] result = new JSONArray[2];
  727. //处理点
  728. Map<Integer, VertexBean> vertexMap = new HashMap<>();
  729. Map<String, PointBean> pointMap = new HashMap<>();
  730. Map<Integer, String> vpMap = new HashMap<>();
  731. JSONArray vertexArr = floor.getJSONArray("vertex-xy");
  732. for(int i = 0; i < vertexArr.size(); i++){
  733. Object o = vertexArr.get(i);
  734. VertexBean vertexBean = JSON.parseObject(JSON.toJSONString(o), VertexBean.class);
  735. Integer vertexId = vertexBean.getId();
  736. vertexMap.put(vertexId, vertexBean);
  737. String pointId = "Point" + i;
  738. pointMap.put(pointId, PointBean.builder().vectorId(pointId).x(vertexBean.getX()).y(vertexBean.getY()).build());
  739. vpMap.put(vertexId, pointId);
  740. }
  741. //处理墙
  742. Map<Integer, SegmentBean> segmentMap = new HashMap<>();
  743. Map<String, WallBean> wallMap = new HashMap<>();
  744. Map<Integer, String> swMap = new HashMap<>();
  745. JSONArray segmentArr = floor.getJSONArray("segment");
  746. Map<String, String> startMap = new HashMap<>();
  747. Map<String, String> endMap = new HashMap<>();
  748. for(int i = 0; i < segmentArr.size(); i++){
  749. Object o = segmentArr.get(i);
  750. SegmentBean segmentBean = JSON.parseObject(JSON.toJSONString(o), SegmentBean.class);
  751. String startPointId = vpMap.get(segmentBean.getA());
  752. String endPointId = vpMap.get(segmentBean.getB());
  753. segmentBean.setStartPointId(startPointId);
  754. segmentBean.setEndPointId(endPointId);
  755. Integer segmentId = segmentBean.getId();
  756. segmentMap.put(segmentId, segmentBean);
  757. String wallId = "Wall" + i;
  758. WallBean wallBean = WallBean.builder()
  759. .vectorId(wallId)
  760. .start(segmentBean.getStartPointId())
  761. .end(segmentBean.getEndPointId())
  762. .children(new String[]{})
  763. .width(0.2d)
  764. .build();
  765. wallMap.put(wallId, wallBean);
  766. startMap.put(wallBean.getStart(), wallBean.getVectorId());
  767. endMap.put(wallBean.getEnd(), wallBean.getVectorId());
  768. swMap.put(segmentId, wallId);
  769. }
  770. Collection<PointBean> pointBeans = pointMap.values();
  771. for (PointBean pointBean : pointBeans) {
  772. Map<String, String> parent = new HashMap<>();
  773. String startParent = startMap.get(pointBean.getVectorId());
  774. String endParent = endMap.get(pointBean.getVectorId());
  775. parent.put(startParent, "start");
  776. parent.put(endParent, "end");
  777. pointBean.setParent(parent);
  778. }
  779. JSONArray pointArr = JSON.parseArray(JSON.toJSONString(pointBeans));
  780. result[0] = pointArr;
  781. Collection<WallBean> wallBeans = wallMap.values();
  782. JSONArray wallArr = JSON.parseArray(JSON.toJSONString(wallBeans));
  783. result[1] = wallArr;
  784. return result;
  785. }
  786. @Override
  787. public ResultData renameCad(RenameCadParamVO param) throws IOException {
  788. String key = String.format(UploadFilePath.DATA_VIEW_PATH, param.getNum()) + "floorplan_cad.json";
  789. String floorplanCadJson = uploadToOssUtil.getObjectContent(this.bucket, key);
  790. if(StrUtil.isEmpty(floorplanCadJson)){
  791. return ResultData.ok();
  792. }
  793. Map<Integer, String> renameMap = new HashMap<>();
  794. param.getFloors().stream().forEach(floor->{
  795. renameMap.put(floor.getSubgroup(), floor.getName());
  796. });
  797. JSONObject jsonObject = JSON.parseObject(floorplanCadJson);
  798. if(Objects.isNull(jsonObject)){
  799. return ResultData.ok();
  800. }
  801. JSONArray floorArr = jsonObject.getJSONArray("floors");
  802. if(CollUtil.isEmpty(floorArr)){
  803. return ResultData.ok();
  804. }
  805. for (Object o : floorArr) {
  806. JSONObject item = (JSONObject)o;
  807. int subgroup = item.getIntValue("subgroup");
  808. String name = renameMap.get(subgroup);
  809. if(StrUtil.isEmpty(name)){
  810. continue;
  811. }
  812. item.put("name", name);
  813. }
  814. uploadToOssUtil.upload(jsonObject.toJSONString().getBytes(), key);
  815. return ResultData.ok();
  816. }
  817. private JSONObject updateFloorName(String sourceFloors, String targeFloors){
  818. JSONArray sourceFloorsJson = JSON.parseArray(sourceFloors);
  819. JSONObject targeFloorsJson = JSONObject.parseObject(targeFloors);
  820. JSONArray array = targeFloorsJson.getJSONArray("floors");
  821. for (int i = 0; i < array.size(); ++i) {
  822. JSONObject targetFloor = array.getJSONObject(i);
  823. int targetId = targetFloor.getIntValue("id");
  824. for (int j = 0; j < sourceFloorsJson.size(); ++j) {
  825. JSONObject floor = sourceFloorsJson.getJSONObject(j);
  826. int id = floor.getIntValue("id");
  827. String name = floor.getString("name");
  828. if (targetId != id)
  829. continue;
  830. targetFloor.put("name", name);
  831. }
  832. }
  833. return targeFloorsJson;
  834. }
  835. @Override
  836. public void upgradeVersionById(Long id) {
  837. this.update(new LambdaUpdateWrapper<SceneEditInfo>()
  838. .setSql("version=version+" + 1)
  839. .eq(SceneEditInfo::getId, id));
  840. }
  841. @Override
  842. public void upgradeSceneJsonVersion(String num, int version) throws IOException {
  843. //更新redis缓存版本号
  844. String key = String.format(RedisKey.SCENE_JSON, num);
  845. String sceneJson = redisUtil.get(key);
  846. if(StrUtil.isNotEmpty(sceneJson)){
  847. SceneJsonBean sceneJsonBean = JSON.parseObject(sceneJson, SceneJsonBean.class);
  848. sceneJsonBean.setVersion(version);
  849. redisUtil.set(key, JSON.toJSONString(sceneJsonBean));
  850. }
  851. //更新oss scene.json版本号
  852. String sceneJsonPath = String.format(UploadFilePath.DATA_VIEW_PATH, num) + "scene.json";
  853. sceneJson= uploadToOssUtil.getObjectContent(this.bucket, sceneJsonPath);
  854. if(StrUtil.isNotEmpty(sceneJson)){
  855. SceneJsonBean sceneJsonBean = JSON.parseObject(sceneJson, SceneJsonBean.class);
  856. sceneJsonBean.setVersion(version);
  857. uploadToOssUtil.upload(JSON.toJSONString(sceneJsonBean).getBytes(StandardCharsets.UTF_8), sceneJsonPath);
  858. }
  859. }
  860. @Override
  861. public ResultData uploadPanorama(String num, MultipartFile file) throws Exception {
  862. //校验压缩包格式
  863. // log.info("全景图压缩包文件名:" + file.getOriginalFilename());
  864. // if(!file.getOriginalFilename().endsWith(".zip")){
  865. // throw new BusinessException(ErrorCode.FAILURE_CODE_7015);
  866. // }
  867. if(!file.getOriginalFilename().endsWith(".zip") && !file.getOriginalFilename().endsWith(".jpg")){
  868. throw new BusinessException(ErrorCode.FAILURE_CODE_7007, "jpg或者zip");
  869. }
  870. // ScenePro scenePro = sceneProService.findBySceneNum(num);
  871. ScenePlus scenePlus = scenePlusService.getScenePlusByNum(num);
  872. if(scenePlus == null){
  873. throw new BusinessException(ErrorCode.FAILURE_CODE_5005);
  874. }
  875. // SceneProExt sceneProExt = sceneProExtService.findBySceneProId(scenePro.getId());
  876. ScenePlusExt scenePlusExt = scenePlusExtService
  877. .getScenePlusExtByPlusId(scenePlus.getId());
  878. //原始计算根目录
  879. // String path = "F:\\test";
  880. String path = scenePlusExt.getDataSource();
  881. //全景图计算根目录
  882. String target = path + "_images";
  883. //解压缩文件存放目录
  884. String targetImagesPath = target + "/extras/images/";
  885. //压缩文件保存目录
  886. String zipTargetFilePath = targetImagesPath + file.getOriginalFilename();
  887. //压缩包保存到本地
  888. // String cachePath = String.format(ConstantFilePath.SCENE_CACHE_IMAGES, num);
  889. //先删除本地文件
  890. FileUtils.deleteDirectory(targetImagesPath);
  891. File targetFile = new File(zipTargetFilePath);
  892. if(!targetFile.getParentFile().exists()){
  893. targetFile.getParentFile().mkdirs();
  894. }
  895. file.transferTo(targetFile);
  896. //如果是压缩包上传,需要解压缩
  897. if(file.getOriginalFilename().endsWith(".zip")){
  898. //解压zip包
  899. FileUtil.unZip(zipTargetFilePath, targetImagesPath);
  900. // FileUtils.decompress(zipTargetFilePath, targetImagesPath);
  901. //删除压缩包
  902. FileUtil.delFile(zipTargetFilePath);
  903. }
  904. //判断文件夹目录结构,图片必须放在压缩包根目录下,不支持空文件夹或其他格式文件上传
  905. File[] files = new File(targetImagesPath).listFiles();
  906. Arrays.stream(files).forEach(item->{
  907. if(item.isDirectory()){
  908. throw new BusinessException(ErrorCode.FAILURE_CODE_7018);
  909. }
  910. });
  911. //获取解压后的文件列表
  912. List<String> uploadFileList = FileUtil.getFileList(targetImagesPath);
  913. if(CollUtil.isEmpty(uploadFileList)){
  914. throw new BusinessException(ErrorCode.FAILURE_CODE_5062);
  915. }
  916. //判断是否有可用的jpg文件
  917. boolean existJpg = false;
  918. if(CollUtil.isNotEmpty(uploadFileList)){
  919. existJpg = uploadFileList.stream().anyMatch(str -> {
  920. if(str.endsWith(".jpg")){
  921. return true;
  922. }
  923. return false;
  924. });
  925. }
  926. if(!existJpg){
  927. throw new BusinessException(ErrorCode.FAILURE_CODE_5062);
  928. }
  929. //列出caches/images中的文件列表
  930. String imgCachePath = String.format(UploadFilePath.IMG_CACHES_PATH, num);
  931. //比对图片列表,不存在的要返回名称集合
  932. String visionPath = path + "/results/vision.txt";
  933. List<String> panoramaImageList = SceneUtil.getPanoramaImageList(visionPath);
  934. List<String> notExistFileList = uploadFileList.stream().filter(filePath -> {
  935. filePath = filePath.substring(filePath.lastIndexOf(File.separator) + 1);
  936. if(panoramaImageList.contains(filePath)){
  937. return false;
  938. }
  939. return true;
  940. }).collect(Collectors.toList());
  941. if(CollUtil.isNotEmpty(notExistFileList)){
  942. //删除错误文件
  943. notExistFileList.parallelStream().forEach(filePath->{
  944. FileUtils.deleteFile(filePath);
  945. });
  946. }
  947. //判断成功的图片,如果成功图片为0,就直接返回,不需要执行算法
  948. uploadFileList = FileUtil.getFileList(targetImagesPath);
  949. if(CollUtil.isEmpty(uploadFileList)){
  950. if(CollUtil.isNotEmpty(notExistFileList)){
  951. notExistFileList = notExistFileList.stream().map(filePath -> {
  952. return filePath.substring(filePath.lastIndexOf(File.separator) + 1);
  953. }).collect(Collectors.toList());
  954. }
  955. return ResultData.ok(new UploadPanoramaVO(0, notExistFileList));
  956. }
  957. //上传
  958. Map<String, String> map = new HashMap<>();
  959. // uploadFileList.stream().forEach(filePath->{
  960. // map.put(filePath, filePath.replace(targetImagesPath, imgCachePath));
  961. // });
  962. String imgViewPath = String.format(UploadFilePath.IMG_VIEW_PATH, num);
  963. FileUtils.downLoadFromUrl(ossUrlPrefix + imgViewPath + "vision.modeldata" + "?m="+new Date().getTime(),
  964. "vision.modeldata", target + File.separator + "extras" + File.separator);
  965. CreateObjUtil.convertVisionmodeldataToTxt(target + File.separator + "extras" + File.separator + "vision.modeldata",
  966. target + File.separator + "extras" + File.separator + "vision.txt");
  967. FileUtils.copyFile(path + File.separator + "data.json", target + File.separator+"data.json", true);
  968. FileUtils.copyFile(path + File.separator + "project.json", target + File.separator+"project.json", true);
  969. //data.json增加extras为执行重建算法
  970. String data = FileUtils.readFile(target + File.separator+"data.json");
  971. if(data != null){
  972. JSONObject floorplanJson = new JSONObject();
  973. floorplanJson.put("has_source_images", true);
  974. floorplanJson.put("has_vision_txt", true);
  975. JSONObject dataJson = JSONObject.parseObject(data);
  976. dataJson.put("extras", floorplanJson);
  977. dataJson.put("split_type", "SPLIT_V8");
  978. //V5表示不需要生成high,low文件
  979. String skyboxType = "SKYBOX_V6";
  980. if(scenePlusExt.getSceneScheme() == 11){
  981. skyboxType = "SKYBOX_V7";
  982. }
  983. //sceneScheme为3切成瓦片图
  984. if(scenePlusExt.getSceneScheme() == 3){
  985. if("4k".equals(scenePlusExt.getSceneResolution())){
  986. skyboxType = "SKYBOX_V14";
  987. }else{
  988. skyboxType = "SKYBOX_V13";
  989. }
  990. }
  991. dataJson.put("skybox_type", skyboxType);
  992. FileUtils.writeFile(target + File.separator+"data.json", new String(dataJson.toString().getBytes(), "UTF-8"));
  993. }
  994. if(new File(target + File.separator + "capture").exists()){
  995. new File(target + File.separator + "capture").delete();
  996. }
  997. if(new File(target + File.separator + "results").exists()){
  998. FileUtils.delAllFile(target + File.separator + "results");
  999. }
  1000. if(StorageType.AWS.code().equals(this.type)){
  1001. //亚马逊保持旧方式,超链接capture
  1002. CreateObjUtil.createSoftConnection(path + File.separator + "capture", target + File.separator + "capture");
  1003. }
  1004. CreateObjUtil.ossUtilCp(ConstantFilePath.OSS_PREFIX + path.replace(ConstantFilePath.BUILD_MODEL_PATH, "") + "/data.fdage", target + File.separator + "capture/");
  1005. CreateObjUtil.build3dModel(target , "1");
  1006. String uploadJsonPath= target + File.separator + "results" +File.separator+"upload.json";
  1007. Thread.sleep(2000);
  1008. boolean exist = ComputerUtil.checkComputeCompleted(uploadJsonPath, maxCheckTimes, waitTime);
  1009. if(!exist){
  1010. throw new BusinessException(ErrorCode.FAILURE_CODE_7013);
  1011. }
  1012. String uploadData = FileUtils.readFile(uploadJsonPath);
  1013. JSONObject uploadJson = null;
  1014. JSONArray array = null;
  1015. if(uploadData!=null) {
  1016. uploadJson = JSONObject.parseObject(uploadData);
  1017. array = uploadJson.getJSONArray("upload");
  1018. }
  1019. if(array == null){
  1020. throw new BusinessException(ErrorCode.FAILURE_CODE_7013);
  1021. }
  1022. JSONObject fileJson = null;
  1023. String fileName = "";
  1024. for(int i = 0, len = array.size(); i < len; i++) {
  1025. fileJson = array.getJSONObject(i);
  1026. fileName = fileJson.getString("file");
  1027. //文件不存在抛出异常
  1028. if (!new File(target + File.separator + "results" + File.separator + fileName)
  1029. .exists()) {
  1030. throw new Exception(
  1031. target + File.separator + "results" + File.separator + fileName + "文件不存在");
  1032. }
  1033. //high文件夹
  1034. if (fileJson.getIntValue("clazz") == 3) {
  1035. map.put(target + File.separator + "results" + File.separator + fileName,
  1036. imgViewPath + "pan/high/" + fileName.replace("high/", ""));
  1037. continue;
  1038. }
  1039. //low文件夹
  1040. if (fileJson.getIntValue("clazz") == 4) {
  1041. map.put(target + File.separator + "results" + File.separator + fileName,
  1042. imgViewPath + "pan/low/" + fileName.replace("low/", ""));
  1043. continue;
  1044. }
  1045. //tiles文件夹,亚马逊没有裁剪图片api,不需要上传4k图
  1046. if (fileJson.getIntValue("clazz") == 5) {
  1047. map.put(target + File.separator + "results" + File.separator + fileName,
  1048. imgViewPath + fileName);
  1049. continue;
  1050. }
  1051. //tiles文件夹,亚马逊瓦片图
  1052. if (fileJson.getIntValue("clazz") == 7) {
  1053. map.put(target + File.separator + "results" + File.separator + fileName,
  1054. imgViewPath + fileName);
  1055. continue;
  1056. }
  1057. }
  1058. if(map.size()>0) {
  1059. uploadToOssUtil.uploadMulFiles(map);
  1060. }
  1061. //拷贝修改后的全景图到缓存目录
  1062. String cachedImagesPath = String.format(ConstantFilePath.SCENE_CACHE_IMAGES, num);
  1063. uploadFileList.stream().forEach(srcPath->{
  1064. cn.hutool.core.io.FileUtil.copy(srcPath, srcPath.replace(targetImagesPath, cachedImagesPath), true);
  1065. });
  1066. //更新数据库版本号
  1067. SceneEditInfo sceneEditInfo = this.getByScenePlusId(scenePlus.getId());
  1068. this.upgradeVersionById(sceneEditInfo.getId());
  1069. //更新scenejson缓存和oss文件版本号
  1070. this.upgradeSceneJsonVersion(num, sceneEditInfo.getVersion() + 1);
  1071. //如果部分成功,则需要返回成功数量和失败列表
  1072. if(CollUtil.isNotEmpty(notExistFileList)){
  1073. notExistFileList = notExistFileList.stream().map(filePath -> {
  1074. return filePath.substring(filePath.lastIndexOf(File.separator) + 1);
  1075. }).collect(Collectors.toList());
  1076. return ResultData.ok(new UploadPanoramaVO(uploadFileList.size(), notExistFileList));
  1077. }
  1078. return ResultData.ok();
  1079. }
  1080. @Override
  1081. public ResultData downloadPanorama(FileParamVO param) throws Exception {
  1082. String num = param.getNum();
  1083. String fileName = param.getFileName();
  1084. ScenePlus scenePlus = scenePlusService.getScenePlusByNum(num);
  1085. if(Objects.isNull(scenePlus)){
  1086. throw new BusinessException(ErrorCode.FAILURE_CODE_5005);
  1087. }
  1088. ScenePlusExt scenePlusExt = scenePlusExtService.getScenePlusExtByPlusId(scenePlus.getId());
  1089. String path = scenePlusExt.getDataSource();
  1090. String cachePath = String.format(ConstantFilePath.SCENE_CACHE, num);
  1091. String imgCachePath = String.format(UploadFilePath.IMG_CACHES_PATH, num);
  1092. String localImagesPath = String.format(ConstantFilePath.SCENE_CACHE_IMAGES, num);
  1093. String cacheFormat = "downloads/scene/%s/caches/";
  1094. String cacheImageFormat = "downloads/scene/%s/caches/images/";
  1095. String url = null;
  1096. String downloadName = null;
  1097. //如果入参文件名不为空,则是单个文件下载,不需要打包
  1098. if(StrUtil.isNotEmpty(fileName)){
  1099. //如果是单张图片,直接提供oss url
  1100. String localFilePath = localImagesPath + fileName;
  1101. String ossFilePath = imgCachePath + fileName;
  1102. if(!cn.hutool.core.io.FileUtil.exist(localFilePath)){
  1103. throw new BusinessException(ErrorCode.FAILURE_CODE_5063);
  1104. }
  1105. uploadToOssUtil.upload(localFilePath, ossFilePath);
  1106. url = ossUrlPrefix + ossFilePath;
  1107. // FileUtils.downLoadFromUrl(imageUrl, fileName, localImagesPath);
  1108. downloadName = fileName;
  1109. }else{
  1110. //从vision.txt中读取有效全景图名称列表
  1111. // String visionPath = path + "/results/vision.txt";
  1112. // List<String> imageList = SceneUtil.getPanoramaImageList(visionPath);
  1113. //
  1114. // //先下载到本地
  1115. // List<String> keyList = cn.hutool.core.io.FileUtil.listFileNames(imgCachePath);
  1116. // if (!StorageType.LOCAL.code().equals(this.type)) {// TODO: 2022/2/15 这里有可能有问题,可能还需要考虑本地部署的情况
  1117. //// System.setProperty("java.util.concurrent.ForkJoinPool.common.parallelism", "8");
  1118. // keyList.stream().forEach(key->{
  1119. // String file = key.substring(key.lastIndexOf("/") + 1);
  1120. // if(imageList.contains(file)){
  1121. // String imageUrl = ossUrlPrefix + imgCachePath + file + "?t=" + System.currentTimeMillis();
  1122. // FileUtils.downLoadFromUrl(imageUrl, file, localImagesPath);
  1123. // }
  1124. // });
  1125. // }
  1126. if(!cn.hutool.core.io.FileUtil.exist(localImagesPath)
  1127. || cn.hutool.core.io.FileUtil.isDirEmpty(new File(localImagesPath))){
  1128. throw new BusinessException(ErrorCode.FAILURE_CODE_5063);
  1129. }
  1130. downloadName = num + "_images.zip";
  1131. //打包
  1132. String zipPath = cachePath + downloadName;
  1133. ZipUtil.zip(localImagesPath, zipPath);
  1134. //上传压缩包
  1135. // uploadToOssUtil.upload(zipPath, String.format(cacheFormat, num) + downloadName);
  1136. uploadToOssUtil.uploadBySh(zipPath, String.format(cacheFormat, num) + downloadName);
  1137. url = ossUrlPrefix + String.format(cacheFormat, num) + downloadName;
  1138. //删除本地压缩包
  1139. FileUtils.deleteFile(zipPath);
  1140. // //删除本地目录
  1141. // FileUtils.deleteDirectory(localImagesPath);
  1142. }
  1143. Map<String, Object> map = new HashMap<>();
  1144. map.put("fileUrl", url + "?t=" + System.currentTimeMillis());
  1145. map.put("fileName", downloadName);
  1146. return ResultData.ok(map);
  1147. }
  1148. @Override
  1149. public void saveTagsToSceneEditInfo(String num, SceneEditInfo sceneEditInfo){
  1150. //查询缓存是否包含热点数据
  1151. String key = String.format(RedisKey.SCENE_HOT_DATA, num);
  1152. Map<String, String> allTagsMap = redisUtil.hmget(key);
  1153. boolean hashTags = false;
  1154. for (Entry<String, String> tagMap : allTagsMap.entrySet()) {
  1155. if(StrUtil.isEmpty(tagMap.getValue())){
  1156. continue;
  1157. }
  1158. hashTags = true;
  1159. break;
  1160. }
  1161. //更改热点状态
  1162. sceneEditInfo.setTags(hashTags ? CommonStatus.YES.code() : CommonStatus.NO.code());
  1163. //version 是空的代表 sceneEditInfo记录集不存在
  1164. if(sceneEditInfo.getVersion() != null){
  1165. sceneEditInfo.setVersion(sceneEditInfo.getVersion() + 1);
  1166. }
  1167. }
  1168. @Override
  1169. public ResultData saveVideoBox(FileNameAndDataParamVO param) throws Exception {
  1170. JSONObject boxVideo = JSONObject.parseObject(param.getData());
  1171. String sid = boxVideo.getString("sid");
  1172. if(StrUtil.isEmpty(sid)){
  1173. throw new BusinessException(ErrorCode.PARAM_REQUIRED);
  1174. }
  1175. // ScenePro scenePro = sceneProService.findBySceneNum(param.getNum());
  1176. ScenePlus scenePlus = scenePlusService.getScenePlusByNum(param.getNum());
  1177. if(Objects.isNull(scenePlus))
  1178. throw new BusinessException(ErrorCode.FAILURE_CODE_5005);
  1179. SceneEditInfo sceneEditInfo = this.getByScenePlusId(scenePlus.getId());
  1180. //转换视频格式
  1181. this.transferToFlv(param.getNum(), param.getFileName());
  1182. //生成boxVideos数据
  1183. String boxVideos = this.createBoxVideos(param.getNum(), sid, boxVideo, sceneEditInfo, OperationType.ADDORUPDATE.code());
  1184. //更新数据库
  1185. this.updateBoxVideos(sceneEditInfo, scenePlus.getId(), boxVideos);
  1186. return ResultData.ok();
  1187. }
  1188. @Override
  1189. public ResultData deleteVideoBox(DeleteSidParamVO param) throws Exception {
  1190. // ScenePro scenePro = sceneProService.findBySceneNum(param.getNum());
  1191. ScenePlus scenePlus = scenePlusService.getScenePlusByNum(param.getNum());
  1192. if(Objects.isNull(scenePlus))
  1193. throw new BusinessException(ErrorCode.FAILURE_CODE_5005);
  1194. SceneEditInfo sceneEditInfo = this.getByScenePlusId(scenePlus.getId());
  1195. //根据sid移除json
  1196. String boxVideos = this.createBoxVideos(param.getNum(), param.getSid(), null, sceneEditInfo, OperationType.DELETE.code());
  1197. //写数据库
  1198. this.updateBoxVideos(sceneEditInfo,scenePlus.getId(),boxVideos);
  1199. return ResultData.ok();
  1200. }
  1201. @Override
  1202. public ResultData saveBoxPhoto(BaseDataParamVO param) throws Exception {
  1203. JSONObject boxPhoto = JSONObject.parseObject(param.getData());
  1204. String sid = boxPhoto.getString("sid");
  1205. if(StrUtil.isEmpty(sid)){
  1206. throw new BusinessException(ServerCode.PARAM_REQUIRED, sid);
  1207. }
  1208. ScenePlus scenePlus = scenePlusService.getScenePlusByNum(param.getNum());
  1209. if(Objects.isNull(scenePlus))
  1210. throw new BusinessException(ErrorCode.FAILURE_CODE_5005);
  1211. SceneEditInfo sceneEditInfo = this.getByScenePlusId(scenePlus.getId());
  1212. //生成boxVideos数据
  1213. String boxPhotos = this.createBoxPhotos(param.getNum(), sid, boxPhoto, sceneEditInfo, OperationType.ADDORUPDATE.code());
  1214. //更新数据库
  1215. this.updateBoxPhotos(sceneEditInfo, boxPhotos);
  1216. return ResultData.ok();
  1217. }
  1218. @Override
  1219. public ResultData deleteBoxPhoto(DeleteSidParamVO param) throws Exception {
  1220. ScenePlus scenePlus = scenePlusService.getScenePlusByNum(param.getNum());
  1221. if(Objects.isNull(scenePlus))
  1222. throw new BusinessException(ErrorCode.FAILURE_CODE_5005);
  1223. SceneEditInfo sceneEditInfo = this.getByScenePlusId(scenePlus.getId());
  1224. //根据sid移除json
  1225. String boxPhotos = this.createBoxPhotos(param.getNum(), param.getSid(), null, sceneEditInfo, OperationType.DELETE.code());
  1226. //写数据库
  1227. this.updateBoxPhotos(sceneEditInfo, boxPhotos);
  1228. return ResultData.ok();
  1229. }
  1230. @Override
  1231. public DownloadVO downloadBallScreenVideo(BallScreenVideoParamVO param) {
  1232. String videoPath = String.format(UploadFilePath.VIDEOS_VIEW_PATH, param.getNum()) + param.getFileName();
  1233. String url = ossUrlPrefix + videoPath + "?t=" + System.currentTimeMillis();
  1234. return DownloadVO.builder()
  1235. .fileName(param.getFileName())
  1236. .url(url)
  1237. .build();
  1238. }
  1239. @Override
  1240. public ResultData uploadBallScreenVideo(String num, String fileName, MultipartFile file) throws Exception {
  1241. //校验格式
  1242. if(!fileName.endsWith(".mp4")){
  1243. throw new BusinessException(ErrorCode.FAILURE_CODE_7016);
  1244. }
  1245. String videosViewPath = String.format(UploadFilePath.VIDEOS_VIEW_PATH, num);
  1246. // ScenePro scenePro = sceneProService.findBySceneNum(num);
  1247. ScenePlus scenePlus = scenePlusService.getScenePlusByNum(num);
  1248. if(scenePlus == null){
  1249. throw new BusinessException(ErrorCode.FAILURE_CODE_5005);
  1250. }
  1251. // SceneProExt sceneProExt = sceneProExtService.findBySceneProId(scenePro.getId());
  1252. ScenePlusExt scenePlusExt = scenePlusExtService.getScenePlusExtByPlusId(scenePlus.getId());
  1253. String path = scenePlusExt.getDataSource();
  1254. //校验文件名
  1255. String videos = scenePlusExt.getVideos();
  1256. if(StrUtil.isEmpty(videos)){
  1257. throw new BusinessException(ErrorCode.FAILURE_CODE_7012);
  1258. }
  1259. JSONObject videosJson = JSON.parseObject(videos);
  1260. JSONArray dataArr = videosJson.getJSONArray("data");
  1261. boolean exists = false;
  1262. for (Object o : dataArr) {
  1263. JSONObject jsonObject = (JSONObject)o;
  1264. if(jsonObject.getString("id").equals(fileName.replace(".mp4", ""))){
  1265. exists = true;
  1266. break;
  1267. }
  1268. }
  1269. if(!exists){
  1270. throw new BusinessException(ErrorCode.FAILURE_CODE_7012);
  1271. }
  1272. if(path != null && !"".equals(path) && path.startsWith("http")){
  1273. path = ConstantFilePath.BUILD_MODEL_PATH + File.separator + path.split("/")[path.split("/").length - 2];
  1274. }
  1275. String target = path + "_images";
  1276. String filePath = target + File.separator + "extras/video" + File.separator + fileName;
  1277. File targetFile = new File(filePath);
  1278. if(!targetFile.getParentFile().exists()){
  1279. targetFile.getParentFile().mkdirs();
  1280. }
  1281. //保存视频到本地
  1282. file.transferTo(targetFile);
  1283. File video = new File(target + File.separator + "extras/video");
  1284. //如果文件夹或者文件不存在,跳出
  1285. if(!video.exists() || video.listFiles() == null || video.listFiles().length == 0){
  1286. throw new BusinessException(ErrorCode.FAILURE_CODE_7009);
  1287. }
  1288. for(String videoName : video.list()){
  1289. log.info("球幕视频名称:{}", videoName);
  1290. uploadToOssUtil.upload(target + File.separator + "extras/video/" + videoName,videosViewPath + videoName);
  1291. CreateObjUtil.mp4ToFlv(target + File.separator + "extras/video/" + videoName,
  1292. target + File.separator + "extras/video/" + videoName.replace("mp4", "flv"));
  1293. uploadToOssUtil.upload(target + File.separator + "extras/video/" + videoName.replace("mp4", "flv"),videosViewPath + videoName.replace("mp4", "flv"));
  1294. //覆盖原始视频资源
  1295. FileUtils.copyFile(target + File.separator + "extras/video/" + videoName,
  1296. path + File.separator + "caches/videos/" + videoName, true);
  1297. }
  1298. FileUtils.deleteDirectory(target + File.separator + "extras/video/");
  1299. SceneEditInfo sceneEditInfo = this.getByScenePlusId(scenePlus.getId());
  1300. this.upgradeVersionById(sceneEditInfo.getId());
  1301. //更新scene.json版本号
  1302. // this.upgradeVersionToSceneJson(num);
  1303. //更新scenejson缓存和oss文件版本号
  1304. this.upgradeSceneJsonVersion(num, sceneEditInfo.getVersion() + 1);
  1305. return ResultData.ok();
  1306. }
  1307. @Override
  1308. public void upgradeVersionToSceneJson(String num) throws Exception{
  1309. String key = String.format(RedisKey.SCENE_JSON, num);
  1310. String objectName = String.format(ConstantFilePath.SCENE_VIEW_DATA_DATA_SCENEJSON, num);
  1311. String objectContent = uploadToOssUtil.getObjectContent(bucket, objectName);
  1312. SceneJsonBean sceneJsonBean = JSON.parseObject(objectContent, SceneJsonBean.class);
  1313. sceneJsonBean.setVersion(sceneJsonBean.getVersion() +1);
  1314. uploadToOssUtil.upload(JSON.toJSONString(sceneJsonBean).getBytes(), objectName);
  1315. redisUtil.set(key, JSON.toJSONString(sceneJsonBean));
  1316. }
  1317. @Override
  1318. public ResultData sceneSync(String num, String type, String floorPlanJson, String ajkJson, String cameraJson,
  1319. MultipartFile[] files) throws Exception {
  1320. String lockKey = String.format(RedisLockKey.LOCK_SCENE_SYNC, num);
  1321. Boolean lock = redisLockUtil.lock(lockKey, RedisKey.EXPIRE_TIME_2_HOUR);
  1322. if(!lock){
  1323. throw new BusinessException(ErrorCode.SYSTEM_BUSY);
  1324. }
  1325. try {
  1326. // ScenePro scenePro = sceneProService.findBySceneNum(num);
  1327. ScenePlus scenePlus = scenePlusService.getScenePlusByNum(num);
  1328. if(scenePlus == null){
  1329. throw new BusinessException(ErrorCode.FAILURE_CODE_5005);
  1330. }
  1331. // SceneProExt sceneProExt = sceneProExtService.findBySceneProId(scenePro.getId());
  1332. ScenePlusExt scenePlusExt = scenePlusExtService.getScenePlusExtByPlusId(scenePlus.getId());
  1333. //更新scene.json文件
  1334. String strsceneInfos = FileUtils.readFile(ConstantFilePath.SCENE_PATH + "data" + File.separator + "data" + num + File.separator + "scene.json");
  1335. if(strsceneInfos == null)
  1336. new File(ConstantFilePath.SCENE_PATH + "data" + File.separator + "data" + num + File.separator + "scene.json").createNewFile();
  1337. String path = scenePlusExt.getDataSource();// /mnt/data/0662c5389/831989883441512448/4898cab04f8c_202104141602356060/
  1338. if(path != null && !"".equals(path) && path.startsWith("http")){
  1339. path = ConstantFilePath.BUILD_MODEL_PATH + File.separator + path.split("/")[path.split("/").length - 2];
  1340. }
  1341. String target = path + "_ajk"; // /mnt/data/0662c5389/831989883441512448/4898cab04f8c_202104141602356060_ajk
  1342. File editPath = new File(target);
  1343. if(!editPath.exists()){
  1344. editPath.mkdirs();
  1345. }
  1346. //创建文件夹软连接并且复制data.json和project.json
  1347. if(new File(target + File.separator + "capture").exists()){
  1348. new File(target + File.separator + "capture").delete();
  1349. }
  1350. if(new File(target + File.separator + "caches").exists()){
  1351. //删除link
  1352. new File(target + File.separator + "caches" + File.separator + "images").delete();
  1353. //删除所有文件
  1354. FileUtils.delAllFile(target + File.separator + "caches");
  1355. }
  1356. if(new File(target + File.separator + "results").exists()){
  1357. FileUtils.delAllFile(target + File.separator + "results");
  1358. }
  1359. //创建文件夹,并link文件夹
  1360. new File(target + File.separator + "caches").mkdirs();
  1361. CreateObjUtil.createSoftConnection(path + File.separator + "capture", target + File.separator + "capture");
  1362. if(new File(path + File.separator + "caches" + File.separator + "images").exists()){
  1363. CreateObjUtil.createSoftConnection(path + File.separator + "caches" + File.separator + "images", target + File.separator + "caches" + File.separator + "images");
  1364. }
  1365. FileUtils.copyFile(path + File.separator + "data.json", target + File.separator+"data.json", true);
  1366. FileUtils.copyFile(path + File.separator + "project.json", target + File.separator+"project.json", true);
  1367. //data.json增加extras为执行重建算法
  1368. String project = FileUtils.readFile(target + File.separator+"project.json");
  1369. if(project != null){
  1370. JSONObject projectJson = JSONObject.parseObject(project);
  1371. projectJson.put("parent", projectJson.get("uuid"));
  1372. projectJson.put("uuid", UUID.randomUUID().toString());
  1373. projectJson.put("time", System.currentTimeMillis());
  1374. FileUtils.writeFile(path + File.separator + "project.json", projectJson.toString());
  1375. }
  1376. String data = FileUtils.readFile(target + File.separator+"data.json");
  1377. if(data != null){
  1378. JSONObject floorplanJson = new JSONObject();
  1379. floorplanJson.put("has_floor_ajk_json", true);
  1380. floorplanJson.put("has_vision_txt", true);
  1381. floorplanJson.put("has_floorplan_json", true);
  1382. JSONObject dataJson = JSONObject.parseObject(data);
  1383. dataJson.put("extras", floorplanJson);
  1384. //V5表示不需要生成high,low文件
  1385. dataJson.put("skybox_type", "SKYBOX_V8");
  1386. dataJson.put("split_type", "SPLIT_V10");
  1387. FileUtils.writeFile(target + File.separator+"data.json", new String(dataJson.toString().getBytes(), "UTF-8"));
  1388. }
  1389. //文件上传的位置可以自定义
  1390. log.info("画墙重建模型开始");
  1391. File targetFile = new File(target + File.separator + "extras" + File.separator + "floor_ajk.json");
  1392. if(!targetFile.getParentFile().exists()){
  1393. targetFile.getParentFile().mkdirs();
  1394. }
  1395. if(targetFile.exists()){
  1396. FileUtils.deleteFile(target + File.separator + "extras" + File.separator + "floor_ajk.json");
  1397. }
  1398. // 保存
  1399. FileUtils.writeFile(target + File.separator + "extras" + File.separator + "floorplan.json", new String(floorPlanJson.getBytes(), "UTF-8"));
  1400. FileUtils.writeFile(target + File.separator + "extras" + File.separator + "floor_ajk.json", new String(ajkJson.getBytes(), "UTF-8"));
  1401. FileUtils.writeFile(ConstantFilePath.SCENE_PATH+"data"+File.separator+"data"+num + File.separator + "floor_ajk.json", new String(ajkJson.getBytes(), "UTF-8"));
  1402. FileUtils.writeFile(target + File.separator + "extras" + File.separator + "vision.txt", new String(cameraJson.getBytes(), "UTF-8"));
  1403. FileUtils.writeFile(ConstantFilePath.SCENE_PATH+"data"+File.separator+"data"+num + File.separator + "camera.json", new String(cameraJson.getBytes(), "UTF-8"));
  1404. for(int i = 0; i < files.length; i ++){
  1405. File cadImg = new File(target + File.separator + "extras" + File.separator + "Floorplans/" + files[i].getOriginalFilename());
  1406. if(!cadImg.getParentFile().exists()){
  1407. cadImg.getParentFile().mkdirs();
  1408. }
  1409. if(cadImg.exists())
  1410. {
  1411. cadImg.delete();
  1412. }
  1413. files[i].transferTo(cadImg);
  1414. }
  1415. //下载封面图
  1416. FileUtils.downLoadFromUrl(scenePlusExt.getThumb() + "?t=" + System.currentTimeMillis(),
  1417. "Cover.png", target + File.separator + "extras" + File.separator + "CoverImage");
  1418. //转换成jpg
  1419. FileUtils.pngToJpg(target + File.separator + "extras" + File.separator + "CoverImage/Cover.png",
  1420. target + File.separator + "extras" + File.separator + "CoverImage/Cover.jpg");
  1421. //安居客算法运行
  1422. log.info("安居客算法:开始建模——"+num);
  1423. CreateObjUtil.build3dModel(target , "1");
  1424. String uploadJsonPath = target + File.separator + "results" + File.separator + "upload.json";
  1425. log.info("uploadJsonPath=" + uploadJsonPath);
  1426. //因为共享目录有延迟,这里循环检测算法是否计算完毕3次,每次隔五秒
  1427. boolean exist = ComputerUtil.checkComputeCompleted(uploadJsonPath, maxCheckTimes, waitTime);
  1428. if(!exist){
  1429. throw new BusinessException(ErrorCode.FAILURE_CODE_7017);
  1430. }
  1431. String zipPath = target + File.separator + "results/" + num + ".zip";
  1432. new File(zipPath).delete();
  1433. ZipUtil.zip(target + File.separator + "results/ajk/", zipPath);
  1434. // String command = "bash /opt/ossutil/gzip.sh " + zipPath.replace(".zip", "") + " " + target + File.separator + "results/ajk/";
  1435. // log.info("压缩文件:" + command);
  1436. // CreateObjUtil.callshell(command);
  1437. if(!new File(zipPath).exists()){
  1438. return ResultData.error(ErrorCode.FAILURE_CODE_5043);
  1439. }
  1440. String fileMD5 = FileMd5Util.getFileMD5(new File(zipPath));
  1441. uploadToOssUtil.upload(zipPath, "data_download/" + num + ".zip");
  1442. SceneDataDownload sceneDataDownload = sceneDataDownloadService.findBySceneNum(num);
  1443. if(sceneDataDownload == null){
  1444. sceneDataDownload = new SceneDataDownload();
  1445. sceneDataDownload.setNum(num);
  1446. sceneDataDownload.setDownloadPath(ossUrlPrefix + "data_download/" + num + ".zip");
  1447. sceneDataDownload.setFileMd5(fileMD5);
  1448. sceneDataDownloadService.save(sceneDataDownload);
  1449. return ResultData.ok();
  1450. }
  1451. sceneDataDownload.setFileMd5(fileMD5);
  1452. sceneDataDownload.setUpdateTime(Calendar.getInstance().getTime());
  1453. sceneDataDownloadService.updateById(sceneDataDownload);
  1454. }catch (Exception e){
  1455. log.error("画墙重建模型失败...", e);
  1456. throw new BusinessException(ErrorCode.FAILURE_CODE_5039);
  1457. }finally {
  1458. redisLockUtil.unlockLua(lockKey);
  1459. }
  1460. return ResultData.ok();
  1461. }
  1462. @Override
  1463. public ResultData checkKey(SceneCheckKeyParamVO param) throws Exception {
  1464. SceneJsonBean sceneJsonBean = null;
  1465. String sceneJson = redisUtil.get(String.format(RedisKey.SCENE_JSON, param.getNum()));
  1466. if(StrUtil.isEmpty(sceneJson)){
  1467. sceneJson = uploadToOssUtil.getObjectContent(bucket,
  1468. String.format(UploadFilePath.DATA_VIEW_PATH, param.getNum()) + "scene.json");
  1469. }
  1470. sceneJsonBean = JSON.parseObject(sceneJson, SceneJsonBean.class);
  1471. if(!param.getPassword().equals(sceneJsonBean.getScenePassword())){
  1472. throw new BusinessException(ErrorCode.FAILURE_CODE_5021);
  1473. }
  1474. return ResultData.ok();
  1475. }
  1476. @Override
  1477. public ResultData addMosaics(BaseDataParamVO param) throws Exception {
  1478. ScenePlus scenePlus = scenePlusService.getScenePlusByNum(param.getNum());
  1479. if(Objects.isNull(scenePlus)){
  1480. throw new BusinessException(ErrorCode.FAILURE_CODE_5005);
  1481. }
  1482. SceneEditInfo sceneEditInfo = this.getByScenePlusId(scenePlus.getId());
  1483. //如果redis数据丢失,从本地文件中同步马赛克数据到redis
  1484. this.syncMosaicFromFileToRedis(param.getNum());
  1485. Map<String, String> map = new HashMap<>();
  1486. JSONArray jsonArray = JSON.parseArray(param.getData());
  1487. for (Object o : jsonArray) {
  1488. JSONObject mosaic = (JSONObject) o;
  1489. String panoId = mosaic.getString("panoId");
  1490. if(StrUtil.isEmpty(panoId)){
  1491. throw new BusinessException(ErrorCode.FAILURE_CODE_5012);
  1492. }
  1493. map.put(panoId, JSON.toJSONString(mosaic));
  1494. }
  1495. String key = String.format(RedisKey.SCENE_MOSAIC_DATA, param.getNum());
  1496. redisUtil.hmset(key, map);
  1497. //写入本地文件,作为备份
  1498. this.writeMosaic(param.getNum());
  1499. //更新数据库
  1500. this.updateMosaicFlag(param.getNum());
  1501. //更新版本号
  1502. this.upgradeVersionById(sceneEditInfo.getId());
  1503. return ResultData.ok();
  1504. }
  1505. private void updateMosaicFlag(String num){
  1506. ScenePlus scenePlus = scenePlusService.getScenePlusByNum(num);
  1507. SceneEditInfoExt sceneEditInfoExt = sceneEditInfoExtService.getByScenePlusId(scenePlus.getId());
  1508. String key = String.format(RedisKey.SCENE_MOSAIC_DATA, num);
  1509. boolean flag = redisUtil.hasKey(key);
  1510. if(flag){
  1511. sceneEditInfoExt.setMosaic(Integer.valueOf(CommonStatus.YES.code()));
  1512. }else{
  1513. sceneEditInfoExt.setMosaic(Integer.valueOf(CommonStatus.NO.code()));
  1514. }
  1515. sceneEditInfoExtService.updateById(sceneEditInfoExt);
  1516. }
  1517. /**
  1518. * <p>
  1519. 保证马赛克数据安全性,当redis宕机导致热点数据丢失时,可以从文件中读取,恢复到redis
  1520. **/
  1521. private void syncMosaicFromFileToRedis(String num) throws Exception{
  1522. String key = String.format(RedisKey.SCENE_MOSAIC_DATA, num);
  1523. boolean exist = redisUtil.hasKey(key);
  1524. if(exist){
  1525. return;
  1526. }
  1527. String lockKey = String.format(RedisLockKey.LOCK_MOSAIC_DATA_SYNC, num);
  1528. boolean lock = redisLockUtil.lock(lockKey, RedisKey.EXPIRE_TIME_1_MINUTE);
  1529. if(!lock){
  1530. throw new BusinessException(ErrorCode.SYSTEM_BUSY);
  1531. }
  1532. try{
  1533. exist = redisUtil.hasKey(key);
  1534. if(exist){
  1535. return;
  1536. }
  1537. String filePath = String.format(ConstantFilePath.SCENE_USER_PATH_V4, num);
  1538. String mosaicData = FileUtils.readFile(filePath + "mosaic.json");
  1539. if(StrUtil.isEmpty(mosaicData)){
  1540. return;
  1541. }
  1542. JSONArray jsonArray = JSON.parseArray(mosaicData);
  1543. if(CollUtil.isEmpty(jsonArray)){
  1544. return;
  1545. }
  1546. Map<String, String> map = new HashMap<>();
  1547. for (Object o : jsonArray) {
  1548. JSONObject jo = (JSONObject)o;
  1549. map.put(jo.getString("panoId"), jo.toJSONString());
  1550. }
  1551. redisUtil.hmset(key, map);
  1552. }finally {
  1553. redisLockUtil.unlockLua(lockKey);
  1554. }
  1555. }
  1556. /**
  1557. * <p>
  1558. 保证马赛克数据安全性,当redis宕机导致热点数据丢失时,可以从文件中读取,恢复到redis
  1559. **/
  1560. private void writeMosaic(String num) throws Exception{
  1561. String mosaicPath = String.format(ConstantFilePath.SCENE_USER_PATH_V4, num) + "mosaic.json";
  1562. String key = String.format(RedisKey.SCENE_MOSAIC_DATA, num);
  1563. Map<String, String> mosaicMap = redisUtil.hmget(key);
  1564. if(CollUtil.isEmpty(mosaicMap)){
  1565. FileUtils.deleteFile(mosaicPath);
  1566. return;
  1567. }
  1568. List<String> mosaicList = Lists.newArrayList(mosaicMap.values());
  1569. JSONArray jsonArr = new JSONArray();
  1570. mosaicList.stream().forEach(mosaic->{
  1571. jsonArr.add(JSONObject.parseObject(mosaic));
  1572. });
  1573. String lockKey = String.format(RedisLockKey.LOCK_MOSAIC_JSON, num);
  1574. boolean lock = redisLockUtil.lock(lockKey, RedisKey.EXPIRE_TIME_1_MINUTE);
  1575. if(!lock){
  1576. return;
  1577. }
  1578. try{
  1579. FileUtils.writeFile(mosaicPath, jsonArr.toJSONString());
  1580. }finally {
  1581. redisLockUtil.unlockLua(lockKey);
  1582. }
  1583. }
  1584. @Override
  1585. public ResultData uploadLinkPan(String num, String sid, String fileName, MultipartFile file) throws Exception {
  1586. ScenePlus scenePlus = scenePlusService.getScenePlusByNum(num);
  1587. if(scenePlus == null){
  1588. throw new BusinessException(ErrorCode.FAILURE_CODE_5005);
  1589. }
  1590. ScenePlusExt scenePlusExt = scenePlusExtService.getScenePlusExtByPlusId(scenePlus.getId());
  1591. String localDataPath = String.format(ConstantFilePath.DATABUFFER_FORMAT, num);
  1592. String localImagesPath = String.format(ConstantFilePath.IMAGESBUFFER_FORMAT, num);
  1593. String path = scenePlusExt.getDataSource();
  1594. String target = localImagesPath + "panorama/" + sid;
  1595. FileUtils.deleteDirectory(target);
  1596. //文件写如本地磁盘
  1597. String filePath = target + File.separator + "extras/images" + File.separator + fileName;
  1598. File targetFile = new File(filePath);
  1599. if(!targetFile.getParentFile().exists()){
  1600. targetFile.getParentFile().mkdirs();
  1601. }
  1602. file.transferTo(targetFile);
  1603. //调用算法切全景图
  1604. FileUtils.copyFile(path + File.separator + "data.json", target + File.separator+"data.json", true);
  1605. FileUtils.copyFile(path + File.separator + "project.json", target + File.separator+"project.json", true);
  1606. JSONObject visionJson = new JSONObject();
  1607. JSONArray visionArray = new JSONArray();
  1608. visionJson.put("uuid", sid);
  1609. visionJson.put("group", 1);
  1610. visionJson.put("subgroup", 0);
  1611. visionArray.add(visionJson);
  1612. JSONObject vision = new JSONObject();
  1613. vision.put("sweepLocations", visionArray);
  1614. cn.hutool.core.io.FileUtil.writeString(vision.toString(),
  1615. target + "/extras" + File.separator + "vision.txt",
  1616. StandardCharsets.UTF_8);
  1617. //data.json增加extras为执行重建算法
  1618. String type = "4k";
  1619. String data = FileUtils.readFile(target + File.separator + "data.json");
  1620. if(data != null){
  1621. JSONObject floorplanJson = new JSONObject();
  1622. floorplanJson.put("has_source_images", true);
  1623. floorplanJson.put("has_vision_txt", true);
  1624. JSONObject dataJson = JSONObject.parseObject(data);
  1625. dataJson.put("extras", floorplanJson);
  1626. dataJson.put("split_type", "SPLIT_V8");//替换全景图算法
  1627. String skyboxType = "SKYBOX_V6";//默认4k minion
  1628. if(SceneFrom.PRO.code().equals(scenePlusExt.getSceneFrom())){
  1629. skyboxType = "SKYBOX_V7";
  1630. type = "2k";
  1631. }
  1632. if(scenePlusExt.getSceneScheme() == 3){
  1633. if("4k".equals(scenePlusExt.getSceneResolution())){
  1634. skyboxType = "SKYBOX_V14";
  1635. }else{
  1636. skyboxType = "SKYBOX_V13";
  1637. }
  1638. }
  1639. dataJson.put("skybox_type", skyboxType);
  1640. cn.hutool.core.io.FileUtil.writeString(dataJson.toString(),
  1641. target + File.separator+"data.json", StandardCharsets.UTF_8);
  1642. }
  1643. //创建文件夹软连接并且复制data.json和project.json
  1644. String capturePath = target + File.separator + "capture";
  1645. String resultPath = target + File.separator + "results";
  1646. log.info("场景关联上传全景图:capturePath={}", capturePath);
  1647. log.info("场景关联上传全景图:resultPath={}", resultPath);
  1648. if(cn.hutool.core.io.FileUtil.exist(capturePath)){
  1649. cn.hutool.core.io.FileUtil.del(capturePath);
  1650. }
  1651. if(cn.hutool.core.io.FileUtil.exist(resultPath)){
  1652. cn.hutool.core.io.FileUtil.del(resultPath);
  1653. }
  1654. //下载data.fdage
  1655. if(StorageType.AWS.code().equals(this.type)){
  1656. //亚马逊保持旧方式,超链接capture
  1657. CreateObjUtil.createSoftConnection(path + File.separator + "capture", capturePath);
  1658. }
  1659. CreateObjUtil.ossUtilCp(ConstantFilePath.OSS_PREFIX + path.replace(ConstantFilePath.BUILD_MODEL_PATH, "") + "/data.fdage", capturePath);
  1660. CreateObjUtil.build3dModel(target , "1");
  1661. //读取upload文件,获取需要上传的文件
  1662. JSONArray array = ComputerUtil.getUploadArray(resultPath + "/upload.json", this.maxCheckTimes, this.waitTime);
  1663. Map<String, String> map = new HashMap<>();
  1664. JSONObject fileJson;
  1665. String uploadFile, uploadFilePath;
  1666. String imgEditPath = String.format(UploadFilePath.IMG_EDIT_PATH, num);
  1667. for(int i = 0, len = array.size(); i < len; i++){
  1668. fileJson = array.getJSONObject(i);
  1669. uploadFile = fileJson.getString("file");
  1670. uploadFilePath = resultPath +File.separator + uploadFile;
  1671. //文件不存在抛出异常
  1672. if(!cn.hutool.core.io.FileUtil.exist(uploadFilePath)){
  1673. throw new Exception(uploadFilePath + "文件不存在");
  1674. }
  1675. Integer clazz = fileJson.getIntValue("clazz");
  1676. if(Objects.isNull(clazz)){
  1677. continue;
  1678. }
  1679. if(clazz == 4 || clazz == 5 || clazz == 7){
  1680. map.put(uploadFilePath, imgEditPath + "panorama/" + sid + File.separator + uploadFile);
  1681. }
  1682. }
  1683. //上传全景图
  1684. map.put(filePath, imgEditPath + "panorama/" + sid + "/high/" + fileName);
  1685. uploadToOssUtil.uploadMulFiles(map);
  1686. Map<String, String> result = new HashMap<>();
  1687. result.put("type", type);
  1688. return ResultData.ok(result);
  1689. }
  1690. @Override
  1691. public ResultData saveLinkPan(SaveLinkPanParamVO param) throws Exception {
  1692. String num = param.getNum();
  1693. ScenePlus scenePlus = scenePlusService.getScenePlusByNum(param.getNum());
  1694. if(Objects.isNull(scenePlus)){
  1695. throw new BusinessException(ErrorCode.FAILURE_CODE_5005);
  1696. }
  1697. //添加场景关联数据
  1698. this.addOrUpdateLinPan(num, param.getLinkPans());
  1699. //添加场景关联图标
  1700. this.addOrUpdateLinkPanStyles(num, param.getStyles());
  1701. //场景关联数据备份到本地
  1702. this.writeLinkScene(num);
  1703. //更新场景关联标识、升级版本号
  1704. this.setLinkScenesAndUpgradeVersion(scenePlus.getId(), num);
  1705. return ResultData.ok();
  1706. }
  1707. @Override
  1708. public ResultData deleteStyles(DeleteLinkSceneStylesParamVO param) throws Exception {
  1709. ScenePlus scenePlus = scenePlusService.getScenePlusByNum(param.getNum());
  1710. if (scenePlus == null)
  1711. throw new BusinessException(ErrorCode.FAILURE_CODE_5005);
  1712. List<String> sidList = param.getSidList();
  1713. this.syncLinkPanStylesFromFileToRedis(param.getNum());
  1714. String key = String.format(RedisKey.SCENE_LINKPAN_STYLES, param.getNum());
  1715. List<String> deleteList = redisUtil.hMultiGet(key, sidList);
  1716. redisUtil.hdel(key, sidList.toArray());
  1717. //写入本地文件,作为备份
  1718. this.writeLinkScene(param.getNum());
  1719. //删除oss文件
  1720. List<String> deleteFileList = deleteList.stream().map(str -> {
  1721. JSONObject parse = JSON.parseObject(str);
  1722. return parse.getString("url");
  1723. }).collect(Collectors.toList());
  1724. sceneUploadService.delete(
  1725. DeleteFileParamVO.builder()
  1726. .num(param.getNum())
  1727. .fileNames(deleteFileList)
  1728. .bizType(FileBizType.LINK_STYLE.code()).build());
  1729. return ResultData.ok();
  1730. }
  1731. @Override
  1732. public ResultData deleteLinkPan(DeleteLinkPanParamVO param) throws Exception {
  1733. ScenePlus scenePlus = scenePlusService.getScenePlusByNum(param.getNum());
  1734. if (scenePlus == null)
  1735. return ResultData.error(ErrorCode.FAILURE_CODE_5005);
  1736. List<String> deleteSidList = param.getSidList();
  1737. //处理删除状态数据
  1738. this.deletelinkPanData(param.getNum(), deleteSidList);
  1739. //写入本地文件,作为备份
  1740. this.writeLinkScene(param.getNum());
  1741. //更新场景关联标识、升级版本号
  1742. this.setLinkScenesAndUpgradeVersion(scenePlus.getId(), param.getNum());
  1743. return ResultData.ok();
  1744. }
  1745. private void deletelinkPanData(String num, List<String> deleteSidList) throws Exception {
  1746. if(CollUtil.isEmpty(deleteSidList)){
  1747. return;
  1748. }
  1749. this.syncLinPanFromFileToRedis(num);
  1750. //从redis中加载热点数据
  1751. String key = String.format(RedisKey.SCENE_LINKPAN_DATA, num);
  1752. List<String> deletDataList = redisUtil.hMultiGet(key, deleteSidList);
  1753. if(CollUtil.isEmpty(deletDataList))
  1754. return;
  1755. //从redis中移除热点数据
  1756. redisUtil.hdel(key, deleteSidList.toArray());
  1757. //删除oss文件
  1758. String imgEditPath = String.format(UploadFilePath.IMG_EDIT_PATH, num);
  1759. deleteSidList.stream().forEach(sid->{
  1760. uploadToOssUtil.deleteFile(imgEditPath + "panorama_edit/" + sid);
  1761. });
  1762. }
  1763. @Override
  1764. public ResultData listLinkPan(String num) throws Exception {
  1765. this.syncLinPanFromFileToRedis(num);
  1766. this.syncLinkPanStylesFromFileToRedis(num);
  1767. JSONObject result = new JSONObject();
  1768. //查询场景关联数据
  1769. String key = String.format(RedisKey.SCENE_LINKPAN_DATA, num);
  1770. Map<String, String> allTagsMap = redisUtil.hmget(key);
  1771. List<JSONObject> tags = Lists.newArrayList();
  1772. List<TagBean> tagBeanList = new ArrayList<>();
  1773. if(CollUtil.isNotEmpty(allTagsMap)){
  1774. allTagsMap.entrySet().stream().forEach(entry -> {
  1775. JSONObject jsonObject = JSON.parseObject(entry.getValue());
  1776. tagBeanList.add(
  1777. TagBean.builder()
  1778. .createTime(jsonObject.getLong("createTime"))
  1779. .tag(jsonObject).build());
  1780. });
  1781. //按创建时间倒叙排序
  1782. tagBeanList.sort(Comparator.comparingLong(TagBean::getCreateTime).reversed());
  1783. //移除createTime字段
  1784. tags = tagBeanList.stream().map(tagBean -> {
  1785. JSONObject tag = tagBean.getTag();
  1786. tag.remove("createTime");
  1787. return tag;
  1788. }).collect(Collectors.toList());
  1789. }
  1790. result.put("tags", tags);
  1791. //封装styles数据
  1792. List<JSONObject> styles = Lists.newArrayList();
  1793. key = String.format(RedisKey.SCENE_LINKPAN_STYLES, num);
  1794. Map<String, String> styleMap = redisUtil.hmget(key);
  1795. if(CollUtil.isNotEmpty(styleMap)) {
  1796. for (String style : styleMap.values()) {
  1797. styles.add(JSON.parseObject(style));
  1798. }
  1799. }
  1800. //图标按写入时间排序
  1801. styles = this.sortStyles(styles);
  1802. result.put("styles", styles);
  1803. return ResultData.ok(result);
  1804. }
  1805. private List<JSONObject> sortStyles(List<JSONObject> styles){
  1806. if(CollUtil.isEmpty(styles)){
  1807. return null;
  1808. }
  1809. //统计使用频次
  1810. List<StyleBean> styleBeans = Lists.newArrayList();
  1811. for (JSONObject style : styles) {
  1812. Long createTime = style.getLong("createTime");
  1813. createTime = Objects.isNull(createTime) ? Calendar.getInstance().getTimeInMillis() : createTime;
  1814. style.remove("createTime");
  1815. styleBeans.add(
  1816. StyleBean.builder().style(style)
  1817. .createTime(createTime).build());
  1818. }
  1819. //排序
  1820. List<JSONObject> styleList = Lists.newArrayList();
  1821. if(CollUtil.isNotEmpty(styleBeans)){
  1822. styleList = styleBeans.stream().sorted(Comparator.comparing(StyleBean::getCreateTime).reversed())
  1823. .map(item -> {
  1824. return item.getStyle();
  1825. }).collect(Collectors.toList());
  1826. }
  1827. return styleList;
  1828. }
  1829. private void setLinkScenesAndUpgradeVersion(Long scenePlusId, String num){
  1830. SceneEditInfo sceneEditInfo = this.getByScenePlusId(scenePlusId);
  1831. //查询缓存是否有场景关联数据
  1832. String key = String.format(RedisKey.SCENE_LINKPAN_DATA, num);
  1833. Map<String, String> allTagsMap = redisUtil.hmget(key);
  1834. boolean hashTags = false;
  1835. for (Entry<String, String> tagMap : allTagsMap.entrySet()) {
  1836. if(StrUtil.isEmpty(tagMap.getValue())){
  1837. continue;
  1838. }
  1839. hashTags = true;
  1840. break;
  1841. }
  1842. //更新linkscenes字段
  1843. sceneEditInfoExtService.update(
  1844. new LambdaUpdateWrapper<SceneEditInfoExt>()
  1845. .set(SceneEditInfoExt::getLinks, hashTags ? CommonStatus.YES.code() : CommonStatus.NO.code())
  1846. .eq(SceneEditInfoExt::getEditInfoId, sceneEditInfo.getId()));
  1847. //更新场景版本
  1848. this.upgradeVersionById(sceneEditInfo.getId());
  1849. }
  1850. /**
  1851. * <p>
  1852. 热点数据保存
  1853. * </p>
  1854. * @author dengsixing
  1855. * @date 2022/3/3
  1856. **/
  1857. private void writeLinkScene(String num) throws Exception{
  1858. String dataKey = String.format(RedisKey.SCENE_LINKPAN_DATA, num);
  1859. Map<String, String> tagMap = redisUtil.hmget(dataKey);
  1860. List<String> tagList = Lists.newArrayList();
  1861. tagMap.entrySet().stream().forEach(entry->{
  1862. if(StrUtil.isNotEmpty(entry.getValue())){
  1863. tagList.add(entry.getValue());
  1864. }
  1865. });
  1866. JSONObject jsonObject = new JSONObject();
  1867. JSONArray tagJsonArr = new JSONArray();
  1868. if(CollUtil.isNotEmpty(tagList)){
  1869. tagList.stream().forEach(linkPan->{
  1870. tagJsonArr.add(JSONObject.parseObject(linkPan));
  1871. });
  1872. }
  1873. jsonObject.put("tags", tagJsonArr);
  1874. String stylesKey = String.format(RedisKey.SCENE_LINKPAN_STYLES, num);
  1875. Map<String, String> styleMap = redisUtil.hmget(stylesKey);
  1876. List<JSONObject> styleList = Lists.newArrayList();
  1877. if(CollUtil.isNotEmpty(styleMap)){
  1878. styleMap.values().stream().forEach(style->{
  1879. styleList.add(JSONObject.parseObject(style));
  1880. });
  1881. }
  1882. jsonObject.put("styles", styleList);
  1883. String linkScenePath = String.format(ConstantFilePath.SCENE_USER_PATH_V4, num) + "links.json";
  1884. String lockKey = String.format(RedisLockKey.LOCK_LINK_SCENE_JSON, num);
  1885. boolean lock = redisLockUtil.lock(lockKey, RedisKey.EXPIRE_TIME_1_MINUTE);
  1886. if(!lock){
  1887. return;
  1888. }
  1889. try{
  1890. cn.hutool.core.io.FileUtil.writeUtf8String(jsonObject.toJSONString(), linkScenePath);
  1891. }finally {
  1892. redisLockUtil.unlockLua(lockKey);
  1893. }
  1894. }
  1895. private void addOrUpdateLinkPanStyles(String num, List<JSONObject> styles) throws Exception{
  1896. this.syncLinkPanStylesFromFileToRedis(num);
  1897. if(CollUtil.isEmpty(styles)){
  1898. return;
  1899. }
  1900. long time = Calendar.getInstance().getTimeInMillis();
  1901. Map<String, String> styleMap = new HashMap<>();
  1902. AtomicInteger index = new AtomicInteger();
  1903. styles.stream().forEach(style->{
  1904. String id = style.getString("sid");
  1905. style.put("createTime", time + index.getAndIncrement());
  1906. styleMap.put(id, style.toJSONString());
  1907. });
  1908. String key = String.format(RedisKey.SCENE_LINKPAN_STYLES, num);
  1909. redisUtil.hmset(key, styleMap);
  1910. }
  1911. /**
  1912. * <p>
  1913. 保证icons数据安全性,当redis宕机导致icons数据丢失时,可以从文件中读取,恢复到redis
  1914. * </p>
  1915. * @author dengsixing
  1916. * @date 2022/3/3
  1917. **/
  1918. private void syncLinkPanStylesFromFileToRedis(String num) throws Exception{
  1919. String key = String.format(RedisKey.SCENE_LINKPAN_STYLES, num);
  1920. boolean exist = redisUtil.hasKey(key);
  1921. if(exist){
  1922. return;
  1923. }
  1924. String lockKey = String.format(RedisLockKey.LOCK_LINKPAN_STYLES_SYNC, num);
  1925. boolean lock = redisLockUtil.lock(lockKey, RedisKey.EXPIRE_TIME_1_MINUTE);
  1926. if(!lock){
  1927. throw new BusinessException(ErrorCode.SYSTEM_BUSY);
  1928. }
  1929. try{
  1930. exist = redisUtil.hasKey(key);
  1931. if(exist){
  1932. return;
  1933. }
  1934. String linkSceneFilePath = String.format(ConstantFilePath.SCENE_USER_PATH_V4, num);
  1935. String linkSceneData = FileUtils.readFile(linkSceneFilePath + "links.json");
  1936. if(StrUtil.isEmpty(linkSceneData)){
  1937. return;
  1938. }
  1939. JSONObject jsonObject = JSON.parseObject(linkSceneData);
  1940. JSONArray stylesArr = jsonObject.getJSONArray("styles");
  1941. if(CollUtil.isEmpty(stylesArr)){
  1942. return;
  1943. }
  1944. Map<String, String> styleMap = new HashMap<>();
  1945. for (Object style : stylesArr) {
  1946. JSONObject styleObj = (JSONObject)style;
  1947. String id = styleObj.getString("sid");
  1948. styleMap.put(id, styleObj.toJSONString());
  1949. }
  1950. redisUtil.hmset(key, styleMap);
  1951. }finally {
  1952. redisLockUtil.unlockLua(lockKey);
  1953. }
  1954. }
  1955. private void addOrUpdateLinPan(String num, List<JSONObject> linkPanList) throws Exception{
  1956. Map<String, String> addOrUpdateMap = new HashMap<>();
  1957. int i = 0;
  1958. for (JSONObject jsonObject : linkPanList) {
  1959. jsonObject.put("createTime", Calendar.getInstance().getTimeInMillis() + i++);
  1960. addOrUpdateMap.put(jsonObject.getString("sid"), jsonObject.toJSONString());
  1961. }
  1962. this.syncLinPanFromFileToRedis(num);
  1963. //处理新增和修改数据
  1964. this.addOrUpdateLinkPanHandler(num, addOrUpdateMap);
  1965. }
  1966. /**
  1967. * <p>
  1968. 保证热点数据安全性,当redis宕机导致热点数据丢失时,可以从文件中读取,恢复到redis
  1969. * </p>
  1970. * @author dengsixing
  1971. * @date 2022/3/3
  1972. **/
  1973. private void syncLinPanFromFileToRedis(String num) throws Exception{
  1974. String key = String.format(RedisKey.SCENE_LINKPAN_DATA, num);
  1975. boolean exist = redisUtil.hasKey(key);
  1976. if(exist){
  1977. return;
  1978. }
  1979. String lockKey = String.format(RedisLockKey.LOCK_LINKPAN_DATA_SYNC, num);
  1980. boolean lock = redisLockUtil.lock(lockKey, RedisKey.EXPIRE_TIME_1_MINUTE);
  1981. if(!lock){
  1982. throw new BusinessException(ErrorCode.SYSTEM_BUSY);
  1983. }
  1984. try{
  1985. exist = redisUtil.hasKey(key);
  1986. if(exist){
  1987. return;
  1988. }
  1989. String linkSceneFilePath = String.format(ConstantFilePath.SCENE_USER_PATH_V4, num);
  1990. String linkSceneData = FileUtils.readFile(linkSceneFilePath + "links.json");
  1991. if(StrUtil.isEmpty(linkSceneData)){
  1992. return;
  1993. }
  1994. JSONObject jsonObject = JSON.parseObject(linkSceneData);
  1995. JSONArray tagsArr = jsonObject.getJSONArray("tags");
  1996. if(CollUtil.isEmpty(tagsArr)){
  1997. return;
  1998. }
  1999. Map<String, String> map = new HashMap<>();
  2000. for (Object o : tagsArr) {
  2001. JSONObject jo = (JSONObject)o;
  2002. map.put(jo.getString("sid"), jo.toJSONString());
  2003. }
  2004. redisUtil.hmset(key, map);
  2005. }finally {
  2006. redisLockUtil.unlockLua(lockKey);
  2007. }
  2008. }
  2009. private void addOrUpdateLinkPanHandler(String num, Map<String, String> addOrUpdateMap){
  2010. if(CollUtil.isEmpty(addOrUpdateMap))
  2011. return;
  2012. //数据验证,新增、修改状态,linkPan不能为空
  2013. for (String sid : addOrUpdateMap.keySet()) {
  2014. String linkPan = addOrUpdateMap.get(sid);
  2015. if(StrUtil.isEmpty(linkPan)){
  2016. throw new BusinessException(ErrorCode.FAILURE_CODE_7022);
  2017. }
  2018. }
  2019. //批量写入缓存
  2020. String key = String.format(RedisKey.SCENE_LINKPAN_DATA, num);
  2021. redisUtil.hmset(key, addOrUpdateMap);
  2022. }
  2023. private void updateBoxVideos(SceneEditInfo sceneEditInfo, Long scenePlusId, String boxVideos){
  2024. if(Objects.isNull(sceneEditInfo)){
  2025. sceneEditInfo = new SceneEditInfo();
  2026. sceneEditInfo.setScenePlusId(scenePlusId);
  2027. sceneEditInfo.setBoxVideos(boxVideos);
  2028. this.save(sceneEditInfo);
  2029. }else{
  2030. this.update(new UpdateWrapper<SceneEditInfo>()
  2031. .setSql("version = version + 1")
  2032. .set("box_videos", boxVideos)
  2033. .eq("id", sceneEditInfo.getId()));
  2034. }
  2035. }
  2036. private void updateBoxPhotos(SceneEditInfo sceneEditInfo, String boxPhotos){
  2037. this.update(new LambdaUpdateWrapper<SceneEditInfo>()
  2038. .set(SceneEditInfo::getBoxPhotos, boxPhotos)
  2039. .setSql("version = version + 1")
  2040. .eq(SceneEditInfo::getId, sceneEditInfo.getId()));
  2041. }
  2042. private String createBoxVideos(
  2043. String num, String sid, JSONObject boxVideo,
  2044. SceneEditInfo sceneEditInfo, int type) throws Exception{
  2045. String boxVideos = null;
  2046. if(sceneEditInfo != null){
  2047. boxVideos = sceneEditInfo.getBoxVideos();
  2048. }
  2049. JSONArray boxVideosJson = null;
  2050. if (StrUtil.isNotEmpty(boxVideos)) {
  2051. boxVideosJson = JSONArray.parseArray(boxVideos);
  2052. }else {
  2053. boxVideosJson = new JSONArray();
  2054. }
  2055. String result = null;
  2056. //删除
  2057. if(type == OperationType.DELETE.code()){
  2058. Set<String> deleteFile = new HashSet<>();
  2059. if(boxVideosJson.size() == 0)
  2060. return null;
  2061. for(int i=0;i<boxVideosJson.size();++i){
  2062. JSONObject ele = boxVideosJson.getJSONObject(i);
  2063. if(ele.getString("sid").equals(sid)){
  2064. String poster = ele.getString("poster");
  2065. if(StrUtil.isNotEmpty(poster))
  2066. deleteFile.add(poster);
  2067. String url = ele.getString("url");
  2068. if(StrUtil.isNotEmpty(url)){
  2069. deleteFile.add(url);
  2070. deleteFile.add(url.replace(".mp4",".flv"));
  2071. }
  2072. boxVideosJson.remove(i);
  2073. }
  2074. }
  2075. //删除资源文件
  2076. if(CollUtil.isNotEmpty(deleteFile))
  2077. sceneUploadService.delete(
  2078. DeleteFileParamVO.builder().num(num)
  2079. .bizType(FileBizType.BOX_VIDEO.code())
  2080. .fileNames(new ArrayList<>(deleteFile)).build());
  2081. }else{
  2082. // TODO: 2022/3/1 目前需求只保留一个,所以这里先注释掉,sid不同时,不追加
  2083. //更新
  2084. // boolean exist = false;
  2085. // for(int i=0;i<boxVideosJson.size();++i){
  2086. // JSONObject ele = boxVideosJson.getJSONObject(i);
  2087. // if(ele.getString("sid").equals(sid)){
  2088. // boxVideosJson.set(i, boxVideo);
  2089. // exist = true;
  2090. // }
  2091. // }
  2092. // //新增
  2093. // if(!exist){
  2094. // boxVideosJson.add(boxVideo);
  2095. // }
  2096. boxVideosJson.clear();
  2097. boxVideosJson.add(boxVideo);
  2098. }
  2099. if(boxVideosJson.size() != 0){
  2100. result = boxVideosJson.toJSONString();
  2101. }
  2102. return result;
  2103. }
  2104. private String createBoxPhotos(String num, String sid, JSONObject boxPhoto, SceneEditInfo sceneEditInfo, int type) throws Exception{
  2105. String boxPhotos = null;
  2106. if(sceneEditInfo != null){
  2107. boxPhotos = sceneEditInfo.getBoxPhotos();
  2108. }
  2109. JSONArray boxPhotosJson = null;
  2110. if (StrUtil.isNotEmpty(boxPhotos)) {
  2111. boxPhotosJson = JSONArray.parseArray(boxPhotos);
  2112. }else {
  2113. boxPhotosJson = new JSONArray();
  2114. }
  2115. String result = null;
  2116. //删除
  2117. if(type == OperationType.DELETE.code()){
  2118. Set<String> deleteFile = new HashSet<>();
  2119. if(boxPhotosJson.size() == 0)
  2120. return null;
  2121. for(int i=0;i<boxPhotosJson.size();++i){
  2122. JSONObject ele = boxPhotosJson.getJSONObject(i);
  2123. if(ele.getString("sid").equals(sid)){
  2124. String poster = ele.getString("poster");
  2125. if(StrUtil.isNotEmpty(poster))
  2126. deleteFile.add(poster);
  2127. String url = ele.getString("url");
  2128. if(StrUtil.isNotEmpty(url))
  2129. deleteFile.add(url);
  2130. boxPhotosJson.remove(i);
  2131. }
  2132. }
  2133. //删除资源文件
  2134. if(CollUtil.isNotEmpty(deleteFile))
  2135. sceneUploadService.delete(
  2136. DeleteFileParamVO.builder().num(num)
  2137. .bizType(FileBizType.BOX_VIDEO.code())
  2138. .fileNames(new ArrayList<>(deleteFile)).build());
  2139. }else{
  2140. //更新
  2141. boolean exist = false;
  2142. for(int i=0;i<boxPhotosJson.size();++i){
  2143. JSONObject ele = boxPhotosJson.getJSONObject(i);
  2144. if(ele.getString("sid").equals(sid)){
  2145. boxPhoto.put("createTime", ele.getLong("createTime"));
  2146. boxPhotosJson.set(i, boxPhoto);
  2147. exist = true;
  2148. }
  2149. }
  2150. //新增
  2151. if(!exist){
  2152. boxPhoto.put("createTime", Calendar.getInstance().getTimeInMillis());
  2153. boxPhotosJson.add(boxPhoto);
  2154. }
  2155. }
  2156. if(boxPhotosJson.size() != 0){
  2157. List<BoxPhotoBean> list = Lists.newArrayList();
  2158. for (Object o : boxPhotosJson) {
  2159. JSONObject jsonObject = (JSONObject)o;
  2160. list.add(BoxPhotoBean.builder().createTime(jsonObject.getLong("createTime")).boxPhoto(jsonObject).build());
  2161. }
  2162. //按创建时间倒叙排序
  2163. list.sort(Comparator.comparingLong(BoxPhotoBean::getCreateTime).reversed());
  2164. // list转JSONArray
  2165. JSONArray array = new JSONArray();
  2166. list.stream().forEach(bean->{
  2167. array.add(bean.getBoxPhoto());
  2168. });
  2169. result = array.toJSONString();
  2170. }
  2171. return result;
  2172. }
  2173. private void transferToFlv(String num, String fileName) throws Exception {
  2174. String userEditPath = String.format(UploadFilePath.USER_EDIT_PATH, num);
  2175. String localImagesPath = String.format(ConstantFilePath.SCENE_USER_PATH_V4, num);
  2176. String localFilePath = localImagesPath + fileName;
  2177. File targetFile = new File(localImagesPath);
  2178. if (!targetFile.exists()){
  2179. targetFile.mkdirs();
  2180. }
  2181. targetFile = new File(localFilePath);
  2182. if (targetFile.exists()){
  2183. FileUtils.deleteFile(localFilePath);
  2184. }
  2185. //从用户编辑目录中下载视频到本地
  2186. if (!StorageType.LOCAL.code().equals(this.type)) {
  2187. String filePath = userEditPath + fileName;
  2188. String imageUrl = ossUrlPrefix + filePath + "?t=" + System.currentTimeMillis();
  2189. FileUtils.downLoadFromUrl(imageUrl, fileName, localImagesPath);
  2190. }
  2191. //视频格式转换
  2192. CreateObjUtil.mp4ToFlv(localFilePath, localFilePath.replace("mp4", "flv"));
  2193. //上传
  2194. String flvFileName = fileName.replace("mp4", "flv");
  2195. // uploadToOssUtil.upload(localFilePath, userEditPath + fileName);
  2196. uploadToOssUtil.upload(localFilePath.replace("mp4", "flv"), userEditPath+flvFileName);
  2197. }
  2198. @Override
  2199. public ResultData deleteMosaics(DeleteMosaicParamVO param) throws Exception {
  2200. ScenePlus scenePlus = scenePlusService.getScenePlusByNum(param.getNum());
  2201. if(Objects.isNull(scenePlus)){
  2202. throw new BusinessException(ErrorCode.FAILURE_CODE_5005);
  2203. }
  2204. SceneEditInfo sceneEditInfo = this.getByScenePlusId(scenePlus.getId());
  2205. //如果redis数据丢失,从本地文件中同步马赛克数据到redis
  2206. this.syncMosaicFromFileToRedis(param.getNum());
  2207. String key = String.format(RedisKey.SCENE_MOSAIC_DATA, param.getNum());
  2208. redisUtil.hdel(key, param.getPanoIdList().toArray());
  2209. //写入本地文件,作为备份
  2210. this.writeMosaic(param.getNum());
  2211. //更新数据库
  2212. this.updateMosaicFlag(param.getNum());
  2213. //更新版本号
  2214. this.upgradeVersionById(sceneEditInfo.getId());
  2215. return ResultData.ok();
  2216. }
  2217. @Override
  2218. public List<JSONObject> getMosaicList(String num) throws Exception {
  2219. //如果redis数据丢失,从本地文件中同步马赛克数据到redis
  2220. this.syncMosaicFromFileToRedis(num);
  2221. String key = String.format(RedisKey.SCENE_MOSAIC_DATA, num);
  2222. Map<String, String> map = redisUtil.hmget(key);
  2223. if(CollUtil.isEmpty(map)){
  2224. ResultData.ok(new String[0]);
  2225. }
  2226. return map.values().stream()
  2227. .map(mosaic-> JSON.parseObject(mosaic))
  2228. .collect(Collectors.toList());
  2229. }
  2230. @Override
  2231. public ResultData addWaterMark(BaseFileParamVO param) throws Exception {
  2232. ScenePlus scenePlus = scenePlusService.getScenePlusByNum(param.getNum());
  2233. if(Objects.isNull(scenePlus)){
  2234. throw new BusinessException(ErrorCode.FAILURE_CODE_5005);
  2235. }
  2236. SceneEditInfoExt sceneEditInfoExt = sceneEditInfoExtService
  2237. .getByScenePlusId(scenePlus.getId());
  2238. sceneEditInfoExt.setWaterMark(param.getFileName());
  2239. sceneEditInfoExtService.updateById(sceneEditInfoExt);
  2240. return ResultData.ok();
  2241. }
  2242. @Override
  2243. public ResultData deleteWaterMark(BaseFileParamVO param) throws Exception {
  2244. ScenePlus scenePlus = scenePlusService.getScenePlusByNum(param.getNum());
  2245. if(Objects.isNull(scenePlus)){
  2246. throw new BusinessException(ErrorCode.FAILURE_CODE_5005);
  2247. }
  2248. sceneUploadService.delete(
  2249. DeleteFileParamVO.builder()
  2250. .num(param.getNum())
  2251. .fileNames(Lists.newArrayList(param.getFileName()))
  2252. .bizType(FileBizType.WATERMARK.code()).build());
  2253. SceneEditInfoExt sceneEditInfoExt = sceneEditInfoExtService.getByScenePlusId(scenePlus.getId());
  2254. sceneEditInfoExt.setWaterMark("");
  2255. sceneEditInfoExtService.updateById(sceneEditInfoExt);
  2256. return ResultData.ok();
  2257. }
  2258. }