BoxModelServiceImpl.java 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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.CharsetUtil;
  5. import cn.hutool.core.util.StrUtil;
  6. import cn.hutool.core.util.ZipUtil;
  7. import com.alibaba.fastjson.JSONArray;
  8. import com.alibaba.fastjson.JSONObject;
  9. import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
  10. import com.fdkankan.common.constant.ErrorCode;
  11. import com.fdkankan.common.constant.OperationType;
  12. import com.fdkankan.common.constant.ServerCode;
  13. import com.fdkankan.common.exception.BusinessException;
  14. import com.fdkankan.common.util.FileUtils;
  15. import com.fdkankan.common.util.OBJToGLBUtil;
  16. import com.fdkankan.fyun.face.FYunFileServiceInterface;
  17. import com.fdkankan.model.constants.ConstantFilePath;
  18. import com.fdkankan.model.constants.UploadFilePath;
  19. import com.fdkankan.model.utils.ComputerUtil;
  20. import com.fdkankan.scene.bean.BoxModelBean;
  21. import com.fdkankan.scene.entity.SceneEditInfo;
  22. import com.fdkankan.scene.entity.ScenePlus;
  23. import com.fdkankan.scene.entity.ScenePlusExt;
  24. import com.fdkankan.scene.service.IBoxModelService;
  25. import com.fdkankan.scene.service.ISceneEditInfoService;
  26. import com.fdkankan.scene.service.IScenePlusExtService;
  27. import com.fdkankan.scene.service.IScenePlusService;
  28. import com.fdkankan.scene.vo.BaseJsonDataParamVO;
  29. import com.fdkankan.scene.vo.DeleteSidParamVO;
  30. import com.fdkankan.web.response.ResultData;
  31. import com.google.common.collect.Lists;
  32. import java.io.File;
  33. import java.io.IOException;
  34. import java.util.*;
  35. import lombok.extern.slf4j.Slf4j;
  36. import org.springframework.beans.factory.annotation.Autowired;
  37. import org.springframework.stereotype.Service;
  38. import org.springframework.web.multipart.MultipartFile;
  39. import javax.annotation.Resource;
  40. /**
  41. * <p>
  42. * TODO
  43. * </p>
  44. *
  45. * @author dengsixing
  46. * @since 2022/10/19
  47. **/
  48. @Slf4j
  49. @Service
  50. public class BoxModelServiceImpl implements IBoxModelService {
  51. @Resource
  52. private FYunFileServiceInterface fYunFileService;
  53. @Autowired
  54. private IScenePlusService scenePlusService;
  55. @Autowired
  56. private ISceneEditInfoService sceneEditInfoService;
  57. @Autowired
  58. private IScenePlusExtService scenePlusExtService;
  59. @Override
  60. public ResultData uploadBoxModel(String num, String sid, MultipartFile file) throws Exception {
  61. ScenePlus scenePlus = scenePlusService.getScenePlusByNum(num);
  62. if(Objects.isNull(scenePlus)){
  63. throw new BusinessException(ErrorCode.FAILURE_CODE_5005);
  64. }
  65. ScenePlusExt scenePlusExt = scenePlusExtService.getScenePlusExtByPlusId(scenePlus.getId());
  66. String bucket = scenePlusExt.getYunFileBucket();
  67. if(!file.getOriginalFilename().endsWith(".zip")){
  68. throw new BusinessException(ErrorCode.FAILURE_CODE_7015);
  69. }
  70. // if(!FileUtils.checkFileSizeIsLimit(file.getSize(), 5, "M")){
  71. // throw new BusinessException(ErrorCode.FAILURE_CODE_7023, "5M");
  72. // }
  73. String path = String.format(ConstantFilePath.SCENE_USER_PATH_V4, num) + "boxModel/" + sid + "/";
  74. try {
  75. String zipPath = path + UUID.randomUUID() + ".zip";
  76. String srcPath = path + "data/";
  77. String glbPath = path + sid + ".glb";
  78. FileUtil.del(path);
  79. FileUtil.mkParentDirs(zipPath);
  80. file.transferTo(new File(zipPath));
  81. //解压
  82. ZipUtil.unzip(zipPath,srcPath, CharsetUtil.CHARSET_GBK);
  83. //校验是否包含目录,如果包含目录提示错误
  84. File srcFile = new File(srcPath);
  85. Arrays.stream(srcFile.listFiles()).forEach(subFile->{
  86. if(subFile.isDirectory()){
  87. throw new BusinessException(ErrorCode.FAILURE_CODE_5065);
  88. }
  89. });
  90. //转glb
  91. OBJToGLBUtil.objToGlb(srcPath, glbPath);
  92. if(!ComputerUtil.checkComputeCompleted(glbPath, 10, 2000)){
  93. throw new BusinessException(ErrorCode.FAILURE_CODE_7013);
  94. }
  95. //上传glb
  96. fYunFileService.uploadFile(bucket, glbPath, String.format(UploadFilePath.USER_EDIT_PATH, num) + "boxModels/" + sid + ".glb");
  97. //上传原始obj相关文件
  98. String objPath = String.format(UploadFilePath.USER_EDIT_PATH, num) + "boxModels/" + sid + "/";
  99. //先删除旧的,只需要保留最新的
  100. fYunFileService.deleteFolder(objPath);
  101. List<File> files = FileUtil.loopFiles(srcPath);
  102. files.stream().forEach(v->{
  103. String ossKey = String.format(UploadFilePath.USER_EDIT_PATH, num) + "boxModels/" + sid + "/" + v.getAbsolutePath().replace(srcPath, "");
  104. fYunFileService.uploadFile(v.getAbsolutePath(), ossKey);
  105. });
  106. return ResultData.ok(sid + ".glb");
  107. }catch (Exception e){
  108. log.error("上传三维模型失败", e);
  109. throw e;
  110. }finally {
  111. FileUtil.del(path);
  112. }
  113. }
  114. public static void main(String[] args) {
  115. ZipUtil.unzip("F:\\test\\新建文件夹\\police模型\\police模型.zip","F:\\test\\新建文件夹\\police模型\\maps");
  116. }
  117. @Override
  118. public ResultData saveBoxModel(BaseJsonDataParamVO param) throws Exception {
  119. JSONObject data = param.getData();
  120. String sid = data.getString("sid");
  121. if(StrUtil.isEmpty(sid)){
  122. throw new BusinessException(ServerCode.PARAM_REQUIRED, sid);
  123. }
  124. ScenePlus scenePlus = scenePlusService.getScenePlusByNum(param.getNum());
  125. if(Objects.isNull(scenePlus))
  126. throw new BusinessException(ErrorCode.FAILURE_CODE_5005);
  127. ScenePlusExt scenePlusExt = scenePlusExtService.getScenePlusExtByPlusId(scenePlus.getId());
  128. SceneEditInfo sceneEditInfo = sceneEditInfoService.getByScenePlusId(scenePlus.getId());
  129. //生成boxVideos数据
  130. String boxPhotos = this.createBoxModels(param.getNum(), scenePlusExt.getYunFileBucket(), sid, data, sceneEditInfo, OperationType.ADDORUPDATE.code());
  131. //更新数据库
  132. this.updateBoxModels(sceneEditInfo, boxPhotos);
  133. return ResultData.ok();
  134. }
  135. @Override
  136. public ResultData deleteBoxModel(DeleteSidParamVO param) throws Exception {
  137. ScenePlus scenePlus = scenePlusService.getScenePlusByNum(param.getNum());
  138. if(Objects.isNull(scenePlus))
  139. throw new BusinessException(ErrorCode.FAILURE_CODE_5005);
  140. ScenePlusExt scenePlusExt = scenePlusExtService.getScenePlusExtByPlusId(scenePlus.getId());
  141. SceneEditInfo sceneEditInfo = sceneEditInfoService.getByScenePlusId(scenePlus.getId());
  142. //根据sid移除json
  143. String boxModels = this.createBoxModels(param.getNum(), scenePlusExt.getYunFileBucket(), param.getSid(), null, sceneEditInfo, OperationType.DELETE.code());
  144. //写数据库
  145. this.updateBoxModels(sceneEditInfo, boxModels);
  146. //删除oss文件
  147. String glbKey = String.format(UploadFilePath.USER_EDIT_PATH, param.getNum()) + "boxModels/" + param.getSid() + ".glb";
  148. fYunFileService.deleteFile(glbKey);
  149. String modelPath = String.format(UploadFilePath.USER_EDIT_PATH, param.getNum()) + "boxModels/" + param.getSid();
  150. fYunFileService.deleteFolder(modelPath);
  151. return ResultData.ok();
  152. }
  153. private void updateBoxModels(SceneEditInfo sceneEditInfo, String boxModels){
  154. sceneEditInfoService.update(new LambdaUpdateWrapper<SceneEditInfo>()
  155. .set(SceneEditInfo::getBoxModels, boxModels)
  156. .setSql("version = version + 1")
  157. .eq(SceneEditInfo::getId, sceneEditInfo.getId()));
  158. }
  159. private String createBoxModels(String num, String bucket, String sid, JSONObject data, SceneEditInfo sceneEditInfo, int type) throws Exception{
  160. String boxModels = null;
  161. if(sceneEditInfo != null){
  162. boxModels = sceneEditInfo.getBoxModels();
  163. }
  164. JSONArray boxModelsJson = null;
  165. if (StrUtil.isNotEmpty(boxModels)) {
  166. boxModelsJson = JSONArray.parseArray(boxModels);
  167. }else {
  168. boxModelsJson = new JSONArray();
  169. }
  170. String result = null;
  171. //删除
  172. if(type == OperationType.DELETE.code()){
  173. Set<String> deleteFile = new HashSet<>();
  174. if(boxModelsJson.size() == 0)
  175. return null;
  176. for(int i=0;i<boxModelsJson.size();++i){
  177. JSONObject ele = boxModelsJson.getJSONObject(i);
  178. if(ele.getString("sid").equals(sid)){
  179. boxModelsJson.remove(i);
  180. deleteFile.add(String.format(UploadFilePath.USER_EDIT_PATH, num) + sid + ".glb");
  181. }
  182. }
  183. //删除资源文件
  184. if(CollUtil.isNotEmpty(deleteFile))
  185. deleteFile.stream().forEach(key -> {
  186. try {
  187. fYunFileService.deleteFile(bucket, key);
  188. } catch (IOException e) {
  189. log.warn("oss删除文件失败,key:{}", key);
  190. }
  191. });
  192. }else{
  193. //更新
  194. boolean exist = false;
  195. for(int i=0;i<boxModelsJson.size();++i){
  196. JSONObject ele = boxModelsJson.getJSONObject(i);
  197. if(ele.getString("sid").equals(sid)){
  198. data.put("createTime", ele.getLong("createTime"));
  199. boxModelsJson.set(i, data);
  200. exist = true;
  201. }
  202. }
  203. //新增
  204. if(!exist){
  205. data.put("createTime", Calendar.getInstance().getTimeInMillis());
  206. boxModelsJson.add(data);
  207. }
  208. }
  209. if(boxModelsJson.size() != 0){
  210. List<BoxModelBean> list = Lists.newArrayList();
  211. for (Object o : boxModelsJson) {
  212. JSONObject jsonObject = (JSONObject)o;
  213. list.add(BoxModelBean.builder().createTime(jsonObject.getLong("createTime")).boxModel(jsonObject).build());
  214. }
  215. //按创建时间倒叙排序
  216. list.sort(Comparator.comparingLong(BoxModelBean::getCreateTime).reversed());
  217. // list转JSONArray
  218. JSONArray array = new JSONArray();
  219. list.stream().forEach(bean->{
  220. array.add(bean.getBoxModel());
  221. });
  222. result = array.toJSONString();
  223. }
  224. return result;
  225. }
  226. }