SceneEditInfoServiceImpl.java 108 KB

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