SceneEditInfoServiceImpl.java 123 KB

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