SceneEditInfoServiceImpl.java 124 KB

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