SceneEditInfoServiceImpl.java 128 KB

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