ModelServiceImpl.java 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. package com.fdkankan.fusion.service.impl;
  2. import java.io.File;
  3. import java.util.ArrayList;
  4. import java.util.HashMap;
  5. import java.util.List;
  6. import java.util.UUID;
  7. import java.util.concurrent.ThreadPoolExecutor;
  8. import java.util.stream.Collectors;
  9. import cn.hutool.core.io.FileUtil;
  10. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  11. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
  12. import com.fdkankan.fusion.common.PageInfo;
  13. import com.fdkankan.fusion.common.util.*;
  14. import com.fdkankan.fusion.common.FilePath;
  15. import com.fdkankan.fusion.common.ResultCode;
  16. import com.fdkankan.fusion.entity.*;
  17. import com.fdkankan.fusion.exception.BusinessException;
  18. import com.fdkankan.fusion.mapper.IModelMapper;
  19. import com.fdkankan.fusion.request.ModelPram;
  20. import com.fdkankan.fusion.response.SceneVo;
  21. import com.fdkankan.fusion.service.ICaseNumService;
  22. import com.fdkankan.fusion.service.ICaseService;
  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.redis.util.RedisUtil;
  27. import org.apache.commons.lang3.StringUtils;
  28. import org.springframework.beans.BeanUtils;
  29. import org.springframework.beans.factory.annotation.Autowired;
  30. import org.springframework.beans.factory.annotation.Value;
  31. import org.springframework.scheduling.annotation.Async;
  32. import org.springframework.stereotype.Service;
  33. import org.springframework.web.multipart.MultipartFile;
  34. /**
  35. * <p>
  36. * 服务实现类
  37. * </p>
  38. *
  39. * @author
  40. * @since 2022-08-03
  41. */
  42. @Service
  43. public class ModelServiceImpl extends ServiceImpl<IModelMapper, Model> implements IModelService {
  44. @Autowired
  45. UploadToOssUtil uploadToOssUtil;
  46. @Autowired
  47. UploadService uploadService;
  48. @Autowired
  49. ICaseNumService caseNumService;
  50. @Autowired
  51. ICaseService caseService;
  52. @Autowired
  53. IFusionNumService fusionNumService;
  54. @Autowired
  55. RedisUtil redisUtil;
  56. @Value("${upload.query-path}")
  57. private String queryPath;
  58. @Value("${spring.profiles.active}")
  59. private String environment;
  60. @Override
  61. public Model uploadObj(MultipartFile file, String username) throws Exception {
  62. if(file.isEmpty()){
  63. throw new BusinessException(ResultCode.UPLOAD_FILE_NO_EXIST);
  64. }
  65. if(file.getSize()>10 * 1024 * 1024 * 100){
  66. throw new BusinessException(ResultCode.UPLOAD_FILE_TO_LONG);
  67. }
  68. //获取文件名
  69. String fileName = file.getOriginalFilename();
  70. if(StringUtils.isEmpty(fileName)){
  71. throw new BusinessException(ResultCode.UPLOAD_FILE_NO_EXIST);
  72. }
  73. if(com.fdkankan.fusion.common.util.StringUtils.isChinese(fileName)){
  74. throw new BusinessException(ResultCode.UPLOAD_FILE_CHINA_NAME);
  75. }
  76. if(fileName.length() >50){
  77. throw new BusinessException(ResultCode.UPLOAD_FILE_NAME_TO_LONG);
  78. }
  79. if(!fileName.toLowerCase().endsWith(".zip")){
  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. redisUtil.set(RedisKeyUtil.modelUpload+model.getModelId(),"0");
  90. fileName = UUID.randomUUID().toString().replace("-","") +".zip";
  91. File newObjFile = null;
  92. File objPathFile = null;
  93. String objPath = String.format(FilePath.OBJ_LOCAL_PATH,environment , "modelId_"+model.getModelId()) ;
  94. newObjFile = new File(objPath +"/" + fileName);
  95. if(!newObjFile.getParentFile().exists()){
  96. newObjFile.mkdirs();
  97. }
  98. file.transferTo(newObjFile);
  99. if(fileName.toLowerCase().endsWith(".zip")){
  100. ShellUtil.unZip(newObjFile.getPath(),objPath);
  101. }
  102. objPathFile = new File(objPath );
  103. if(!objPathFile.isDirectory()){
  104. setCreateStatus(model,-1);
  105. throw new BusinessException(ResultCode.UPLOAD_FILE_TYPE_ERROR);
  106. }
  107. List<File> fileList = new ArrayList<>();
  108. FileWriterUtil.getCanRunList(fileList,objPathFile);
  109. if(fileList.size() != 1){
  110. setCreateStatus(model,-1);
  111. throw new BusinessException(ResultCode.UPLOAD_FILE_MSG_ERROR);
  112. }
  113. File file1 = fileList.get(0);
  114. if(file1 == null){
  115. setCreateStatus(model,-1);
  116. throw new BusinessException(ResultCode.UPLOAD_FILE_OBJ_ERROR);
  117. }
  118. if(com.fdkankan.fusion.common.util.StringUtils.isChinese(file1.getName())){
  119. setCreateStatus(model,-1);
  120. throw new BusinessException(ResultCode.UPLOAD_FILE_CHINA_NAME);
  121. }
  122. redisUtil.set(RedisKeyUtil.modelUpload+model.getModelId(),"20");
  123. runThread(file1,model,newObjFile,objPathFile,this);
  124. return model;
  125. }
  126. private void setCreateStatus(Model model,Integer status){
  127. String redisKey = RedisKeyUtil.modelCancelUpload+model.getModelId();
  128. if(redisUtil.hasKey(redisKey)){
  129. if(redisUtil.get(redisKey).equals("-2")){
  130. return;
  131. }
  132. }
  133. model.setCreateStatus(status);
  134. this.saveOrUpdate(model);
  135. if(status != 1){
  136. redisUtil.set(RedisKeyUtil.modelUpload+model.getModelId(),status.toString());
  137. }
  138. }
  139. public void runThread(File file1,Model model,File newObjFile,File objPathFile,IModelService modelService){
  140. new Thread(new Runnable() {
  141. @Override
  142. public void run() {
  143. File mntFile = null;
  144. try {
  145. String glbOssPath = String.format(FilePath.GLB_OSS_PATH,environment, model.getModelId());
  146. String name = file1.getName();
  147. if(name.contains("obj") || name.contains("OBJ")){
  148. glbOssPath = glbOssPath.replace("mesh.glb",file1.getName().replace(".obj",".glb"));
  149. model.setModelDateType("obj");
  150. model.setModelType("glb");
  151. OBJToGLBUtil.objToGlb2(file1.getPath(), file1.getPath().replace(".obj",".glb"));
  152. redisUtil.set(RedisKeyUtil.modelUpload+model.getModelId(),"65");
  153. uploadToOssUtil.uploadOss(file1.getPath().replace(".obj",".glb"),glbOssPath);
  154. if(!uploadToOssUtil.existKey(glbOssPath)){
  155. setCreateStatus(model,-1);
  156. redisUtil.set(RedisKeyUtil.modelUpload+model.getModelId(),"-1");
  157. return;
  158. }
  159. }
  160. if(name.contains(".ply")){
  161. model.setModelDateType("ply");
  162. model.setModelType("ply");
  163. }
  164. if(name.contains(".las")){
  165. model.setModelDateType("las");
  166. model.setModelType("las");
  167. }
  168. if("las".equals(model.getModelType()) || "ply".equals(model.getModelType()) ){
  169. mntFile = OBJToGLBUtil.lasOrPlyToBin(file1);
  170. glbOssPath = mntFile.getPath().replace("/mnt/","")+"/webcloud";
  171. redisUtil.set(RedisKeyUtil.modelUpload+model.getModelId(),"65");
  172. uploadToOssUtil.uploadFileOss(mntFile );
  173. if(!uploadToOssUtil.existKey(glbOssPath+"/cloud.js")){
  174. setCreateStatus(model,-1);
  175. redisUtil.set(RedisKeyUtil.modelUpload+model.getModelId(),"-1");
  176. return;
  177. }
  178. }
  179. model.setModelGlbUrl(queryPath + glbOssPath);
  180. setCreateStatus(model,1);
  181. modelService.saveOrUpdate(model);
  182. redisUtil.set(RedisKeyUtil.modelUpload+model.getModelId(),"100");
  183. }catch (Exception e){
  184. setCreateStatus(model,-1);
  185. }finally {
  186. if(newObjFile!=null){
  187. FileUtil.del(newObjFile);
  188. }
  189. if(objPathFile!=null){
  190. FileUtil.del(objPathFile);
  191. }
  192. if(mntFile!=null){
  193. FileUtil.del(mntFile.getParentFile());
  194. }
  195. }
  196. }
  197. }).start();
  198. }
  199. @Override
  200. public PageInfo pageList(ModelPram param, String token) {
  201. String username = JwtUtil.getUsername(token);
  202. LambdaQueryWrapper<Model> wrapper = new LambdaQueryWrapper<>();
  203. wrapper.eq(Model::getUserName,username);
  204. wrapper.eq(Model::getType,3);
  205. wrapper.notIn(Model::getCreateStatus,-2);
  206. if(param.getStatus()!=null){ //参数2为成功,数据库中成功为1
  207. wrapper.eq(Model::getCreateStatus,param.getStatus() == 2 ? 1 :param.getStatus());
  208. }
  209. if(StringUtils.isNotBlank(param.getModelTitle())){
  210. wrapper.like(Model::getModelTitle,param.getModelTitle());
  211. }
  212. wrapper.orderByDesc(Model::getCreateTime);
  213. Page<Model> page = this.page(new Page<>(param.getPageNum(),param.getPageSize()),wrapper);
  214. for (Model model : page.getRecords()) {
  215. if(model.getType() == 3 && StringUtils.isEmpty(model.getNum())) {
  216. model.setNum(model.getModelId().toString());
  217. }
  218. }
  219. return PageInfo.PageInfo(page);
  220. }
  221. @Override
  222. public void delete(Integer modelId) {
  223. List<CaseNumEntity> caseNumEntityList = caseNumService.getByNum(modelId.toString());
  224. List<FusionNum> fusionNumList = fusionNumService.getByNum(modelId.toString());
  225. if(caseNumEntityList.size() >0 || fusionNumList.size() >0){
  226. StringBuilder title = new StringBuilder();
  227. List<Integer> caseIdIds = caseNumEntityList.parallelStream().map(CaseNumEntity::getCaseId).collect(Collectors.toList());
  228. if(caseIdIds.size() >0){
  229. List<CaseEntity> list = caseService.getByIds(caseIdIds);
  230. List<String> collect = list.parallelStream().map(CaseEntity::getCaseTitle).collect(Collectors.toList());
  231. for (String str : collect) {
  232. title.append(str).append(",");
  233. }
  234. if(title.length()>0){
  235. title.delete(title.length()-1,title.length());
  236. }
  237. }
  238. throw new BusinessException(ResultCode.CASE_USE.code, String.format(ResultCode.CASE_USE.msg,title));
  239. }
  240. Model model = this.getById(modelId);
  241. if(model == null ){
  242. throw new BusinessException(ResultCode.MODEL_NOT_EXIST);
  243. }
  244. this.removeById(modelId);
  245. fusionNumService.deleteByModelId(modelId);
  246. if(StringUtils.isNotBlank(model.getModelGlbUrl())){
  247. uploadService.deleteOssUrl(model.getModelGlbUrl());
  248. }
  249. }
  250. @Override
  251. public List<Model> getListByNum(List<String> numList) {
  252. LambdaQueryWrapper<Model> wrapper = new LambdaQueryWrapper<>();
  253. wrapper.in(Model::getNum,numList);
  254. return this.list(wrapper);
  255. }
  256. @Override
  257. public List<Model> getListByModelIds(List<Integer> modelIds) {
  258. LambdaQueryWrapper<Model> wrapper = new LambdaQueryWrapper<>();
  259. wrapper.in(Model::getModelId,modelIds);
  260. return this.list(wrapper);
  261. }
  262. @Override
  263. public List<Model> getListByModelIdStrs(List<String> numList) {
  264. LambdaQueryWrapper<Model> wrapper = new LambdaQueryWrapper<>();
  265. wrapper.in(Model::getModelId,numList);
  266. wrapper.orderByDesc(Model::getCreateTime);
  267. return this.list(wrapper);
  268. }
  269. @Override
  270. public void deleteByNum(List<String> numList) {
  271. LambdaQueryWrapper<Model> wrapper = new LambdaQueryWrapper<>();
  272. wrapper.in(Model::getNum,numList);
  273. this.remove(wrapper);
  274. }
  275. @Override
  276. public List<Model> getByUserName(String username) {
  277. LambdaQueryWrapper<Model> wrapper = new LambdaQueryWrapper<>();
  278. wrapper.eq(Model::getUserName,username)
  279. .eq(Model::getType,3);
  280. return this.list(wrapper);
  281. }
  282. @Override
  283. public Model getIsNullNewByNum(String num) {
  284. LambdaQueryWrapper<Model> wrapper = new LambdaQueryWrapper<>();
  285. wrapper.eq(Model::getNum,num);
  286. Model model = this.getOne(wrapper);
  287. if(model == null){
  288. model = new Model();
  289. }
  290. return model;
  291. }
  292. @Override
  293. public Object getInfo(Integer modelId) {
  294. Model model = this.getById(modelId);
  295. if(model == null){
  296. throw new BusinessException(ResultCode.SCENE_NOT_EXIST);
  297. }
  298. SceneVo sceneVo = new SceneVo();
  299. BeanUtils.copyProperties(model,sceneVo);
  300. return sceneVo;
  301. }
  302. @Override
  303. public String uploadObjProgress(Integer modelId) {
  304. String redisKey = RedisKeyUtil.modelUpload+modelId;
  305. String cancel = RedisKeyUtil.modelCancelUpload+modelId;
  306. if(redisUtil.hasKey(cancel)){
  307. return redisUtil.get(cancel);
  308. }
  309. if(redisUtil.hasKey(redisKey)){
  310. return redisUtil.get(redisKey);
  311. }
  312. return "0";
  313. }
  314. @Override
  315. public void cancelUpload(Integer modelId) {
  316. String redisKey = RedisKeyUtil.modelCancelUpload+modelId;
  317. if(redisUtil.hasKey(redisKey)){
  318. return;
  319. }
  320. Model model = this.getById(modelId);
  321. if(model == null){
  322. return;
  323. }
  324. model.setCreateStatus(-2);
  325. this.updateById(model);
  326. redisUtil.set(redisKey,"-2");
  327. }
  328. @Override
  329. public HashMap<String, Model> getMapByNum(List<String> numList) {
  330. HashMap<String,Model> map = new HashMap<>();
  331. List<Model> modelList = this.getListByNum(numList);
  332. modelList.forEach(entity-> map.put(entity.getNum() + entity.getType(),entity));
  333. return map;
  334. }
  335. }