SceneEditInfoServiceImpl.java 121 KB

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