SceneEditInfoServiceImpl.java 111 KB

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