Scene3dfamilyServiceImpl.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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.utils.CreateObjUtil;
  17. import com.fdkankan.scene.bean.EulerAngles;
  18. import com.fdkankan.scene.bean.ResponseScene;
  19. import com.fdkankan.scene.common.house.JsonToHouseUtil;
  20. import com.fdkankan.scene.entity.Scene3dfamily;
  21. import com.fdkankan.scene.entity.ScenePro;
  22. import com.fdkankan.scene.mapper.IScene3dfamilyMapper;
  23. import com.fdkankan.scene.service.IScene3dfamilyService;
  24. import com.fdkankan.scene.service.ISceneProService;
  25. import com.fdkankan.web.response.Result;
  26. import com.fdkankan.web.response.ResultData;
  27. import java.io.File;
  28. import java.io.FileWriter;
  29. import java.util.Date;
  30. import java.util.List;
  31. import java.util.concurrent.CompletableFuture;
  32. import lombok.extern.slf4j.Slf4j;
  33. import org.springframework.beans.factory.annotation.Autowired;
  34. import org.springframework.beans.factory.annotation.Value;
  35. import org.springframework.stereotype.Service;
  36. /**
  37. * Created by Hb_zzZ on 2019/6/28.
  38. */
  39. @Slf4j
  40. @Service
  41. public class Scene3dfamilyServiceImpl extends ServiceImpl<IScene3dfamilyMapper, Scene3dfamily> implements IScene3dfamilyService {
  42. private static String DIR_NAME = "downloads/furnished/";
  43. @Autowired
  44. private ISceneProService sceneProService;
  45. @Autowired
  46. private FYunFileServiceInterface fYunFileService;
  47. @Value("${fyun.host}")
  48. private String fyunHost;
  49. @Override
  50. public Result createZip(ScenePro sceneProEntity,long userId) throws Exception{
  51. Scene3dfamily scene3dFamilyEntity = this.findSceneNumByNum(sceneProEntity.getNum(),0);
  52. String path = sceneProEntity.getDataSource() + "_3df" + File.separator + sceneProEntity.getNum();
  53. try {
  54. if(scene3dFamilyEntity == null){
  55. throw new BusinessException(ErrorCode.FAILURE_CODE_5032);
  56. }
  57. log.info("三维家 信息保存 路径 : " + path);
  58. //创建文件夹
  59. FileUtils.createDir(path);
  60. //生成vision.json
  61. this.downloadVisionData(sceneProEntity);
  62. String dataBuf = "data" + File.separator + "data" + sceneProEntity.getNum() + File.separator;
  63. this.visionTxtToPano(ConstantFilePath.SCENE_PATH + dataBuf //本地路径
  64. + "vision.json",path+ File.separator +"scene"+ File.separator+ dataBuf );
  65. //拉取data
  66. fYunFileService.downloadFileByCommand(path + File.separator + "scene" + File.separator + "data" + File.separator + "data" + sceneProEntity.getNum(),
  67. "data" + File.separator + "data" + sceneProEntity.getNum() + File.separator);
  68. //转换house.json
  69. Integer bo = JsonToHouseUtil.ToHouseJson(path + File.separator + "scene" + File.separator + dataBuf);
  70. if(bo!=0){
  71. switch (bo){
  72. case -1:
  73. throw new BusinessException(ErrorCode.FAILURE_CODE_5029.code(), "点位数据为空");
  74. case -2:
  75. throw new BusinessException(ErrorCode.FAILURE_CODE_5029.code(), "点位数据重复");
  76. case -3:
  77. throw new BusinessException(ErrorCode.FAILURE_CODE_5029.code(), "转换异常");
  78. case -4:
  79. throw new BusinessException(ErrorCode.FAILURE_CODE_5029.code(), "源文件不存在");
  80. }
  81. }
  82. //写入 scene.json 改名为 getinfo.json
  83. ResultData result = sceneProService.getInfo(sceneProEntity.getNum());
  84. ResponseScene responseScene = (ResponseScene) result.getData();
  85. JSONObject dataJson = (JSONObject) JSONObject.toJSON(responseScene);
  86. FileUtils.writeFile(path + File.separator + "scene" + File.separator + "data" + File.separator + "data"
  87. + sceneProEntity.getNum() + File.separator + "getinfo.json", dataJson.toString());
  88. //下载场景封面图
  89. String ossImagePath = "images" + File.separator + "images" + sceneProEntity.getNum() + File.separator;
  90. String ImagePath = path + File.separator + "scene" + File.separator + "images" + File.separator + "images" + sceneProEntity.getNum();
  91. FileUtils.downLoadFromUrl(sceneProEntity.getThumb(),"cover.jpg", ImagePath);
  92. //拉取images
  93. fYunFileService.downloadFile(ossImagePath + "floorLogoImg.png", ImagePath + "/floorLogoImg.png");
  94. fYunFileService.downloadFile(ossImagePath + "floorplan.png", ImagePath + "/floorplan.png");
  95. fYunFileService.downloadFile(ossImagePath + "logo-main.png", ImagePath + "/logo-main.png");
  96. fYunFileService.downloadFile(ossImagePath + "logo-main-en.png", ImagePath + "/logo-main-en.png");
  97. fYunFileService.downloadFile(ossImagePath + "smallPic.jpg", ImagePath + "/smallPic.jpg");
  98. fYunFileService.downloadFile(ossImagePath + "thumbBigImg.jpg", ImagePath + "/thumbBigImg.jpg");
  99. fYunFileService.downloadFile(ossImagePath + "thumbSmallImg.jpg", ImagePath + "/thumbSmallImg.jpg");
  100. fYunFileService.downloadFileByCommand(ImagePath + "/pan", ossImagePath + "pan");
  101. // 异步执行
  102. CompletableFuture.runAsync(()->{
  103. //压缩zip
  104. try {
  105. log.info("开始异步执行打包逻辑……");
  106. //获取六面体图
  107. fYunFileService.downloadFileByCommand(ImagePath + "/tiles", ossImagePath + "tiles");
  108. //拉取video
  109. fYunFileService.downloadFileByCommand(path + File.separator + "scene" + File.separator + "video" + File.separator + "video" + sceneProEntity.getNum(),
  110. "video" + File.separator + "video" + sceneProEntity.getNum() + File.separator);
  111. //拉取voice
  112. fYunFileService.downloadFileByCommand(path + File.separator + "scene" + File.separator + "voice" + File.separator + "voice" + sceneProEntity.getNum(),
  113. "voice" + File.separator + "voice" + sceneProEntity.getNum() + File.separator);
  114. ZipUtil.zip(path, path + ".zip");
  115. log.info("三维家 zip完成 : " + path);
  116. //上传oss
  117. // 上传到阿里云sso
  118. fYunFileService.uploadFileByCommand(path + ".zip", DIR_NAME + sceneProEntity.getNum() + ".zip");
  119. log.info("upload success");
  120. String url = fyunHost + DIR_NAME + sceneProEntity.getNum() + ".zip";
  121. log.info("upload url: {}" + url);
  122. scene3dFamilyEntity.setZipLink(url);
  123. scene3dFamilyEntity.setStatus(1);
  124. scene3dFamilyEntity.setZipEndTime(new Date());
  125. scene3dFamilyEntity.setUpdateTime(new Date());
  126. this.updateById(scene3dFamilyEntity);
  127. } catch (Exception e) {
  128. e.printStackTrace();
  129. scene3dFamilyEntity.setStatus(2);
  130. scene3dFamilyEntity.setZipEndTime(new Date());
  131. scene3dFamilyEntity.setUpdateTime(new Date());
  132. this.updateById(scene3dFamilyEntity);
  133. }finally {
  134. FileUtils.deleteDirectory(path);
  135. FileUtils.deleteFile(path + ".zip");
  136. }
  137. });
  138. }catch (Exception e){
  139. scene3dFamilyEntity.setStatus(2);
  140. scene3dFamilyEntity.setZipEndTime(new Date());
  141. scene3dFamilyEntity.setUpdateTime(new Date());
  142. this.updateById(scene3dFamilyEntity);
  143. e.printStackTrace();
  144. throw e;
  145. }
  146. return Result.success();
  147. }
  148. @Override
  149. public Scene3dfamily findSceneNumByNum(String num,Integer status){
  150. LambdaQueryWrapper<Scene3dfamily> wrapper = new LambdaQueryWrapper<Scene3dfamily>()
  151. .eq(Scene3dfamily::getSceneNum, num);
  152. if(status != null){
  153. wrapper.eq(Scene3dfamily::getStatus, status);
  154. }
  155. List<Scene3dfamily> list = this.list(wrapper);
  156. if(CollUtil.isEmpty(list)){
  157. return null;
  158. }
  159. return list.get(0);
  160. }
  161. @Override
  162. public JSONObject findSceneNumByNum2(ScenePro sceneProEntity){
  163. JSONObject jsonObject = new JSONObject();
  164. Scene3dfamily scene3dFamilyEntity = this.findSceneNumByNum(sceneProEntity.getNum(), null);
  165. if(scene3dFamilyEntity!=null){
  166. jsonObject.put("url",scene3dFamilyEntity.getZipLink() == null ?"":scene3dFamilyEntity.getZipLink());
  167. jsonObject.put("status",scene3dFamilyEntity.getStatus());
  168. }else{
  169. jsonObject.put("url","");
  170. jsonObject.put("status",3);
  171. }
  172. //验证是否在打包
  173. return jsonObject;
  174. }
  175. @Override
  176. public void updateSceneNumByNum (ScenePro sceneProEntity){
  177. this.update(new LambdaUpdateWrapper<Scene3dfamily>().set(Scene3dfamily::getRecStatus,
  178. RecStatus.DISABLE.code()).eq(Scene3dfamily::getSceneNum, sceneProEntity.getNum()));
  179. }
  180. private void downloadVisionData(ScenePro sceneProEntity) throws Exception {
  181. StringBuffer dataBuf = new StringBuffer().append("data").append(File.separator)
  182. .append("data").append(sceneProEntity.getNum()).append(File.separator);
  183. StringBuffer imagesBuf = new StringBuffer().append("images").append(File.separator)
  184. .append("images").append(sceneProEntity.getNum()).append(File.separator);
  185. StringBuffer dataBuffer = new StringBuffer(ConstantFilePath.SCENE_PATH).append(dataBuf.toString());
  186. StringBuffer imagesBuffer = new StringBuffer(ConstantFilePath.SCENE_PATH).append(imagesBuf.toString());
  187. fYunFileService.downloadFile(imagesBuf.toString() + "vision.modeldata", dataBuffer.toString() + "vision.modeldata");
  188. File file = new File(dataBuffer.toString() + "vision.modeldata");
  189. if(!file.exists()) {
  190. throw new BusinessException(ErrorCode.FAILURE_CODE_5012);
  191. }
  192. CreateObjUtil.convertVisionmodeldataToTxt(dataBuffer.toString() + "vision.modeldata", dataBuffer.toString() + "vision.json");
  193. fYunFileService.uploadFile(dataBuffer.toString() + "vision.json", dataBuf.toString() + "vision.json");
  194. }
  195. /**
  196. *
  197. * @param src vision.json 路径
  198. * @param tag pano.json 目录路径
  199. */
  200. private void visionTxtToPano(String src,String tag){
  201. try {
  202. log.info("QuaternionToEulerUtil---------src:{},tag:{}",src,tag);
  203. String str = FileUtil.readUtf8String(src);
  204. JSONObject jsonObject = JSONObject.parseObject(str);
  205. JSONArray sweepLocations = jsonObject.getJSONArray("sweepLocations");
  206. for (Object o : sweepLocations) {
  207. JSONObject obj = (JSONObject) o;
  208. JSONObject pose = obj.getJSONObject("pose");
  209. JSONObject rotation = pose.getJSONObject("rotation");
  210. JSONObject translation = pose.getJSONObject("translation");
  211. Double w = rotation.getDouble("w");
  212. Double x = rotation.getDouble("x");
  213. Double y = rotation.getDouble("y");
  214. Double z = rotation.getDouble("z");
  215. Double x1 = translation.getDouble("x");
  216. Double y1 = translation.getDouble("y");
  217. Double z1 = translation.getDouble("z");
  218. JSONObject newRotation = new JSONObject();
  219. EulerAngles eulerAngles = new EulerAngles(w,x,y,z,x1,y1,z1);
  220. newRotation.put("x",eulerAngles.x);
  221. newRotation.put("y",eulerAngles.y);
  222. newRotation.put("z",eulerAngles.z);
  223. pose.put("target",newRotation);
  224. pose.remove("rotation");
  225. }
  226. File file = new File(tag);
  227. if(!file.exists()){
  228. file.mkdirs();
  229. }
  230. FileWriter fw = new FileWriter(tag+ "pano.json");
  231. fw.write(jsonObject.toJSONString());
  232. fw.close();
  233. }catch (Exception e){
  234. log.error("QuaternionToEulerUtil---------error:",e);
  235. e.printStackTrace();
  236. }
  237. }
  238. }