SceneEditInfoServiceImpl.java 121 KB

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