ScenePlusServiceImpl.java 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. package com.fdkankan.openApi.service.www.impl;
  2. import cn.hutool.core.bean.BeanUtil;
  3. import cn.hutool.core.collection.CollUtil;
  4. import cn.hutool.core.io.FileUtil;
  5. import cn.hutool.core.util.StrUtil;
  6. import com.alibaba.fastjson.JSON;
  7. import com.alibaba.fastjson.JSONArray;
  8. import com.alibaba.fastjson.JSONObject;
  9. import com.baomidou.dynamic.datasource.annotation.DS;
  10. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  11. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
  12. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  13. import com.fdkankan.common.constant.*;
  14. import com.fdkankan.common.exception.BusinessException;
  15. import com.fdkankan.common.util.DateUtil;
  16. import com.fdkankan.common.util.FileUtils;
  17. import com.fdkankan.fyun.config.FYunFileConfig;
  18. import com.fdkankan.fyun.face.FYunFileServiceInterface;
  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.CreateObjUtil;
  23. import com.fdkankan.openApi.bean.www.SceneJsonBean;
  24. import com.fdkankan.openApi.common.PageInfo;
  25. import com.fdkankan.openApi.entity.laser.SceneEntity;
  26. import com.fdkankan.openApi.entity.www.*;
  27. import com.fdkankan.openApi.mapper.www.IScenePlusMapper;
  28. import com.fdkankan.openApi.service.laser.FilterService;
  29. import com.fdkankan.openApi.service.laser.SceneService;
  30. import com.fdkankan.openApi.service.www.*;
  31. import com.fdkankan.openApi.vo.www.CreateFicTitiousSceneParamVO;
  32. import com.fdkankan.openApi.vo.www.PageScenesParamVo;
  33. import com.fdkankan.openApi.vo.www.SceneEditControlsVO;
  34. import com.fdkankan.openApi.vo.www.SceneVO;
  35. import com.fdkankan.redis.constant.RedisKey;
  36. import com.fdkankan.redis.util.RedisUtil;
  37. import com.fdkankan.web.response.ResultData;
  38. import org.springframework.beans.factory.annotation.Autowired;
  39. import org.springframework.beans.factory.annotation.Value;
  40. import org.springframework.stereotype.Service;
  41. import org.springframework.util.ObjectUtils;
  42. import java.util.*;
  43. import java.util.stream.Collectors;
  44. /**
  45. * <p>
  46. * 场景主表 服务实现类
  47. * </p>
  48. *
  49. * @author
  50. * @since 2023-02-15
  51. */
  52. @DS("www")
  53. @Service
  54. public class ScenePlusServiceImpl extends ServiceImpl<IScenePlusMapper, ScenePlus> implements IScenePlusService {
  55. @Value("${main.url}")
  56. private String mainUrl;
  57. @Value("${scene.pro.new.url}")
  58. private String sceneUrl;
  59. @Autowired
  60. private FYunFileServiceInterface fYunFileService;
  61. @Autowired
  62. private ISceneEditInfoService sceneEditInfoService;
  63. @Autowired
  64. private IScenePlusExtService scenePlusExtService;
  65. @Autowired
  66. private ISceneEditInfoExtService sceneEditInfoExtService;
  67. @Autowired
  68. private ISceneEditControlsService sceneEditControlsService;
  69. @Autowired
  70. private RedisUtil redisUtil;
  71. @Autowired
  72. private ICameraService cameraService;
  73. @Autowired
  74. private FYunFileConfig fYunFileConfig;
  75. @Autowired
  76. private FilterService filterService;
  77. @Autowired
  78. private SceneService sceneService;
  79. @Override
  80. public ScenePlus getByNumAndUserId(long userId, String num) {
  81. return this.getOne(new LambdaQueryWrapper<ScenePlus>().eq(ScenePlus::getNum, num).eq(ScenePlus::getUserId, userId));
  82. }
  83. @Override
  84. public ScenePlus getByNum(String num) {
  85. return this.getOne(new LambdaQueryWrapper<ScenePlus>().eq(ScenePlus::getNum, num));
  86. }
  87. @Override
  88. public ResultData createVirtualScene(CreateFicTitiousSceneParamVO param) throws Exception {
  89. this.checkParams4createVirtualScene(param);
  90. this.buildScene4CreateVirtualScene(param);
  91. String website = this.updateScene4CreateVirtualScene(param);
  92. return ResultData.ok(website);
  93. }
  94. private void checkParams4createVirtualScene(CreateFicTitiousSceneParamVO param){
  95. SceneFrom sceneFrom = SceneFrom.get(param.getSceneFrom());
  96. if(Objects.isNull(sceneFrom)){
  97. throw new BusinessException(ErrorCode.PARAM_ERROR.code(), "sceneFrom错误");
  98. }
  99. Integer sceneSource = null;
  100. switch (sceneFrom){
  101. case SXZ:
  102. sceneSource = SceneSource.YJHZ.code();
  103. break;
  104. }
  105. if(Objects.isNull(sceneSource)){
  106. throw new BusinessException(ErrorCode.PARAM_ERROR.code(), "sceneFrom错误");
  107. }
  108. param.setSceneSource(sceneSource);
  109. }
  110. private void buildScene4CreateVirtualScene(CreateFicTitiousSceneParamVO param) throws Exception {
  111. String num = param.getNum();
  112. String objName = param.getObjName();
  113. String mtlName = param.getMtlName();
  114. String localImagePath = String.format(ConstantFilePath.SCENE_IMAGES_PATH_V4, num);
  115. String ossImagePath = String.format(UploadFilePath.IMG_VIEW_PATH, num);
  116. String ossObjPath = ossImagePath + objName;
  117. if(!fYunFileService.fileExist(ossObjPath)){
  118. throw new BusinessException(ErrorCode.FAILURE_CODE_4001, objName);
  119. }
  120. String ossMtlPath = ossImagePath + mtlName;
  121. if(!fYunFileService.fileExist(ossMtlPath)){
  122. throw new BusinessException(ErrorCode.FAILURE_CODE_4001, mtlName);
  123. }
  124. String ossTexturePath = ossImagePath + "dacf7dfa24ae47fab8fcebfe4dc41ab9_50k_texture_jpg_high1/";
  125. // if(CollUtil.isEmpty(fYunFileService.listRemoteFiles(ossTexturePath))){
  126. // throw new BusinessException(ErrorCode.FAILURE_CODE_4001, "贴图");
  127. // }
  128. String ossVisionTxtPath = ossImagePath + "vision.txt";
  129. if(!fYunFileService.fileExist(ossVisionTxtPath)){
  130. throw new BusinessException(ErrorCode.FAILURE_CODE_4001, "vision.txt");
  131. }
  132. FileUtil.del(localImagePath + "/result/");
  133. FileUtil.del(localImagePath + "extras/");
  134. fYunFileService.downloadFile(ossObjPath, localImagePath + "extras/" + "mesh.obj");
  135. fYunFileService.downloadFile(ossMtlPath, localImagePath + "extras/" + mtlName);
  136. fYunFileService.downloadFileByCommand(localImagePath + "extras/", ossTexturePath);
  137. fYunFileService.downloadFile(ossVisionTxtPath, localImagePath + "vision.txt");
  138. //创建data.json
  139. JSONObject dataJson = new JSONObject();
  140. dataJson.put("obj2txt", true);
  141. dataJson.put("split_type", "SPLIT_V6");
  142. dataJson.put("data_describe", "double spherical");
  143. dataJson.put("skybox_type", "SKYBOX_V5");
  144. FileUtil.writeUtf8String(dataJson.toJSONString(), localImagePath + "data.json");
  145. //调用objToTxt算法
  146. CreateObjUtil.build3dModel(localImagePath, "");
  147. Thread.sleep(2000);
  148. String uploadJsonPath = localImagePath + "results/upload.json";
  149. boolean exist = ComputerUtil.checkComputeCompleted(uploadJsonPath, 3, 3000);
  150. if(!exist){
  151. throw new BusinessException(ErrorCode.FAILURE_CODE_5042);
  152. }
  153. String uploadData = FileUtils.readFile(uploadJsonPath);
  154. JSONObject uploadJson = null;
  155. JSONArray array = null;
  156. if(uploadData!=null) {
  157. uploadJson = JSONObject.parseObject(uploadData);
  158. array = uploadJson.getJSONArray("upload");
  159. }
  160. if(array == null){
  161. log.error("upload.json数据出错");
  162. throw new BusinessException(ErrorCode.FAILURE_CODE_5012);
  163. }
  164. Map<String,String> map = new HashMap<String,String>();
  165. JSONObject fileJson = null;
  166. String fileName = "";
  167. for(int i = 0, len = array.size(); i < len; i++) {
  168. fileJson = array.getJSONObject(i);
  169. fileName = fileJson.getString("file");
  170. String filePath = localImagePath + "results/" + fileName;
  171. //文件不存在抛出异常
  172. if(!FileUtil.exist(filePath)){
  173. throw new BusinessException(ErrorCode.FAILURE_CODE_4002, filePath);
  174. }
  175. //tex文件夹
  176. if (fileJson.getIntValue("clazz") == 15) {
  177. map.put(filePath, ossImagePath + "dacf7dfa24ae47fab8fcebfe4dc41ab9_50k_texture_jpg_high1/" + fileName.replace("tex/", ""));
  178. continue;
  179. }
  180. }
  181. CreateObjUtil.convertTxtToDam(localImagePath + "results/" + "modeldata.txt", localImagePath + "results/" + "dacf7dfa24ae47fab8fcebfe4dc41ab9_50k.dam");
  182. // CreateObjUtil.convertDamToLzma(imagesBuffer.toString() + File.separator + "results" +File.separator+ "dacf7dfa24ae47fab8fcebfe4dc41ab9_50k.dam");
  183. // CreateObjUtil.convertTxtToDam(imagesBuffer.toString() + File.separator + "results" +File.separator+"modeldata.txt", imagesBuffer.toString() + File.separator + "results" + File.separator+ "dacf7dfa24ae47fab8fcebfe4dc41ab9_50k.dam");
  184. // map.put(imagesBuffer.toString() + File.separator + "results" +File.separator+"dacf7dfa24ae47fab8fcebfe4dc41ab9_50k.dam.lzma", "images/images"+param.getNum()+"/"+"dacf7dfa24ae47fab8fcebfe4dc41ab9_50k.dam.lzma");
  185. map.put(localImagePath + "results/" + "dacf7dfa24ae47fab8fcebfe4dc41ab9_50k.dam", ossImagePath + "dacf7dfa24ae47fab8fcebfe4dc41ab9_50k.dam");
  186. FileUtils.deleteFile(localImagePath + "vision.modeldata");
  187. CreateObjUtil.convertTxtToVisionmodeldata(localImagePath + "vision.txt", localImagePath + "vision.modeldata");
  188. map.put(localImagePath + "vision.modeldata", ossImagePath + "vision.modeldata");
  189. fYunFileService.uploadMulFiles(map);
  190. }
  191. private String updateScene4CreateVirtualScene(CreateFicTitiousSceneParamVO param){
  192. ScenePlus scenePlus = this.getByNum(param.getNum());
  193. ScenePlusExt scenePlusExt = null;
  194. SceneEditInfo sceneEditInfo = null;
  195. SceneEditInfoExt sceneEditInfoExt = null;
  196. SceneEditControls sceneEditControls = null;
  197. if(Objects.nonNull(scenePlus)){
  198. scenePlusExt = scenePlusExtService.getByPlusId(scenePlus.getId());
  199. sceneEditInfo = sceneEditInfoService.getByScenePlusId(scenePlus.getId());
  200. sceneEditInfoExt = sceneEditInfoExtService.getByEditInfoId(sceneEditInfo.getId());
  201. sceneEditControls = sceneEditControlsService.getByEditInfoId(sceneEditInfo.getId());
  202. }else {
  203. scenePlus = new ScenePlus();
  204. scenePlusExt = new ScenePlusExt();
  205. sceneEditInfo = new SceneEditInfo();
  206. sceneEditInfoExt = new SceneEditInfoExt();
  207. sceneEditControls = new SceneEditControls();
  208. }
  209. scenePlus.setNum(param.getNum());
  210. scenePlus.setUserId(param.getUserId());
  211. scenePlus.setTitle("四维看看");
  212. scenePlus.setDescription("<p>四维看看 让空间讲故事</p>");
  213. scenePlus.setSceneStatus(SceneStatus.SUCCESS.code());
  214. scenePlus.setPayStatus(PayStatus.PAY.code());
  215. scenePlus.setSceneSource(param.getSceneSource());
  216. this.saveOrUpdate(scenePlus);
  217. scenePlus = this.getByNum(param.getNum());
  218. scenePlusExt.setPlusId(scenePlus.getId());
  219. scenePlusExt.setWebSite(mainUrl + "/" + sceneUrl + param.getNum());
  220. scenePlusExt.setBuildType("V3");
  221. scenePlusExt.setSceneResolution(param.getSceneResolution());
  222. scenePlusExt.setSceneFrom(param.getSceneFrom());
  223. scenePlusExt.setSceneKind(param.getSceneKind());
  224. scenePlusExt.setModelKind(ModelKind.DAM.code());
  225. scenePlusExtService.saveOrUpdate(scenePlusExt);
  226. scenePlusExt = scenePlusExtService.getByPlusId(scenePlus.getId());
  227. sceneEditInfo.setScenePlusId(scenePlus.getId());
  228. sceneEditInfo.setVersion(Objects.isNull(sceneEditInfo.getVersion()) ? 0 : sceneEditInfo.getVersion() + 1);
  229. sceneEditInfo.setImgVersion(Objects.isNull(sceneEditInfo.getImgVersion()) ? 0 : sceneEditInfo.getImgVersion() + 1);
  230. sceneEditInfo.setTitle(scenePlus.getTitle());
  231. sceneEditInfo.setDescription(scenePlus.getDescription());
  232. sceneEditInfoService.saveOrUpdate(sceneEditInfo);
  233. sceneEditInfo = sceneEditInfoService.getByScenePlusId(scenePlus.getId());
  234. sceneEditInfoExt.setEditInfoId(sceneEditInfo.getId());
  235. sceneEditInfoExt.setScenePlusId(scenePlus.getId());
  236. sceneEditInfoExtService.saveOrUpdate(sceneEditInfoExt);
  237. sceneEditInfoExt = sceneEditInfoExtService.getByEditInfoId(sceneEditInfo.getId());
  238. sceneEditControls.setEditInfoId(sceneEditInfo.getId());
  239. sceneEditControlsService.saveOrUpdate(sceneEditControls);
  240. sceneEditControls = sceneEditControlsService.getByEditInfoId(sceneEditInfo.getId());
  241. SceneJsonBean sceneJson = new SceneJsonBean();
  242. BeanUtil.copyProperties(sceneEditInfoExt, sceneJson);
  243. BeanUtil.copyProperties(sceneEditInfo, sceneJson);
  244. SceneEditControlsVO sceneEditControlsVO = BeanUtil.copyProperties(sceneEditControls, SceneEditControlsVO.class);
  245. sceneJson.setControls(sceneEditControlsVO);
  246. sceneJson.setNum(param.getNum());
  247. sceneJson.setCreateTime(scenePlus.getCreateTime());
  248. sceneJson.setSceneResolution(scenePlusExt.getSceneResolution());
  249. sceneJson.setVersion(sceneEditInfo.getVersion());
  250. sceneJson.setImgVersion(sceneEditInfo.getImgVersion());
  251. sceneJson.setSceneFrom(scenePlusExt.getSceneFrom());
  252. sceneJson.setSceneKind(scenePlusExt.getSceneKind());
  253. sceneJson.setModelKind(scenePlusExt.getModelKind());
  254. sceneJson.setPayStatus(scenePlus.getPayStatus());
  255. String sceneJsonStr = JSON.toJSONString(sceneJson);
  256. //上传sceneJson文件
  257. fYunFileService.uploadFile(sceneJsonStr.getBytes(), String.format(UploadFilePath.DATA_VIEW_PATH, param.getNum()) + "scene.json");
  258. //scenejson写入缓存
  259. redisUtil.set(String.format(RedisKey.SCENE_JSON, param.getNum()), sceneJsonStr);
  260. return scenePlusExt.getWebSite();
  261. }
  262. @Override
  263. public PageInfo<SceneVO> getScenesByUserId(Long userId, PageScenesParamVo param) {
  264. LambdaQueryWrapper<ScenePlus> queryWrapper = new LambdaQueryWrapper<>();
  265. queryWrapper.eq(ScenePlus::getUserId,userId);
  266. if (!ObjectUtils.isEmpty(param.getCameraType())) {
  267. switch (param.getCameraType()){
  268. case "mega":
  269. queryWrapper.eq(ScenePlus::getSceneSource, 4);
  270. break;
  271. case "kankan":
  272. queryWrapper.ne(ScenePlus::getSceneSource, 4);
  273. break;
  274. }
  275. }
  276. Page<ScenePlus> scenePage = this.page(new Page<>(param.getPageNum(), param.getPageSize()), queryWrapper);
  277. if(CollUtil.isEmpty(scenePage.getRecords())){
  278. return PageInfo.PageInfo(scenePage);
  279. }
  280. List<Long> plusIdList = new ArrayList<>();
  281. List<String> megaSceneNumList = new ArrayList<>();
  282. Set<Long> cameraIds = new HashSet<>();
  283. scenePage.getRecords().stream().forEach(plus -> {
  284. plusIdList.add(plus.getId());
  285. if(plus.getSceneSource() == SceneSource.JG.code()){
  286. megaSceneNumList.add(plus.getNum());
  287. }
  288. if(Objects.nonNull(plus.getCameraId())){
  289. cameraIds.add(plus.getCameraId());
  290. }
  291. });
  292. Map<Long, String> cameraMap = new HashMap<>();
  293. if(CollUtil.isNotEmpty(cameraIds)){
  294. cameraMap = cameraService
  295. .list(new LambdaQueryWrapper<Camera>().in(Camera::getId, cameraIds))
  296. .stream().collect(Collectors.toMap(Camera::getId, Camera::getSnCode));
  297. }
  298. List<ScenePlusExt> scenePlusExtList = scenePlusExtService.list(new LambdaQueryWrapper<ScenePlusExt>().in(ScenePlusExt::getPlusId, plusIdList));
  299. Map<Long, ScenePlusExt> plusExtMap = new HashMap<>();
  300. scenePlusExtList.stream().forEach(ext->plusExtMap.put(ext.getPlusId(), ext));
  301. List<SceneEntity> megaSceneList = null;
  302. Map<String, SceneEntity> megaSceneMap = new HashMap<>();
  303. if(CollUtil.isNotEmpty(megaSceneNumList)){
  304. megaSceneList = sceneService.list(new LambdaQueryWrapper<SceneEntity>().in(SceneEntity::getSceneCode, megaSceneNumList));
  305. if(CollUtil.isNotEmpty(megaSceneList)){
  306. megaSceneList.stream().forEach(scene -> megaSceneMap.put(scene.getSceneCode(), scene));
  307. }
  308. }
  309. Map<Long, String> finalCameraMap = cameraMap;
  310. List<SceneVO> sceneVos = scenePage.getRecords().stream().map(scenePlus -> {
  311. SceneVO vo = new SceneVO();
  312. vo.setSceneCode(scenePlus.getNum());
  313. vo.setSceneName(scenePlus.getTitle());
  314. vo.setCreateTime(DateUtil.date2String(scenePlus.getCreateTime(), null));
  315. ScenePlusExt plusExt = plusExtMap.get(scenePlus.getId());
  316. if(Objects.nonNull(plusExt)){
  317. vo.setShootCount(plusExt.getShootCount());
  318. }
  319. vo.setSnCode(finalCameraMap.get(scenePlus.getCameraId()));
  320. vo.setCameraType("kankan");
  321. if (scenePlus.getSceneSource() == 4) {
  322. SceneEntity scene = megaSceneMap.get(scenePlus.getNum());
  323. if (Objects.nonNull(scene) && StrUtil.isNotEmpty(scene.getTitle())) {
  324. vo.setSceneName(scene.getTitle());
  325. }
  326. vo.setCameraType("mega");
  327. }
  328. return vo;
  329. }).collect(Collectors.toList());
  330. return PageInfo.PageInfo(scenePage.getCurrent(), scenePage.getSize(), scenePage.getTotal(), sceneVos);
  331. }
  332. @Override
  333. public Object getPointInfo(String sceneCode) {
  334. // 获取vision.txt 文件内容返回
  335. String content = fYunFileService.getFileContent(String.format(UploadFilePath.IMG_VIEW_PATH, sceneCode).concat("vision.txt"));
  336. JSONObject jsonObject = JSONObject.parseObject(content);
  337. return jsonObject.get("sweepLocations");
  338. }
  339. @Override
  340. public List<String> getSceneObjFilePaths(String sceneCode) {
  341. List<String> objFiles = fYunFileService.listRemoteFiles(String.format(UploadFilePath.DATA_VIEW_PATH, sceneCode).concat("mesh"))
  342. .stream().map(file -> fYunFileConfig.getHost() + file).collect(Collectors.toList());
  343. return objFiles;
  344. }
  345. @Override
  346. public List<String> getScenePanoramicImageFiles(String sceneCode) {
  347. // 根据vision.txt 获取全景图文件
  348. String content = fYunFileService.getFileContent(String.format(UploadFilePath.IMG_VIEW_PATH, sceneCode).concat("vision.txt"));
  349. JSONObject jsonObject = JSONObject.parseObject(content);
  350. return jsonObject.getJSONArray("sweepLocations").stream()
  351. .map(json -> String.format(fYunFileConfig.getHost()
  352. + UploadFilePath.scene_result_data_path, sceneCode).concat("caches/images/")
  353. + ((JSONObject) json).getString("uuid").concat(".jpg")).collect(Collectors.toList());
  354. }
  355. }