SceneEditInfoServiceImpl.java 122 KB

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