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