ScenePlusServiceImpl.java 17 KB

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