ModelServiceImpl.java 17 KB

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