SceneEditInfoServiceImpl.java 109 KB

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