SceneEditInfoServiceImpl.java 122 KB

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