ModelServiceImpl.java 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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 com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  8. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
  9. import com.fdkankan.common.response.PageInfo;
  10. import com.fdkankan.common.util.CreateObjUtil;
  11. import com.fdkankan.common.util.JwtUtil;
  12. import com.fdkankan.fusion.common.FilePath;
  13. import com.fdkankan.fusion.common.ResultCode;
  14. import com.fdkankan.fusion.common.util.OBJToGLBUtil;
  15. import com.fdkankan.fusion.entity.CaseNumEntity;
  16. import com.fdkankan.fusion.entity.FusionNum;
  17. import com.fdkankan.fusion.entity.Model;
  18. import com.fdkankan.fusion.exception.BusinessException;
  19. import com.fdkankan.fusion.mapper.IModelMapper;
  20. import com.fdkankan.fusion.request.ModelPram;
  21. import com.fdkankan.fusion.request.ScenePram;
  22. import com.fdkankan.fusion.service.ICaseNumService;
  23. import com.fdkankan.fusion.service.IFusionNumService;
  24. import com.fdkankan.fusion.service.IModelService;
  25. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  26. import com.fdkankan.fyun.oss.UploadToOssUtil;
  27. import org.apache.commons.lang3.StringUtils;
  28. import org.apache.tools.zip.ZipFile;
  29. import org.springframework.beans.factory.annotation.Autowired;
  30. import org.springframework.beans.factory.annotation.Value;
  31. import org.springframework.stereotype.Service;
  32. import org.springframework.web.multipart.MultipartFile;
  33. /**
  34. * <p>
  35. * 服务实现类
  36. * </p>
  37. *
  38. * @author
  39. * @since 2022-08-03
  40. */
  41. @Service
  42. public class ModelServiceImpl extends ServiceImpl<IModelMapper, Model> implements IModelService {
  43. @Autowired
  44. UploadToOssUtil uploadToOssUtil;
  45. @Autowired
  46. ICaseNumService caseNumService;
  47. @Autowired
  48. IFusionNumService fusionNumService;
  49. @Value("${local.obj_path}")
  50. private String OBJ_PATH;
  51. @Value("${local.glb_path}")
  52. private String GLB_PATH;
  53. @Value("${upload.query-path}")
  54. private String queryPath;
  55. @Override
  56. public void uploadObj(MultipartFile file, String username) throws Exception {
  57. if(file.isEmpty()){
  58. throw new BusinessException(ResultCode.UPLOAD_FILE_NO_EXIST);
  59. }
  60. if(file.getSize()>10 * 1024 * 1024 * 100){
  61. System.out.println(file.getSize());
  62. throw new BusinessException(ResultCode.UPLOAD_FILE_TO_LONG);
  63. }
  64. //获取文件名
  65. String fileName = file.getOriginalFilename();
  66. if(StringUtils.isEmpty(fileName)){
  67. throw new BusinessException(ResultCode.UPLOAD_FILE_NO_EXIST);
  68. }
  69. if(!fileName.toLowerCase().contains("zip") && !fileName.toLowerCase().contains("rar")){
  70. throw new BusinessException(ResultCode.UPLOAD_FILE_TYPE_ERROR);
  71. }
  72. //获取文件后缀名
  73. String modelName = fileName.substring(0,fileName.lastIndexOf("."));
  74. Model model = new Model();
  75. model.setModelTitle(modelName);
  76. model.setModelDateType("obj");
  77. model.setModelSize(file.getSize());
  78. model.setUserName(username);
  79. this.save(model);
  80. try {
  81. String objPath = String.format(OBJ_PATH , "modelId_"+model.getModelId());
  82. String glbPath = String.format(GLB_PATH , "modelId_"+model.getModelId());
  83. String glbOssPath = String.format(FilePath.GLB_OSS_PATH, model.getModelId());
  84. model.setModelObjUrl(objPath);
  85. model.setModelGlbUrl(queryPath +"/"+glbPath + "/"+modelName +"/mesh.glb");
  86. File newObjFile = new File(objPath +"/" + fileName);
  87. if(!newObjFile.getParentFile().exists()){
  88. newObjFile.mkdirs();
  89. }
  90. file.transferTo(newObjFile);
  91. if(fileName.toLowerCase().contains("zip")){
  92. CreateObjUtil.unZip(newObjFile.getPath(),objPath);
  93. }
  94. if(fileName.toLowerCase().contains("rar")){
  95. CreateObjUtil.unRar(newObjFile.getPath(),objPath);
  96. }
  97. OBJToGLBUtil.objToGlb(objPath+"/"+modelName,glbPath+"/"+modelName +"/mesh.glb");
  98. uploadToOssUtil.upload(glbPath,glbOssPath);
  99. model.setCreateStatus(1); //上传成功
  100. this.updateById(model);
  101. }catch (Exception e){
  102. model.setCreateStatus(-1);
  103. this.updateById(model);
  104. e.printStackTrace();
  105. throw e;
  106. }
  107. }
  108. @Override
  109. public PageInfo pageList(ModelPram param, String token) {
  110. String username = JwtUtil.getUsername(token);
  111. LambdaQueryWrapper<Model> wrapper = new LambdaQueryWrapper<>();
  112. wrapper.eq(Model::getUserName,username);
  113. wrapper.eq(Model::getType,3);
  114. if(StringUtils.isNotBlank(param.getModelTitle())){
  115. wrapper.like(Model::getModelTitle,param.getModelTitle());
  116. }
  117. wrapper.orderByDesc(Model::getCreateTime);
  118. Page<Model> page = this.page(new Page<>(param.getPageNum(),param.getPageSize()),wrapper);
  119. return PageInfo.PageInfo(page);
  120. }
  121. @Override
  122. public void delete(Integer modelId) {
  123. List<CaseNumEntity> caseNumEntityList = caseNumService.getByNum(modelId.toString());
  124. List<FusionNum> fusionNumList = fusionNumService.getByNum(modelId.toString());
  125. if(caseNumEntityList.size() >0 || fusionNumList.size() >0){
  126. throw new BusinessException(ResultCode.CASE_USE);
  127. }
  128. this.removeById(modelId);
  129. }
  130. @Override
  131. public List<Model> getListByModeId(List<String> numList) {
  132. LambdaQueryWrapper<Model> wrapper = new LambdaQueryWrapper<>();
  133. wrapper.in(Model::getModelId,numList);
  134. return this.list(wrapper);
  135. }
  136. }