123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436 |
- package com.fdkankan.fusion.service.impl;
- import java.io.File;
- import java.io.IOException;
- import java.util.*;
- import java.util.concurrent.CompletableFuture;
- import java.util.concurrent.ExecutorService;
- import java.util.concurrent.ThreadPoolExecutor;
- import java.util.stream.Collectors;
- import cn.hutool.core.io.FileUtil;
- import cn.hutool.core.thread.ThreadUtil;
- import com.alibaba.fastjson.JSONArray;
- import com.alibaba.fastjson.JSONObject;
- import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
- import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
- import com.fdkankan.fusion.common.PageInfo;
- import com.fdkankan.fusion.common.util.*;
- import com.fdkankan.fusion.common.FilePath;
- import com.fdkankan.fusion.common.ResultCode;
- import com.fdkankan.fusion.config.CacheUtil;
- import com.fdkankan.fusion.entity.*;
- import com.fdkankan.fusion.exception.BusinessException;
- import com.fdkankan.fusion.mapper.IModelMapper;
- import com.fdkankan.fusion.request.ModelPram;
- import com.fdkankan.fusion.response.SceneVo;
- import com.fdkankan.fusion.service.*;
- import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
- import com.fdkankan.redis.util.RedisUtil;
- import lombok.SneakyThrows;
- import lombok.extern.slf4j.Slf4j;
- import org.apache.commons.io.FileUtils;
- import org.apache.commons.lang3.StringUtils;
- import org.springframework.beans.BeanUtils;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.beans.factory.annotation.Value;
- import org.springframework.scheduling.annotation.Async;
- import org.springframework.stereotype.Service;
- import org.springframework.web.multipart.MultipartFile;
- /**
- * <p>
- * 服务实现类
- * </p>
- *
- * @author
- * @since 2022-08-03
- */
- @Service
- @Slf4j
- public class ModelServiceImpl extends ServiceImpl<IModelMapper, Model> implements IModelService {
- @Autowired
- LocalToOssUtil localToOssUtil;
- @Autowired
- UploadService uploadService;
- @Autowired
- ICaseNumService caseNumService;
- @Autowired
- ICaseService caseService;
- @Autowired
- IFusionNumService fusionNumService;
- @Autowired
- RedisUtil redisUtil;
- @Autowired
- ITmDepartmentService tmDepartmentService;
- @Autowired
- ITmUserService tmUserService;
- @Autowired
- ITmCameraService tmCameraService;
- @Autowired
- ThreadService threadService;
- @Autowired
- ISceneService sceneService;
- @Value("${spring.profiles.active}")
- private String environment;
- @Override
- public Model uploadObj(MultipartFile file,String username) throws Exception {
- if(file.isEmpty()){
- throw new BusinessException(ResultCode.UPLOAD_FILE_NO_EXIST);
- }
- if(file.getSize()>10 * 1024 * 1024 * 100){
- throw new BusinessException(ResultCode.UPLOAD_FILE_TO_LONG);
- }
- //获取文件名
- String fileName = file.getOriginalFilename();
- if(StringUtils.isEmpty(fileName)){
- throw new BusinessException(ResultCode.UPLOAD_FILE_NO_EXIST);
- }
- if(com.fdkankan.fusion.common.util.StringUtils.isChinese(fileName)){
- throw new BusinessException(ResultCode.UPLOAD_FILE_CHINA_NAME);
- }
- if(fileName.length() >50){
- throw new BusinessException(ResultCode.UPLOAD_FILE_NAME_TO_LONG);
- }
- if(!fileName.toLowerCase().endsWith(".zip")){
- throw new BusinessException(ResultCode.UPLOAD_FILE_TYPE_ERROR);
- }
- //获取文件后缀名
- String modelName = fileName.substring(0,fileName.lastIndexOf("."));
- TmUser tmUser = tmUserService.getLoginUser();
- Model model = new Model();
- model.setModelTitle(modelName);
- model.setModelSize(FileWriterUtil.setFileSize(file.getSize()));
- model.setUserName(username);
- model.setDeptId(tmUser.getDeptId());
- redisUtil.set(RedisKeyUtil.modelUpload+model.getModelId(),"0");
- this.save(model);
- String fileName2 = UUID.randomUUID().toString().replace("-","") +".zip";
- String objPath = String.format(FilePath.OBJ_LOCAL_PATH,environment , "modelId_"+model.getModelId()) ;
- log.info("uploadObj--ThreadStart-fileName:{},modeId:{}",fileName2,model.getModelId());
- File newObjFile = new File(objPath +"/" + fileName2);
- if(!newObjFile.getParentFile().exists()){
- newObjFile.getParentFile().mkdirs();
- }
- file.transferTo(newObjFile);
- threadService.uploadModelObj(fileName2,objPath,newObjFile,model);
- return model;
- }
- @Override
- public PageInfo pageList(ModelPram param, String userName) {
- LambdaQueryWrapper<Model> wrapper = new LambdaQueryWrapper<>();
- List<String> deptIds = tmDepartmentService.getDeptIds();
- if(deptIds.size() <=0){
- return PageInfo.PageInfo(new Page<>(param.getPageNum(),param.getPageSize()));
- }
- if(param.getCaseId() !=null){
- String deptId = caseService.getDeptId(param.getCaseId());
- wrapper.eq(Model::getDeptId,deptId);
- }
- List<String> deptIds2 = tmDepartmentService.getSonByDeptIdAndDeptIds(deptIds, param.getDeptId(),param.getSearchType());
- wrapper.in(Model::getDeptId,deptIds2);
- wrapper.eq(Model::getType,3);
- wrapper.notIn(Model::getCreateStatus,-2);
- if(param.getStatus()!=null){ //参数2为成功,数据库中成功为1
- wrapper.eq(Model::getCreateStatus,param.getStatus() == 2 ? 1 :param.getStatus());
- }
- if(StringUtils.isNotBlank(param.getModelTitle())){
- wrapper.like(Model::getModelTitle,param.getModelTitle());
- }
- wrapper.orderByDesc(Model::getCreateTime);
- Page<Model> page = this.page(new Page<>(param.getPageNum(),param.getPageSize()),wrapper);
- Set<String> deptIdset = page.getRecords().stream().map(Model::getDeptId).collect(Collectors.toSet());
- HashMap<String, TmDepartment> mapByDept = tmDepartmentService.getMapByDeptIds(deptIdset);
- for (Model model : page.getRecords()) {
- if(model.getType() == 3 && StringUtils.isEmpty(model.getNum())) {
- model.setNum(model.getModelId().toString());
- }
- if(StringUtils.isNotBlank(model.getDeptId())){
- TmDepartment tmDepartment = mapByDept.get(model.getDeptId());
- if(tmDepartment != null){
- model.setDeptName(tmDepartment.getName());
- }
- }
- }
- return PageInfo.PageInfo(page);
- }
- private List<Model> getByDeptIds(List<String> asList) {
- if(asList.size() >0){
- LambdaQueryWrapper<Model> wrapper = new LambdaQueryWrapper<>();
- wrapper.in(Model::getDeptId,asList);
- return list(wrapper);
- }
- return new ArrayList<>();
- }
- @Override
- public void delete(Integer modelId) {
- List<CaseNumEntity> caseNumEntityList = caseNumService.getByNum(modelId.toString());
- List<FusionNum> fusionNumList = fusionNumService.getByNum(modelId.toString());
- if(caseNumEntityList.size() >0 || fusionNumList.size() >0){
- StringBuilder title = new StringBuilder();
- List<Integer> caseIdIds = caseNumEntityList.parallelStream().map(CaseNumEntity::getCaseId).collect(Collectors.toList());
- if(caseIdIds.size() >0){
- List<CaseEntity> list = caseService.getByIds(caseIdIds);
- List<String> collect = list.parallelStream().map(CaseEntity::getCaseTitle).collect(Collectors.toList());
- for (String str : collect) {
- title.append(str).append(",");
- }
- if(title.length()>0){
- title.delete(title.length()-1,title.length());
- }
- }
- throw new BusinessException(ResultCode.CASE_USE.code, String.format(ResultCode.CASE_USE.msg,title));
- }
- Model model = this.getById(modelId);
- if(model == null ){
- throw new BusinessException(ResultCode.MODEL_NOT_EXIST);
- }
- this.removeById(modelId);
- fusionNumService.deleteByModelId(modelId);
- if(StringUtils.isNotBlank(model.getModelGlbUrl())){
- uploadService.deleteOssUrl(model.getModelGlbUrl());
- }
- }
- @Override
- public List<Model> getListByNum(List<String> numList) {
- LambdaQueryWrapper<Model> wrapper = new LambdaQueryWrapper<>();
- wrapper.in(Model::getNum,numList);
- return this.list(wrapper);
- }
- @Override
- public List<Model> getListByModelIds(List<Integer> modelIds) {
- if(modelIds == null || modelIds.isEmpty()){
- return new ArrayList<>();
- }
- LambdaQueryWrapper<Model> wrapper = new LambdaQueryWrapper<>();
- wrapper.in(Model::getModelId,modelIds);
- return this.list(wrapper);
- }
- @Override
- public List<Model> getListByModelIdStrs(List<String> numList) {
- LambdaQueryWrapper<Model> wrapper = new LambdaQueryWrapper<>();
- wrapper.in(Model::getModelId,numList);
- wrapper.orderByDesc(Model::getCreateTime);
- return this.list(wrapper);
- }
- @Override
- public void deleteByNum(List<String> numList) {
- LambdaQueryWrapper<Model> wrapper = new LambdaQueryWrapper<>();
- wrapper.in(Model::getNum,numList);
- this.remove(wrapper);
- }
- @Override
- public List<Model> getByUserName(String username) {
- LambdaQueryWrapper<Model> wrapper = new LambdaQueryWrapper<>();
- wrapper.eq(Model::getUserName,username)
- .eq(Model::getType,3);
- return this.list(wrapper);
- }
- @Override
- public Model getIsNullNewByNum(String num,Integer type, Integer isObj) {
- LambdaQueryWrapper<Model> wrapper = new LambdaQueryWrapper<>();
- wrapper.eq(Model::getNum,num);
- wrapper.eq(Model::getType,type);
- Model model = this.getOne(wrapper);
- if(model == null){
- model = new Model();
- }
- if(model.getModelId() != null && StringUtils.isNotBlank(model.getModelGlbUrl()) && StringUtils.isNotBlank(model.getModelSize())){
- return model;
- }
- model.setModelDateType("obj");
- model.setType(type);
- model.setModelType("pointcloud"); //深时点云类型
- model.setCreateStatus(1);
- model.setIsObj(isObj);
- if(isObj == 1){ //看看,看见
- Scene scene = sceneService.getByNum(num);
- if(scene == null){
- throw new BusinessException(ResultCode.SCENE_NOT_EXIST);
- }
- String mesh3DtilesPath = String.format(FilePath.OBJ_OSS_PATH,num) + "/images/3dtiles/tileset.json";
- String sizePath = scene.getWebPath() + String.format(FilePath.OBJ_OSS_PATH,num) + "/images/3dtiles";
- if(localToOssUtil.existKey(scene.getWebPath() +mesh3DtilesPath)){
- model.setModelDateType("b3dm");
- model.setModelType("b3dm");
- model.setModelGlbUrl(JSONArray.toJSONString(Arrays.asList(scene.getMapping() +"/" +mesh3DtilesPath)));
- model.setModelSize(FileWriterUtil.setFileSize(localToOssUtil.getSizeCount( sizePath)));
- }else {
- String meshPath = String.format(FilePath.OBJ_OSS_PATH, num)+"/data/mesh";
- if(localToOssUtil.existKey( scene.getWebPath() +meshPath +"/mesh.obj")){
- Long size = localToOssUtil.getSizeCount( scene.getWebPath() +meshPath);
- model.setModelSize(FileWriterUtil.setFileSize(size));
- model.setModelGlbUrl(JSONArray.toJSONString(Arrays.asList(scene.getMapping()+"/" + meshPath+"/mesh.obj")));
- }else {
- List<String> objPaths = new ArrayList<>();
- String meshPathjs = String.format(FilePath.OBJ_OSS_PATH, num)+"/data/";
- String jsonPath =scene.getWebPath() + meshPath + "/floors.json";
- if(!localToOssUtil.existKey(jsonPath)){
- jsonPath =scene.getWebPath() + meshPath +"mesh.json";
- }
- if(localToOssUtil.existKey(jsonPath)){
- String objectContent = localToOssUtil.getObjectContent(meshPath + "/floors.json");
- JSONObject jsonObject = JSONObject.parseObject(objectContent);
- JSONArray floors1 = jsonObject.getJSONArray("floors");
- for (Object object : floors1) {
- JSONObject jb = (JSONObject) object;
- String string = jb.getString("objPath");
- objPaths.add(scene.getMapping()+"/" +meshPathjs + string);
- }
- model.setModelGlbUrl(JSONArray.toJSONString(objPaths));
- Long size = localToOssUtil.getSize(scene.getWebPath() +meshPath);
- model.setModelSize(FileWriterUtil.setFileSize(size));
- }
- }
- model.setModelType("obj");
- }
- }
- model.setNum(num);
- this.saveOrUpdate(model);
- return model;
- }
- @Override
- public SceneVo getInfo(Integer modelId) {
- Model model = this.getById(modelId);
- if(model == null){
- throw new BusinessException(ResultCode.SCENE_NOT_EXIST);
- }
- SceneVo sceneVo = new SceneVo();
- BeanUtils.copyProperties(model,sceneVo);
- return sceneVo;
- }
- @Override
- public String uploadObjProgress(Integer modelId) {
- String redisKey = RedisKeyUtil.modelUpload+modelId;
- String cancel = RedisKeyUtil.modelCancelUpload+modelId;
- if(redisUtil.hasKey(cancel)){
- return redisUtil.get(cancel);
- }
- if(redisUtil.hasKey(redisKey)){
- return redisUtil.get(redisKey);
- }
- return "0";
- }
- @Override
- public void cancelUpload(Integer modelId) {
- String redisKey = RedisKeyUtil.modelCancelUpload+modelId;
- if(redisUtil.hasKey(redisKey)){
- return;
- }
- Model model = this.getById(modelId);
- if(model == null){
- return;
- }
- model.setCreateStatus(-2);
- this.updateById(model);
- redisUtil.set(redisKey,"-2");
- }
- @Override
- public HashMap<String, Model> getMapByNum(List<String> numList) {
- HashMap<String,Model> map = new HashMap<>();
- List<Model> modelList = this.getListByNum(numList);
- modelList.forEach(entity-> map.put(entity.getNum() + entity.getType(),entity));
- return map;
- }
- @Override
- public void copyModel(Integer modelId) {
- Model modelEntity = this.getById(modelId);
- if(modelEntity == null){
- throw new BusinessException(ResultCode.MODEL_NOT_EXIST);
- }
- Integer oldId = modelEntity.getModelId();
- modelEntity.setModelTitle(modelEntity.getModelTitle() +"(copy)");
- modelEntity.setModelId(null);
- this.save(modelEntity);
- // if(StringUtils.isNotBlank(modelEntity.getModelGlbUrl())){
- // String modelGlbUrl = modelEntity.getModelGlbUrl();
- // copyOssResource(modelGlbUrl,oldId,modelEntity.getModelId());
- // String newModelObjUrl = modelGlbUrl.replaceAll(oldId+"",modelEntity.getModelId()+"");
- // modelEntity.setModelGlbUrl(newModelObjUrl);
- // this.updateById(modelEntity);
- // }
- }
- private String getGlbUrl(Integer type, String num,Model model,String scenePath) {
- if(type == 0 || type == 1 || type == 4 || type == 6 || type == 7){ //看看,看见
- //ShellUtil.yunDownload(String.format(FilePath.OBJ_OSS_PATH, num)+"/data/mesh" ,objPath);
- String objPathSource = scenePath + String.format(FilePath.OBJ_OSS_PATH, num)+"/data/mesh";
- String objPath = String.format(FilePath.OBJ_LOCAL_PATH ,environment,num);
- ShellUtil.yunDownload(objPathSource ,objPath);
- List<String> localGlbPaths = new ArrayList<>();
- List<String> ossGlbPaths = new ArrayList<>();
- File localFile = new File(objPath);
- this.toGlB(localFile,localGlbPaths);
- Long modelSize = 0L;
- if(localGlbPaths.size() >0){
- for (String localGlbPath : localGlbPaths) {
- String ossGlbPath = localGlbPath.replace(FilePath.LOCAL_BASE_PATH,"fusion/");
- localToOssUtil.uploadOss(localGlbPath,ossGlbPath);
- if(ossGlbPath.contains("lod_")){
- if(ossGlbPath.contains("lod_0")){
- ossGlbPaths.add(CacheUtil.mapping +ossGlbPath);
- modelSize += localToOssUtil.getSize(ossGlbPath);
- }
- continue;
- }
- modelSize += localToOssUtil.getSize(ossGlbPath);
- ossGlbPaths.add( CacheUtil.mapping +ossGlbPath);
- }
- model.setModelSize(FileWriterUtil.setFileSize(modelSize));
- FileUtil.del(objPath);
- return JSONArray.toJSONString(ossGlbPaths);
- }
- FileUtil.del(objPath);
- }
- return null;
- }
- private void toGlB(File localFile, List<String> localGlbPath) {
- File[] files = localFile.listFiles();
- for (File file : files) {
- if(file.isDirectory()){
- toGlB(file,localGlbPath);
- }
- if(file.getName().contains(".obj")){
- String glbPath = OBJToGLBUtil.objToGlb(file.getPath(),file.getPath().replace(".obj",".glb") );
- localGlbPath.add(glbPath);
- }
- }
- }
- }
|