CaseNumServiceImpl.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. package com.fdkankan.fusion.service.impl;
  2. import cn.hutool.core.io.FileUtil;
  3. import com.alibaba.fastjson.JSONArray;
  4. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  5. import com.fdkankan.fusion.common.FilePath;
  6. import com.fdkankan.fusion.common.ResultCode;
  7. import com.fdkankan.fusion.common.ResultData;
  8. import com.fdkankan.fusion.common.util.*;
  9. import com.fdkankan.fusion.entity.CaseNumEntity;
  10. import com.fdkankan.fusion.entity.Model;
  11. import com.fdkankan.fusion.exception.BusinessException;
  12. import com.fdkankan.fusion.httpClient.client.FdKKClient;
  13. import com.fdkankan.fusion.mapper.ICaseNumMapper;
  14. import com.fdkankan.fusion.request.SceneNumParam;
  15. import com.fdkankan.fusion.service.*;
  16. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  17. import com.fdkankan.fusion.websocket.SessionService;
  18. import com.fdkankan.fusion.websocket.enums.CommonEnum;
  19. import com.fdkankan.fusion.websocket.vo.WsMessage;
  20. import org.apache.commons.lang3.StringUtils;
  21. import org.springframework.beans.factory.annotation.Autowired;
  22. import org.springframework.beans.factory.annotation.Value;
  23. import org.springframework.stereotype.Service;
  24. import javax.annotation.Resource;
  25. import java.io.File;
  26. import java.sql.BatchUpdateException;
  27. import java.util.*;
  28. import java.util.stream.Collectors;
  29. /**
  30. * <p>
  31. * 服务实现类
  32. * </p>
  33. *
  34. * @author
  35. * @since 2022-07-27
  36. */
  37. @Service
  38. public class CaseNumServiceImpl extends ServiceImpl<ICaseNumMapper, CaseNumEntity> implements ICaseNumService {
  39. @Autowired
  40. UploadToOssUtil uploadToOssUtil;
  41. @Value("${upload.query-path}")
  42. private String queryPath;
  43. @Value("${spring.profiles.active}")
  44. private String environment;
  45. @Autowired
  46. IModelService modelService;
  47. @Autowired
  48. IFusionNumService fusionNumService;
  49. @Autowired
  50. ICaseViewService caseViewService;
  51. @Autowired
  52. IFusionMeterService fusionMeterService;
  53. @Autowired
  54. IFusionGuidePathService fusionGuidePathService;
  55. @Autowired
  56. ICaseTagService caseTagService;
  57. @Autowired
  58. ICaseTagPointService caseTagPointService;
  59. @Autowired
  60. SessionService sessionService;
  61. @Override
  62. public List<CaseNumEntity> getByCaseId(Integer caseId) {
  63. LambdaQueryWrapper<CaseNumEntity> wrapper = new LambdaQueryWrapper<>();
  64. wrapper.eq(CaseNumEntity::getCaseId,caseId);
  65. return this.list(wrapper);
  66. }
  67. @Override
  68. public void addBatch(Integer caseId, List<SceneNumParam> sceneNumParam) {
  69. List<String> addNumList = this.updateByNumList(caseId, sceneNumParam);
  70. if(addNumList == null || addNumList.size()<=0){
  71. return;
  72. }
  73. List<CaseNumEntity> newCaseNums = new ArrayList<>();
  74. List<Model> modelList = new ArrayList<>();
  75. for (SceneNumParam param : sceneNumParam) {
  76. List<String> numList = param.getNumList();
  77. HashSet<String> setNum = new HashSet<>(numList);
  78. for (String num : setNum) {
  79. if(!addNumList.contains(num)){
  80. continue;
  81. }
  82. CaseNumEntity caseNumEntity = new CaseNumEntity();
  83. caseNumEntity.setCaseId(caseId);
  84. caseNumEntity.setNumType(param.getType());
  85. caseNumEntity.setNum(num);
  86. newCaseNums.add(caseNumEntity);
  87. if(param.getType() == 3){ //用户上传三维模型跳过
  88. continue;
  89. }
  90. Model model = modelService.getIsNullNewByNum(num,param.getType());
  91. if(model.getModelId() != null && StringUtils.isNotBlank(model.getModelGlbUrl()) && StringUtils.isNotBlank(model.getModelSize())){
  92. continue;
  93. }
  94. model.setModelDateType("obj");
  95. model.setType(param.getType());
  96. model.setModelType("pointcloud"); //深时点云类型
  97. model.setCreateStatus(1);
  98. if(param.getType() == 0 || param.getType() == 1 || param.getType() == 4 || param.getType() == 6){ //看看,看见
  99. String mesh3DtilesPath = String.format(FilePath.OBJ_OSS_PATH,num) + "/images/3dtiles/tileset.json";
  100. String sizePath = String.format(FilePath.OBJ_OSS_PATH,num) + "/images/3dtiles";
  101. if(uploadToOssUtil.existKey(mesh3DtilesPath)){
  102. model.setModelDateType("b3dm");
  103. model.setModelType("b3dm");
  104. model.setModelGlbUrl(JSONArray.toJSONString(Arrays.asList(queryPath +mesh3DtilesPath)));
  105. model.setModelSize(FileWriterUtil.setFileSize(uploadToOssUtil.getSize( sizePath)));
  106. }else {
  107. model.setModelObjUrl(String.format(FilePath.OBJ_LOCAL_PATH,environment ,num) +"/mesh.obj");
  108. model.setModelGlbUrl(getGlbUrl(param.getType(),num,model));
  109. model.setModelType("glb");
  110. }
  111. }
  112. model.setNum(num);
  113. model.setCreateStatus(1);
  114. modelList.add(model);
  115. }
  116. }
  117. if(newCaseNums.size() >0){
  118. this.saveBatch(newCaseNums);
  119. }
  120. if(modelList.size() >0){
  121. modelService.saveOrUpdateBatch(modelList);
  122. }
  123. }
  124. private String getGlbUrl(Integer type, String num,Model model) {
  125. if(type == 0 || type == 1 || type == 4 || type == 6){ //看看,看见
  126. String objPath = String.format(FilePath.OBJ_LOCAL_PATH ,environment,num);
  127. // if(uploadToOssUtil.existKey(glbOssPath)){
  128. // return queryPath + "/"+glbOssPath;
  129. // }
  130. ShellUtil.yunDownload(String.format(FilePath.OBJ_OSS_PATH, num)+"/data/mesh" ,objPath);
  131. List<String> localGlbPaths = new ArrayList<>();
  132. List<String> ossGlbPaths = new ArrayList<>();
  133. File localFile = new File(objPath);
  134. this.toGlB(localFile,localGlbPaths);
  135. Long modelSize = 0L;
  136. if(localGlbPaths.size() >0){
  137. for (String localGlbPath : localGlbPaths) {
  138. String ossGlbPath = localGlbPath.replace(FilePath.LOCAL_BASE_PATH,"fusion/");
  139. uploadToOssUtil.uploadOss(localGlbPath,ossGlbPath);
  140. if(ossGlbPath.contains("lod_")){
  141. if(ossGlbPath.contains("lod_0")){
  142. ossGlbPaths.add(queryPath +ossGlbPath);
  143. modelSize += uploadToOssUtil.getSize(ossGlbPath);
  144. }
  145. continue;
  146. }
  147. modelSize += uploadToOssUtil.getSize(ossGlbPath);
  148. ossGlbPaths.add(queryPath +ossGlbPath);
  149. }
  150. model.setModelSize(FileWriterUtil.setFileSize(modelSize));
  151. FileUtil.del(objPath);
  152. return JSONArray.toJSONString(ossGlbPaths);
  153. }
  154. FileUtil.del(objPath);
  155. }
  156. return null;
  157. }
  158. private void toGlB(File localFile, List<String> localGlbPath) {
  159. File[] files = localFile.listFiles();
  160. for (File file : files) {
  161. if(file.isDirectory()){
  162. toGlB(file,localGlbPath);
  163. }
  164. if(file.getName().contains(".obj")){
  165. String glbPath = OBJToGLBUtil.objToGlb(file.getPath(),file.getPath().replace(".obj",".glb") );
  166. localGlbPath.add(glbPath);
  167. }
  168. }
  169. }
  170. private List<String> updateByNumList(Integer caseId, List<SceneNumParam> sceneNumParam) {
  171. List<String> addList = new ArrayList<>();
  172. List<String> delMsgList = new ArrayList<>();
  173. for (SceneNumParam param : sceneNumParam) {
  174. Integer type = param.getType();
  175. List<String> numList = param.getNumList();
  176. LambdaQueryWrapper<CaseNumEntity> wrapper = new LambdaQueryWrapper<>();
  177. wrapper.eq(CaseNumEntity::getCaseId,caseId);
  178. wrapper.eq(CaseNumEntity::getNumType,type);
  179. List<CaseNumEntity> list = this.list(wrapper);
  180. List<String> hanNumList = list.parallelStream().map(CaseNumEntity::getNum).collect(Collectors.toList());
  181. List<String> delList = new ArrayList<>();
  182. for (String num : hanNumList) {
  183. if(!numList.contains(num)){
  184. delList.add(num);
  185. delMsgList.add(num);
  186. }
  187. }
  188. for (String num : numList) {
  189. if(!hanNumList.contains(num)){
  190. addList.add(num);
  191. }
  192. }
  193. this.deleteByNum(caseId,delList,param.getType());
  194. }
  195. if(!delMsgList.isEmpty()){
  196. sessionService.sendSingleByCaseId(caseId, WsMessage.okResult(CommonEnum.NOTICE, ResultData.ok(ResultCode.CASE_REMOVE_SCENE)));
  197. }
  198. return addList;
  199. }
  200. @Override
  201. public void deleteByNum(Integer caseId, List<String> delList,Integer type) {
  202. if(delList.size() >0){
  203. LambdaQueryWrapper<CaseNumEntity> wrapper = new LambdaQueryWrapper<>();
  204. wrapper.eq(CaseNumEntity::getCaseId,caseId);
  205. wrapper.eq(CaseNumEntity::getNumType,type);
  206. wrapper.in(CaseNumEntity::getNum,delList);
  207. this.remove(wrapper);
  208. fusionNumService.deleteByNumList(caseId,delList,true,type);
  209. }
  210. }
  211. @Override
  212. public void deleteByCaseId(Integer caseId) {
  213. LambdaQueryWrapper<CaseNumEntity> wrapper = new LambdaQueryWrapper<>();
  214. wrapper.eq(CaseNumEntity::getCaseId,caseId);
  215. this.remove(wrapper);
  216. fusionNumService.deleteByCaseId(caseId);
  217. caseViewService.deleteByCaseId(caseId);
  218. fusionGuidePathService.deleteByCaseId(caseId);
  219. fusionMeterService.deleteByCaseId(caseId);
  220. caseTagService.deletePointByCaseId(caseId);
  221. }
  222. @Override
  223. public HashMap<Integer, List<String>> getTypeMap(Integer caseId) {
  224. List<CaseNumEntity> caseNumList = this.getByCaseId(caseId);
  225. HashMap<Integer,List<String>> typeMap = new HashMap<>();
  226. for (CaseNumEntity caseNumEntity : caseNumList) {
  227. List<String> numList ;
  228. if(typeMap.get(caseNumEntity.getNumType()) == null){
  229. numList = new ArrayList<>();
  230. }else {
  231. numList = typeMap.get(caseNumEntity.getNumType());
  232. }
  233. numList.add(caseNumEntity.getNum());
  234. typeMap.put(caseNumEntity.getNumType(),numList);
  235. }
  236. return typeMap;
  237. }
  238. @Override
  239. public List<CaseNumEntity> getByNum(String num) {
  240. LambdaQueryWrapper<CaseNumEntity> wrapper = new LambdaQueryWrapper<>();
  241. wrapper.eq(CaseNumEntity::getNum,num);
  242. return this.list(wrapper);
  243. }
  244. }