SceneEditInfoServiceImpl.java 116 KB

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