SceneEditInfoServiceImpl.java 111 KB

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