SceneEditInfoServiceImpl.java 81 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863
  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.net.multipart.UploadFile;
  5. import cn.hutool.core.util.StrUtil;
  6. import cn.hutool.core.util.ZipUtil;
  7. import com.alibaba.fastjson.JSON;
  8. import com.alibaba.fastjson.JSONArray;
  9. import com.alibaba.fastjson.JSONObject;
  10. import com.alibaba.fastjson.serializer.SerializerFeature;
  11. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  12. import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
  13. import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
  14. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  15. import com.dtflys.forest.exceptions.ForestRuntimeException;
  16. import com.dtflys.forest.http.ForestRequest;
  17. import com.dtflys.forest.http.ForestResponse;
  18. import com.fdkankan.common.constant.*;
  19. import com.fdkankan.common.exception.BusinessException;
  20. import com.fdkankan.common.response.Result;
  21. import com.fdkankan.common.response.ResultData;
  22. import com.fdkankan.common.util.ComputerUtil;
  23. import com.fdkankan.common.util.CreateObjUtil;
  24. import com.fdkankan.common.util.FileMd5Util;
  25. import com.fdkankan.common.util.FileUtil;
  26. import com.fdkankan.common.util.FileUtils;
  27. import com.fdkankan.common.util.SceneUtil;
  28. import com.fdkankan.fyun.constant.StorageType;
  29. import com.fdkankan.fyun.oss.UploadToOssUtil;
  30. import com.fdkankan.platform.api.feign.PlatformGoodsClient;
  31. import com.fdkankan.platform.api.feign.PlatformUserClient;
  32. import com.fdkankan.platform.api.dto.CameraDetail;
  33. import com.fdkankan.platform.api.dto.Company;
  34. import com.fdkankan.redis.constant.RedisKey;
  35. import com.fdkankan.redis.constant.RedisLockKey;
  36. import com.fdkankan.redis.util.RedisLockUtil;
  37. import com.fdkankan.redis.util.RedisUtil;
  38. import com.fdkankan.scene.api.dto.SceneInfoDTO;
  39. import com.fdkankan.scene.bean.BoxPhotoBean;
  40. import com.fdkankan.scene.bean.SceneJsonBean;
  41. import com.fdkankan.scene.bean.TagBean;
  42. import com.fdkankan.scene.callback.FdkkMiniReqErrorCallback;
  43. import com.fdkankan.scene.callback.FdkkMiniReqSuccessCallback;
  44. import com.fdkankan.scene.entity.SceneDataDownload;
  45. import com.fdkankan.scene.entity.SceneEditControls;
  46. import com.fdkankan.scene.entity.SceneEditInfo;
  47. import com.fdkankan.scene.entity.SceneEditInfoExt;
  48. import com.fdkankan.scene.entity.ScenePlus;
  49. import com.fdkankan.scene.entity.ScenePlusExt;
  50. import com.fdkankan.scene.entity.ScenePro;
  51. import com.fdkankan.scene.entity.SceneProV3;
  52. import com.fdkankan.scene.factory.FloorLogoHandlerFactory;
  53. import com.fdkankan.scene.factory.MusicHandlerFactory;
  54. import com.fdkankan.scene.factory.ScreenshotHandlerFactory;
  55. import com.fdkankan.scene.httpclient.FdkankanMiniClient;
  56. import com.fdkankan.scene.mapper.ISceneEditInfoMapper;
  57. import com.fdkankan.scene.service.ISceneDataDownloadService;
  58. import com.fdkankan.scene.service.ISceneEditControlsService;
  59. import com.fdkankan.scene.service.ISceneEditInfoExtService;
  60. import com.fdkankan.scene.service.ISceneEditInfoService;
  61. import com.fdkankan.scene.service.IScenePlusExtService;
  62. import com.fdkankan.scene.service.IScenePlusService;
  63. import com.fdkankan.scene.service.ISceneProExtService;
  64. import com.fdkankan.scene.service.ISceneProService;
  65. import com.fdkankan.scene.service.ISceneUploadService;
  66. import com.fdkankan.scene.vo.*;
  67. import com.google.common.collect.Lists;
  68. import com.google.errorprone.annotations.Var;
  69. import java.io.File;
  70. import java.nio.charset.Charset;
  71. import java.util.ArrayList;
  72. import java.util.Arrays;
  73. import java.util.Comparator;
  74. import java.util.Date;
  75. import java.util.HashMap;
  76. import java.util.HashSet;
  77. import java.util.List;
  78. import java.util.Map;
  79. import java.util.Map.Entry;
  80. import java.util.Set;
  81. import java.util.UUID;
  82. import java.util.stream.Collectors;
  83. import lombok.extern.slf4j.Slf4j;
  84. import org.apache.commons.lang3.StringUtils;
  85. import org.springframework.beans.factory.annotation.Autowired;
  86. import org.springframework.beans.factory.annotation.Value;
  87. import org.springframework.cloud.context.config.annotation.RefreshScope;
  88. import org.springframework.stereotype.Service;
  89. import java.io.IOException;
  90. import java.util.Calendar;
  91. import java.util.Objects;
  92. import org.springframework.transaction.annotation.Transactional;
  93. import org.springframework.util.ObjectUtils;
  94. import org.springframework.web.multipart.MultipartFile;
  95. /**
  96. * <p>
  97. * 服务实现类
  98. * </p>
  99. *
  100. * @author
  101. * @since 2022-01-18
  102. */
  103. @Slf4j
  104. @RefreshScope
  105. @Service
  106. public class SceneEditInfoServiceImpl extends ServiceImpl<ISceneEditInfoMapper, SceneEditInfo> implements ISceneEditInfoService {
  107. @Value("${oss.prefix.ali}")
  108. private String prefixAli;
  109. @Value("${oss.prefix.url}")
  110. private String ossUrlPrefix;
  111. @Value("${upload.type}")
  112. private String type;
  113. @Value("${oss.bucket:4dkankan}")
  114. private String bucket;
  115. @Value("${main.url}")
  116. private String mainUrl;
  117. @Value("${ecs.checkFile.maxTimes:5}")
  118. private int maxCheckTimes;
  119. @Value("${ecs.checkFile.waitTime:5000}")
  120. private int waitTime;
  121. @Value("${http.host.4dkankanMini}")
  122. private String fkankanMiniHost;
  123. @Autowired
  124. private ISceneEditControlsService sceneEditControlsService;
  125. @Autowired
  126. private ISceneProService sceneProService;
  127. @Autowired
  128. private UploadToOssUtil uploadToOssUtil;
  129. @Autowired
  130. RedisUtil redisUtil;
  131. @Autowired
  132. private ISceneProExtService sceneProExtService;
  133. @Autowired
  134. private RedisLockUtil redisLockUtil;
  135. @Autowired
  136. ISceneDataDownloadService sceneDataDownloadService;
  137. @Autowired
  138. PlatformGoodsClient platformGoodsClient;
  139. @Autowired
  140. PlatformUserClient platformUserClient;
  141. @Autowired
  142. private ISceneEditInfoExtService sceneEditInfoExtService;
  143. @Autowired
  144. IScenePlusService scenePlusService;
  145. @Autowired
  146. IScenePlusExtService scenePlusExtService;
  147. @Autowired
  148. private FdkankanMiniClient fdkankanMiniClient;
  149. @Autowired
  150. private ISceneUploadService sceneUploadService;
  151. @Transactional
  152. @Override
  153. public SceneEditInfoVO saveScene(SceneEditInfoParamVO param) {
  154. ScenePlus scenePlus = scenePlusService.getScenePlusByNum(param.getNum());
  155. // ScenePro scenePro = sceneProService.findBySceneNum(param.getNum());
  156. if(Objects.isNull(scenePlus)){
  157. throw new BusinessException(ErrorCode.FAILURE_CODE_5005);
  158. }
  159. SceneEditInfo sceneEditInfoDb = this.getByScenePlusId(scenePlus.getId());
  160. SceneEditControls sceneEditControlsDb = null;
  161. if(Objects.nonNull(sceneEditInfoDb)){
  162. sceneEditControlsDb = sceneEditControlsService.getBySceneEditId(sceneEditInfoDb.getId());
  163. }
  164. SceneEditInfo sceneEditInfo = BeanUtil.copyProperties(param, SceneEditInfo.class);
  165. sceneEditInfo.setScenePlusId(scenePlus.getId());
  166. if(Objects.isNull(sceneEditInfoDb)){
  167. this.save(sceneEditInfo);
  168. }else{
  169. sceneEditInfo.setId(sceneEditInfoDb.getId());
  170. sceneEditInfo.setVersion(sceneEditInfoDb.getVersion() + 1);
  171. this.updateById(sceneEditInfo);
  172. }
  173. if(Objects.nonNull(param.getControls())){
  174. SceneEditControls sceneEditControls = BeanUtil.copyProperties(param.getControls(), SceneEditControls.class);
  175. sceneEditControls.setEditInfoId(sceneEditInfo.getId());
  176. if(Objects.isNull(sceneEditControlsDb)){
  177. sceneEditControlsService.save(sceneEditControls);
  178. }else{
  179. sceneEditControls.setId(sceneEditControlsDb.getId());
  180. sceneEditControlsService.updateById(sceneEditControls);
  181. }
  182. }
  183. SceneEditInfoVO result = BeanUtil.copyProperties(param, SceneEditInfoVO.class);
  184. result.setCreateTime(sceneEditInfo.getCreateTime());
  185. return result;
  186. }
  187. @Override
  188. public SceneEditInfo getBySceneProId(long sceneProId) {
  189. return this.getOne(new LambdaQueryWrapper<SceneEditInfo>()
  190. .eq(SceneEditInfo::getSceneProId, sceneProId));
  191. }
  192. @Override
  193. public SceneEditInfo getByScenePlusId(long scenePlusId) {
  194. return this.getOne(new LambdaQueryWrapper<SceneEditInfo>()
  195. .eq(SceneEditInfo::getScenePlusId, scenePlusId));
  196. }
  197. @Transactional
  198. @Override
  199. public ResultData publicScene(SceneEditInfoParamVO param) throws Exception{
  200. String num = param.getNum();
  201. //根据场景码查询数据库
  202. ScenePro scenePro = sceneProService.findBySceneNum(num);
  203. ScenePlus scenePlus = scenePlusService.getScenePlusByNum(num);
  204. ScenePlusExt scenePlusExt = scenePlusExtService.getScenePlusExtByPlusId(scenePlus.getId());
  205. SceneEditInfo sceneEditInfo = this.getByScenePlusId(scenePlus.getId());
  206. SceneEditInfoExt sceneEditInfoExt = null;
  207. SceneEditControls sceneEditControls = null;
  208. if(sceneEditInfo == null){
  209. sceneEditInfo = new SceneEditInfo();
  210. sceneEditInfo.setSceneProId(scenePlus.getId());
  211. }else{
  212. sceneEditControls = sceneEditControlsService.getBySceneEditId(sceneEditInfo.getId());
  213. sceneEditInfoExt = sceneEditInfoExtService.getByEditInfoId(sceneEditInfo.getId());
  214. }
  215. if(sceneEditInfoExt == null){
  216. sceneEditInfoExt = new SceneEditInfoExt();
  217. sceneEditInfoExt.setFloorPlanAngle(0f);
  218. sceneEditInfoExt.setFloorPlanCompass(0f);
  219. }
  220. // if(sceneEditControls == null){
  221. // sceneEditControls = new SceneEditControls();
  222. // }
  223. //生成sceneJson
  224. SceneJsonBean sceneJson = new SceneJsonBean();
  225. BeanUtil.copyProperties(sceneEditInfoExt, sceneJson);
  226. BeanUtil.copyProperties(sceneEditInfo, sceneJson);
  227. SceneEditControlsVO sceneEditControlsVO = BeanUtil.copyProperties(sceneEditControls, SceneEditControlsVO.class);
  228. sceneJson.setControls(sceneEditControlsVO);
  229. sceneJson.setNum(num);
  230. // if(StrUtil.isNotEmpty(sceneEditInfo.getFloorPlanPath())){
  231. // sceneJson.setFloorPlanPaths(sceneEditInfo.getFloorPlanPath().split(","));
  232. // }
  233. sceneJson.setCreateTime(scenePlus.getCreateTime());
  234. sceneJson.setSceneResolution(scenePlusExt.getSceneResolution());
  235. sceneJson.setSceneFrom(scenePlusExt.getSceneFrom());
  236. if(StrUtil.isNotEmpty(scenePlusExt.getVideos())){
  237. sceneJson.setVideos(scenePlusExt.getVideos());
  238. }
  239. //处理热点数据,生成hot.json
  240. this.publicHotData(num, sceneEditInfo);
  241. // TODO: 2022/3/2 这里的逻辑放在上传球幕视频接口中做了,这里先暂时注释掉,以后要删除
  242. //处理球幕视频
  243. // this.buildVideo(sceneEditInfo, sceneProExt.getDataSource(), sceneNum);
  244. //本地写sceneJson文件
  245. String localSceneJsonPath = String.format(ConstantFilePath.SCENE_DATA_PATH_V4, num) + "scene.json";
  246. FileUtils.writeFile(localSceneJsonPath, JSON.toJSONString(sceneJson));
  247. //上传sceneJson文件
  248. String sceneJsonPath = String.format(UploadFilePath.DATA_VIEW_PATH+"scene.json", num);
  249. uploadToOssUtil.upload(JSON.toJSONBytes(sceneJson), sceneJsonPath);
  250. //sceneJson放入缓存
  251. String key = String.format(RedisKey.SCENE_JSON, num);
  252. redisUtil.set(key, JSON.toJSONString(sceneJson));
  253. //删除发布数据中的user目录
  254. String publicUserPath = String.format(UploadFilePath.USER_VIEW_PATH, num);
  255. uploadToOssUtil.deleteFile(publicUserPath);
  256. // String editUserPath = String.format(UploadFilePath.USER_EDIT_PATH, sceneNum);
  257. //复制编辑目录到发布目录
  258. String editPath = String.format(UploadFilePath.USER_EDIT_PATH, param.getNum());
  259. String viewPath = String.format(UploadFilePath.USER_VIEW_PATH, param.getNum());
  260. uploadToOssUtil.copyFiles(editPath,viewPath);
  261. //入库
  262. if(sceneEditInfo.getId() == null){
  263. this.save(sceneEditInfo);
  264. }else{
  265. this.updateById(sceneEditInfo);
  266. }
  267. sceneEditInfoExt.setSceneProId(scenePro.getId());
  268. sceneEditInfoExt.setScenePlusId(scenePlus.getId());
  269. sceneEditInfoExt.setEditInfoId(sceneEditInfo.getId());
  270. sceneEditInfoExtService.saveOrUpdate(sceneEditInfoExt);
  271. // sceneEditControls.setEditInfoId(sceneEditInfo.getId());
  272. // if(sceneEditControls.getId() == null){
  273. // sceneEditControlsService.save(sceneEditControls);
  274. // }else{
  275. // sceneEditControlsService.updateById(sceneEditControls);
  276. // }
  277. return ResultData.ok();
  278. }
  279. private void buildVideo(SceneEditInfo sceneEditInfo, String path, String num) throws Exception{
  280. if(CommonStatus.NO.equals(sceneEditInfo.getBuildVideoStatus())){
  281. return;
  282. }
  283. if(path != null && !"".equals(path) && path.startsWith("http")){
  284. path = ConstantFilePath.BUILD_MODEL_PATH + File.separator + path.split("/")[path.split("/").length - 2];
  285. }
  286. String target = path + "_images";
  287. log.info("球幕视频路径:{}", target + File.separator + "extras/video");
  288. File video = new File(target + File.separator + "extras/video");
  289. //如果文件夹或者文件不存在,跳出
  290. if(!video.exists() || video.listFiles() == null || video.listFiles().length == 0){
  291. return;
  292. }
  293. String userEditPath = String.format(UploadFilePath.USER_EDIT_PATH, num);
  294. for(String videoName : video.list()){
  295. log.info("球幕视频名称:{}", videoName);
  296. uploadToOssUtil.upload(target + File.separator + "extras/video/" + videoName,userEditPath + videoName);
  297. CreateObjUtil.mp4ToFlv(target + File.separator + "extras/video/" + videoName,
  298. target + File.separator + "extras/video/" + videoName.replace("mp4", "flv"));
  299. uploadToOssUtil.upload(target + File.separator + "extras/video/" + videoName.replace("mp4", "flv"),userEditPath + videoName.replace("mp4", "flv"));
  300. //覆盖原始视频资源
  301. FileUtils.copyFile(target + File.separator + "extras/video/" + videoName,
  302. path + File.separator + "caches/videos/" + videoName, true);
  303. }
  304. FileUtils.deleteDirectory(target + File.separator + "extras/video/");
  305. //重置状态
  306. sceneEditInfo.setBuildVideoStatus(CommonStatus.NO.code());
  307. }
  308. private void publicHotData(String sceneNum, SceneEditInfo sceneEditInfo) throws IOException {
  309. String hotDataKey = String.format(RedisKey.SCENE_HOT_DATA, sceneNum);
  310. Map<String, String> hotMap = redisUtil.hmget(hotDataKey);
  311. List<String> hotList = Lists.newArrayList();
  312. hotMap.entrySet().stream().forEach(entry->{
  313. if(StrUtil.isNotEmpty(entry.getValue())){
  314. hotList.add(entry.getValue());
  315. }
  316. });
  317. if(CollUtil.isNotEmpty(hotList)){
  318. JSONArray jsonhots = new JSONArray();
  319. hotList.stream().forEach(hot->{
  320. jsonhots.add(JSONObject.parseObject(hot));
  321. });
  322. String hotJsonPath = String.format(UploadFilePath.USER_EDIT_PATH, sceneNum) + "hot.json";
  323. uploadToOssUtil.upload(jsonhots.toString().getBytes(), hotJsonPath);
  324. //修改tags状态为是,标识有热点数据
  325. this.saveTagsToSceneEditInfo(sceneNum, sceneEditInfo);
  326. }
  327. }
  328. @Override
  329. public SceneInfoVO getSceneInfo(@Var SceneInfoParamVO param) throws Exception{
  330. SceneInfoReqType sceneInfoReqType = SceneInfoReqType.get(param.getReqType());
  331. // TODO: 2022/5/19 请求v3的接口,获取场景的状态 待v3停机后要换成v4d的逻辑-------------------------start
  332. String url = fkankanMiniHost + "/api-v4/getSceneProByNum?num=" + param.getNum();
  333. Result<SceneProV3> result = fdkankanMiniClient
  334. .getSceneProByNum(url, new FdkkMiniReqSuccessCallback(),
  335. new FdkkMiniReqErrorCallback());
  336. SceneProV3 sceneProV3 = result.getData();
  337. if(sceneProV3 == null || sceneProV3.getId() == null){
  338. throw new BusinessException(ErrorCode.FAILURE_CODE_5005);
  339. }
  340. if(sceneProV3.getRecStatus().equals("I")){
  341. throw new BusinessException(ErrorCode.FAILURE_CODE_5009);
  342. }
  343. if(SceneStatus.SUCCESS.code() != sceneProV3.getStatus()
  344. && SceneStatus.NO_DISPLAY.code() != sceneProV3.getStatus()){
  345. throw new BusinessException(ErrorCode.FAILURE_CODE_5033);
  346. }
  347. if(PayStatus.PAY.code() != sceneProV3.getPayStatus()){
  348. throw new BusinessException(ErrorCode.FAILURE_CODE_5034);
  349. }
  350. // TODO: 2022/5/19 请求v3的接口,获取场景的状态 待v3停机后要换成v4d的逻辑-------------------------send
  351. switch (sceneInfoReqType){
  352. //如果是编辑页面请求,查数据库
  353. case EDIT:
  354. return this.getSceneInfo4Edit(param.getNum());
  355. //如果是查看页面请求,查redis
  356. case VIEW:
  357. return this.getSceneInfo4View(param.getNum());
  358. }
  359. return null;
  360. }
  361. @Override
  362. public SceneInfoDTO getSceneInfo4Download(SceneInfoParamVO param) throws Exception{
  363. SceneInfoVO sceneInfo4View = this.getSceneInfo4View(param.getNum());
  364. if(Objects.isNull(sceneInfo4View)){
  365. return null;
  366. }
  367. SceneInfoDTO sceneInfoDTO = BeanUtil.copyProperties(sceneInfo4View, SceneInfoDTO.class);
  368. ScenePlus scenePlus = scenePlusService.getScenePlusByNum(param.getNum());
  369. sceneInfoDTO.setSceneSource(scenePlus.getSceneSource());
  370. return sceneInfoDTO;
  371. }
  372. /**
  373. * <p>
  374. 编辑页面获取场景详情信息
  375. * </p>
  376. * @author dengsixing
  377. * @date 2022/1/21
  378. * @param num
  379. * @return com.fdkankan.scene.vo.SceneInfoVO
  380. **/
  381. private SceneInfoVO getSceneInfo4Edit(String num) throws Exception{
  382. // ScenePro scenePro = sceneProService.findBySceneNum(num);
  383. ScenePlus scenePlus = scenePlusService.getScenePlusByNum(num);
  384. if(Objects.isNull(scenePlus)){
  385. throw new BusinessException(ErrorCode.FAILURE_CODE_5005);
  386. }
  387. // SceneProExt sceneProExt = sceneProExtService.findBySceneProId(scenePro.getId());
  388. ScenePlusExt scenePlusExt = scenePlusExtService.getScenePlusExtByPlusId(scenePlus.getId());
  389. SceneEditInfo sceneEditInfo = this.getByScenePlusId(scenePlus.getId());
  390. if(Objects.isNull(sceneEditInfo)){
  391. throw new BusinessException(ErrorCode.FAILURE_CODE_5005);
  392. }
  393. SceneEditInfoExt sceneEditInfoExt = sceneEditInfoExtService.getByEditInfoId(sceneEditInfo.getId());
  394. SceneEditControls sceneEditControls = sceneEditControlsService.getBySceneEditId(sceneEditInfo.getId());
  395. SceneInfoVO sceneInfoVO = new SceneInfoVO();
  396. BeanUtil.copyProperties(sceneEditInfoExt, sceneInfoVO);
  397. BeanUtil.copyProperties(sceneEditInfo, sceneInfoVO);
  398. if(Objects.isNull(sceneInfoVO.getFloorPlanAngle())){
  399. sceneInfoVO.setFloorPlanAngle(0f);
  400. }
  401. if(Objects.isNull(sceneInfoVO.getFloorPlanCompass())){
  402. sceneInfoVO.setFloorPlanCompass(0f);
  403. }
  404. sceneInfoVO.setControls(BeanUtil.copyProperties(sceneEditControls, SceneEditControlsVO.class));
  405. sceneInfoVO.setNum(num);
  406. //生成sceneJson
  407. // if(StrUtil.isNotEmpty(sceneEditInfo.getFloorPlanPath())){
  408. // sceneInfoVO.setFloorPlanPaths(sceneEditInfo.getFloorPlanPath().split(","));
  409. // }
  410. sceneInfoVO.setCreateTime(scenePlus.getCreateTime());
  411. sceneInfoVO.setSceneResolution(scenePlusExt.getSceneResolution());
  412. sceneInfoVO.setSceneFrom(scenePlusExt.getSceneFrom());
  413. sceneInfoVO.setVideos(scenePlusExt.getVideos());
  414. // TODO: 2022/4/24 v3版本停机要切换---------------------------start
  415. // this.setExtData(sceneInfoVO, scenePlus.getCameraId());
  416. this.setExtDataFromV3(sceneInfoVO, scenePlus.getCameraId());
  417. // TODO: 2022/4/24 v3版本停机要切换---------------------------end
  418. return sceneInfoVO;
  419. }
  420. /**
  421. * <p>
  422. getInfo接口返回字段扩展
  423. * </p>
  424. * @author dengsixing
  425. * @date 2022/3/9
  426. **/
  427. private void setExtDataFromV3(SceneInfoVO sceneInfoVO, Long cameraId) throws Exception{
  428. if(Objects.isNull(cameraId)){
  429. return;
  430. }
  431. String url = fkankanMiniHost + "/api-v4/getDataSyncByCameraId?cameraId=" + cameraId;
  432. Result<String> dataSyncResult = fdkankanMiniClient
  433. .getDataSyncType(url, new FdkkMiniReqSuccessCallback(), new FdkkMiniReqErrorCallback());
  434. sceneInfoVO.setDataSync(dataSyncResult.getMsg());
  435. }
  436. /**
  437. * <p>
  438. getInfo接口返回字段扩展
  439. * </p>
  440. * @author dengsixing
  441. * @date 2022/3/9
  442. **/
  443. private void setExtData(SceneInfoVO sceneInfoVO, Long cameraId) throws Exception{
  444. ResultData<CameraDetail> cameraResultData = platformGoodsClient.getCameraDetailByCameraId(cameraId);
  445. if(!cameraResultData.getSuccess()){
  446. throw new Exception(ServerCode.FEIGN_REQUEST_FAILD.message());
  447. }
  448. CameraDetail camera = cameraResultData.getData();
  449. if(Objects.isNull(camera)){
  450. throw new BusinessException(ErrorCode.FAILURE_CODE_7010);
  451. }
  452. if(Objects.isNull(camera.getCompanyId())){
  453. return;
  454. }
  455. ResultData<Company> companyResultData = platformUserClient.getCompanyById(camera.getCompanyId());
  456. if(!companyResultData.getSuccess()){
  457. throw new Exception(ServerCode.FEIGN_REQUEST_FAILD.message());
  458. }
  459. Company company = companyResultData.getData();
  460. if(Objects.isNull(company)){
  461. return;
  462. }
  463. sceneInfoVO.setDataSync(company.getDataSync());
  464. }
  465. /**
  466. * <p>
  467. 查看页面获取场景详情信息
  468. * </p>
  469. * @author dengsixing
  470. * @date 2022/1/21
  471. * @param num
  472. * @return com.fdkankan.scene.vo.SceneInfoVO
  473. **/
  474. private SceneInfoVO getSceneInfo4View(String num) throws Exception{
  475. // ScenePro scenePro = sceneProService.findBySceneNum(num);
  476. ScenePlus scenePlus = scenePlusService.getScenePlusByNum(num);
  477. if(Objects.isNull(scenePlus)){
  478. throw new BusinessException(ErrorCode.FAILURE_CODE_5005);
  479. }
  480. String key = String.format(RedisKey.SCENE_JSON, num);
  481. String sceneJson = redisUtil.get(key);
  482. SceneInfoVO sceneInfoVO = null;
  483. //先查询redis
  484. if(StrUtil.isNotEmpty(sceneJson)) {
  485. sceneInfoVO = JSON.parseObject(sceneJson, SceneInfoVO.class);
  486. // TODO: 2022/4/24 v3版本停机要切换---------------------------start
  487. // this.setExtData(sceneInfoVO, scenePlus.getCameraId());
  488. this.setExtDataFromV3(sceneInfoVO, scenePlus.getCameraId());
  489. // TODO: 2022/4/24 v3版本停机要切换---------------------------end
  490. sceneInfoVO.setScenePassword(null);
  491. if(Objects.isNull(sceneInfoVO.getFloorPlanAngle())){
  492. sceneInfoVO.setFloorPlanAngle(0f);
  493. }
  494. if(Objects.isNull(sceneInfoVO.getFloorPlanCompass())){
  495. sceneInfoVO.setFloorPlanCompass(0f);
  496. }
  497. return sceneInfoVO;
  498. }
  499. //如果redis没找到,从scene.json中获取
  500. String objectName = String.format(ConstantFilePath.SCENE_VIEW_DATA_DATA_SCENEJSON, num);
  501. String objectContent = uploadToOssUtil.getObjectContent(bucket, objectName);
  502. if(StrUtil.isEmpty(objectContent))
  503. return null;
  504. sceneInfoVO = JSON.parseObject(objectContent, SceneInfoVO.class);
  505. // TODO: 2022/4/24 v3版本停机要切换---------------------------start
  506. // this.setExtData(sceneInfoVO, scenePlus.getCameraId());
  507. this.setExtDataFromV3(sceneInfoVO, scenePlus.getCameraId());
  508. // TODO: 2022/4/24 v3版本停机要切换---------------------------end
  509. sceneInfoVO.setScenePassword(null);
  510. if(Objects.isNull(sceneInfoVO.getFloorPlanAngle())){
  511. sceneInfoVO.setFloorPlanAngle(0f);
  512. }
  513. if(Objects.isNull(sceneInfoVO.getFloorPlanCompass())){
  514. sceneInfoVO.setFloorPlanCompass(0f);
  515. }
  516. redisUtil.set(key, JSON.toJSONString(sceneInfoVO));
  517. return sceneInfoVO;
  518. }
  519. @Override
  520. // @Transactional
  521. public ResultData saveUpload(SaveUploadParamVO param) throws Exception {
  522. FileBizType fileBizType = FileBizType.get(param.getBizType());
  523. String num = param.getNum();
  524. String fileInfo = param.getFileInfo();
  525. if(Objects.isNull(fileBizType)){
  526. throw new BusinessException(ErrorCode.FAILURE_CODE_7003);
  527. }
  528. switch (fileBizType){
  529. case MUSIC:
  530. return new MusicHandlerFactory().getHandler().save(num, fileInfo);
  531. case FLOOR_LOGO:
  532. return new FloorLogoHandlerFactory().getHandler().save(num, fileInfo);
  533. // case FLOOR_PLAN:
  534. // return new FloorPlanHandlerFactory().getHandler().save(num, fileInfo);
  535. case SCREENSHOT:
  536. return new ScreenshotHandlerFactory().getHandler().save(num, fileInfo);
  537. }
  538. return ResultData.ok();
  539. }
  540. @Override
  541. public ResultData saveCad(BaseDataParamVO param) throws Exception {
  542. String num = param.getNum();
  543. // ScenePro scenePro = sceneProService.findBySceneNum(num);
  544. ScenePlus scenePlus = scenePlusService.getScenePlusByNum(num);
  545. if(Objects.isNull(scenePlus))
  546. throw new BusinessException(ErrorCode.FAILURE_CODE_5005);
  547. String editUserPath = String.format(UploadFilePath.USER_EDIT_PATH, num);
  548. String localDataPath = String.format(ConstantFilePath.SCENE_USER_PATH_V4, num);
  549. JSONObject fileInfoJson = JSON.parseObject(param.getData());
  550. Float angel = null;
  551. Float compass = null;
  552. String floorJsonData = fileInfoJson.getString("floorJsonData");
  553. if(StrUtil.isNotBlank(floorJsonData)){
  554. JSONObject jsonObject = JSON.parseObject(floorJsonData);
  555. angel = jsonObject.getFloat("angle");
  556. compass = jsonObject.getFloat("compass");
  557. }
  558. //处理户型图数据
  559. //上传floorplan_user.json文件
  560. FileUtils.writeFile(localDataPath + "floorplan_user.json", floorJsonData);
  561. uploadToOssUtil.upload(localDataPath + "floorplan_user.json", editUserPath + "floorplan_user.json");
  562. //写入数据库
  563. Byte floorPlanUser = null;
  564. if(StrUtil.isNotEmpty(floorJsonData)){
  565. floorPlanUser = CommonStatus.YES.code();
  566. }
  567. SceneEditInfo sceneEditInfoDb = this.getByScenePlusId(scenePlus.getId());
  568. if(Objects.nonNull(sceneEditInfoDb)){
  569. LambdaUpdateWrapper<SceneEditInfo> updateWrapper = new LambdaUpdateWrapper<SceneEditInfo>()
  570. .setSql("version=version+" + 1)
  571. .eq(SceneEditInfo::getId, sceneEditInfoDb.getId());
  572. if(floorPlanUser != null){
  573. updateWrapper.set(SceneEditInfo::getFloorPlanUser, floorPlanUser);
  574. }
  575. this.update(updateWrapper);
  576. }else{
  577. sceneEditInfoDb = new SceneEditInfo();
  578. sceneEditInfoDb.setScenePlusId(scenePlus.getId());
  579. sceneEditInfoDb.setFloorPlanUser(floorPlanUser);
  580. this.save(sceneEditInfoDb);
  581. }
  582. SceneEditInfoExt sceneEditInfoExt = sceneEditInfoExtService.getByEditInfoId(sceneEditInfoDb.getId());
  583. if(Objects.isNull(sceneEditInfoExt)){
  584. sceneEditInfoExt = new SceneEditInfoExt();
  585. sceneEditInfoExt.setEditInfoId(sceneEditInfoDb.getId());
  586. }
  587. if(Objects.nonNull(angel)){
  588. sceneEditInfoExt.setFloorPlanAngle(angel);
  589. }
  590. if(Objects.nonNull(compass)){
  591. sceneEditInfoExt.setFloorPlanCompass(compass);
  592. }
  593. sceneEditInfoExtService.saveOrUpdate(sceneEditInfoExt);
  594. return ResultData.ok();
  595. }
  596. @Override
  597. public ResultData resetCad(String num){
  598. ScenePlus scenePlus = scenePlusService.getScenePlusByNum(num);
  599. if(Objects.isNull(scenePlus))
  600. throw new BusinessException(ErrorCode.FAILURE_CODE_5005);
  601. ScenePlusExt scenePlusExt = scenePlusExtService
  602. .getScenePlusExtByPlusId(scenePlus.getId());
  603. String path = scenePlusExt.getDataSource();
  604. //户型图上传
  605. String userEditPath = UploadFilePath.USER_EDIT_PATH + "floor-cad-%s.%s";
  606. String floorCadPath = path + "/results/floorplan_cad";
  607. List<String> floorCadList = FileUtil.getFileList(floorCadPath);
  608. if(CollUtil.isNotEmpty(floorCadList)){
  609. floorCadList.stream().forEach(str->{
  610. String substring = str.substring(str.lastIndexOf(File.separator) + 1);
  611. String[] arr = substring.split("floor");
  612. String[] arr2 = arr[1].split("\\.");
  613. uploadToOssUtil.upload(str, String.format(userEditPath, num, arr2[0], arr2[1]));
  614. });
  615. }
  616. ScenePro scenePro = sceneProService.findBySceneNum(num);
  617. SceneEditInfo sceneEditInfoDb = this.getByScenePlusId(scenePlus.getId());
  618. SceneEditInfoExt sceneEditInfoExt = null;
  619. if(Objects.nonNull(sceneEditInfoDb)){
  620. LambdaUpdateWrapper<SceneEditInfo> updateWrapper = new LambdaUpdateWrapper<SceneEditInfo>()
  621. .setSql("version=version+" + 1)
  622. .set(SceneEditInfo::getFloorPlanUser, CommonStatus.NO.code())
  623. .eq(SceneEditInfo::getId, sceneEditInfoDb.getId());
  624. this.update(updateWrapper);
  625. sceneEditInfoExt = sceneEditInfoExtService.getByEditInfoId(sceneEditInfoDb.getId());
  626. }else{
  627. sceneEditInfoDb = new SceneEditInfo();
  628. sceneEditInfoDb.setSceneProId(scenePro.getId());
  629. sceneEditInfoDb.setScenePlusId(scenePlus.getId());
  630. sceneEditInfoDb.setFloorPlanUser(CommonStatus.NO.code());
  631. this.save(sceneEditInfoDb);
  632. }
  633. if(Objects.isNull(sceneEditInfoExt)){
  634. sceneEditInfoExt = new SceneEditInfoExt();
  635. sceneEditInfoExt.setSceneProId(scenePro.getId());
  636. sceneEditInfoDb.setScenePlusId(scenePlus.getId());
  637. sceneEditInfoExt.setEditInfoId(sceneEditInfoDb.getId());
  638. }
  639. sceneEditInfoExt.setFloorPlanAngle(0f);
  640. sceneEditInfoExt.setFloorPlanCompass(0f);
  641. sceneEditInfoExtService.saveOrUpdate(sceneEditInfoExt);
  642. return ResultData.ok();
  643. }
  644. @Override
  645. public ResultData renameCad(RenameCadParamVO param) throws IOException {
  646. String key = String.format(UploadFilePath.DATA_VIEW_PATH, param.getNum()) + "floorplan_cad.json";
  647. String floorplanCadJson = uploadToOssUtil.getObjectContent(this.bucket, key);
  648. if(StrUtil.isEmpty(floorplanCadJson)){
  649. return ResultData.ok();
  650. }
  651. Map<Integer, String> renameMap = new HashMap<>();
  652. param.getFloors().stream().forEach(floor->{
  653. renameMap.put(floor.getSubgroup(), floor.getName());
  654. });
  655. JSONObject jsonObject = JSON.parseObject(floorplanCadJson);
  656. if(Objects.isNull(jsonObject)){
  657. return ResultData.ok();
  658. }
  659. JSONArray floorArr = jsonObject.getJSONArray("floors");
  660. if(CollUtil.isEmpty(floorArr)){
  661. return ResultData.ok();
  662. }
  663. for (Object o : floorArr) {
  664. JSONObject item = (JSONObject)o;
  665. int subgroup = item.getIntValue("subgroup");
  666. String name = renameMap.get(subgroup);
  667. if(StrUtil.isEmpty(name)){
  668. continue;
  669. }
  670. item.put("name", name);
  671. }
  672. uploadToOssUtil.upload(jsonObject.toJSONString().getBytes(), key);
  673. return ResultData.ok();
  674. }
  675. private JSONObject updateFloorName(String sourceFloors, String targeFloors){
  676. JSONArray sourceFloorsJson = JSON.parseArray(sourceFloors);
  677. JSONObject targeFloorsJson = JSONObject.parseObject(targeFloors);
  678. JSONArray array = targeFloorsJson.getJSONArray("floors");
  679. for (int i = 0; i < array.size(); ++i) {
  680. JSONObject targetFloor = array.getJSONObject(i);
  681. int targetId = targetFloor.getIntValue("id");
  682. for (int j = 0; j < sourceFloorsJson.size(); ++j) {
  683. JSONObject floor = sourceFloorsJson.getJSONObject(j);
  684. int id = floor.getIntValue("id");
  685. String name = floor.getString("name");
  686. if (targetId != id)
  687. continue;
  688. targetFloor.put("name", name);
  689. }
  690. }
  691. return targeFloorsJson;
  692. }
  693. @Override
  694. public void upgradeVersionById(Long id) {
  695. this.update(new LambdaUpdateWrapper<SceneEditInfo>()
  696. .setSql("version=version+" + 1)
  697. .eq(SceneEditInfo::getId, id));
  698. }
  699. @Override
  700. public ResultData uploadPanorama(String num, MultipartFile file) throws Exception {
  701. //校验压缩包格式
  702. // log.info("全景图压缩包文件名:" + file.getOriginalFilename());
  703. // if(!file.getOriginalFilename().endsWith(".zip")){
  704. // throw new BusinessException(ErrorCode.FAILURE_CODE_7015);
  705. // }
  706. if(!file.getOriginalFilename().endsWith(".zip") && !file.getOriginalFilename().endsWith(".jpg")){
  707. throw new BusinessException(ErrorCode.FAILURE_CODE_7007, "jpg或者zip");
  708. }
  709. // ScenePro scenePro = sceneProService.findBySceneNum(num);
  710. ScenePlus scenePlus = scenePlusService.getScenePlusByNum(num);
  711. if(scenePlus == null){
  712. throw new BusinessException(ErrorCode.FAILURE_CODE_5005);
  713. }
  714. // SceneProExt sceneProExt = sceneProExtService.findBySceneProId(scenePro.getId());
  715. ScenePlusExt scenePlusExt = scenePlusExtService
  716. .getScenePlusExtByPlusId(scenePlus.getId());
  717. //原始计算根目录
  718. // String path = "F:\\test";
  719. String path = scenePlusExt.getDataSource();
  720. //全景图计算根目录
  721. String target = path + "_images";
  722. //解压缩文件存放目录
  723. String targetImagesPath = target + "/extras/images/";
  724. //压缩文件保存目录
  725. String zipTargetFilePath = targetImagesPath + file.getOriginalFilename();
  726. //压缩包保存到本地
  727. // String cachePath = String.format(ConstantFilePath.SCENE_CACHE_IMAGES, num);
  728. //先删除本地文件
  729. FileUtils.deleteDirectory(targetImagesPath);
  730. File targetFile = new File(zipTargetFilePath);
  731. if(!targetFile.getParentFile().exists()){
  732. targetFile.getParentFile().mkdirs();
  733. }
  734. file.transferTo(targetFile);
  735. //如果是压缩包上传,需要解压缩
  736. if(file.getOriginalFilename().endsWith(".zip")){
  737. //解压zip包
  738. FileUtil.unZip(zipTargetFilePath, targetImagesPath);
  739. // FileUtils.decompress(zipTargetFilePath, targetImagesPath);
  740. //删除压缩包
  741. FileUtil.delFile(zipTargetFilePath);
  742. }
  743. //判断文件夹目录结构,图片必须放在压缩包根目录下,不支持空文件夹或其他格式文件上传
  744. File[] files = new File(targetImagesPath).listFiles();
  745. Arrays.stream(files).forEach(item->{
  746. if(item.isDirectory()){
  747. throw new BusinessException(ErrorCode.FAILURE_CODE_7018);
  748. }
  749. });
  750. //获取解压后的文件列表
  751. List<String> uploadFileList = FileUtil.getFileList(targetImagesPath);
  752. if(CollUtil.isEmpty(uploadFileList)){
  753. throw new BusinessException(ErrorCode.FAILURE_CODE_5062);
  754. }
  755. //判断是否有可用的jpg文件
  756. boolean existJpg = false;
  757. if(CollUtil.isNotEmpty(uploadFileList)){
  758. existJpg = uploadFileList.stream().anyMatch(str -> {
  759. if(str.endsWith(".jpg")){
  760. return true;
  761. }
  762. return false;
  763. });
  764. }
  765. if(!existJpg){
  766. throw new BusinessException(ErrorCode.FAILURE_CODE_5062);
  767. }
  768. //列出caches/images中的文件列表
  769. String imgCachePath = String.format(UploadFilePath.IMG_CACHES_PATH, num);
  770. //比对图片列表,不存在的要返回名称集合
  771. String visionPath = path + "/results/vision.txt";
  772. List<String> panoramaImageList = SceneUtil.getPanoramaImageList(visionPath);
  773. List<String> notExistFileList = uploadFileList.stream().filter(filePath -> {
  774. filePath = filePath.substring(filePath.lastIndexOf(File.separator) + 1);
  775. if(panoramaImageList.contains(filePath)){
  776. return false;
  777. }
  778. return true;
  779. }).collect(Collectors.toList());
  780. if(CollUtil.isNotEmpty(notExistFileList)){
  781. //删除错误文件
  782. notExistFileList.parallelStream().forEach(filePath->{
  783. FileUtils.deleteFile(filePath);
  784. });
  785. }
  786. //判断成功的图片,如果成功图片为0,就直接返回,不需要执行算法
  787. uploadFileList = FileUtil.getFileList(targetImagesPath);
  788. if(CollUtil.isEmpty(uploadFileList)){
  789. if(CollUtil.isNotEmpty(notExistFileList)){
  790. notExistFileList = notExistFileList.stream().map(filePath -> {
  791. return filePath.substring(filePath.lastIndexOf(File.separator) + 1);
  792. }).collect(Collectors.toList());
  793. }
  794. return ResultData.ok(new UploadPanoramaVO(0, notExistFileList));
  795. }
  796. //上传
  797. Map<String, String> map = new HashMap<>();
  798. // uploadFileList.stream().forEach(filePath->{
  799. // map.put(filePath, filePath.replace(targetImagesPath, imgCachePath));
  800. // });
  801. String imgViewPath = String.format(UploadFilePath.IMG_VIEW_PATH, num);
  802. FileUtils.downLoadFromUrl(ossUrlPrefix + imgViewPath + "vision.modeldata" + "?m="+new Date().getTime(),
  803. "vision.modeldata", target + File.separator + "extras" + File.separator);
  804. CreateObjUtil.convertVisionmodeldataToTxt(target + File.separator + "extras" + File.separator + "vision.modeldata",
  805. target + File.separator + "extras" + File.separator + "vision.txt");
  806. FileUtils.copyFile(path + File.separator + "data.json", target + File.separator+"data.json", true);
  807. FileUtils.copyFile(path + File.separator + "project.json", target + File.separator+"project.json", true);
  808. //data.json增加extras为执行重建算法
  809. String data = FileUtils.readFile(target + File.separator+"data.json");
  810. if(data != null){
  811. JSONObject floorplanJson = new JSONObject();
  812. floorplanJson.put("has_source_images", true);
  813. floorplanJson.put("has_vision_txt", true);
  814. JSONObject dataJson = JSONObject.parseObject(data);
  815. dataJson.put("extras", floorplanJson);
  816. //V5表示不需要生成high,low文件
  817. dataJson.put("skybox_type", "SKYBOX_V6");
  818. if(scenePlusExt.getSceneScheme() == 11){
  819. dataJson.put("skybox_type", "SKYBOX_V7");
  820. }
  821. dataJson.put("split_type", "SPLIT_V8");
  822. //sceneScheme为3切成瓦片图
  823. if(scenePlusExt.getSceneScheme() == 3){
  824. dataJson.put("skybox_type", "SKYBOX_V4");
  825. }
  826. FileUtils.writeFile(target + File.separator+"data.json", new String(dataJson.toString().getBytes(), "UTF-8"));
  827. }
  828. if(new File(target + File.separator + "capture").exists()){
  829. new File(target + File.separator + "capture").delete();
  830. }
  831. if(new File(target + File.separator + "results").exists()){
  832. FileUtils.delAllFile(target + File.separator + "results");
  833. }
  834. if(StorageType.AWS.code().equals(this.type)){
  835. //亚马逊保持旧方式,超链接capture
  836. CreateObjUtil.createSoftConnection(path + File.separator + "capture", target + File.separator + "capture");
  837. }
  838. CreateObjUtil.ossUtilCp(ConstantFilePath.OSS_PREFIX + path.replace(ConstantFilePath.BUILD_MODEL_PATH, "") + "/data.fdage", target + File.separator + "capture/");
  839. CreateObjUtil.build3dModel(target , "1");
  840. String uploadJsonPath= target + File.separator + "results" +File.separator+"upload.json";
  841. Thread.sleep(2000);
  842. boolean exist = ComputerUtil.checkComputeCompleted(uploadJsonPath, maxCheckTimes, waitTime);
  843. if(!exist){
  844. throw new BusinessException(ErrorCode.FAILURE_CODE_7013);
  845. }
  846. String uploadData = FileUtils.readFile(uploadJsonPath);
  847. JSONObject uploadJson = null;
  848. JSONArray array = null;
  849. if(uploadData!=null) {
  850. uploadJson = JSONObject.parseObject(uploadData);
  851. array = uploadJson.getJSONArray("upload");
  852. }
  853. if(array == null){
  854. throw new BusinessException(ErrorCode.FAILURE_CODE_7013);
  855. }
  856. JSONObject fileJson = null;
  857. String fileName = "";
  858. for(int i = 0, len = array.size(); i < len; i++) {
  859. fileJson = array.getJSONObject(i);
  860. fileName = fileJson.getString("file");
  861. //文件不存在抛出异常
  862. if (!new File(target + File.separator + "results" + File.separator + fileName)
  863. .exists()) {
  864. throw new Exception(
  865. target + File.separator + "results" + File.separator + fileName + "文件不存在");
  866. }
  867. //high文件夹
  868. if (fileJson.getIntValue("clazz") == 3) {
  869. map.put(target + File.separator + "results" + File.separator + fileName,
  870. imgViewPath + "pan/high/" + fileName.replace("high/", ""));
  871. continue;
  872. }
  873. //low文件夹
  874. if (fileJson.getIntValue("clazz") == 4) {
  875. map.put(target + File.separator + "results" + File.separator + fileName,
  876. imgViewPath + "pan/low/" + fileName.replace("low/", ""));
  877. continue;
  878. }
  879. //tiles文件夹,亚马逊没有裁剪图片api,不需要上传4k图
  880. if (fileJson.getIntValue("clazz") == 5) {
  881. map.put(target + File.separator + "results" + File.separator + fileName,
  882. imgViewPath + fileName);
  883. continue;
  884. }
  885. //tiles文件夹,亚马逊瓦片图
  886. if (fileJson.getIntValue("clazz") == 7) {
  887. map.put(target + File.separator + "results" + File.separator + fileName,
  888. imgViewPath + fileName);
  889. continue;
  890. }
  891. }
  892. if(map.size()>0) {
  893. uploadToOssUtil.uploadMulFiles(map);
  894. }
  895. //拷贝修改后的全景图到缓存目录
  896. String cachedImagesPath = String.format(ConstantFilePath.SCENE_CACHE_IMAGES, num);
  897. uploadFileList.stream().forEach(srcPath->{
  898. cn.hutool.core.io.FileUtil.copy(srcPath, srcPath.replace(targetImagesPath, cachedImagesPath), true);
  899. });
  900. //更新版本号
  901. SceneEditInfo sceneEditInfo = this.getByScenePlusId(scenePlus.getId());
  902. this.upgradeVersionById(sceneEditInfo.getId());
  903. //如果部分成功,则需要返回成功数量和失败列表
  904. if(CollUtil.isNotEmpty(notExistFileList)){
  905. notExistFileList = notExistFileList.stream().map(filePath -> {
  906. return filePath.substring(filePath.lastIndexOf(File.separator) + 1);
  907. }).collect(Collectors.toList());
  908. return ResultData.ok(new UploadPanoramaVO(uploadFileList.size(), notExistFileList));
  909. }
  910. return ResultData.ok();
  911. }
  912. @Override
  913. public ResultData downloadPanorama(FileParamVO param) throws Exception {
  914. String num = param.getNum();
  915. String fileName = param.getFileName();
  916. ScenePlus scenePlus = scenePlusService.getScenePlusByNum(num);
  917. if(Objects.isNull(scenePlus)){
  918. throw new BusinessException(ErrorCode.FAILURE_CODE_5005);
  919. }
  920. ScenePlusExt scenePlusExt = scenePlusExtService.getScenePlusExtByPlusId(scenePlus.getId());
  921. String path = scenePlusExt.getDataSource();
  922. String cachePath = String.format(ConstantFilePath.SCENE_CACHE, num);
  923. String imgCachePath = String.format(UploadFilePath.IMG_CACHES_PATH, num);
  924. String localImagesPath = String.format(ConstantFilePath.SCENE_CACHE_IMAGES, num);
  925. String cacheFormat = "downloads/scene/%s/caches/";
  926. String cacheImageFormat = "downloads/scene/%s/caches/images/";
  927. String url = null;
  928. String downloadName = null;
  929. //如果入参文件名不为空,则是单个文件下载,不需要打包
  930. if(StrUtil.isNotEmpty(fileName)){
  931. //如果是单张图片,直接提供oss url
  932. String localFilePath = localImagesPath + fileName;
  933. String ossFilePath = imgCachePath + fileName;
  934. uploadToOssUtil.upload(localFilePath, ossFilePath);
  935. url = ossUrlPrefix + ossFilePath;
  936. // FileUtils.downLoadFromUrl(imageUrl, fileName, localImagesPath);
  937. downloadName = fileName;
  938. }else{
  939. //从vision.txt中读取有效全景图名称列表
  940. // String visionPath = path + "/results/vision.txt";
  941. // List<String> imageList = SceneUtil.getPanoramaImageList(visionPath);
  942. //
  943. // //先下载到本地
  944. // List<String> keyList = cn.hutool.core.io.FileUtil.listFileNames(imgCachePath);
  945. // if (!StorageType.LOCAL.code().equals(this.type)) {// TODO: 2022/2/15 这里有可能有问题,可能还需要考虑本地部署的情况
  946. //// System.setProperty("java.util.concurrent.ForkJoinPool.common.parallelism", "8");
  947. // keyList.stream().forEach(key->{
  948. // String file = key.substring(key.lastIndexOf("/") + 1);
  949. // if(imageList.contains(file)){
  950. // String imageUrl = ossUrlPrefix + imgCachePath + file + "?t=" + System.currentTimeMillis();
  951. // FileUtils.downLoadFromUrl(imageUrl, file, localImagesPath);
  952. // }
  953. // });
  954. // }
  955. if(!cn.hutool.core.io.FileUtil.exist(localImagesPath)){
  956. throw new BusinessException(ErrorCode.FAILURE_CODE_7006);
  957. }
  958. downloadName = num + "_images.zip";
  959. //打包
  960. String zipPath = cachePath + downloadName;
  961. ZipUtil.zip(localImagesPath, zipPath);
  962. //上传压缩包
  963. // uploadToOssUtil.upload(zipPath, String.format(cacheFormat, num) + downloadName);
  964. uploadToOssUtil.uploadBySh(zipPath, String.format(cacheFormat, num) + downloadName);
  965. url = ossUrlPrefix + String.format(cacheFormat, num) + downloadName;
  966. //删除本地压缩包
  967. FileUtils.deleteFile(zipPath);
  968. // //删除本地目录
  969. // FileUtils.deleteDirectory(localImagesPath);
  970. }
  971. Map<String, Object> map = new HashMap<>();
  972. map.put("fileUrl", url + "?t=" + System.currentTimeMillis());
  973. map.put("fileName", downloadName);
  974. return ResultData.ok(map);
  975. }
  976. @Override
  977. public void saveTagsToSceneEditInfo(String num, SceneEditInfo sceneEditInfo){
  978. //查询缓存是否包含热点数据
  979. String key = String.format(RedisKey.SCENE_HOT_DATA, num);
  980. Map<String, String> allTagsMap = redisUtil.hmget(key);
  981. boolean hashTags = false;
  982. for (Entry<String, String> tagMap : allTagsMap.entrySet()) {
  983. if(StrUtil.isEmpty(tagMap.getValue())){
  984. continue;
  985. }
  986. hashTags = true;
  987. break;
  988. }
  989. //更改热点状态
  990. sceneEditInfo.setTags(hashTags ? CommonStatus.YES.code() : CommonStatus.NO.code());
  991. //version 是空的代表 sceneEditInfo记录集不存在
  992. if(sceneEditInfo.getVersion() != null){
  993. sceneEditInfo.setVersion(sceneEditInfo.getVersion() + 1);
  994. }
  995. }
  996. @Override
  997. public ResultData saveVideoBox(FileNameAndDataParamVO param) throws Exception {
  998. JSONObject boxVideo = JSONObject.parseObject(param.getData());
  999. String sid = boxVideo.getString("sid");
  1000. if(StrUtil.isEmpty(sid)){
  1001. throw new BusinessException(ErrorCode.PARAM_REQUIRED);
  1002. }
  1003. // ScenePro scenePro = sceneProService.findBySceneNum(param.getNum());
  1004. ScenePlus scenePlus = scenePlusService.getScenePlusByNum(param.getNum());
  1005. if(Objects.isNull(scenePlus))
  1006. throw new BusinessException(ErrorCode.FAILURE_CODE_5005);
  1007. SceneEditInfo sceneEditInfo = this.getByScenePlusId(scenePlus.getId());
  1008. //转换视频格式
  1009. this.transferToFlv(param.getNum(), param.getFileName());
  1010. //生成boxVideos数据
  1011. String boxVideos = this.createBoxVideos(param.getNum(), sid, boxVideo, sceneEditInfo, OperationType.ADDORUPDATE.code());
  1012. //更新数据库
  1013. this.updateBoxVideos(sceneEditInfo, scenePlus.getId(), boxVideos);
  1014. return ResultData.ok();
  1015. }
  1016. @Override
  1017. public ResultData deleteVideoBox(DeleteSidParamVO param) throws Exception {
  1018. // ScenePro scenePro = sceneProService.findBySceneNum(param.getNum());
  1019. ScenePlus scenePlus = scenePlusService.getScenePlusByNum(param.getNum());
  1020. if(Objects.isNull(scenePlus))
  1021. throw new BusinessException(ErrorCode.FAILURE_CODE_5005);
  1022. SceneEditInfo sceneEditInfo = this.getByScenePlusId(scenePlus.getId());
  1023. //根据sid移除json
  1024. String boxVideos = this.createBoxVideos(param.getNum(), param.getSid(), null, sceneEditInfo, OperationType.DELETE.code());
  1025. //写数据库
  1026. this.updateBoxVideos(sceneEditInfo,scenePlus.getId(),boxVideos);
  1027. return ResultData.ok();
  1028. }
  1029. @Override
  1030. public ResultData saveBoxPhoto(BaseDataParamVO param) throws Exception {
  1031. JSONObject boxPhoto = JSONObject.parseObject(param.getData());
  1032. String sid = boxPhoto.getString("sid");
  1033. if(StrUtil.isEmpty(sid)){
  1034. throw new BusinessException(ServerCode.PARAM_REQUIRED, sid);
  1035. }
  1036. ScenePlus scenePlus = scenePlusService.getScenePlusByNum(param.getNum());
  1037. if(Objects.isNull(scenePlus))
  1038. throw new BusinessException(ErrorCode.FAILURE_CODE_5005);
  1039. SceneEditInfo sceneEditInfo = this.getByScenePlusId(scenePlus.getId());
  1040. //生成boxVideos数据
  1041. String boxPhotos = this.createBoxPhotos(param.getNum(), sid, boxPhoto, sceneEditInfo, OperationType.ADDORUPDATE.code());
  1042. //更新数据库
  1043. this.updateBoxPhotos(sceneEditInfo, boxPhotos);
  1044. return ResultData.ok();
  1045. }
  1046. @Override
  1047. public ResultData deleteBoxPhoto(DeleteSidParamVO param) throws Exception {
  1048. ScenePlus scenePlus = scenePlusService.getScenePlusByNum(param.getNum());
  1049. if(Objects.isNull(scenePlus))
  1050. throw new BusinessException(ErrorCode.FAILURE_CODE_5005);
  1051. SceneEditInfo sceneEditInfo = this.getByScenePlusId(scenePlus.getId());
  1052. //根据sid移除json
  1053. String boxPhotos = this.createBoxPhotos(param.getNum(), param.getSid(), null, sceneEditInfo, OperationType.DELETE.code());
  1054. //写数据库
  1055. this.updateBoxPhotos(sceneEditInfo, boxPhotos);
  1056. return ResultData.ok();
  1057. }
  1058. @Override
  1059. public DownloadVO downloadBallScreenVideo(BallScreenVideoParamVO param) {
  1060. String videoPath = String.format(UploadFilePath.VIDEOS_VIEW_PATH, param.getNum()) + param.getFileName();
  1061. String url = ossUrlPrefix + videoPath + "?t=" + System.currentTimeMillis();
  1062. return DownloadVO.builder()
  1063. .fileName(param.getFileName())
  1064. .url(url)
  1065. .build();
  1066. }
  1067. @Override
  1068. public ResultData uploadBallScreenVideo(String num, String fileName, MultipartFile file) throws Exception {
  1069. //校验格式
  1070. if(!fileName.endsWith(".mp4")){
  1071. throw new BusinessException(ErrorCode.FAILURE_CODE_7016);
  1072. }
  1073. String videosViewPath = String.format(UploadFilePath.VIDEOS_VIEW_PATH, num);
  1074. // ScenePro scenePro = sceneProService.findBySceneNum(num);
  1075. ScenePlus scenePlus = scenePlusService.getScenePlusByNum(num);
  1076. if(scenePlus == null){
  1077. throw new BusinessException(ErrorCode.FAILURE_CODE_5005);
  1078. }
  1079. // SceneProExt sceneProExt = sceneProExtService.findBySceneProId(scenePro.getId());
  1080. ScenePlusExt scenePlusExt = scenePlusExtService.getScenePlusExtByPlusId(scenePlus.getId());
  1081. String path = scenePlusExt.getDataSource();
  1082. //校验文件名
  1083. String videos = scenePlusExt.getVideos();
  1084. if(StrUtil.isEmpty(videos)){
  1085. throw new BusinessException(ErrorCode.FAILURE_CODE_7012);
  1086. }
  1087. JSONObject videosJson = JSON.parseObject(videos);
  1088. JSONArray dataArr = videosJson.getJSONArray("data");
  1089. boolean exists = false;
  1090. for (Object o : dataArr) {
  1091. JSONObject jsonObject = (JSONObject)o;
  1092. if(jsonObject.getString("id").equals(fileName.replace(".mp4", ""))){
  1093. exists = true;
  1094. break;
  1095. }
  1096. }
  1097. if(!exists){
  1098. throw new BusinessException(ErrorCode.FAILURE_CODE_7012);
  1099. }
  1100. if(path != null && !"".equals(path) && path.startsWith("http")){
  1101. path = ConstantFilePath.BUILD_MODEL_PATH + File.separator + path.split("/")[path.split("/").length - 2];
  1102. }
  1103. String target = path + "_images";
  1104. String filePath = target + File.separator + "extras/video" + File.separator + fileName;
  1105. File targetFile = new File(filePath);
  1106. if(!targetFile.getParentFile().exists()){
  1107. targetFile.getParentFile().mkdirs();
  1108. }
  1109. //保存视频到本地
  1110. file.transferTo(targetFile);
  1111. File video = new File(target + File.separator + "extras/video");
  1112. //如果文件夹或者文件不存在,跳出
  1113. if(!video.exists() || video.listFiles() == null || video.listFiles().length == 0){
  1114. throw new BusinessException(ErrorCode.FAILURE_CODE_7009);
  1115. }
  1116. for(String videoName : video.list()){
  1117. log.info("球幕视频名称:{}", videoName);
  1118. uploadToOssUtil.upload(target + File.separator + "extras/video/" + videoName,videosViewPath + videoName);
  1119. CreateObjUtil.mp4ToFlv(target + File.separator + "extras/video/" + videoName,
  1120. target + File.separator + "extras/video/" + videoName.replace("mp4", "flv"));
  1121. uploadToOssUtil.upload(target + File.separator + "extras/video/" + videoName.replace("mp4", "flv"),videosViewPath + videoName.replace("mp4", "flv"));
  1122. //覆盖原始视频资源
  1123. FileUtils.copyFile(target + File.separator + "extras/video/" + videoName,
  1124. path + File.separator + "caches/videos/" + videoName, true);
  1125. }
  1126. FileUtils.deleteDirectory(target + File.separator + "extras/video/");
  1127. SceneEditInfo sceneEditInfo = this.getByScenePlusId(scenePlus.getId());
  1128. this.upgradeVersionById(sceneEditInfo.getId());
  1129. //更新scene.json版本号
  1130. this.upgradeVersionToSceneJson(num);
  1131. return ResultData.ok();
  1132. }
  1133. @Override
  1134. public void upgradeVersionToSceneJson(String num) throws Exception{
  1135. String key = String.format(RedisKey.SCENE_JSON, num);
  1136. String objectName = String.format(ConstantFilePath.SCENE_VIEW_DATA_DATA_SCENEJSON, num);
  1137. String objectContent = uploadToOssUtil.getObjectContent(bucket, objectName);
  1138. SceneJsonBean sceneJsonBean = JSON.parseObject(objectContent, SceneJsonBean.class);
  1139. sceneJsonBean.setVersion(sceneJsonBean.getVersion() +1);
  1140. uploadToOssUtil.upload(JSON.toJSONString(sceneJsonBean).getBytes(), objectName);
  1141. redisUtil.set(key, JSON.toJSONString(sceneJsonBean));
  1142. }
  1143. @Override
  1144. public ResultData sceneSync(String num, String type, String floorPlanJson, String ajkJson, String cameraJson,
  1145. MultipartFile[] files) throws Exception {
  1146. String lockKey = String.format(RedisLockKey.LOCK_SCENE_SYNC, num);
  1147. Boolean lock = redisLockUtil.lock(lockKey, RedisKey.EXPIRE_TIME_2_HOUR);
  1148. if(!lock){
  1149. throw new BusinessException(ErrorCode.SYSTEM_BUSY);
  1150. }
  1151. try {
  1152. // ScenePro scenePro = sceneProService.findBySceneNum(num);
  1153. ScenePlus scenePlus = scenePlusService.getScenePlusByNum(num);
  1154. if(scenePlus == null){
  1155. throw new BusinessException(ErrorCode.FAILURE_CODE_5005);
  1156. }
  1157. // SceneProExt sceneProExt = sceneProExtService.findBySceneProId(scenePro.getId());
  1158. ScenePlusExt scenePlusExt = scenePlusExtService.getScenePlusExtByPlusId(scenePlus.getId());
  1159. //更新scene.json文件
  1160. String strsceneInfos = FileUtils.readFile(ConstantFilePath.SCENE_PATH + "data" + File.separator + "data" + num + File.separator + "scene.json");
  1161. if(strsceneInfos == null)
  1162. new File(ConstantFilePath.SCENE_PATH + "data" + File.separator + "data" + num + File.separator + "scene.json").createNewFile();
  1163. String path = scenePlusExt.getDataSource();// /mnt/data/0662c5389/831989883441512448/4898cab04f8c_202104141602356060/
  1164. if(path != null && !"".equals(path) && path.startsWith("http")){
  1165. path = ConstantFilePath.BUILD_MODEL_PATH + File.separator + path.split("/")[path.split("/").length - 2];
  1166. }
  1167. String target = path + "_ajk"; // /mnt/data/0662c5389/831989883441512448/4898cab04f8c_202104141602356060_ajk
  1168. File editPath = new File(target);
  1169. if(!editPath.exists()){
  1170. editPath.mkdirs();
  1171. }
  1172. //创建文件夹软连接并且复制data.json和project.json
  1173. if(new File(target + File.separator + "capture").exists()){
  1174. new File(target + File.separator + "capture").delete();
  1175. }
  1176. if(new File(target + File.separator + "caches").exists()){
  1177. //删除link
  1178. new File(target + File.separator + "caches" + File.separator + "images").delete();
  1179. //删除所有文件
  1180. FileUtils.delAllFile(target + File.separator + "caches");
  1181. }
  1182. if(new File(target + File.separator + "results").exists()){
  1183. FileUtils.delAllFile(target + File.separator + "results");
  1184. }
  1185. //创建文件夹,并link文件夹
  1186. new File(target + File.separator + "caches").mkdirs();
  1187. CreateObjUtil.createSoftConnection(path + File.separator + "capture", target + File.separator + "capture");
  1188. if(new File(path + File.separator + "caches" + File.separator + "images").exists()){
  1189. CreateObjUtil.createSoftConnection(path + File.separator + "caches" + File.separator + "images", target + File.separator + "caches" + File.separator + "images");
  1190. }
  1191. FileUtils.copyFile(path + File.separator + "data.json", target + File.separator+"data.json", true);
  1192. FileUtils.copyFile(path + File.separator + "project.json", target + File.separator+"project.json", true);
  1193. //data.json增加extras为执行重建算法
  1194. String project = FileUtils.readFile(target + File.separator+"project.json");
  1195. if(project != null){
  1196. JSONObject projectJson = JSONObject.parseObject(project);
  1197. projectJson.put("parent", projectJson.get("uuid"));
  1198. projectJson.put("uuid", UUID.randomUUID().toString());
  1199. projectJson.put("time", System.currentTimeMillis());
  1200. FileUtils.writeFile(path + File.separator + "project.json", projectJson.toString());
  1201. }
  1202. String data = FileUtils.readFile(target + File.separator+"data.json");
  1203. if(data != null){
  1204. JSONObject floorplanJson = new JSONObject();
  1205. floorplanJson.put("has_floor_ajk_json", true);
  1206. floorplanJson.put("has_vision_txt", true);
  1207. floorplanJson.put("has_floorplan_json", true);
  1208. JSONObject dataJson = JSONObject.parseObject(data);
  1209. dataJson.put("extras", floorplanJson);
  1210. //V5表示不需要生成high,low文件
  1211. dataJson.put("skybox_type", "SKYBOX_V8");
  1212. dataJson.put("split_type", "SPLIT_V10");
  1213. FileUtils.writeFile(target + File.separator+"data.json", new String(dataJson.toString().getBytes(), "UTF-8"));
  1214. }
  1215. //文件上传的位置可以自定义
  1216. log.info("画墙重建模型开始");
  1217. File targetFile = new File(target + File.separator + "extras" + File.separator + "floor_ajk.json");
  1218. if(!targetFile.getParentFile().exists()){
  1219. targetFile.getParentFile().mkdirs();
  1220. }
  1221. if(targetFile.exists()){
  1222. FileUtils.deleteFile(target + File.separator + "extras" + File.separator + "floor_ajk.json");
  1223. }
  1224. // 保存
  1225. FileUtils.writeFile(target + File.separator + "extras" + File.separator + "floorplan.json", new String(floorPlanJson.getBytes(), "UTF-8"));
  1226. FileUtils.writeFile(target + File.separator + "extras" + File.separator + "floor_ajk.json", new String(ajkJson.getBytes(), "UTF-8"));
  1227. FileUtils.writeFile(ConstantFilePath.SCENE_PATH+"data"+File.separator+"data"+num + File.separator + "floor_ajk.json", new String(ajkJson.getBytes(), "UTF-8"));
  1228. FileUtils.writeFile(target + File.separator + "extras" + File.separator + "vision.txt", new String(cameraJson.getBytes(), "UTF-8"));
  1229. FileUtils.writeFile(ConstantFilePath.SCENE_PATH+"data"+File.separator+"data"+num + File.separator + "camera.json", new String(cameraJson.getBytes(), "UTF-8"));
  1230. for(int i = 0; i < files.length; i ++){
  1231. File cadImg = new File(target + File.separator + "extras" + File.separator + "Floorplans/" + files[i].getOriginalFilename());
  1232. if(!cadImg.getParentFile().exists()){
  1233. cadImg.getParentFile().mkdirs();
  1234. }
  1235. if(cadImg.exists())
  1236. {
  1237. cadImg.delete();
  1238. }
  1239. files[i].transferTo(cadImg);
  1240. }
  1241. //下载封面图
  1242. FileUtils.downLoadFromUrl(scenePlusExt.getThumb() + "?t=" + System.currentTimeMillis(),
  1243. "Cover.png", target + File.separator + "extras" + File.separator + "CoverImage");
  1244. //转换成jpg
  1245. FileUtils.pngToJpg(target + File.separator + "extras" + File.separator + "CoverImage/Cover.png",
  1246. target + File.separator + "extras" + File.separator + "CoverImage/Cover.jpg");
  1247. //安居客算法运行
  1248. log.info("安居客算法:开始建模——"+num);
  1249. CreateObjUtil.build3dModel(target , "1");
  1250. String uploadJsonPath = target + File.separator + "results" + File.separator + "upload.json";
  1251. log.info("uploadJsonPath=" + uploadJsonPath);
  1252. //因为共享目录有延迟,这里循环检测算法是否计算完毕3次,每次隔五秒
  1253. boolean exist = ComputerUtil.checkComputeCompleted(uploadJsonPath, maxCheckTimes, waitTime);
  1254. if(!exist){
  1255. throw new BusinessException(ErrorCode.FAILURE_CODE_7017);
  1256. }
  1257. String zipPath = target + File.separator + "results/" + num + ".zip";
  1258. new File(zipPath).delete();
  1259. ZipUtil.zip(target + File.separator + "results/ajk/", zipPath);
  1260. // String command = "bash /opt/ossutil/gzip.sh " + zipPath.replace(".zip", "") + " " + target + File.separator + "results/ajk/";
  1261. // log.info("压缩文件:" + command);
  1262. // CreateObjUtil.callshell(command);
  1263. if(!new File(zipPath).exists()){
  1264. return ResultData.error(ErrorCode.FAILURE_CODE_5043);
  1265. }
  1266. String fileMD5 = FileMd5Util.getFileMD5(new File(zipPath));
  1267. uploadToOssUtil.upload(zipPath, "data_download/" + num + ".zip");
  1268. SceneDataDownload sceneDataDownload = sceneDataDownloadService.findBySceneNum(num);
  1269. if(sceneDataDownload == null){
  1270. sceneDataDownload = new SceneDataDownload();
  1271. sceneDataDownload.setNum(num);
  1272. sceneDataDownload.setDownloadPath(ossUrlPrefix + "data_download/" + num + ".zip");
  1273. sceneDataDownload.setFileMd5(fileMD5);
  1274. sceneDataDownloadService.save(sceneDataDownload);
  1275. return ResultData.ok();
  1276. }
  1277. sceneDataDownload.setFileMd5(fileMD5);
  1278. sceneDataDownload.setUpdateTime(Calendar.getInstance().getTime());
  1279. sceneDataDownloadService.updateById(sceneDataDownload);
  1280. }catch (Exception e){
  1281. log.error("画墙重建模型失败...", e);
  1282. throw new BusinessException(ErrorCode.FAILURE_CODE_5039);
  1283. }finally {
  1284. redisLockUtil.unlockLua(lockKey);
  1285. }
  1286. return ResultData.ok();
  1287. }
  1288. @Override
  1289. public ResultData checkKey(SceneCheckKeyParamVO param) throws Exception {
  1290. SceneJsonBean sceneJsonBean = null;
  1291. String sceneJson = redisUtil.get(String.format(RedisKey.SCENE_JSON, param.getNum()));
  1292. if(StrUtil.isEmpty(sceneJson)){
  1293. sceneJson = uploadToOssUtil.getObjectContent(bucket, String.format(UploadFilePath.DATA_VIEW_PATH + "scene.json"));
  1294. }
  1295. sceneJsonBean = JSON.parseObject(sceneJson, SceneJsonBean.class);
  1296. if(!param.getPassword().equals(sceneJsonBean.getScenePassword())){
  1297. throw new BusinessException(ErrorCode.FAILURE_CODE_5021);
  1298. }
  1299. return ResultData.ok();
  1300. }
  1301. @Override
  1302. public ResultData addMosaics(BaseDataParamVO param) throws Exception {
  1303. JSONArray mosaicArr = JSONArray.parseArray(param.getData());
  1304. if (CollUtil.isEmpty(mosaicArr) || mosaicArr.size() > 1) {
  1305. throw new BusinessException(ErrorCode.FAILURE_CODE_5012);
  1306. }
  1307. JSONObject mosicObj= mosaicArr.getJSONObject(0);
  1308. ScenePlus scenePlus = scenePlusService.getScenePlusByNum(param.getNum());
  1309. if(Objects.isNull(scenePlus)){
  1310. throw new BusinessException(ErrorCode.FAILURE_CODE_5005);
  1311. }
  1312. ScenePlusExt scenePlusExt = scenePlusExtService.getScenePlusExtByPlusId(scenePlus.getId());
  1313. SceneEditInfo sceneEditInfo = this.getByScenePlusId(scenePlus.getId());
  1314. SceneEditInfoExt sceneEditInfoExt = sceneEditInfoExtService.getByEditInfoId(sceneEditInfo.getId());
  1315. String dataViewPath = String.format(UploadFilePath.DATA_VIEW_PATH, param.getNum());
  1316. String imgViewPath = String.format(UploadFilePath.IMG_VIEW_PATH, param.getNum());
  1317. String path = scenePlusExt.getDataSource();
  1318. if(StrUtil.isNotEmpty(path) && path.startsWith("http")){
  1319. path = ConstantFilePath.BUILD_MODEL_PATH + File.separator + path.split("/")[path.split("/").length - 2];
  1320. }
  1321. String target = path + "_roi";
  1322. cn.hutool.core.io.FileUtil.del(target);
  1323. //文件上传的位置可以自定义
  1324. String filePath = target + File.separator + "extras" + File.separator + "images/";
  1325. File imageFile = new File(filePath);
  1326. if(!imageFile.getParentFile().exists()){
  1327. imageFile.getParentFile().mkdirs();
  1328. }
  1329. FileUtils.copyFolderAllFiles(path + "/caches/images/", filePath, true);
  1330. String mosaicData = sceneEditInfoExt.getMosaics();
  1331. JSONArray jsonArray = new JSONArray();
  1332. if(StrUtil.isNotEmpty(mosaicData)){
  1333. jsonArray = JSONArray.parseArray(mosaicData);
  1334. }
  1335. jsonArray.add(mosicObj);
  1336. // 准备算法数据
  1337. List<JSONObject> mosaicsList = new ArrayList<>();
  1338. for(int i = 0, len = jsonArray.size(); i < len; i++){
  1339. JSONObject jsonObject = jsonArray.getJSONObject(i);
  1340. boolean contains = false;
  1341. for (JSONObject item : mosaicsList) {
  1342. if(StringUtils.equals(item.getString("panoId"),jsonObject.getString("panoId"))){
  1343. contains = true;
  1344. item.getJSONArray("rect").addAll(jsonObject.getJSONArray("rect"));
  1345. }
  1346. }
  1347. if(!contains){
  1348. mosaicsList.add(jsonObject);
  1349. }
  1350. }
  1351. JSONObject roiImageJSon = new JSONObject();
  1352. roiImageJSon.put("batch",mosaicsList);
  1353. FileUtils.writeFile(target + File.separator + "extras" + File.separator + "image-ROI.json", JSON.toJSONString(roiImageJSon,SerializerFeature.DisableCircularReferenceDetect));
  1354. FileUtils.copyFile(path + File.separator + "results/vision.txt", target + File.separator+"extras/vision.txt", true);
  1355. FileUtils.copyFile(path + File.separator + "results/vision2.txt", target + File.separator+"extras/vision2.txt", true);
  1356. FileUtils.copyFile(path + File.separator + "data.json", target + File.separator+"data.json", true);
  1357. String data = FileUtils.readFile(target + File.separator+"data.json");
  1358. if(data != null){
  1359. JSONObject floorplanJson = new JSONObject();
  1360. floorplanJson.put("has_source_images", true);
  1361. floorplanJson.put("has_vision_txt", true);
  1362. JSONObject dataJson = JSONObject.parseObject(data);
  1363. dataJson.put("extras", floorplanJson);
  1364. //V5表示不需要生成high,low文件
  1365. dataJson.put("skybox_type", "SKYBOX_V6");
  1366. if(scenePlusExt.getSceneScheme() == 11){
  1367. dataJson.put("skybox_type", "SKYBOX_V7");
  1368. }
  1369. dataJson.put("split_type", "SPLIT_V18");
  1370. //sceneScheme为3切成瓦片图
  1371. if(scenePlusExt.getSceneScheme() == 3){
  1372. dataJson.put("skybox_type", "SKYBOX_V4");
  1373. }
  1374. FileUtils.writeFile(target + File.separator+"data.json", new String(dataJson.toString().getBytes(), "UTF-8"));
  1375. }
  1376. FileUtils.copyFile(path + File.separator + "project.json", target + File.separator+"project.json", true);
  1377. //进行计算
  1378. log.info("马赛克建模开始, num:{}, path:{}" + param.getNum(), target);
  1379. CreateObjUtil.build3dModel2(target , "1");
  1380. log.info("马赛克建模结束, num:{}, path:{}" + param.getNum(), target);
  1381. //读取upload文件,检验需要上传的文件是否存在
  1382. String uploadJsonPath = target + File.separator + "results" + File.separator + "upload.json";
  1383. boolean exist = ComputerUtil.checkComputeCompleted(uploadJsonPath, maxCheckTimes, waitTime);
  1384. if(!exist){
  1385. throw new BusinessException(ErrorCode.FAILURE_CODE_5042);
  1386. }
  1387. String uploadData = FileUtils.readFile(target + File.separator + "results" + File.separator + "upload.json");
  1388. JSONObject uploadJson = JSONObject.parseObject(uploadData);
  1389. JSONArray array = uploadJson.getJSONArray("upload");
  1390. Map<String,String> map = new HashMap<>();
  1391. JSONObject fileJson = null;
  1392. String fileName = "";
  1393. for(int i = 0, len = array.size(); i < len; i++){
  1394. fileJson = array.getJSONObject(i);
  1395. fileName = fileJson.getString("file");
  1396. //文件不存在抛出异常
  1397. if(!new File(target + File.separator + "results" +File.separator + fileName).exists()){
  1398. throw new Exception(target + File.separator + "results" +File.separator + fileName+"文件不存在");
  1399. }
  1400. //high文件夹
  1401. if(fileJson.getIntValue("clazz") == 3){
  1402. map.put(target + File.separator + "results" +File.separator+ fileName,
  1403. imgViewPath + "pan/high/"+ fileName.replace("high/", ""));
  1404. }
  1405. //low文件夹
  1406. if(fileJson.getIntValue("clazz") == 4){
  1407. map.put(target + File.separator + "results" +File.separator+ fileName,
  1408. imgViewPath + "pan/low/"+ fileName.replace("low/", ""));
  1409. }
  1410. //tiles文件夹,亚马逊没有裁剪图片api,不需要上传4k图
  1411. if(fileJson.getIntValue("clazz") == 5){
  1412. map.put(target + File.separator + "results" + File.separator+ fileName,
  1413. imgViewPath + fileName);
  1414. }
  1415. //tiles文件夹,亚马逊瓦片图
  1416. if(fileJson.getIntValue("clazz") == 7 ){
  1417. map.put(target + File.separator + "results" + File.separator+ fileName,
  1418. imgViewPath + fileName);
  1419. }
  1420. }
  1421. for(String imagesName : imageFile.list()){
  1422. //覆盖原始图片资源
  1423. FileUtils.copyFile(target + File.separator + "extras/images/" + imagesName,
  1424. path + File.separator + "caches/images/" + imagesName, true);
  1425. FileUtils.deleteFile(target + File.separator + "extras/images/" + imagesName);
  1426. }
  1427. uploadToOssUtil.uploadMulFiles(map);
  1428. this.upgradeVersionById(sceneEditInfo.getId());
  1429. sceneEditInfoExt.setMosaics(JSON.toJSONString(jsonArray,SerializerFeature.DisableCircularReferenceDetect));
  1430. sceneEditInfoExtService.updateById(sceneEditInfoExt);
  1431. // 更新 scene.json
  1432. String sceneJsonPath = dataViewPath + "scene.json";
  1433. String sceneJson = uploadToOssUtil.getObjectContent(this.bucket, sceneJsonPath);
  1434. JSONObject scenejson = new JSONObject();
  1435. if(StrUtil.isNotEmpty(sceneJson)) {
  1436. scenejson = JSONObject.parseObject(sceneJson);
  1437. }
  1438. scenejson.put("mosaics",JSON.toJSONString(jsonArray, SerializerFeature.DisableCircularReferenceDetect));
  1439. Integer version = scenejson.getIntValue("version");
  1440. version = Objects.isNull(version) ? 1 : version + 1;
  1441. scenejson.put("version", version);
  1442. uploadToOssUtil.upload(scenejson.toJSONString().getBytes(), sceneJsonPath);
  1443. Map<String,Object> result = new HashMap<>(1);
  1444. result.put("mosaics",jsonArray);
  1445. return ResultData.ok(result);
  1446. }
  1447. private void updateBoxVideos(SceneEditInfo sceneEditInfo, Long scenePlusId, String boxVideos){
  1448. if(Objects.isNull(sceneEditInfo)){
  1449. sceneEditInfo = new SceneEditInfo();
  1450. sceneEditInfo.setScenePlusId(scenePlusId);
  1451. sceneEditInfo.setBoxVideos(boxVideos);
  1452. this.save(sceneEditInfo);
  1453. }else{
  1454. this.update(new UpdateWrapper<SceneEditInfo>()
  1455. .setSql("version = version + 1")
  1456. .set("box_videos", boxVideos)
  1457. .eq("id", sceneEditInfo.getId()));
  1458. }
  1459. }
  1460. private void updateBoxPhotos(SceneEditInfo sceneEditInfo, String boxPhotos){
  1461. this.update(new LambdaUpdateWrapper<SceneEditInfo>()
  1462. .set(SceneEditInfo::getBoxPhotos, boxPhotos)
  1463. .setSql("version = version + 1")
  1464. .eq(SceneEditInfo::getId, sceneEditInfo.getId()));
  1465. }
  1466. private String createBoxVideos(
  1467. String num, String sid, JSONObject boxVideo,
  1468. SceneEditInfo sceneEditInfo, int type) throws Exception{
  1469. String boxVideos = null;
  1470. if(sceneEditInfo != null){
  1471. boxVideos = sceneEditInfo.getBoxVideos();
  1472. }
  1473. JSONArray boxVideosJson = null;
  1474. if (StrUtil.isNotEmpty(boxVideos)) {
  1475. boxVideosJson = JSONArray.parseArray(boxVideos);
  1476. }else {
  1477. boxVideosJson = new JSONArray();
  1478. }
  1479. String result = null;
  1480. //删除
  1481. if(type == OperationType.DELETE.code()){
  1482. Set<String> deleteFile = new HashSet<>();
  1483. if(boxVideosJson.size() == 0)
  1484. return null;
  1485. for(int i=0;i<boxVideosJson.size();++i){
  1486. JSONObject ele = boxVideosJson.getJSONObject(i);
  1487. if(ele.getString("sid").equals(sid)){
  1488. String poster = ele.getString("poster");
  1489. if(StrUtil.isNotEmpty(poster))
  1490. deleteFile.add(poster);
  1491. String url = ele.getString("url");
  1492. if(StrUtil.isNotEmpty(url)){
  1493. deleteFile.add(url);
  1494. deleteFile.add(url.replace(".mp4",".flv"));
  1495. }
  1496. boxVideosJson.remove(i);
  1497. }
  1498. }
  1499. //删除资源文件
  1500. if(CollUtil.isNotEmpty(deleteFile))
  1501. sceneUploadService.delete(
  1502. DeleteFileParamVO.builder().num(num)
  1503. .bizType(FileBizType.BOX_VIDEO.code())
  1504. .fileNames(new ArrayList<>(deleteFile)).build());
  1505. }else{
  1506. // TODO: 2022/3/1 目前需求只保留一个,所以这里先注释掉,sid不同时,不追加
  1507. //更新
  1508. // boolean exist = false;
  1509. // for(int i=0;i<boxVideosJson.size();++i){
  1510. // JSONObject ele = boxVideosJson.getJSONObject(i);
  1511. // if(ele.getString("sid").equals(sid)){
  1512. // boxVideosJson.set(i, boxVideo);
  1513. // exist = true;
  1514. // }
  1515. // }
  1516. // //新增
  1517. // if(!exist){
  1518. // boxVideosJson.add(boxVideo);
  1519. // }
  1520. boxVideosJson.clear();
  1521. boxVideosJson.add(boxVideo);
  1522. }
  1523. if(boxVideosJson.size() != 0){
  1524. result = boxVideosJson.toJSONString();
  1525. }
  1526. return result;
  1527. }
  1528. private String createBoxPhotos(String num, String sid, JSONObject boxPhoto, SceneEditInfo sceneEditInfo, int type) throws Exception{
  1529. String boxPhotos = null;
  1530. if(sceneEditInfo != null){
  1531. boxPhotos = sceneEditInfo.getBoxPhotos();
  1532. }
  1533. JSONArray boxPhotosJson = null;
  1534. if (StrUtil.isNotEmpty(boxPhotos)) {
  1535. boxPhotosJson = JSONArray.parseArray(boxPhotos);
  1536. }else {
  1537. boxPhotosJson = new JSONArray();
  1538. }
  1539. String result = null;
  1540. //删除
  1541. if(type == OperationType.DELETE.code()){
  1542. Set<String> deleteFile = new HashSet<>();
  1543. if(boxPhotosJson.size() == 0)
  1544. return null;
  1545. for(int i=0;i<boxPhotosJson.size();++i){
  1546. JSONObject ele = boxPhotosJson.getJSONObject(i);
  1547. if(ele.getString("sid").equals(sid)){
  1548. String poster = ele.getString("poster");
  1549. if(StrUtil.isNotEmpty(poster))
  1550. deleteFile.add(poster);
  1551. String url = ele.getString("url");
  1552. if(StrUtil.isNotEmpty(url))
  1553. deleteFile.add(url);
  1554. boxPhotosJson.remove(i);
  1555. }
  1556. }
  1557. //删除资源文件
  1558. if(CollUtil.isNotEmpty(deleteFile))
  1559. sceneUploadService.delete(
  1560. DeleteFileParamVO.builder().num(num)
  1561. .bizType(FileBizType.BOX_VIDEO.code())
  1562. .fileNames(new ArrayList<>(deleteFile)).build());
  1563. }else{
  1564. //更新
  1565. boolean exist = false;
  1566. for(int i=0;i<boxPhotosJson.size();++i){
  1567. JSONObject ele = boxPhotosJson.getJSONObject(i);
  1568. if(ele.getString("sid").equals(sid)){
  1569. boxPhoto.put("createTime", ele.getLong("createTime"));
  1570. boxPhotosJson.set(i, boxPhoto);
  1571. exist = true;
  1572. }
  1573. }
  1574. //新增
  1575. if(!exist){
  1576. boxPhoto.put("createTime", Calendar.getInstance().getTimeInMillis());
  1577. boxPhotosJson.add(boxPhoto);
  1578. }
  1579. }
  1580. if(boxPhotosJson.size() != 0){
  1581. List<BoxPhotoBean> list = Lists.newArrayList();
  1582. for (Object o : boxPhotosJson) {
  1583. JSONObject jsonObject = (JSONObject)o;
  1584. list.add(BoxPhotoBean.builder().createTime(jsonObject.getLong("createTime")).boxPhoto(jsonObject).build());
  1585. }
  1586. //按创建时间倒叙排序
  1587. list.sort(Comparator.comparingLong(BoxPhotoBean::getCreateTime).reversed());
  1588. // list转JSONArray
  1589. JSONArray array = new JSONArray();
  1590. list.stream().forEach(bean->{
  1591. array.add(bean.getBoxPhoto());
  1592. });
  1593. result = array.toJSONString();
  1594. }
  1595. return result;
  1596. }
  1597. private void transferToFlv(String num, String fileName) throws Exception {
  1598. String userEditPath = String.format(UploadFilePath.USER_EDIT_PATH, num);
  1599. String localImagesPath = String.format(ConstantFilePath.SCENE_USER_PATH_V4, num);
  1600. String localFilePath = localImagesPath + fileName;
  1601. File targetFile = new File(localImagesPath);
  1602. if (!targetFile.exists()){
  1603. targetFile.mkdirs();
  1604. }
  1605. targetFile = new File(localFilePath);
  1606. if (targetFile.exists()){
  1607. FileUtils.deleteFile(localFilePath);
  1608. }
  1609. //从用户编辑目录中下载视频到本地
  1610. if (!StorageType.LOCAL.code().equals(this.type)) {
  1611. String filePath = userEditPath + fileName;
  1612. String imageUrl = ossUrlPrefix + filePath + "?t=" + System.currentTimeMillis();
  1613. FileUtils.downLoadFromUrl(imageUrl, fileName, localImagesPath);
  1614. }
  1615. //视频格式转换
  1616. CreateObjUtil.mp4ToFlv(localFilePath, localFilePath.replace("mp4", "flv"));
  1617. //上传
  1618. String flvFileName = fileName.replace("mp4", "flv");
  1619. // uploadToOssUtil.upload(localFilePath, userEditPath + fileName);
  1620. uploadToOssUtil.upload(localFilePath.replace("mp4", "flv"), userEditPath+flvFileName);
  1621. }
  1622. }