SceneEditInfoServiceImpl.java 121 KB

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