CaseNumServiceImpl.java 10 KB

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