package com.fdkankan.openApi.service.system.impl; import cn.hutool.core.collection.CollectionUtil; import cn.hutool.core.io.FileUtil; import cn.hutool.core.util.ObjectUtil; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.fdkankan.common.exception.BusinessException; import com.fdkankan.fyun.face.FYunFileServiceInterface; import com.fdkankan.openApi.bean.vo.SceneDataDownloadVO; import com.fdkankan.openApi.entity.system.SceneDataDownloadEntity; import com.fdkankan.openApi.mapper.system.SceneDataDownloadMapper; import com.fdkankan.openApi.service.laser.SceneService; import com.fdkankan.openApi.service.system.SceneDataDownloadService; import com.fdkankan.web.response.ResultData; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.stream.Collectors; /** * Created by Xiewj on 2022年11月21日11:22:02 */ @Slf4j @Service public class SceneDataDownloadServiceImpl extends ServiceImpl implements SceneDataDownloadService { @Value("${laserConfig.defaultFolder}") private String laserDefaultFolder; @Value("${laserConfig.ossUrl}") private String laserOSSurl; @Value("${openApiPath.temp}") private String tempPath; private String laserBucket="laser-data"; @Autowired SceneService sceneService; @Autowired private FYunFileServiceInterface fYunFileService; @Override @Transactional(rollbackFor = Exception.class) public ResultData sceneDownloadDepthMapAndPly(String sceneCode, Long userId) { List sceneDataDownloadEntityList = findByOssDeleteIsNullBySceneCode(sceneCode); String xlsxUrl="sceneRawData/"+sceneCode+"/"+sceneCode+".xlsx"; String e57Url=laserDefaultFolder + "/" + sceneCode + "/data/"+sceneCode+"_e57.zip"; boolean e57Flag=false; if(fYunFileService.fileExist(laserBucket,e57Url)){ e57Flag=true; } boolean e57DBFlag = sceneDataDownloadEntityList.stream().allMatch(a -> a.getFileName().contains("e57")); if (CollectionUtil.isNotEmpty(sceneDataDownloadEntityList) &&fYunFileService.fileExist(laserBucket,xlsxUrl)&&e57Flag&&e57DBFlag){ log.info("场景存在库"); List sceneDataDownloadVOS = sceneDataDownloadEntityList.stream() .map(sceneData -> new SceneDataDownloadVO(sceneData.getFileName(),laserOSSurl+sceneData.getOssKey())) .collect(Collectors.toList()); return ResultData.ok(sceneDataDownloadVOS); }else { List copyFiles=new ArrayList<>(); //先检查e57 if(e57Flag){ String e57CopyUrl="sceneRawData/"+sceneCode+"/"+ FileUtil.getName(e57Url); fYunFileService.copyFileInBucket(laserBucket,e57Url,e57CopyUrl); copyFiles.add(e57CopyUrl); e57Flag=true; } List strings = fYunFileService.listRemoteFiles(laserBucket, laserDefaultFolder + "/" + sceneCode + "/data/" + sceneCode + "/depthmap/"); boolean has_cloud=false; boolean has_png=false; ArrayList> rows =new ArrayList<>(); for (String file : strings) { String name = FileUtil.getName(file); if (name.contains("ply")&&e57Flag){ continue; } if (FileUtil.extName(file).equalsIgnoreCase("png")&& has_png==false){ has_png=true; } if (FileUtil.extName(file).equalsIgnoreCase("ply")&& has_cloud==false){ has_cloud=true; } String copyUrl="sceneRawData/"+sceneCode+"/"+ FileUtil.getName(file); fYunFileService.copyFileInBucket(laserBucket,file,copyUrl); copyFiles.add(copyUrl); } if ((has_png && has_cloud )|| (has_png && e57Flag)){ return saveAndDownLoad(sceneCode, copyFiles,userId); }else if (!has_png && !has_cloud ){ throw new BusinessException(-1,"数据不全,请重算后再尝试下载"); }else if (has_png && (!has_cloud||!e57Flag )){ throw new BusinessException(-1,"点云数据不全,请重算后再尝试下载"); }else if (!has_png && has_cloud){ throw new BusinessException(-1,"深度图数据不全,请重算后再尝试下载"); } } return ResultData.ok(); } private ResultData saveAndDownLoad(String sceneCode, List data,Long userId) { List dataDownloadEntities=new ArrayList<>(); for (String file : data) { String name = FileUtil.getName(file); //上传OSS并且入库 dataDownloadEntities.add(findAndSave(sceneCode, userId, file, name, 1)); } List sceneDataDownloadVOS = dataDownloadEntities.stream() .map(sceneData -> new SceneDataDownloadVO(sceneData.getFileName(),laserOSSurl+sceneData.getOssKey())) .collect(Collectors.toList()); return ResultData.ok(sceneDataDownloadVOS); } private SceneDataDownloadEntity findAndSave(String sceneCode, Long userId, String xlsxUrl,String fileName,int type) { SceneDataDownloadEntity sceneDataDownload = findBySceneCodeAndFileNameAndOssDeleteIsNull( sceneCode,fileName); if (ObjectUtil.isNull(sceneDataDownload)){ sceneDataDownload=new SceneDataDownloadEntity(); sceneDataDownload.setOssKey(xlsxUrl); sceneDataDownload.setFileName(fileName); sceneDataDownload.setType(type); sceneDataDownload.setSceneCode(sceneCode); sceneDataDownload.setBucket(laserBucket); sceneDataDownload.setUserId(userId); save(sceneDataDownload); return sceneDataDownload; } return sceneDataDownload; } // @Override public List findByOssDeleteIsNull() { return list(Wrappers.lambdaQuery().isNull(SceneDataDownloadEntity::getOssDelete)); } @Override public List findByOssDeleteIsNullBySceneCode(String sceneCode) { LambdaQueryWrapper wrapper = Wrappers.lambdaQuery(); wrapper.isNull(SceneDataDownloadEntity::getOssDelete); wrapper.eq(SceneDataDownloadEntity::getSceneCode,sceneCode); return list(wrapper); } @Override public SceneDataDownloadEntity findBySceneCodeAndFileNameAndOssDeleteIsNull(String sceneCode, String fileName) { LambdaQueryWrapper wrapper = Wrappers.lambdaQuery(); wrapper.isNull(SceneDataDownloadEntity::getOssDelete); wrapper.eq(SceneDataDownloadEntity::getSceneCode,sceneCode); wrapper.eq(SceneDataDownloadEntity::getFileName,fileName); return getOne(wrapper); } }