SceneProServiceImpl.java 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126
  1. package com.fdkankan.scene.service.impl;
  2. import cn.hutool.core.collection.CollUtil;
  3. import cn.hutool.core.io.FileUtil;
  4. import cn.hutool.core.util.RuntimeUtil;
  5. import cn.hutool.core.util.StrUtil;
  6. import cn.hutool.core.util.ZipUtil;
  7. import com.alibaba.fastjson.JSON;
  8. import com.alibaba.fastjson.JSONArray;
  9. import com.alibaba.fastjson.JSONObject;
  10. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  11. import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
  12. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  13. import com.fdkankan.common.constant.*;
  14. import com.fdkankan.model.constants.ConstantFileName;
  15. import com.fdkankan.model.constants.ConstantFilePath;
  16. import com.fdkankan.model.constants.UploadFilePath;
  17. import com.fdkankan.common.exception.BusinessException;
  18. import com.fdkankan.web.response.ResultData;
  19. import com.fdkankan.model.utils.ComputerUtil;
  20. import com.fdkankan.model.utils.ConvertUtils;
  21. import com.fdkankan.model.utils.CreateObjUtil;
  22. import com.fdkankan.common.util.FileUtils;
  23. import com.fdkankan.common.util.OkHttpUtils;
  24. import com.fdkankan.fyun.constant.FYunTypeEnum;
  25. import com.fdkankan.fyun.face.FYunFileServiceInterface;
  26. import com.fdkankan.redis.constant.RedisKey;
  27. import com.fdkankan.redis.constant.RedisLockKey;
  28. import com.fdkankan.redis.util.RedisLockUtil;
  29. import com.fdkankan.redis.util.RedisUtil;
  30. import com.fdkankan.scene.bean.IconBean;
  31. import com.fdkankan.scene.bean.TagBean;
  32. import com.fdkankan.scene.entity.SceneEditInfo;
  33. import com.fdkankan.scene.entity.ScenePlus;
  34. import com.fdkankan.scene.entity.ScenePlusExt;
  35. import com.fdkankan.scene.entity.ScenePro;
  36. import com.fdkankan.scene.mapper.ISceneProMapper;
  37. import com.fdkankan.scene.service.ISceneDataDownloadService;
  38. import com.fdkankan.scene.service.ISceneEditControlsService;
  39. import com.fdkankan.scene.service.ISceneEditInfoService;
  40. import com.fdkankan.scene.service.IScenePlusExtService;
  41. import com.fdkankan.scene.service.IScenePlusService;
  42. import com.fdkankan.scene.service.ISceneProService;
  43. import com.fdkankan.scene.service.ISceneUploadService;
  44. import com.fdkankan.scene.vo.BaseDataParamVO;
  45. import com.fdkankan.scene.vo.DeleteFileParamVO;
  46. import com.fdkankan.scene.vo.DeleteHotIconParamVO;
  47. import com.fdkankan.scene.vo.DeleteHotParamVO;
  48. import com.fdkankan.scene.vo.FileNameAndDataParamVO;
  49. import com.fdkankan.scene.vo.HotParamVO;
  50. import com.fdkankan.scene.vo.SaveTagsParamVO;
  51. import com.fdkankan.scene.vo.SaveTagsVisibleParamVO;
  52. import com.google.common.collect.Lists;
  53. import com.google.common.collect.Sets;
  54. import java.io.File;
  55. import java.io.IOException;
  56. import java.nio.charset.StandardCharsets;
  57. import java.util.*;
  58. import java.util.Map.Entry;
  59. import java.util.stream.Collectors;
  60. import lombok.extern.slf4j.Slf4j;
  61. import org.redisson.Redisson;
  62. import org.redisson.RedissonLock;
  63. import org.springframework.beans.factory.annotation.Autowired;
  64. import org.springframework.beans.factory.annotation.Qualifier;
  65. import org.springframework.beans.factory.annotation.Value;
  66. import org.springframework.stereotype.Service;
  67. import org.springframework.transaction.annotation.Transactional;
  68. import org.springframework.web.multipart.MultipartFile;
  69. /**
  70. * <p>
  71. * pro场景表 服务实现类
  72. * </p>
  73. *
  74. * @author dengsixing
  75. * @since 2021-12-23
  76. */
  77. @Slf4j
  78. @Service
  79. public class SceneProServiceImpl extends ServiceImpl<ISceneProMapper, ScenePro> implements ISceneProService {
  80. @Value("${fyun.host}")
  81. private String ossUrlPrefix;
  82. @Value("${fyun.type}")
  83. private String fyunType;
  84. @Value("${main.url}")
  85. private String mainUrl;
  86. @Value("${scene.url}")
  87. private String sceneUrl;
  88. @Value("${scene.pro.url}")
  89. private String sceneProUrl;
  90. @Value("${scene.pro.new.url}")
  91. private String sceneProNewUrl;
  92. @Value("${ecs.checkFile.maxTimes:5}")
  93. private int maxCheckTimes;
  94. @Value("${ecs.checkFile.waitTime:5000}")
  95. private int waitTime;
  96. @Autowired
  97. private FYunFileServiceInterface fYunFileService;
  98. @Autowired
  99. private RedisLockUtil redisLockUtil;
  100. @Autowired
  101. private RedisUtil redisUtil;
  102. @Autowired
  103. private ISceneDataDownloadService sceneDataDownloadService;
  104. @Autowired
  105. private ISceneProService sceneProService;
  106. @Autowired
  107. private ISceneEditInfoService sceneEditInfoService;
  108. @Autowired
  109. private ISceneEditControlsService sceneEditControlsService;
  110. @Autowired
  111. private IScenePlusService scenePlusService;
  112. @Autowired
  113. private IScenePlusExtService scenePlusExtService;
  114. @Autowired
  115. private ISceneUploadService sceneUploadService;
  116. @Transactional
  117. @Override
  118. public ResultData saveInitialPage(FileNameAndDataParamVO param) throws Exception{
  119. ScenePlus scenePlus = scenePlusService.getScenePlusByNum(param.getNum());
  120. if(scenePlus == null){
  121. throw new BusinessException(ErrorCode.FAILURE_CODE_5005);
  122. }
  123. ScenePlusExt scenePlusExt = scenePlusExtService.getScenePlusExtByPlusId(scenePlus.getId());
  124. //更新缩略图url
  125. String thumbUrl = this.ossUrlPrefix + String.format(UploadFilePath.USER_EDIT_PATH, param.getNum()) + param.getFileName();
  126. scenePlusExt.setThumb(thumbUrl);
  127. scenePlusExtService.updateById(scenePlusExt);
  128. SceneEditInfo sceneEditInfo = sceneEditInfoService.getByScenePlusId(scenePlus.getId());
  129. if(sceneEditInfo == null){
  130. sceneEditInfo = new SceneEditInfo();
  131. sceneEditInfo.setScenePlusId(scenePlus.getId());
  132. sceneEditInfo.setEntry(param.getData());
  133. sceneEditInfoService.save(sceneEditInfo);
  134. }else{
  135. sceneEditInfoService.update(
  136. new LambdaUpdateWrapper<SceneEditInfo>()
  137. .set(SceneEditInfo::getEntry, param.getData())
  138. .setSql("version = version + 1")
  139. .eq(SceneEditInfo::getId, sceneEditInfo.getId()));
  140. }
  141. return ResultData.ok();
  142. }
  143. @Override
  144. public ResultData addOrUpdateTag(SaveTagsParamVO param) throws Exception {
  145. // ScenePro scenePro = this.findBySceneNum(param.getNum());
  146. ScenePlus scenePlus = scenePlusService.getScenePlusByNum(param.getNum());
  147. if (scenePlus == null)
  148. return ResultData.error(ErrorCode.FAILURE_CODE_5005);
  149. this.addOrUpdateHotData(param.getNum(), param.getHotDataList());
  150. this.addOrUpdateIcons(param.getNum(), param.getIcons());
  151. //写入本地文件,作为备份
  152. this.writeHotJson(param.getNum());
  153. //保存数据库
  154. SceneEditInfo sceneEditInfo = sceneEditInfoService.getByScenePlusId(scenePlus.getId());
  155. sceneEditInfoService.saveTagsToSceneEditInfo(param.getNum(), sceneEditInfo);
  156. sceneEditInfoService.updateById(sceneEditInfo);
  157. return ResultData.ok();
  158. }
  159. private void addOrUpdateHotData(String num, List<HotParamVO> hotDataList) throws Exception{
  160. Map<String, String> addOrUpdateMap = new HashMap<>();
  161. int i = 0;
  162. for (HotParamVO hotParamVO : hotDataList) {
  163. JSONObject jsonObject = JSON.parseObject(hotParamVO.getHotData());
  164. jsonObject.put("createTime", Calendar.getInstance().getTimeInMillis() + i++);
  165. addOrUpdateMap.put(hotParamVO.getSid(), jsonObject.toJSONString());
  166. }
  167. this.syncHotFromFileToRedis(num);
  168. //处理新增和修改数据
  169. this.addOrUpdateHotDataHandler(num, addOrUpdateMap);
  170. }
  171. private void addOrUpdateIcons(String num, List<String> icons) throws Exception{
  172. if(CollUtil.isEmpty(icons)){
  173. return;
  174. }
  175. this.syncIconsFromFileToRedis(num);
  176. String key = String.format(RedisKey.SCENE_HOT_ICONS, num);
  177. redisUtil.sSet(key, icons.toArray());
  178. }
  179. @Override
  180. public ResultData deleteTag(DeleteHotParamVO param) throws Exception {
  181. // ScenePro scenePro = this.findBySceneNum(param.getNum());
  182. ScenePlus scenePlus = scenePlusService.getScenePlusByNum(param.getNum());
  183. if (scenePlus == null)
  184. throw new BusinessException(ErrorCode.FAILURE_CODE_5005);
  185. ScenePlusExt scenePlusExt = scenePlusExtService.getScenePlusExtByPlusId(scenePlus.getId());
  186. String bucket = scenePlusExt.getYunFileBucket();
  187. List<String> deleteSidList = param.getSidList();
  188. //处理删除状态数据
  189. this.deleteHotData(param.getNum(), deleteSidList, bucket);
  190. //删除导览中的热点数据
  191. this.deleteHotDataFromTourJson(param.getNum(), param.getSidList(), bucket);
  192. //写入本地文件,作为备份
  193. this.writeHotJson(param.getNum());
  194. //保存数据库
  195. SceneEditInfo sceneEditInfo = sceneEditInfoService.getByScenePlusId(scenePlus.getId());
  196. sceneEditInfoService.saveTagsToSceneEditInfo(param.getNum(), sceneEditInfo);
  197. sceneEditInfoService.updateById(sceneEditInfo);
  198. return ResultData.ok();
  199. }
  200. private void deleteHotDataFromTourJson(String num, List<String> sidList, String bucket){
  201. String key = String.format(UploadFilePath.USER_EDIT_PATH, num) + "tour.json";
  202. String tourJson = fYunFileService.getFileContent(bucket, key);
  203. if(StrUtil.isEmpty(tourJson)){
  204. return;
  205. }
  206. JSONArray jsonArray = JSON.parseArray(tourJson);
  207. if(CollUtil.isEmpty(jsonArray)){
  208. return;
  209. }
  210. jsonArray.stream().forEach(tour->{
  211. JSONObject obj = (JSONObject) tour;
  212. JSONArray itemArra = obj.getJSONArray("list");
  213. itemArra.stream().forEach(item->{
  214. JSONObject itemObj = (JSONObject) item;
  215. String tagId = itemObj.getString("tagId");
  216. if(tagId != null && sidList.contains(tagId)){
  217. itemObj.remove("tagId");
  218. }
  219. });
  220. });
  221. fYunFileService.uploadFile(bucket, jsonArray.toJSONString().getBytes(StandardCharsets.UTF_8), key);
  222. }
  223. @Override
  224. public ResultData deleteIcons(DeleteHotIconParamVO param) throws Exception {
  225. ScenePlus scenePlus = scenePlusService.getScenePlusByNum(param.getNum());
  226. if (scenePlus == null)
  227. throw new BusinessException(ErrorCode.FAILURE_CODE_5005);
  228. List<String> fileNameList = param.getFileNameList();
  229. this.syncIconsFromFileToRedis(param.getNum());
  230. String key = String.format(RedisKey.SCENE_HOT_ICONS, param.getNum());
  231. redisUtil.setRemove(key, fileNameList.toArray());
  232. //写入本地文件,作为备份
  233. this.writeHotJson(param.getNum());
  234. //删除oss文件
  235. sceneUploadService.delete(
  236. DeleteFileParamVO.builder()
  237. .num(param.getNum())
  238. .fileNames(fileNameList)
  239. .bizType(FileBizType.TAG_ICON.code()).build());
  240. return ResultData.ok();
  241. }
  242. @Override
  243. public ResultData listTags(String num) throws Exception{
  244. //保证热点数据安全性,当redis宕机导致热点数据丢失时,可以从文件中读取,恢复到redis
  245. this.syncHotFromFileToRedis(num);
  246. //保证icons数据安全性,当redis宕机导致icons数据丢失时,可以从文件中读取,恢复到redis
  247. this.syncIconsFromFileToRedis(num);
  248. JSONObject result = new JSONObject();
  249. //查询缓存是否包含热点数据
  250. String key = String.format(RedisKey.SCENE_HOT_DATA, num);
  251. Map<String, String> allTagsMap = redisUtil.hmget(key);
  252. List<JSONObject> tags = Lists.newArrayList();
  253. List<TagBean> tagBeanList = new ArrayList<>();
  254. if(CollUtil.isNotEmpty(allTagsMap)){
  255. allTagsMap.entrySet().stream().forEach(entry -> {
  256. JSONObject jsonObject = JSON.parseObject(entry.getValue());
  257. tagBeanList.add(
  258. TagBean.builder()
  259. .createTime(jsonObject.getLong("createTime"))
  260. .tag(jsonObject).build());
  261. });
  262. //按创建时间倒叙排序
  263. tagBeanList.sort(Comparator.comparingLong(TagBean::getCreateTime).reversed());
  264. //移除createTime字段
  265. tags = tagBeanList.stream().map(tagBean -> {
  266. JSONObject tag = tagBean.getTag();
  267. tag.remove("createTime");
  268. return tag;
  269. }).collect(Collectors.toList());
  270. }
  271. result.put("tags", tags);
  272. //查询缓存是否包含icons
  273. key = String.format(RedisKey.SCENE_HOT_ICONS, num);
  274. Set<String> icons = redisUtil.sGet(key);
  275. if(icons == null){
  276. icons = Sets.newHashSet();
  277. }
  278. List<String> iconList = this.sortIcons(tags, icons);
  279. result.put("icons", iconList);
  280. return ResultData.ok(result);
  281. }
  282. private List<String> sortIcons(List<JSONObject> tags, Set<String> icons){
  283. //统计使用频次
  284. List<IconBean> iconBeans = Lists.newArrayList();
  285. for (String icon : icons) {
  286. int count = 0;
  287. for (JSONObject tag : tags) {
  288. String sid = tag.getString("icon");
  289. if(StrUtil.isEmpty(sid) || !icon.equals(sid)){
  290. continue;
  291. }
  292. ++count;
  293. }
  294. iconBeans.add(IconBean.builder().icon(icon).count(count).build());
  295. }
  296. //排序
  297. List<String> iconList = iconBeans.stream().sorted(Comparator.comparing(IconBean::getCount).reversed())
  298. .map(item -> {
  299. return item.getIcon();
  300. }).collect(Collectors.toList());
  301. return iconList;
  302. }
  303. /**
  304. * <p>
  305. 保证热点数据安全性,当redis宕机导致热点数据丢失时,可以从文件中读取,恢复到redis
  306. * </p>
  307. * @author dengsixing
  308. * @date 2022/3/3
  309. **/
  310. private void syncHotFromFileToRedis(String num) throws Exception{
  311. String key = String.format(RedisKey.SCENE_HOT_DATA, num);
  312. boolean exist = redisUtil.hasKey(key);
  313. if(exist){
  314. return;
  315. }
  316. String lockKey = String.format(RedisLockKey.LOCK_HOT_DATA_SYNC, num);
  317. String lockVal = cn.hutool.core.lang.UUID.randomUUID().toString();
  318. boolean lock = redisLockUtil.lock(lockKey, lockVal, RedisKey.EXPIRE_TIME_1_MINUTE);
  319. if(!lock){
  320. throw new BusinessException(ErrorCode.SYSTEM_BUSY);
  321. }
  322. try{
  323. exist = redisUtil.hasKey(key);
  324. if(exist){
  325. return;
  326. }
  327. String tagsFilePath = String.format(ConstantFilePath.SCENE_USER_PATH_V4, num) + "hot.json";
  328. String tagsData = FileUtils.readUtf8String(tagsFilePath);
  329. if(StrUtil.isEmpty(tagsData)){
  330. return;
  331. }
  332. JSONObject jsonObject = JSON.parseObject(tagsData);
  333. JSONArray tagsArr = jsonObject.getJSONArray("tags");
  334. if(CollUtil.isEmpty(tagsArr)){
  335. return;
  336. }
  337. Map<String, String> map = new HashMap<>();
  338. for (Object o : tagsArr) {
  339. JSONObject jo = (JSONObject)o;
  340. map.put(jo.getString("sid"), jo.toJSONString());
  341. }
  342. redisUtil.hmset(key, map);
  343. }finally {
  344. redisLockUtil.unlockLua(lockKey, lockVal);
  345. }
  346. }
  347. /**
  348. * <p>
  349. 保证icons数据安全性,当redis宕机导致icons数据丢失时,可以从文件中读取,恢复到redis
  350. * </p>
  351. * @author dengsixing
  352. * @date 2022/3/3
  353. **/
  354. private void syncIconsFromFileToRedis(String num) throws Exception{
  355. String key = String.format(RedisKey.SCENE_HOT_ICONS, num);
  356. boolean exist = redisUtil.hasKey(key);
  357. if(exist){
  358. return;
  359. }
  360. String lockKey = String.format(RedisLockKey.LOCK_HOT_ICONS_SYNC, num);
  361. String lockVal = cn.hutool.core.lang.UUID.randomUUID().toString();
  362. boolean lock = redisLockUtil.lock(lockKey, lockVal, RedisKey.EXPIRE_TIME_1_MINUTE);
  363. if(!lock){
  364. throw new BusinessException(ErrorCode.SYSTEM_BUSY);
  365. }
  366. try{
  367. exist = redisUtil.hasKey(key);
  368. if(exist){
  369. return;
  370. }
  371. String tagsFilePath = String.format(ConstantFilePath.SCENE_USER_PATH_V4, num) + "hot.json";
  372. String tagsData = FileUtils.readUtf8String(tagsFilePath);
  373. if(StrUtil.isEmpty(tagsData)){
  374. return;
  375. }
  376. JSONObject jsonObject = JSON.parseObject(tagsData);
  377. JSONArray iconArr = jsonObject.getJSONArray("icons");
  378. if(CollUtil.isEmpty(iconArr)){
  379. return;
  380. }
  381. redisUtil.sSet(key, iconArr.toJavaList(String.class).toArray());
  382. }finally {
  383. redisLockUtil.unlockLua(lockKey, lockVal);
  384. }
  385. }
  386. /**
  387. * <p>
  388. 热点数据保存
  389. * </p>
  390. * @author dengsixing
  391. * @date 2022/3/3
  392. **/
  393. private void writeHotJson(String num) throws Exception{
  394. String dataKey = String.format(RedisKey.SCENE_HOT_DATA, num);
  395. Map<String, String> tagMap = redisUtil.hmget(dataKey);
  396. List<String> tagList = Lists.newArrayList();
  397. tagMap.entrySet().stream().forEach(entry->{
  398. if(StrUtil.isNotEmpty(entry.getValue())){
  399. tagList.add(entry.getValue());
  400. }
  401. });
  402. JSONObject jsonObject = new JSONObject();
  403. JSONArray tagJsonArr = new JSONArray();
  404. if(CollUtil.isNotEmpty(tagList)){
  405. tagList.stream().forEach(hot->{
  406. tagJsonArr.add(JSONObject.parseObject(hot));
  407. });
  408. }
  409. jsonObject.put("tags", tagJsonArr);
  410. String iconsKey = String.format(RedisKey.SCENE_HOT_ICONS, num);
  411. Set<String> iconList = redisUtil.sGet(iconsKey);
  412. jsonObject.put("icons", iconList);
  413. String hotJsonPath = String.format(ConstantFilePath.SCENE_USER_PATH_V4, num) + "hot.json";
  414. String lockKey = String.format(RedisLockKey.LOCK_HOT_JSON, num);
  415. String lockVal = cn.hutool.core.lang.UUID.randomUUID().toString();
  416. boolean lock = redisLockUtil.lock(lockKey, lockVal, RedisKey.EXPIRE_TIME_1_MINUTE);
  417. if(!lock){
  418. return;
  419. }
  420. try{
  421. FileUtils.writeFile(hotJsonPath, jsonObject.toJSONString());
  422. }finally {
  423. redisLockUtil.unlockLua(lockKey, lockVal);
  424. }
  425. }
  426. private void addOrUpdateHotDataHandler(String num, Map<String, String> addOrUpdateMap){
  427. if(CollUtil.isEmpty(addOrUpdateMap))
  428. return;
  429. //数据验证,新增、修改状态,hotdata不能为空
  430. for (String sid : addOrUpdateMap.keySet()) {
  431. String hotData = addOrUpdateMap.get(sid);
  432. if(StrUtil.isEmpty(hotData)){
  433. throw new BusinessException(ErrorCode.FAILURE_CODE_7004);
  434. }
  435. }
  436. //批量写入缓存
  437. String key = String.format(RedisKey.SCENE_HOT_DATA, num);
  438. redisUtil.hmset(key, addOrUpdateMap);
  439. }
  440. private void deleteHotData(String num, List<String> deleteSidList, String bucket) throws Exception {
  441. this.syncHotFromFileToRedis(num);
  442. if(CollUtil.isEmpty(deleteSidList)){
  443. return;
  444. }
  445. //从redis中加载热点数据
  446. String key = String.format(RedisKey.SCENE_HOT_DATA, num);
  447. List<String> deletDataList = redisUtil.hMultiGet(key, deleteSidList);
  448. if(CollUtil.isEmpty(deletDataList))
  449. return;
  450. String userDataPath = String.format(UploadFilePath.USER_EDIT_PATH, num);
  451. //删除图片音频视频等资源文件
  452. for (String data : deletDataList) {
  453. if(StrUtil.isBlank(data)){
  454. continue;
  455. }
  456. JSONObject jsonObject = JSON.parseObject(data);
  457. String sid = jsonObject.getString("sid");
  458. if(jsonObject.containsKey("media")){
  459. String fileType = jsonObject.getString("media");
  460. if(fileType.contains("photo"))
  461. {
  462. fYunFileService.deleteFile(bucket,userDataPath + "hot"+sid+".jpg");
  463. }
  464. if(fileType.contains("audio") || fileType.contains("voice"))
  465. {
  466. fYunFileService.deleteFile(bucket,userDataPath + "hot"+sid+".mp3");
  467. }
  468. if(fileType.contains("video"))
  469. {
  470. fYunFileService.deleteFile(bucket,userDataPath + "hot"+sid+".mp4");
  471. }
  472. }
  473. }
  474. //从redis中移除热点数据
  475. redisUtil.hdel(key, deleteSidList.toArray());
  476. }
  477. @Override
  478. public ResultData saveTagsVisible(SaveTagsVisibleParamVO param) throws Exception {
  479. ScenePlus scenePlus = scenePlusService.getScenePlusByNum(param.getNum());
  480. if (scenePlus == null ) {
  481. return ResultData.error(ErrorCode.FAILURE_CODE_5005);
  482. }
  483. JSONArray visiblePanos = JSONArray.parseArray(param.getData());
  484. //如果redis找不到,就从本地文件中reload
  485. this.syncHotFromFileToRedis(param.getNum());
  486. //从缓存中获取热点数据,如果为空,抛出异常
  487. String key = String.format(RedisKey.SCENE_HOT_DATA, param.getNum());
  488. Map<String, String> map = redisUtil.hmget(key);
  489. if (CollUtil.isEmpty(map)) {
  490. throw new BusinessException(ErrorCode.FAILURE_CODE_7005);
  491. }
  492. List<Entry<String, String>> allTags = map.entrySet().stream().filter(item -> {
  493. if (StrUtil.isBlank(item.getValue())) {
  494. return false;
  495. }
  496. return true;
  497. }).collect(Collectors.toList());
  498. if (CollUtil.isEmpty(allTags)) {
  499. throw new BusinessException(ErrorCode.FAILURE_CODE_7005);
  500. }
  501. allTags.stream().forEach(entry->{
  502. JSONObject hot = JSON.parseObject(entry.getValue());
  503. visiblePanos.stream().forEach(item->{
  504. if (hot.getString("sid").equals(((JSONObject) item).getString("sid"))) {
  505. hot.put("visiblePanos", ((JSONObject) item).getJSONArray("value"));
  506. hot.put("isHidden", ((JSONObject) item).getBoolean("isHidden"));
  507. entry.setValue(hot.toJSONString());
  508. }
  509. });
  510. });
  511. //更新版本号
  512. SceneEditInfo editInfo = sceneEditInfoService.getByScenePlusId(scenePlus.getId());
  513. sceneEditInfoService.upgradeVersionById(editInfo.getId());
  514. //放入缓存
  515. Map<String, String> finalMap = new HashMap<>();
  516. allTags.stream().forEach(entry->{
  517. finalMap.put(entry.getKey(), entry.getValue());
  518. });
  519. redisUtil.hmset(key, finalMap);
  520. //写入本地文件,作为备份,以防redis数据丢失
  521. this.writeHotJson(param.getNum());
  522. return ResultData.ok();
  523. }
  524. @Override
  525. public ResultData saveRoam(BaseDataParamVO param) throws Exception {
  526. // ScenePro scenePro = this.findBySceneNum(param.getNum());
  527. ScenePlus scenePlus = scenePlusService.getScenePlusByNum(param.getNum());
  528. if (scenePlus == null ) {
  529. return ResultData.error(ErrorCode.FAILURE_CODE_5005);
  530. }
  531. ScenePlusExt scenePlusExt = scenePlusExtService.getScenePlusExtByPlusId(scenePlus.getId());
  532. String bucket = scenePlusExt.getYunFileBucket();
  533. JSONArray inputData = JSONObject.parseArray(param.getData());
  534. String localDataPath = String.format(ConstantFilePath.SCENE_DATA_PATH_V4, param.getNum());
  535. File directory = new File(localDataPath);
  536. if (!directory.exists()) {
  537. directory.mkdirs();
  538. }
  539. String viewImagesPath = String.format(UploadFilePath.IMG_VIEW_PATH, param.getNum());
  540. String modeldataUrl = ossUrlPrefix + viewImagesPath + "vision.modeldata?t=" + System.currentTimeMillis();
  541. //如果是云存储,将vision.modeldata下载到本地,如果是本地存储,场景计算完就已经将这个文件拷贝到编辑目录了存在这个文件了,不需要再下载
  542. fYunFileService.downloadFile(bucket, viewImagesPath + "vision.modeldata", localDataPath + "vision.modeldata");
  543. //检查vision.modeldata本地是否存在,不存在抛出异常
  544. File file = new File(localDataPath + "vision.modeldata");
  545. if(!file.exists()) {
  546. return ResultData.error(ErrorCode.FAILURE_CODE_5012);
  547. }
  548. //将vision.modeldata解压缩至vision.json
  549. ConvertUtils.convertVisionModelDataToTxt(localDataPath + "vision.modeldata", localDataPath + "vision.json");
  550. String str = FileUtils.readFile(localDataPath + "vision.json");
  551. JSONObject json = JSONObject.parseObject(str);
  552. JSONArray panos = json.getJSONArray("sweepLocations");
  553. for (int i = 0; i < panos.size(); ++i) {
  554. JSONObject pano = panos.getJSONObject(i);
  555. for (int j = 0; j < inputData.size(); ++j) {
  556. JSONObject jo = inputData.getJSONObject(j);
  557. String currentPanoId = jo.getString("panoID");
  558. JSONArray visibles = jo.getJSONArray("visibles");
  559. JSONArray visibles3 = jo.getJSONArray("visibles3");
  560. if (pano.getString("uuid").equals(currentPanoId)) {
  561. pano.put("visibles", visibles);
  562. pano.put("visibles3", visibles3);
  563. }
  564. }
  565. }
  566. FileUtils.deleteFile(localDataPath + "vision.json");
  567. FileUtils.deleteFile(localDataPath + "vision.modeldata");
  568. FileUtils.writeFile(localDataPath + "vision.json", json.toString());
  569. ConvertUtils.convertTxtToVisionModelData(localDataPath + "vision.json", localDataPath + "vision.modeldata");
  570. fYunFileService.uploadFile(bucket, localDataPath + "vision.modeldata", viewImagesPath + "vision.modeldata");
  571. //更新版本号
  572. SceneEditInfo editInfo = sceneEditInfoService.getByScenePlusId(scenePlus.getId());
  573. if(Objects.isNull(editInfo)){
  574. editInfo = new SceneEditInfo();
  575. editInfo.setScenePlusId(scenePlus.getId());
  576. sceneEditInfoService.save(editInfo);
  577. }else{
  578. sceneEditInfoService.upgradeVersionAndImgVersionById(editInfo.getId());
  579. //更新scenejson缓存和oss文件版本号
  580. sceneEditInfoService.upgradeSceneJsonVersion(param.getNum(), editInfo.getVersion() + 1, editInfo.getImgVersion() + 1, bucket);
  581. }
  582. return ResultData.ok();
  583. }
  584. @Override
  585. public void updateUserIdByCameraId(Long userId, Long cameraId) {
  586. this.update(new LambdaUpdateWrapper<ScenePro>()
  587. .eq(ScenePro::getCameraId, cameraId)
  588. .set(ScenePro::getUserId, userId));
  589. }
  590. @Override
  591. public ResultData uploadObjAndImg(String num, MultipartFile file) throws Exception{
  592. if(StrUtil.isEmpty(num)){
  593. throw new BusinessException(ServerCode.PARAM_REQUIRED, "num");
  594. }
  595. if(!file.getOriginalFilename().endsWith(".zip")){
  596. throw new BusinessException(ErrorCode.FAILURE_CODE_7015);
  597. }
  598. ScenePlus scenePlus = scenePlusService.getScenePlusByNum(num);
  599. if(scenePlus == null){
  600. throw new BusinessException(ErrorCode.FAILURE_CODE_5005);
  601. }
  602. ScenePlusExt scenePlusExt = scenePlusExtService.getScenePlusExtByPlusId(scenePlus.getId());
  603. String bucket = scenePlusExt.getYunFileBucket();
  604. if(ModelKind.THREE_D_TILE.code().equals(scenePlusExt.getModelKind())){
  605. this.buildModel43dtiles(num, bucket, scenePlusExt.getDataSource(), file);
  606. }else{
  607. this.buildModel4Dam(num, bucket, scenePlusExt.getDataSource(), scenePlusExt.getBuildType(), file);
  608. }
  609. //更新版本信息
  610. SceneEditInfo sceneEditInfo = sceneEditInfoService.getByScenePlusId(scenePlus.getId());
  611. if(Objects.isNull(sceneEditInfo)){
  612. sceneEditInfo = new SceneEditInfo();
  613. sceneEditInfo.setScenePlusId(scenePlus.getId());
  614. sceneEditInfo.setFloorPublishVer(1);
  615. sceneEditInfo.setFloorEditVer(1);
  616. sceneEditInfo.setIsUploadObj(CommonStatus.YES.code());
  617. sceneEditInfoService.save(sceneEditInfo);
  618. }else{
  619. sceneEditInfoService.update(
  620. new LambdaUpdateWrapper<SceneEditInfo>()
  621. .setSql("version = version + 1")
  622. .setSql("floor_edit_ver = floor_edit_ver + 1")
  623. .setSql("floor_publish_ver = floor_publish_ver + 1")
  624. .setSql("img_version = img_version + 1")
  625. .set(SceneEditInfo::getIsUploadObj, CommonStatus.YES.code())
  626. .eq(SceneEditInfo::getId, sceneEditInfo.getId()));
  627. sceneEditInfoService.upgradeSceneJsonVersion(num, sceneEditInfo.getVersion() + 1, sceneEditInfo.getImgVersion() + 1, bucket);
  628. }
  629. return ResultData.ok();
  630. }
  631. /**
  632. * 老算法(dam)上传模型逻辑
  633. * @param num
  634. * @param bucket
  635. * @param dataSource
  636. * @param buildType
  637. * @throws Exception
  638. */
  639. private void buildModel4Dam(String num, String bucket, String dataSource, String buildType, MultipartFile file) throws Exception {
  640. //文件上传的位置可以自定义
  641. String path = dataSource + "_obj2txt";
  642. String zipPath = path + "/zip/";
  643. String filePath = path + "/extras/";
  644. String resultPath = path + "/results/";
  645. //压缩文件处理:解压缩,解压缩后复制等操作
  646. this.objAndImgFileHandler(resultPath, filePath, zipPath, file);
  647. //创建data.json
  648. this.writeDataJson(path);
  649. //调用算法,不同的类型调用不同的算法
  650. if("V2".equals(buildType)){
  651. CreateObjUtil.objToTxt(path , "1");
  652. }
  653. if("V3".equals(buildType)){
  654. CreateObjUtil.build3dModel(path , "1");
  655. }
  656. //算法计算完后,生成压缩文件,上传到oss
  657. this.uploadFileofterRebuildPanoram(path, filePath, num, bucket);
  658. }
  659. /**
  660. * 新算法(3dtiles)上传模型逻辑
  661. * @param num
  662. * @param bucket
  663. * @param dataSource
  664. * @throws Exception
  665. */
  666. private void buildModel43dtiles(String num, String bucket, String dataSource, MultipartFile file) throws Exception {
  667. //文件上传的位置可以自定义
  668. String path = dataSource + "_obj2Tiles" + File.separator;
  669. String meshPath = path + "mesh";
  670. String zipPath = path + "zip" + File.separator;
  671. String zipFilePath = zipPath + file.getOriginalFilename();
  672. //压缩文件处理:解压缩,解压缩后复制等操作
  673. FileUtil.del(path);
  674. FileUtil.mkdir(zipPath);
  675. File zipFile = new File(zipFilePath);
  676. file.transferTo(zipFile);
  677. ZipUtil.unzip(zipFilePath, meshPath);
  678. //检测文件
  679. String floorsJsonPath = meshPath + File.separator + "floors.json";
  680. if(!FileUtil.exist(floorsJsonPath)){
  681. throw new BusinessException(ErrorCode.FAILURE_CODE_5068);
  682. }
  683. String floorsJsonStr = FileUtil.readUtf8String(floorsJsonPath);
  684. JSONObject floorsJsonObj = JSON.parseObject(floorsJsonStr);
  685. JSONArray floorArr = floorsJsonObj.getJSONArray("floors");
  686. if(CollUtil.isEmpty(floorArr)){
  687. throw new BusinessException(ErrorCode.FAILURE_CODE_5069);
  688. }
  689. Set<String> floorNameSet = new HashSet<>();
  690. floorArr.stream().forEach(item->{
  691. JSONObject itemObj = (JSONObject) item;
  692. //楼层目录是否存在
  693. String name = itemObj.getString("name");
  694. if(StrUtil.isEmpty(name) || !FileUtil.exist(meshPath + File.separator + name)){
  695. throw new BusinessException(ErrorCode.FAILURE_CODE_5070);
  696. }
  697. //检测obj文件是否存在
  698. String objPath = itemObj.getString("objPath");
  699. if(StrUtil.isEmpty(objPath) || !FileUtil.exist(path + objPath)){
  700. throw new BusinessException(ErrorCode.FAILURE_CODE_5070);
  701. }
  702. if(floorNameSet.contains(name)){
  703. throw new BusinessException(ErrorCode.FAILURE_CODE_5069);
  704. }
  705. floorNameSet.add(name);
  706. });
  707. //读取oss上的floors.jsoon用于校验用户上传的模型楼层数是否一一对应
  708. String ossFloorsJson = fYunFileService.getFileContent(bucket, String.format(UploadFilePath.DATA_VIEW_PATH, num) + "mesh/floors.json");
  709. JSONObject orginFloorsJsonObj = JSON.parseObject(ossFloorsJson);
  710. JSONArray orginFloorArr = orginFloorsJsonObj.getJSONArray("floors");
  711. Set<String> orginFloorNameSet = orginFloorArr.stream().map(item -> {
  712. JSONObject itemObj = (JSONObject) item;
  713. return itemObj.getString("name");
  714. }).collect(Collectors.toSet());
  715. if(floorNameSet.size() != orginFloorNameSet.size()){
  716. throw new BusinessException(ErrorCode.FAILURE_CODE_5070);
  717. }
  718. orginFloorNameSet.stream().forEach(orginName->{
  719. if(!floorNameSet.contains(orginName)){
  720. throw new BusinessException(ErrorCode.FAILURE_CODE_5070);
  721. }
  722. });
  723. //调用算法
  724. String command = "bash /home/ubuntu/bin/Obj2Tiles.sh " + path;
  725. log.info("上传3dtiles模型开始, num:{}, targetPath:{}", num, path);
  726. // RuntimeUtil.exec(command);
  727. CreateObjUtil.callshell(command);
  728. log.info("上传3dtiles模型结束, num:{}, targetPath:{}", num, path);
  729. //检测计算结果
  730. String tilesPath = path + "3dtiles";
  731. String tilesetJsonPath = tilesPath + File.separator + "tileset.json";
  732. boolean success = ComputerUtil.checkComputeCompleted(tilesetJsonPath, maxCheckTimes, waitTime);
  733. if(!success){
  734. throw new BusinessException(ErrorCode.FAILURE_CODE_7013);
  735. }
  736. //删除logs
  737. FileUtil.del(tilesPath + File.separator + "logs");
  738. //算法计算完后,生成压缩文件,上传到oss
  739. //上传3dtiles
  740. fYunFileService.deleteFolder(bucket, String.format(UploadFilePath.IMG_VIEW_PATH, num) + "3dtiles");
  741. fYunFileService.uploadFileByCommand(bucket, tilesPath, String.format(UploadFilePath.IMG_VIEW_PATH, num) + "3dtiles");
  742. //上传mesh
  743. fYunFileService.deleteFolder(bucket, String.format(UploadFilePath.DATA_VIEW_PATH, num) + "mesh");
  744. fYunFileService.uploadFileByCommand(bucket, meshPath, String.format(UploadFilePath.DATA_VIEW_PATH, num) + "mesh");
  745. }
  746. private void uploadFileofterRebuildPanoram(String path, String filePath, String sceneNum, String bucket) throws Exception {
  747. //因为共享目录有延迟,这里循环检测算法是否计算完毕3次,每次隔五秒
  748. String uploadJsonPath = path + File.separator + "results" +File.separator+"upload.json";
  749. boolean exist = ComputerUtil.checkComputeCompleted(uploadJsonPath, maxCheckTimes, waitTime);
  750. if(!exist){
  751. throw new BusinessException(ErrorCode.FAILURE_CODE_7013);
  752. }
  753. String uploadData = FileUtils.readFile(uploadJsonPath);
  754. JSONObject uploadJson = null;
  755. JSONArray array = null;
  756. if(uploadData!=null) {
  757. uploadJson = JSONObject.parseObject(uploadData);
  758. array = uploadJson.getJSONArray("upload");
  759. }
  760. Map<String,String> map = new HashMap<String,String>();
  761. JSONObject fileJson = null;
  762. String fileName = "";
  763. String imgViewPath = String.format(UploadFilePath.IMG_VIEW_PATH, sceneNum);
  764. for(int i = 0, len = array.size(); i < len; i++) {
  765. fileJson = array.getJSONObject(i);
  766. fileName = fileJson.getString("file");
  767. //文件不存在抛出异常
  768. if (!new File(path + File.separator + "results" + File.separator + fileName).exists()) {
  769. throw new Exception(path + File.separator + "results" + File.separator + fileName + "文件不存在");
  770. }
  771. //tex文件夹
  772. if (fileJson.getIntValue("clazz") == 15) {
  773. map.put(path + File.separator + "results" + File.separator + fileName,
  774. imgViewPath + ConstantFileName.modelUUID + "_50k_texture_jpg_high1/" + fileName.replace("tex/", ""));
  775. continue;
  776. }
  777. }
  778. String damPath = path + File.separator + "results" +File.separator+ ConstantFileName.modelUUID+"_50k.dam";
  779. CreateObjUtil.convertTxtToDam( path + File.separator + "results" +File.separator+"modeldata.txt", damPath);
  780. boolean existDam = ComputerUtil.checkComputeCompleted(damPath, 5, 2);
  781. if(!existDam){
  782. throw new BusinessException(ErrorCode.FAILURE_CODE_7013);
  783. }
  784. // CreateObjUtil.convertDamToLzma(path + File.separator + "results");
  785. // CreateObjUtil.convertTxtToDam( path + File.separator + "results" +File.separator+"modeldata.txt", path + File.separator + "results" + File.separator+ConstantFileName.modelUUID+"_50k.dam");
  786. // map.put(path + File.separator + "results" +File.separator+ConstantFileName.modelUUID+"_50k.dam.lzma", imgViewPath +ConstantFileName.modelUUID+"_50k.dam.lzma");
  787. map.put(path + File.separator + "results" +File.separator+ConstantFileName.modelUUID+"_50k.dam", imgViewPath+ConstantFileName.modelUUID+"_50k.dam");
  788. String ossMeshPath = String.format(UploadFilePath.DATA_VIEW_PATH, sceneNum) + "mesh";
  789. //删除oss中的mesh
  790. fYunFileService.deleteFolder(bucket, ossMeshPath);
  791. //上传obj相关文件
  792. List<String> fileNames = FileUtil.listFileNames(filePath);
  793. fileNames.stream().forEach(name->map.put(filePath + name, ossMeshPath + File.separator + name));
  794. fYunFileService.uploadMulFiles(bucket, map);
  795. }
  796. private void writeDataJson(String path) throws IOException {
  797. JSONObject dataJson = new JSONObject();
  798. dataJson.put("obj2txt", true);
  799. dataJson.put("split_type", "SPLIT_V6");
  800. dataJson.put("data_describe", "double spherical");
  801. dataJson.put("skybox_type", "SKYBOX_V5");
  802. FileUtils.writeFile(path + "/data.json", dataJson.toString());
  803. }
  804. private void objAndImgFileHandler(String resultPath, String filePath, String zipPath, MultipartFile file)
  805. throws Exception {
  806. FileUtils.delAllFile(resultPath);
  807. File targetFile = new File(filePath);
  808. if (!targetFile.exists()) {
  809. targetFile.mkdirs();
  810. }else {
  811. FileUtils.delAllFile(filePath);
  812. }
  813. targetFile = new File(zipPath);
  814. if (!targetFile.exists()) {
  815. targetFile.mkdirs();
  816. }else {
  817. FileUtils.delAllFile(zipPath);
  818. }
  819. targetFile = new File(zipPath + file.getOriginalFilename());
  820. if(!targetFile.getParentFile().exists()){
  821. targetFile.getParentFile().mkdirs();
  822. }
  823. // 保存压缩包到本地
  824. if(targetFile.exists())
  825. {
  826. FileUtils.deleteFile(zipPath + file.getOriginalFilename());
  827. }
  828. file.transferTo(targetFile);
  829. ZipUtil.unzip(zipPath + file.getOriginalFilename(), zipPath + "data/");
  830. //源文件数据,判断是否有多个文件夹,有多个就提示错误,有一个就将文件夹里数据迁移到extras目录,无直接迁移到extras目录
  831. boolean flag = false;
  832. //目录名称,如果不为空,则压缩文件第一层是目录
  833. String targetName = "";
  834. File dataFile = new File(zipPath + "data/");
  835. for(File data : dataFile.listFiles()){
  836. if(data.isDirectory() && flag){
  837. throw new BusinessException(ErrorCode.FAILURE_CODE_5018);
  838. }
  839. if(data.isDirectory() && !flag){
  840. flag = true;
  841. targetName = data.getName();
  842. }
  843. }
  844. //是否包含obj文件
  845. boolean objFlag = false;
  846. //是否包含mtl文件
  847. boolean mtlFlag = false;
  848. File[] files = null;
  849. String dataPath = null;
  850. if(StrUtil.isEmpty(targetName)){
  851. files = dataFile.listFiles();
  852. dataPath = zipPath + "data/";
  853. }else{
  854. files = new File(zipPath + "data/" + targetName).listFiles();
  855. dataPath = zipPath + "data/" + targetName + File.separator;
  856. }
  857. for(File data : files){
  858. if(data.isDirectory()){
  859. throw new BusinessException(ErrorCode.FAILURE_CODE_5018);
  860. }
  861. if(data.getName().endsWith(".jpg") || data.getName().endsWith(".png")){
  862. if(!FileUtils.checkFileSizeIsLimit(data.length(), 1.5, "M")){
  863. throw new BusinessException(ErrorCode.FAILURE_CODE_5020);
  864. }
  865. }
  866. if(data.getName().endsWith(".obj")){
  867. if(objFlag){
  868. throw new BusinessException(ErrorCode.FAILURE_CODE_5019);
  869. }
  870. if(!data.getName().equals("mesh.obj")){
  871. throw new BusinessException(ErrorCode.FAILURE_CODE_5060);
  872. }
  873. if(!FileUtils.checkFileSizeIsLimit(data.length(), 20, "M")){
  874. throw new BusinessException(ErrorCode.FAILURE_CODE_5020);
  875. }
  876. objFlag = true;
  877. FileUtils.copyFile(dataPath + data.getName(), filePath + "mesh.obj", true);
  878. continue;
  879. }
  880. if(data.getName().endsWith(".mtl")){
  881. mtlFlag = true;
  882. }
  883. FileUtils.copyFile(dataPath + data.getName(), filePath + data.getName(), true);
  884. }
  885. //压缩文件中必须有且仅有一个obj和mtl文件,否则抛出异常
  886. if(!mtlFlag || !objFlag){
  887. throw new BusinessException(ErrorCode.FAILURE_CODE_5059);
  888. }
  889. }
  890. public ResultData downloadTexData(String num) throws Exception {
  891. if(StrUtil.isEmpty(num)){
  892. throw new BusinessException(ErrorCode.PARAM_REQUIRED);
  893. }
  894. ScenePlus scenePlus = scenePlusService.getScenePlusByNum(num);
  895. if(scenePlus == null){
  896. throw new BusinessException(ErrorCode.FAILURE_CODE_5005);
  897. }
  898. ScenePlusExt scenePlusExt = scenePlusExtService.getScenePlusExtByPlusId(scenePlus.getId());
  899. String bucket = scenePlusExt.getYunFileBucket();
  900. SceneEditInfo sceneEditInfo = sceneEditInfoService.getByScenePlusId(scenePlus.getId());
  901. if(ModelKind.THREE_D_TILE.code().equals(scenePlusExt.getModelKind())){
  902. return this.downloadModel43dtiles(num, bucket, scenePlusExt, sceneEditInfo);
  903. }
  904. return this.downloadModel4Dam(num, bucket);
  905. }
  906. @Override
  907. public ScenePro getByNum(String num) {
  908. return this.getOne(new LambdaQueryWrapper<ScenePro>().eq(ScenePro::getNum, num));
  909. }
  910. private ResultData downloadModel43dtiles(String num, String bucket, ScenePlusExt scenePlusExt, SceneEditInfo sceneEditInfo){
  911. //下载mesh到本地
  912. String meshOssPath = String.format(UploadFilePath.DATA_VIEW_PATH, num) + "mesh/";
  913. String meshLocalPath = String.format(ConstantFilePath.SCENE_DATA_PATH_V4, num) + "mesh";
  914. String zipName = num + "_mesh.zip";
  915. String zipFilePath = String.format(ConstantFilePath.SCENE_DATA_PATH_V4, num) + zipName;
  916. //下载
  917. fYunFileService.downloadFileByCommand(bucket, meshLocalPath, meshOssPath);
  918. //打包
  919. ZipUtil.zip(meshLocalPath,zipFilePath);
  920. //上传压缩包
  921. fYunFileService.uploadFile(bucket, zipFilePath, "downloads/extras/" + zipName);
  922. //删除本地文件
  923. FileUtil.del(meshLocalPath);
  924. FileUtil.del(zipFilePath);
  925. String url = ossUrlPrefix + "downloads/extras/" + zipName + "?t=" + Calendar.getInstance().getTimeInMillis();
  926. return ResultData.ok(url);
  927. }
  928. public static void main(String[] args) {
  929. ZipUtil.zip("C:\\Users\\dsx\\Desktop\\新建文件夹\\temp\\mesh\\aaa\\mesh", "C:\\Users\\dsx\\Desktop\\新建文件夹\\temp\\mesh\\aaa\\mesh\\mesh.zip");
  930. System.out.println(123);
  931. }
  932. private ResultData downloadModel4Dam(String num, String bucket){
  933. String localImagePath = String.format(ConstantFilePath.IMAGESBUFFER_FORMAT, num);
  934. if(!new File(localImagePath).exists()){
  935. new File(localImagePath).mkdirs();
  936. }
  937. String zipName = num + "_extras.zip";
  938. String zipPath = localImagePath + zipName;
  939. String dataViewPath = String.format(UploadFilePath.DATA_VIEW_PATH, num);
  940. //V3版本去oss下载2048模型
  941. String meshPath = String.format(ConstantFilePath.DATABUFFER_FORMAT, num) + "mesh";
  942. FileUtils.deleteDirectory(meshPath);
  943. fYunFileService.downloadFileByCommand(bucket, meshPath, dataViewPath + "mesh");
  944. log.info("meshPath="+meshPath);
  945. if(!new File(meshPath).exists() || new File(meshPath).listFiles().length < 1){
  946. throw new BusinessException(ErrorCode.FAILURE_CODE_7006);
  947. }
  948. for(File file : new File(meshPath).listFiles()){
  949. if(file.isDirectory()){
  950. for (File item : file.listFiles()) {
  951. if(item.getName().endsWith(".obj") && !"output.house.obj".equals(item.getName()) &&
  952. !"mesh.obj".equals(item.getName())){
  953. item.delete();
  954. }
  955. if(item.getName().endsWith(".mtl") && !"output.house.mtl".equals(item.getName()) &&
  956. !"mesh.mtl".equals(item.getName())){
  957. item.delete();
  958. }
  959. }
  960. continue;
  961. }
  962. if(file.getName().endsWith(".obj") && !"output.house.obj".equals(file.getName()) &&
  963. !"mesh.obj".equals(file.getName())){
  964. file.delete();
  965. }
  966. if(file.getName().endsWith(".mtl") && !"output.house.mtl".equals(file.getName()) &&
  967. !"mesh.mtl".equals(file.getName())){
  968. file.delete();
  969. }
  970. }
  971. //打包
  972. ZipUtil.zip(meshPath, zipPath);
  973. //上传压缩包
  974. fYunFileService.uploadFile(bucket, zipPath, "downloads/extras/" + zipName);
  975. String url = ossUrlPrefix + "downloads/extras/" + zipName + "?t=" + Calendar.getInstance().getTimeInMillis();
  976. FileUtil.del(zipPath);
  977. return ResultData.ok(url);
  978. }
  979. }