Scene3dfamilyServiceImpl.java 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. package com.fdkankan.scene.service.impl;
  2. import cn.hutool.core.collection.CollUtil;
  3. import cn.hutool.core.io.FileUtil;
  4. import cn.hutool.core.util.ZipUtil;
  5. import com.alibaba.fastjson.JSONArray;
  6. import com.alibaba.fastjson.JSONObject;
  7. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  8. import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
  9. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  10. import com.fdkankan.common.constant.ErrorCode;
  11. import com.fdkankan.common.constant.RecStatus;
  12. import com.fdkankan.common.exception.BusinessException;
  13. import com.fdkankan.common.util.FileUtils;
  14. import com.fdkankan.fyun.face.FYunFileServiceInterface;
  15. import com.fdkankan.model.constants.ConstantFilePath;
  16. import com.fdkankan.model.constants.UploadFilePath;
  17. import com.fdkankan.model.utils.CreateObjUtil;
  18. import com.fdkankan.scene.bean.EulerAngles;
  19. import com.fdkankan.scene.bean.ResponseScene;
  20. import com.fdkankan.scene.common.house.JsonToHouseUtil;
  21. import com.fdkankan.scene.entity.Scene3dfamily;
  22. import com.fdkankan.scene.entity.ScenePro;
  23. import com.fdkankan.scene.mapper.IScene3dfamilyMapper;
  24. import com.fdkankan.scene.service.IScene3dfamilyService;
  25. import com.fdkankan.scene.service.ISceneProService;
  26. import com.fdkankan.web.response.Result;
  27. import com.fdkankan.web.response.ResultData;
  28. import lombok.extern.slf4j.Slf4j;
  29. import org.springframework.beans.factory.annotation.Autowired;
  30. import org.springframework.beans.factory.annotation.Value;
  31. import org.springframework.stereotype.Service;
  32. import java.io.File;
  33. import java.io.FileWriter;
  34. import java.util.Date;
  35. import java.util.List;
  36. import java.util.concurrent.CompletableFuture;
  37. /**
  38. * Created by Hb_zzZ on 2019/6/28.
  39. */
  40. @Slf4j
  41. @Service
  42. public class Scene3dfamilyServiceImpl extends ServiceImpl<IScene3dfamilyMapper, Scene3dfamily> implements IScene3dfamilyService {
  43. private static String DIR_NAME = "downloads/furnished/";
  44. @Autowired
  45. private ISceneProService sceneProService;
  46. @Autowired
  47. private FYunFileServiceInterface fYunFileService;
  48. @Value("${fyun.host}")
  49. private String fyunHost;
  50. @Override
  51. public Result createZip(ScenePro sceneProEntity,long userId) throws Exception{
  52. Scene3dfamily scene3dFamilyEntity = this.findSceneNumByNum(sceneProEntity.getNum(),0);
  53. String path = sceneProEntity.getDataSource() + "_3df" + File.separator + sceneProEntity.getNum();
  54. try {
  55. if(scene3dFamilyEntity == null){
  56. throw new BusinessException(ErrorCode.FAILURE_CODE_5032);
  57. }
  58. log.info("三维家 信息保存 路径 : " + path);
  59. //创建文件夹
  60. FileUtils.createDir(path);
  61. //生成vision.json
  62. this.downloadVisionData(sceneProEntity);
  63. String dataBuf = "data" + File.separator + "data" + sceneProEntity.getNum() + File.separator;
  64. this.visionTxtToPano(ConstantFilePath.SCENE_PATH + dataBuf //本地路径
  65. + "vision.json",path+ File.separator +"scene"+ File.separator+ dataBuf );
  66. //拉取data
  67. fYunFileService.downloadFileByCommand(path + File.separator + "scene" + File.separator + "data" + File.separator + "data" + sceneProEntity.getNum(),
  68. "data" + File.separator + "data" + sceneProEntity.getNum() + File.separator);
  69. //转换house.json
  70. Integer bo = JsonToHouseUtil.ToHouseJson(path + File.separator + "scene" + File.separator + dataBuf);
  71. if(bo!=0){
  72. switch (bo){
  73. case -1:
  74. throw new BusinessException(ErrorCode.FAILURE_CODE_5029.code(), "点位数据为空");
  75. case -2:
  76. throw new BusinessException(ErrorCode.FAILURE_CODE_5029.code(), "点位数据重复");
  77. case -3:
  78. throw new BusinessException(ErrorCode.FAILURE_CODE_5029.code(), "转换异常");
  79. case -4:
  80. throw new BusinessException(ErrorCode.FAILURE_CODE_5029.code(), "源文件不存在");
  81. }
  82. }
  83. //写入 scene.json 改名为 getinfo.json
  84. ResultData result = sceneProService.getInfo(sceneProEntity.getNum());
  85. ResponseScene responseScene = (ResponseScene) result.getData();
  86. JSONObject dataJson = (JSONObject) JSONObject.toJSON(responseScene);
  87. FileUtils.writeFile(path + File.separator + "scene" + File.separator + "data" + File.separator + "data"
  88. + sceneProEntity.getNum() + File.separator + "getinfo.json", dataJson.toString());
  89. //下载场景封面图
  90. String ossImagePath = "images" + File.separator + "images" + sceneProEntity.getNum() + File.separator;
  91. String ImagePath = path + File.separator + "scene" + File.separator + "images" + File.separator + "images" + sceneProEntity.getNum();
  92. FileUtils.downLoadFromUrl(sceneProEntity.getThumb(),"cover.jpg", ImagePath);
  93. //拉取images
  94. fYunFileService.downloadFile(ossImagePath + "floorLogoImg.png", ImagePath + "/floorLogoImg.png");
  95. fYunFileService.downloadFile(ossImagePath + "floorplan.png", ImagePath + "/floorplan.png");
  96. fYunFileService.downloadFile(ossImagePath + "logo-main.png", ImagePath + "/logo-main.png");
  97. fYunFileService.downloadFile(ossImagePath + "logo-main-en.png", ImagePath + "/logo-main-en.png");
  98. fYunFileService.downloadFile(ossImagePath + "smallPic.jpg", ImagePath + "/smallPic.jpg");
  99. fYunFileService.downloadFile(ossImagePath + "thumbBigImg.jpg", ImagePath + "/thumbBigImg.jpg");
  100. fYunFileService.downloadFile(ossImagePath + "thumbSmallImg.jpg", ImagePath + "/thumbSmallImg.jpg");
  101. fYunFileService.downloadFileByCommand(ImagePath + "/pan", ossImagePath + "pan");
  102. // 异步执行
  103. CompletableFuture.runAsync(()->{
  104. //压缩zip
  105. try {
  106. log.info("开始异步执行打包逻辑……");
  107. //获取六面体图
  108. fYunFileService.downloadFileByCommand(ImagePath + "/tiles", ossImagePath + "tiles");
  109. //拉取video
  110. fYunFileService.downloadFileByCommand(path + File.separator + "scene" + File.separator + "video" + File.separator + "video" + sceneProEntity.getNum(),
  111. "video" + File.separator + "video" + sceneProEntity.getNum() + File.separator);
  112. //拉取voice
  113. fYunFileService.downloadFileByCommand(path + File.separator + "scene" + File.separator + "voice" + File.separator + "voice" + sceneProEntity.getNum(),
  114. "voice" + File.separator + "voice" + sceneProEntity.getNum() + File.separator);
  115. ZipUtil.zip(path, path + ".zip");
  116. log.info("三维家 zip完成 : " + path);
  117. //上传oss
  118. // 上传到阿里云sso
  119. fYunFileService.uploadFileByCommand(path + ".zip", DIR_NAME + sceneProEntity.getNum() + ".zip");
  120. log.info("upload success");
  121. String url = fyunHost + DIR_NAME + sceneProEntity.getNum() + ".zip";
  122. log.info("upload url: {}" + url);
  123. scene3dFamilyEntity.setZipLink(url);
  124. scene3dFamilyEntity.setStatus(1);
  125. scene3dFamilyEntity.setZipEndTime(new Date());
  126. scene3dFamilyEntity.setUpdateTime(new Date());
  127. this.updateById(scene3dFamilyEntity);
  128. } catch (Exception e) {
  129. e.printStackTrace();
  130. scene3dFamilyEntity.setStatus(2);
  131. scene3dFamilyEntity.setZipEndTime(new Date());
  132. scene3dFamilyEntity.setUpdateTime(new Date());
  133. this.updateById(scene3dFamilyEntity);
  134. }finally {
  135. FileUtils.deleteDirectory(path);
  136. FileUtils.deleteFile(path + ".zip");
  137. }
  138. });
  139. }catch (Exception e){
  140. scene3dFamilyEntity.setStatus(2);
  141. scene3dFamilyEntity.setZipEndTime(new Date());
  142. scene3dFamilyEntity.setUpdateTime(new Date());
  143. this.updateById(scene3dFamilyEntity);
  144. e.printStackTrace();
  145. throw e;
  146. }
  147. return Result.success();
  148. }
  149. @Override
  150. public Scene3dfamily findSceneNumByNum(String num,Integer status){
  151. LambdaQueryWrapper<Scene3dfamily> wrapper = new LambdaQueryWrapper<Scene3dfamily>()
  152. .eq(Scene3dfamily::getSceneNum, num);
  153. if(status != null){
  154. wrapper.eq(Scene3dfamily::getStatus, status);
  155. }
  156. List<Scene3dfamily> list = this.list(wrapper);
  157. if(CollUtil.isEmpty(list)){
  158. return null;
  159. }
  160. return list.get(0);
  161. }
  162. @Override
  163. public JSONObject findSceneNumByNum2(ScenePro sceneProEntity){
  164. JSONObject jsonObject = new JSONObject();
  165. Scene3dfamily scene3dFamilyEntity = this.findSceneNumByNum(sceneProEntity.getNum(), null);
  166. if(scene3dFamilyEntity!=null){
  167. jsonObject.put("url",scene3dFamilyEntity.getZipLink() == null ?"":scene3dFamilyEntity.getZipLink());
  168. jsonObject.put("status",scene3dFamilyEntity.getStatus());
  169. }else{
  170. jsonObject.put("url","");
  171. jsonObject.put("status",3);
  172. }
  173. //验证是否在打包
  174. return jsonObject;
  175. }
  176. @Override
  177. public void updateSceneNumByNum (String sceneNum){
  178. this.update(new LambdaUpdateWrapper<Scene3dfamily>().set(Scene3dfamily::getRecStatus,
  179. RecStatus.DISABLE.code()).eq(Scene3dfamily::getSceneNum, sceneNum));
  180. }
  181. private void downloadVisionData(ScenePro sceneProEntity) throws Exception {
  182. StringBuffer dataBuf = new StringBuffer().append("data").append(File.separator)
  183. .append("data").append(sceneProEntity.getNum()).append(File.separator);
  184. StringBuffer imagesBuf = new StringBuffer().append("images").append(File.separator)
  185. .append("images").append(sceneProEntity.getNum()).append(File.separator);
  186. StringBuffer dataBuffer = new StringBuffer(ConstantFilePath.SCENE_PATH).append(dataBuf.toString());
  187. StringBuffer imagesBuffer = new StringBuffer(ConstantFilePath.SCENE_PATH).append(imagesBuf.toString());
  188. fYunFileService.downloadFile(imagesBuf.toString() + "vision.modeldata", dataBuffer.toString() + "vision.modeldata");
  189. File file = new File(dataBuffer.toString() + "vision.modeldata");
  190. if(!file.exists()) {
  191. throw new BusinessException(ErrorCode.FAILURE_CODE_5012);
  192. }
  193. CreateObjUtil.convertVisionmodeldataToTxt(dataBuffer.toString() + "vision.modeldata", dataBuffer.toString() + "vision.json");
  194. fYunFileService.uploadFile(dataBuffer.toString() + "vision.json", dataBuf.toString() + "vision.json");
  195. }
  196. private void downloadVisionData(String sceneNum) throws Exception {
  197. String dataBuf = String.format(UploadFilePath.IMG_VIEW_PATH,sceneNum);
  198. String dataBuffer = String.format(ConstantFilePath.SCENE_IMAGES_PATH_V4,sceneNum);
  199. fYunFileService.downloadFile(dataBuf.toString() + "vision.modeldata", dataBuffer.toString() + "vision.modeldata");
  200. File file = new File(dataBuffer.toString() + "vision.modeldata");
  201. if(!file.exists()) {
  202. throw new BusinessException(ErrorCode.FAILURE_CODE_5012);
  203. }
  204. CreateObjUtil.convertVisionmodeldataToTxt(dataBuffer.toString() + "vision.modeldata", dataBuffer.toString() + "vision.json");
  205. fYunFileService.uploadFile(dataBuffer.toString() + "vision.json", dataBuf.toString() + "vision.json");
  206. }
  207. /**
  208. *
  209. * @param src vision.json 路径
  210. * @param tag pano.json 目录路径
  211. */
  212. private void visionTxtToPano(String src,String tag){
  213. try {
  214. log.info("QuaternionToEulerUtil---------src:{},tag:{}",src,tag);
  215. String str = FileUtil.readUtf8String(src);
  216. JSONObject jsonObject = JSONObject.parseObject(str);
  217. JSONArray sweepLocations = jsonObject.getJSONArray("sweepLocations");
  218. for (Object o : sweepLocations) {
  219. JSONObject obj = (JSONObject) o;
  220. JSONObject pose = obj.getJSONObject("pose");
  221. JSONObject rotation = pose.getJSONObject("rotation");
  222. JSONObject translation = pose.getJSONObject("translation");
  223. Double w = rotation.getDouble("w");
  224. Double x = rotation.getDouble("x");
  225. Double y = rotation.getDouble("y");
  226. Double z = rotation.getDouble("z");
  227. Double x1 = translation.getDouble("x");
  228. Double y1 = translation.getDouble("y");
  229. Double z1 = translation.getDouble("z");
  230. JSONObject newRotation = new JSONObject();
  231. EulerAngles eulerAngles = new EulerAngles(w,x,y,z,x1,y1,z1);
  232. newRotation.put("x",eulerAngles.x);
  233. newRotation.put("y",eulerAngles.y);
  234. newRotation.put("z",eulerAngles.z);
  235. pose.put("target",newRotation);
  236. pose.remove("rotation");
  237. }
  238. File file = new File(tag);
  239. if(!file.exists()){
  240. file.mkdirs();
  241. }
  242. FileWriter fw = new FileWriter(tag+ "pano.json");
  243. fw.write(jsonObject.toJSONString());
  244. fw.close();
  245. }catch (Exception e){
  246. log.error("QuaternionToEulerUtil---------error:",e);
  247. e.printStackTrace();
  248. }
  249. }
  250. @Override
  251. public Result createZip(String sceneNum, String dataSource, String thumb, Long userId) throws Exception {
  252. Scene3dfamily scene3dFamilyEntity = this.findSceneNumByNum(sceneNum,0);
  253. String path = dataSource + "_3df" + File.separator + sceneNum;
  254. try {
  255. if(scene3dFamilyEntity == null){
  256. throw new BusinessException(ErrorCode.FAILURE_CODE_5032);
  257. }
  258. log.info("三维家 信息保存 路径 : " + path);
  259. //创建文件夹
  260. FileUtils.createDir(path);
  261. //生成vision.json
  262. this.downloadVisionData(sceneNum);
  263. String dataBuffer = String.format(ConstantFilePath.SCENE_IMAGES_PATH_V4,sceneNum);
  264. String dataBuf = "data" + File.separator + "data" + sceneNum + File.separator;
  265. this.visionTxtToPano(dataBuffer + "vision.json",path+ File.separator +"scene"+ File.separator+ dataBuf );
  266. //拉取data
  267. fYunFileService.downloadFileByCommand(path + File.separator + "scene" + File.separator + "data" + File.separator + "data" + sceneNum,
  268. String.format(UploadFilePath.DATA_VIEW_PATH,sceneNum));
  269. // 下载用户的户型图数据
  270. if (fYunFileService.fileExist(String.format(UploadFilePath.USER_VIEW_PATH, sceneNum) + "floorplan.json")) {
  271. fYunFileService.downloadFile(String.format(UploadFilePath.USER_VIEW_PATH, sceneNum) + "floorplan.json",
  272. path + File.separator + "scene" + File.separator + "data" + File.separator + "data" + sceneNum+"/floorplan.json"
  273. );
  274. }
  275. // 文件重命名
  276. FileUtils.copyFile(path + File.separator + "scene" + File.separator + "data" + File.separator + "data" + sceneNum+"/floorplan.json",
  277. path + File.separator + "scene" + File.separator + "data" + File.separator + "data" + sceneNum+"/house.json", true);
  278. //下载 scene.json 改名为 getinfo.json
  279. fYunFileService.downloadFile(String.format(UploadFilePath.DATA_VIEW_PATH, sceneNum) + "scene.json",
  280. path + File.separator + "scene" + File.separator + "data" + File.separator + "data"
  281. + sceneNum + File.separator + "getinfo.json");
  282. //下载场景封面图
  283. String ossImagePath = String.format(UploadFilePath.IMG_VIEW_PATH, sceneNum);
  284. String ImagePath = path + File.separator + "scene" + File.separator + "images" + File.separator + "images" + sceneNum;
  285. FileUtils.downLoadFromUrl(thumb,"cover.jpg", ImagePath);
  286. //拉取images
  287. fYunFileService.downloadFile(ossImagePath + "floorLogoImg.png", ImagePath + "/floorLogoImg.png");
  288. fYunFileService.downloadFile(ossImagePath + "floorplan.png", ImagePath + "/floorplan.png");
  289. fYunFileService.downloadFile(ossImagePath + "logo-main.png", ImagePath + "/logo-main.png");
  290. fYunFileService.downloadFile(ossImagePath + "logo-main-en.png", ImagePath + "/logo-main-en.png");
  291. fYunFileService.downloadFile(ossImagePath + "smallPic.jpg", ImagePath + "/smallPic.jpg");
  292. fYunFileService.downloadFile(ossImagePath + "thumbBigImg.jpg", ImagePath + "/thumbBigImg.jpg");
  293. fYunFileService.downloadFile(ossImagePath + "thumbSmallImg.jpg", ImagePath + "/thumbSmallImg.jpg");
  294. fYunFileService.downloadFileByCommand(ImagePath + "/pan", ossImagePath + "pan");
  295. // 异步执行
  296. CompletableFuture.runAsync(()->{
  297. //压缩zip
  298. try {
  299. log.info("开始异步执行打包逻辑……");
  300. //获取六面体图
  301. fYunFileService.downloadFileByCommand(ImagePath + "/tiles", ossImagePath + "tiles");
  302. //拉取video
  303. fYunFileService.downloadFileByCommand(path + File.separator + "scene" + File.separator + "video" + File.separator + "video" + sceneNum,
  304. String.format(UploadFilePath.VIDEOS_VIEW_PATH, sceneNum));
  305. //拉取voice
  306. fYunFileService.downloadFileByCommand(path + File.separator + "scene" + File.separator + "voice" + File.separator + "voice" + sceneNum,
  307. String.format(UploadFilePath.VOICE_VIEW_PATH, sceneNum));
  308. ZipUtil.zip(path, path + ".zip");
  309. log.info("三维家 zip完成 : " + path);
  310. //上传oss
  311. // 上传到阿里云sso
  312. fYunFileService.uploadFileByCommand(path + ".zip", DIR_NAME + sceneNum + ".zip");
  313. log.info("upload success");
  314. String url = fyunHost + DIR_NAME + sceneNum + ".zip";
  315. log.info("upload url: {}" + url);
  316. scene3dFamilyEntity.setZipLink(url);
  317. scene3dFamilyEntity.setStatus(1);
  318. scene3dFamilyEntity.setZipEndTime(new Date());
  319. scene3dFamilyEntity.setUpdateTime(new Date());
  320. this.updateById(scene3dFamilyEntity);
  321. } catch (Exception e) {
  322. e.printStackTrace();
  323. scene3dFamilyEntity.setStatus(2);
  324. scene3dFamilyEntity.setZipEndTime(new Date());
  325. scene3dFamilyEntity.setUpdateTime(new Date());
  326. this.updateById(scene3dFamilyEntity);
  327. }finally {
  328. FileUtils.deleteDirectory(path);
  329. FileUtils.deleteFile(path + ".zip");
  330. }
  331. });
  332. }catch (Exception e){
  333. scene3dFamilyEntity.setStatus(2);
  334. scene3dFamilyEntity.setZipEndTime(new Date());
  335. scene3dFamilyEntity.setUpdateTime(new Date());
  336. this.updateById(scene3dFamilyEntity);
  337. e.printStackTrace();
  338. throw e;
  339. }
  340. return Result.success();
  341. }
  342. }