SceneEditInfoServiceImpl.java 102 KB

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