SceneProServiceImpl.java 59 KB

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