SceneEditInfoServiceImpl.java 124 KB

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