SceneEditInfoServiceImpl.java 100 KB

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