CaseNumServiceImpl.java 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. package com.fdkankan.fusion.service.impl;
  2. import cn.hutool.core.io.FileUtil;
  3. import com.alibaba.fastjson.JSONArray;
  4. import com.alibaba.fastjson.JSONObject;
  5. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  6. import com.fdkankan.fusion.common.FilePath;
  7. import com.fdkankan.fusion.common.ResultCode;
  8. import com.fdkankan.fusion.common.ResultData;
  9. import com.fdkankan.fusion.common.util.*;
  10. import com.fdkankan.fusion.entity.*;
  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 lombok.extern.slf4j.Slf4j;
  18. import org.apache.commons.lang3.StringUtils;
  19. import org.springframework.beans.factory.annotation.Autowired;
  20. import org.springframework.beans.factory.annotation.Value;
  21. import org.springframework.stereotype.Service;
  22. import javax.annotation.Resource;
  23. import java.io.File;
  24. import java.sql.BatchUpdateException;
  25. import java.util.*;
  26. import java.util.stream.Collectors;
  27. /**
  28. * <p>
  29. * 服务实现类
  30. * </p>
  31. *
  32. * @author
  33. * @since 2022-07-27
  34. */
  35. @Service
  36. @Slf4j
  37. public class CaseNumServiceImpl extends ServiceImpl<ICaseNumMapper, CaseNumEntity> implements ICaseNumService {
  38. @Autowired
  39. LocalToOssUtil localToOssUtil;
  40. @Value("${upload.query-path}")
  41. private String queryPath;
  42. @Value("${spring.profiles.active}")
  43. private String environment;
  44. @Autowired
  45. IModelService modelService;
  46. @Autowired
  47. IFusionNumService fusionNumService;
  48. @Autowired
  49. ICaseViewService caseViewService;
  50. @Autowired
  51. IFusionMeterService fusionMeterService;
  52. @Autowired
  53. IFusionGuidePathService fusionGuidePathService;
  54. @Autowired
  55. ICaseTagService caseTagService;
  56. @Autowired
  57. ICaseTagPointService caseTagPointService;
  58. @Autowired
  59. ThreadService threadService;
  60. @Autowired
  61. ISceneService sceneService;
  62. @Override
  63. public List<CaseNumEntity> getByCaseId(Integer caseId) {
  64. LambdaQueryWrapper<CaseNumEntity> wrapper = new LambdaQueryWrapper<>();
  65. wrapper.eq(CaseNumEntity::getCaseId,caseId);
  66. return this.list(wrapper);
  67. }
  68. @Override
  69. public void addBatch(Integer caseId, List<SceneNumParam> sceneNumParam) {
  70. List<String> addNumList = this.updateByNumList(caseId, sceneNumParam);
  71. if(addNumList == null || addNumList.size()<=0){
  72. return;
  73. }
  74. List<CaseNumEntity> newCaseNums = new ArrayList<>();
  75. List<Model> modelList = new ArrayList<>();
  76. HashMap<Integer,HashSet<String>> map = new HashMap<>();
  77. for (SceneNumParam numParam : sceneNumParam) {
  78. if(map.get(numParam.getType()) == null){
  79. HashSet<String> set = new HashSet<>(numParam.getNumList());
  80. map.put(numParam.getType(),set);
  81. }else {
  82. map.get(numParam.getType()).addAll(numParam.getNumList());
  83. }
  84. }
  85. for (Integer type : map.keySet()) {
  86. HashSet<String> nulList = map.get(type);
  87. List<String> numList = new ArrayList<>(nulList);
  88. HashSet<String> setNum = new HashSet<>(numList);
  89. for (String num : setNum) {
  90. if(!addNumList.contains(num)){
  91. continue;
  92. }
  93. CaseNumEntity caseNumEntity = new CaseNumEntity();
  94. caseNumEntity.setCaseId(caseId);
  95. caseNumEntity.setNumType(type);
  96. caseNumEntity.setNum(num);
  97. newCaseNums.add(caseNumEntity);
  98. if(type == 3){ //用户上传三维模型跳过
  99. continue;
  100. }
  101. Model model = modelService.getIsNullNewByNum(num,type);
  102. if(model.getModelId() != null && StringUtils.isNotBlank(model.getModelGlbUrl()) && StringUtils.isNotBlank(model.getModelSize())){
  103. continue;
  104. }
  105. model.setModelDateType("obj");
  106. model.setType(type);
  107. model.setModelType("pointcloud"); //深时点云类型
  108. model.setCreateStatus(1);
  109. Scene scene = sceneService.getByNum(num);
  110. if(scene == null){
  111. continue;
  112. }
  113. if(type == 0 || type == 1 || type == 4 || type == 6 || type == 7){ //看看,看见
  114. String mesh3DtilesPath = String.format(FilePath.OBJ_OSS_PATH,num) + "/images/3dtiles/tileset.json";
  115. String sizePath = scene.getWebPath() + String.format(FilePath.OBJ_OSS_PATH,num) + "/images/3dtiles";
  116. if(localToOssUtil.existKey(scene.getWebPath() +mesh3DtilesPath)){
  117. model.setModelDateType("b3dm");
  118. model.setModelType("b3dm");
  119. model.setModelGlbUrl(JSONArray.toJSONString(Arrays.asList(scene.getMapping() +File.separator +mesh3DtilesPath)));
  120. model.setModelSize(FileWriterUtil.setFileSize(localToOssUtil.getSizeCount( sizePath)));
  121. }else {
  122. String meshPath = String.format(FilePath.OBJ_OSS_PATH, num)+"/data/mesh";
  123. if(localToOssUtil.existKey( scene.getWebPath() +meshPath +"/mesh.obj")){
  124. Long size = localToOssUtil.getSizeCount( scene.getWebPath() +meshPath);
  125. model.setModelSize(FileWriterUtil.setFileSize(size));
  126. model.setModelGlbUrl(JSONArray.toJSONString(Arrays.asList(scene.getMapping()+File.separator + meshPath+"/mesh.obj")));
  127. }else {
  128. List<String> objPaths = new ArrayList<>();
  129. String meshPathjs = String.format(FilePath.OBJ_OSS_PATH, num)+"/data/";
  130. String jsonPath =scene.getWebPath() + meshPath + "/floors.json";
  131. if(!localToOssUtil.existKey(jsonPath)){
  132. jsonPath =scene.getWebPath() + meshPath +"mesh.json";
  133. }
  134. if(localToOssUtil.existKey(jsonPath)){
  135. String objectContent = localToOssUtil.getObjectContent(meshPath + "/floors.json");
  136. JSONObject jsonObject = JSONObject.parseObject(objectContent);
  137. JSONArray floors1 = jsonObject.getJSONArray("floors");
  138. for (Object object : floors1) {
  139. JSONObject jb = (JSONObject) object;
  140. String string = jb.getString("objPath");
  141. objPaths.add(scene.getMapping()+File.separator +meshPathjs + string);
  142. }
  143. model.setModelGlbUrl(JSONArray.toJSONString(objPaths));
  144. Long size = localToOssUtil.getSize(scene.getWebPath() +meshPath);
  145. model.setModelSize(FileWriterUtil.setFileSize(size));
  146. }
  147. }
  148. model.setModelType("obj");
  149. }
  150. }
  151. model.setNum(num);
  152. modelList.add(model);
  153. }
  154. }
  155. if(newCaseNums.size() >0){
  156. this.saveBatch(newCaseNums);
  157. }
  158. if(modelList.size() >0){
  159. modelService.saveOrUpdateBatch(modelList);
  160. }
  161. }
  162. private String getGlbUrl(Integer type, String num,Model model) {
  163. if(type == 0 || type == 1 || type == 4 || type == 6 || type == 7){ //看看,看见
  164. String objPath = String.format(FilePath.OBJ_LOCAL_PATH ,environment,num);
  165. ShellUtil.yunDownload(String.format(FilePath.OBJ_OSS_PATH, num)+"/data/mesh" ,objPath);
  166. List<String> localGlbPaths = new ArrayList<>();
  167. List<String> ossGlbPaths = new ArrayList<>();
  168. File localFile = new File(objPath);
  169. this.toGlB(localFile,localGlbPaths);
  170. Long modelSize = 0L;
  171. if(localGlbPaths.size() >0){
  172. for (String localGlbPath : localGlbPaths) {
  173. String ossGlbPath = localGlbPath.replace(FilePath.LOCAL_BASE_PATH,"fusion/");
  174. localToOssUtil.uploadOss(localGlbPath,ossGlbPath);
  175. if(ossGlbPath.contains("lod_")){
  176. if(ossGlbPath.contains("lod_0")){
  177. ossGlbPaths.add(queryPath +ossGlbPath);
  178. modelSize += localToOssUtil.getSize(ossGlbPath);
  179. }
  180. continue;
  181. }
  182. modelSize += localToOssUtil.getSize(ossGlbPath);
  183. ossGlbPaths.add(queryPath +ossGlbPath);
  184. }
  185. model.setModelSize(FileWriterUtil.setFileSize(modelSize));
  186. FileUtil.del(objPath);
  187. return JSONArray.toJSONString(ossGlbPaths);
  188. }
  189. FileUtil.del(objPath);
  190. }
  191. return null;
  192. }
  193. private void toGlB(File localFile, List<String> localGlbPath) {
  194. File[] files = localFile.listFiles();
  195. for (File file : files) {
  196. if(file.isDirectory()){
  197. toGlB(file,localGlbPath);
  198. }
  199. if(file.getName().contains(".obj")){
  200. String glbPath = OBJToGLBUtil.objToGlb(file.getPath(),file.getPath().replace(".obj",".glb") );
  201. localGlbPath.add(glbPath);
  202. }
  203. }
  204. }
  205. private List<String> updateByNumList(Integer caseId, List<SceneNumParam> sceneNumParam) {
  206. List<String> addList = new ArrayList<>();
  207. HashMap<Integer, FusionNum> fusionNumHashMap = fusionNumService.getByCaseId(caseId);
  208. HashMap<Integer,List<String>> delMap = new HashMap<>();
  209. for (SceneNumParam param : sceneNumParam) {
  210. Integer type = param.getType();
  211. List<String> numList = param.getNumList();
  212. LambdaQueryWrapper<CaseNumEntity> wrapper = new LambdaQueryWrapper<>();
  213. wrapper.eq(CaseNumEntity::getCaseId,caseId);
  214. wrapper.eq(CaseNumEntity::getNumType,type);
  215. List<CaseNumEntity> list = this.list(wrapper);
  216. List<String> hanNumList = list.parallelStream().map(CaseNumEntity::getNum).collect(Collectors.toList());
  217. List<String> delList = new ArrayList<>();
  218. for (String num : hanNumList) {
  219. if(!numList.contains(num)){
  220. delList.add(num);
  221. }
  222. }
  223. for (String num : numList) {
  224. if(!hanNumList.contains(num)){
  225. addList.add(num);
  226. }
  227. }
  228. if(!delList.isEmpty()){
  229. HashMap<String, Model> mapByNum = modelService.getMapByNum(delList);
  230. for (String key : mapByNum.keySet()) {
  231. Model model = mapByNum.get(key);
  232. if(model != null && model.getType().equals(param.getType()) && fusionNumHashMap.containsKey(model.getModelId())){
  233. throw new BusinessException(ResultCode.DEL_NUM_ERROR);
  234. }
  235. }
  236. delMap.put(param.getType(),delList);
  237. }
  238. }
  239. if(!delMap.isEmpty()){
  240. for (Integer type : delMap.keySet()) {
  241. this.deleteByNum(caseId,delMap.get(type),type);
  242. }
  243. }
  244. return addList;
  245. }
  246. @Override
  247. public void deleteByNum(Integer caseId, List<String> delList,Integer type) {
  248. if(delList.size() >0){
  249. LambdaQueryWrapper<CaseNumEntity> wrapper = new LambdaQueryWrapper<>();
  250. wrapper.eq(CaseNumEntity::getCaseId,caseId);
  251. wrapper.eq(CaseNumEntity::getNumType,type);
  252. wrapper.in(CaseNumEntity::getNum,delList);
  253. this.remove(wrapper);
  254. fusionNumService.deleteByNumList(caseId,delList,true,type);
  255. }
  256. }
  257. @Override
  258. public void deleteByCaseId(Integer caseId) {
  259. LambdaQueryWrapper<CaseNumEntity> wrapper = new LambdaQueryWrapper<>();
  260. wrapper.eq(CaseNumEntity::getCaseId,caseId);
  261. this.remove(wrapper);
  262. fusionNumService.deleteByCaseId(caseId);
  263. caseViewService.deleteByCaseId(caseId);
  264. fusionGuidePathService.deleteByCaseId(caseId);
  265. fusionMeterService.deleteByCaseId(caseId);
  266. caseTagService.deletePointByCaseId(caseId);
  267. }
  268. @Override
  269. public HashMap<Integer, List<String>> getTypeMap(Integer caseId) {
  270. List<CaseNumEntity> caseNumList = this.getByCaseId(caseId);
  271. HashMap<Integer,List<String>> typeMap = new HashMap<>();
  272. for (CaseNumEntity caseNumEntity : caseNumList) {
  273. List<String> numList ;
  274. if(typeMap.get(caseNumEntity.getNumType()) == null){
  275. numList = new ArrayList<>();
  276. }else {
  277. numList = typeMap.get(caseNumEntity.getNumType());
  278. }
  279. numList.add(caseNumEntity.getNum());
  280. typeMap.put(caseNumEntity.getNumType(),numList);
  281. }
  282. return typeMap;
  283. }
  284. @Override
  285. public List<CaseNumEntity> getByNum(String num) {
  286. LambdaQueryWrapper<CaseNumEntity> wrapper = new LambdaQueryWrapper<>();
  287. wrapper.eq(CaseNumEntity::getNum,num);
  288. return this.list(wrapper);
  289. }
  290. @Override
  291. public void addModeByCaseId(Integer caseId, Integer modelId) {
  292. LambdaQueryWrapper<CaseNumEntity> wrapper = new LambdaQueryWrapper<>();
  293. wrapper.eq(CaseNumEntity::getCaseId,caseId);
  294. wrapper.eq(CaseNumEntity::getNum,modelId);
  295. List<CaseNumEntity> list = this.list(wrapper);
  296. if(list.isEmpty()){
  297. CaseNumEntity caseNumEntity = new CaseNumEntity();
  298. caseNumEntity.setCaseId(caseId);
  299. caseNumEntity.setNumType(3);
  300. caseNumEntity.setNum(modelId.toString());
  301. this.save(caseNumEntity);
  302. }
  303. }
  304. @Override
  305. public void deleteByModel(Integer modelId) {
  306. LambdaQueryWrapper<CaseNumEntity> wrapper = new LambdaQueryWrapper<>();
  307. wrapper.eq(CaseNumEntity::getNum,modelId.toString());
  308. this.remove(wrapper);
  309. modelService.removeById(modelId);
  310. }
  311. @Override
  312. public List<CaseNumEntity> getByNumList(List<String> numList) {
  313. LambdaQueryWrapper<CaseNumEntity> wrapper = new LambdaQueryWrapper<>();
  314. wrapper.in(CaseNumEntity::getNum,numList);
  315. return this.list(wrapper);
  316. }
  317. @Autowired
  318. ICaseService caseService;
  319. @Override
  320. public HashMap<String, CaseEntity> getMapByNumList(List<String> numList) {
  321. HashMap<String, CaseEntity> map = new HashMap<>();
  322. if(numList == null || numList.isEmpty()){
  323. return map;
  324. }
  325. List<CaseNumEntity> list = this.getByNumList(numList);
  326. Set<Integer> setIds = list.stream().map(CaseNumEntity::getCaseId).collect(Collectors.toSet());
  327. HashMap<Integer,CaseEntity> caseMap = new HashMap<>();
  328. if(!setIds.isEmpty()){
  329. List<CaseEntity> cases = caseService.listByIds(setIds);
  330. if(!cases.isEmpty()){
  331. cases.forEach(e -> caseMap.put(e.getCaseId(),e));
  332. }
  333. }
  334. for (CaseNumEntity caseNum : list) {
  335. if(caseMap.get(caseNum.getCaseId()) != null){
  336. map.put(caseNum.getNum() +"_"+ caseNum.getNumType() ,caseMap.get(caseNum.getCaseId()));
  337. }
  338. }
  339. return map;
  340. }
  341. }