SceneEditInfoServiceImpl.java 102 KB

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