ModelServiceImpl.java 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. package com.fdkankan.fusion.service.impl;
  2. import java.io.File;
  3. import java.io.IOException;
  4. import java.math.BigDecimal;
  5. import java.util.Arrays;
  6. import java.util.List;
  7. import java.util.stream.Collectors;
  8. import cn.hutool.core.io.FileUtil;
  9. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  10. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
  11. import com.fdkankan.fusion.common.PageInfo;
  12. import com.fdkankan.fusion.common.util.*;
  13. import com.fdkankan.fusion.common.FilePath;
  14. import com.fdkankan.fusion.common.ResultCode;
  15. import com.fdkankan.fusion.entity.CaseEntity;
  16. import com.fdkankan.fusion.entity.CaseNumEntity;
  17. import com.fdkankan.fusion.entity.FusionNum;
  18. import com.fdkankan.fusion.entity.Model;
  19. import com.fdkankan.fusion.exception.BusinessException;
  20. import com.fdkankan.fusion.mapper.IModelMapper;
  21. import com.fdkankan.fusion.request.ModelPram;
  22. import com.fdkankan.fusion.request.ScenePram;
  23. import com.fdkankan.fusion.response.SceneVo;
  24. import com.fdkankan.fusion.service.ICaseNumService;
  25. import com.fdkankan.fusion.service.ICaseService;
  26. import com.fdkankan.fusion.service.IFusionNumService;
  27. import com.fdkankan.fusion.service.IModelService;
  28. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  29. import jdk.nashorn.tools.Shell;
  30. import org.apache.commons.lang3.StringUtils;
  31. import org.springframework.beans.BeanUtils;
  32. import org.springframework.beans.factory.annotation.Autowired;
  33. import org.springframework.beans.factory.annotation.Value;
  34. import org.springframework.stereotype.Service;
  35. import org.springframework.web.multipart.MultipartFile;
  36. import javax.xml.transform.Result;
  37. /**
  38. * <p>
  39. * 服务实现类
  40. * </p>
  41. *
  42. * @author
  43. * @since 2022-08-03
  44. */
  45. @Service
  46. public class ModelServiceImpl extends ServiceImpl<IModelMapper, Model> implements IModelService {
  47. @Autowired
  48. UploadToOssUtil uploadToOssUtil;
  49. @Autowired
  50. UploadService uploadService;
  51. @Autowired
  52. ICaseNumService caseNumService;
  53. @Autowired
  54. ICaseService caseService;
  55. @Autowired
  56. IFusionNumService fusionNumService;
  57. @Value("${local.obj_path}")
  58. private String OBJ_PATH;
  59. @Value("${local.glb_path}")
  60. private String GLB_PATH;
  61. @Value("${upload.query-path}")
  62. private String queryPath;
  63. @Override
  64. public void uploadObj(MultipartFile file, String username) throws Exception {
  65. if(file.isEmpty()){
  66. throw new BusinessException(ResultCode.UPLOAD_FILE_NO_EXIST);
  67. }
  68. if(file.getSize()>10 * 1024 * 1024 * 100){
  69. throw new BusinessException(ResultCode.UPLOAD_FILE_TO_LONG);
  70. }
  71. //获取文件名
  72. String fileName = file.getOriginalFilename();
  73. if(StringUtils.isEmpty(fileName)){
  74. throw new BusinessException(ResultCode.UPLOAD_FILE_NO_EXIST);
  75. }
  76. if(com.fdkankan.fusion.common.util.StringUtils.isChinese(fileName)){
  77. throw new BusinessException(ResultCode.UPLOAD_FILE_CHINA_NAME);
  78. }
  79. if(!fileName.toLowerCase().contains("zip") && !fileName.toLowerCase().contains("rar")){
  80. throw new BusinessException(ResultCode.UPLOAD_FILE_TYPE_ERROR);
  81. }
  82. //获取文件后缀名
  83. String modelName = fileName.substring(0,fileName.lastIndexOf("."));
  84. Model model = new Model();
  85. model.setModelTitle(modelName);
  86. model.setModelSize(FileWriterUtil.setFileSize(file.getSize()));
  87. model.setUserName(username);
  88. this.save(model);
  89. File newObjFile = null;
  90. File objPathFile = null;
  91. File mntFile = null;
  92. String objPath = String.format(OBJ_PATH , "modelId_"+model.getModelId()) ;
  93. try {
  94. String glbOssPath = String.format(FilePath.GLB_OSS_PATH, model.getModelId());
  95. newObjFile = new File(objPath +"/" + fileName);
  96. if(!newObjFile.getParentFile().exists()){
  97. newObjFile.mkdirs();
  98. }
  99. file.transferTo(newObjFile);
  100. if(fileName.toLowerCase().contains("zip")){
  101. ShellUtil.unZip(newObjFile.getPath(),objPath);
  102. }
  103. if(fileName.toLowerCase().contains("rar")){
  104. ShellUtil.unRar(newObjFile.getPath(),objPath);
  105. }
  106. objPathFile = new File(objPath );
  107. if(!objPathFile.isDirectory()){
  108. throw new BusinessException(ResultCode.UPLOAD_FILE_TYPE_ERROR);
  109. }
  110. File file1 = FileWriterUtil.getObjLasPlyFile(objPathFile);
  111. if(file1 == null){
  112. throw new BusinessException(ResultCode.UPLOAD_FILE_TYPE_ERROR);
  113. }
  114. String name = file1.getName();
  115. if(name.contains("obj") || name.contains("OBJ")){
  116. glbOssPath = glbOssPath.replace("mesh.glb",file1.getName().replace("obj","glb"));
  117. model.setModelDateType("obj");
  118. model.setModelType("glb");
  119. OBJToGLBUtil.objToGlb(objPath,name, file1.getPath().replace("obj","glb"));
  120. uploadToOssUtil.uploadOss(file1.getPath(),glbOssPath);
  121. }
  122. if(name.contains(".ply")){
  123. model.setModelDateType("ply");
  124. model.setModelType("ply");
  125. }
  126. if(name.contains(".las")){
  127. model.setModelDateType("las");
  128. model.setModelType("las");
  129. }
  130. if("las".equals(model.getModelType()) || "ply".equals(model.getModelType()) ){
  131. mntFile = OBJToGLBUtil.lasOrPlyToBin(file1);
  132. glbOssPath = mntFile.getPath().replace("/mnt/","")+"/webcloud";
  133. uploadToOssUtil.uploadFileOss(mntFile );
  134. }
  135. model.setModelObjUrl(objPath);
  136. model.setModelGlbUrl(queryPath + glbOssPath);
  137. model.setCreateStatus(1); //上传成功
  138. this.saveOrUpdate(model);
  139. }catch (Exception e){
  140. model.setCreateStatus(-1);
  141. this.saveOrUpdate(model);
  142. throw e;
  143. }finally {
  144. if(newObjFile!=null){
  145. FileUtil.del(newObjFile);
  146. }
  147. if(objPathFile!=null){
  148. FileUtil.del(objPathFile);
  149. }
  150. if(mntFile!=null){
  151. FileUtil.del(mntFile);
  152. }
  153. }
  154. }
  155. @Override
  156. public PageInfo pageList(ModelPram param, String token) {
  157. String username = JwtUtil.getUsername(token);
  158. LambdaQueryWrapper<Model> wrapper = new LambdaQueryWrapper<>();
  159. wrapper.eq(Model::getUserName,username);
  160. wrapper.eq(Model::getType,3);
  161. if(param.getStatus()!=null){ //参数2为成功,数据库中成功为1
  162. wrapper.eq(Model::getCreateStatus,param.getStatus() == 2 ? 1 :param.getStatus());
  163. }
  164. if(StringUtils.isNotBlank(param.getModelTitle())){
  165. wrapper.like(Model::getModelTitle,param.getModelTitle());
  166. }
  167. wrapper.orderByDesc(Model::getCreateTime);
  168. Page<Model> page = this.page(new Page<>(param.getPageNum(),param.getPageSize()),wrapper);
  169. for (Model model : page.getRecords()) {
  170. if(model.getType() == 3 && StringUtils.isEmpty(model.getNum())) {
  171. model.setNum(model.getModelId().toString());
  172. }
  173. }
  174. return PageInfo.PageInfo(page);
  175. }
  176. @Override
  177. public void delete(Integer modelId) {
  178. List<CaseNumEntity> caseNumEntityList = caseNumService.getByNum(modelId.toString());
  179. List<FusionNum> fusionNumList = fusionNumService.getByNum(modelId.toString());
  180. if(caseNumEntityList.size() >0 || fusionNumList.size() >0){
  181. StringBuilder title = new StringBuilder();
  182. List<Integer> caseIdIds = caseNumEntityList.parallelStream().map(CaseNumEntity::getCaseId).collect(Collectors.toList());
  183. if(caseIdIds.size() >0){
  184. List<CaseEntity> list = caseService.getByIds(caseIdIds);
  185. List<String> collect = list.parallelStream().map(CaseEntity::getCaseTitle).collect(Collectors.toList());
  186. for (String str : collect) {
  187. title.append(str).append(",");
  188. }
  189. if(title.length()>0){
  190. title.delete(title.length()-1,title.length());
  191. }
  192. }
  193. throw new BusinessException(ResultCode.CASE_USE.code, String.format(ResultCode.CASE_USE.msg,title));
  194. }
  195. Model model = this.getById(modelId);
  196. if(model == null ){
  197. throw new BusinessException(ResultCode.MODEL_NOT_EXIST);
  198. }
  199. this.removeById(modelId);
  200. fusionNumService.deleteByModelId(modelId);
  201. if(StringUtils.isNotBlank(model.getModelGlbUrl())){
  202. uploadService.deleteOssUrl(model.getModelGlbUrl());
  203. }
  204. }
  205. @Override
  206. public List<Model> getListByNum(List<String> numList) {
  207. LambdaQueryWrapper<Model> wrapper = new LambdaQueryWrapper<>();
  208. wrapper.in(Model::getNum,numList);
  209. return this.list(wrapper);
  210. }
  211. @Override
  212. public List<Model> getListByModelIds(List<Integer> modelIds) {
  213. LambdaQueryWrapper<Model> wrapper = new LambdaQueryWrapper<>();
  214. wrapper.in(Model::getModelId,modelIds);
  215. return this.list(wrapper);
  216. }
  217. @Override
  218. public List<Model> getListByModelIdStrs(List<String> numList) {
  219. LambdaQueryWrapper<Model> wrapper = new LambdaQueryWrapper<>();
  220. wrapper.in(Model::getModelId,numList);
  221. wrapper.orderByDesc(Model::getCreateTime);
  222. return this.list(wrapper);
  223. }
  224. @Override
  225. public void deleteByNum(List<String> numList) {
  226. LambdaQueryWrapper<Model> wrapper = new LambdaQueryWrapper<>();
  227. wrapper.in(Model::getNum,numList);
  228. this.remove(wrapper);
  229. }
  230. @Override
  231. public List<Model> getByUserName(String username) {
  232. LambdaQueryWrapper<Model> wrapper = new LambdaQueryWrapper<>();
  233. wrapper.eq(Model::getUserName,username)
  234. .eq(Model::getType,3);
  235. return this.list(wrapper);
  236. }
  237. @Override
  238. public Model getIsNullNewByNum(String num) {
  239. LambdaQueryWrapper<Model> wrapper = new LambdaQueryWrapper<>();
  240. wrapper.eq(Model::getNum,num);
  241. Model model = this.getOne(wrapper);
  242. if(model == null){
  243. model = new Model();
  244. }
  245. return model;
  246. }
  247. @Override
  248. public Object getInfo(Integer modelId) {
  249. Model model = this.getById(modelId);
  250. if(model == null){
  251. throw new BusinessException(ResultCode.SCENE_NOT_EXIST);
  252. }
  253. SceneVo sceneVo = new SceneVo();
  254. BeanUtils.copyProperties(model,sceneVo);
  255. return sceneVo;
  256. }
  257. }