SceneEditInfoServiceImpl.java 101 KB

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