SceneEditInfoServiceImpl.java 126 KB

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