SceneEditInfoServiceImpl.java 108 KB

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