SceneProServiceImpl.java 51 KB

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