Parcourir la source

修改获取fileId的逻辑

tianboguang il y a 2 ans
Parent
commit
fb0d88b06c

+ 1 - 7
pom.xml

@@ -142,13 +142,7 @@
 
     <dependency>
       <groupId>com.fdkankan</groupId>
-      <artifactId>4dkankan-utils-fyun-s3</artifactId>
-      <version>3.0.0-SNAPSHOT</version>
-    </dependency>
-
-    <dependency>
-      <groupId>com.fdkankan</groupId>
-      <artifactId>4dkankan-utils-fyun-oss</artifactId>
+      <artifactId>4dkankan-utils-fyun-https</artifactId>
       <version>3.0.0-SNAPSHOT</version>
     </dependency>
 

+ 110 - 10
src/main/java/com/fdkankan/contro/controller/SceneFileController.java

@@ -1,29 +1,36 @@
 package com.fdkankan.contro.controller;
 
+import cn.hutool.core.util.ZipUtil;
 import com.alibaba.fastjson.JSON;
 import com.alibaba.fastjson.JSONArray;
 import com.alibaba.fastjson.JSONObject;
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import com.fdkankan.common.constant.ErrorCode;
 import com.fdkankan.common.exception.BusinessException;
-import com.fdkankan.contro.entity.Company;
+import com.fdkankan.common.util.FileUtils;
+import com.fdkankan.common.util.SnowflakeIdGenerator;
+import com.fdkankan.contro.constant.RedisConstants;
+import com.fdkankan.contro.entity.SceneFileBuild;
+import com.fdkankan.contro.entity.ScenePro;
 import com.fdkankan.contro.service.ISceneFileBuildService;
+import com.fdkankan.contro.service.IScenePlusService;
+import com.fdkankan.contro.service.ISceneProService;
 import com.fdkankan.contro.vo.ResponseSceneFile;
 import com.fdkankan.fyun.face.FYunFileServiceInterface;
-import com.fdkankan.rabbitmq.bean.BuildSceneCallMessage;
-import com.fdkankan.rabbitmq.util.RabbitMqProducer;
+import com.fdkankan.model.constants.ConstantFilePath;
+import com.fdkankan.redis.util.RedisLockUtil;
+import com.fdkankan.redis.util.RedisUtil;
 import com.fdkankan.web.response.ResultData;
 import lombok.extern.log4j.Log4j2;
 import org.apache.commons.lang3.ObjectUtils;
 import org.apache.commons.lang3.StringUtils;
+import org.apache.tomcat.util.bcel.Const;
 import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.beans.factory.annotation.Value;
 import org.springframework.web.bind.annotation.*;
 
+import java.io.File;
 import java.io.IOException;
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
+import java.util.*;
 
 /**
  * 场景文件上传模块
@@ -39,6 +46,18 @@ public class SceneFileController{
     @Autowired
     private FYunFileServiceInterface fYunFileService;
 
+    @Autowired
+    private RedisLockUtil redisLockUtil;
+
+    @Autowired
+    private RedisUtil redisUtil;
+
+    @Autowired
+    private IScenePlusService scenePlusService;
+
+    @Autowired
+    private ISceneProService sceneProService;
+
     /**
      * 场景文件上传之前先获取fileId
      * @param params
@@ -120,22 +139,103 @@ public class SceneFileController{
 
     @PostMapping("sendCallAlgorithm")
     public ResultData sendCallAlgorithm(Map<String,String> params) throws Exception {
+        log.info("sendCallAlgorithm 参数为:{}",JSONObject.toJSONString(params));
         String filePath = params.get("filePath");
         if(ObjectUtils.isEmpty(filePath)){
             throw new BusinessException(ErrorCode.PARAM_ERROR,"filePath为空。");
         }
+        if(!filePath.endsWith(File.separator)){
+            filePath = filePath.concat(File.separator);
+        }
         // 读取本地文件并校验文件
         // 读取config.json
+        if(!new File(filePath+"config.json").exists()){
+            throw new BusinessException(ErrorCode.PARAM_ERROR,"缺少 config.json 文件");
+        }
 
+        JSONObject configJson = JSONObject.parseObject(FileUtils.readFile(filePath + "config.json"));
 
+        String folderName = configJson.getString("id");
+        String sncode = configJson.getString("sn");
+        String zipFileName = configJson.getString("zipFileName");
+        if(ObjectUtils.isEmpty(folderName) || ObjectUtils.isEmpty(sncode) || ObjectUtils.isEmpty(zipFileName)){
+            throw new BusinessException(ErrorCode.PARAM_ERROR,"config.json 文件有误!");
+        }
 
+        // 检测是否有生成
+        String fileId = getFileIdByFolderName(folderName);
+        String subFolder = sncode.concat(File.separator).concat(fileId).concat(File.separator).concat(folderName);
         // 解压获取dataSource 并上传资源到OSS
-        String dataSource = null;
-        // 通知计算
+        String dataSource = ConstantFilePath.BUILD_MODEL_PATH.concat(subFolder);
+        log.info("dataSource 为:{}", dataSource);
 
+        // 解压资源文件上传
+        ZipUtil.unzip(filePath.concat(zipFileName),dataSource.concat(File.separator).concat("capture"));
+
+        // 上传oaas
+        fYunFileService.uploadFileByCommand(dataSource.concat(File.separator).concat("capture"), ConstantFilePath.OSS_PREFIX.concat(subFolder));
+
+        // 通知计算
         return sceneFileBuildService.copyDataAndBuild(null,dataSource ,"V4");
     }
 
+    private String getFileIdByFolderName(String folderName) {
+        // 检测是否有生成
+        String fileId = redisUtil.get(String.format(RedisConstants.FOLDER_FILEID_BUILD, folderName));
+        if (!org.springframework.util.ObjectUtils.isEmpty(fileId)) {
+            return fileId;
+        }
+
+        SceneFileBuild sceneFileBuild = sceneFileBuildService.findByUnicode(folderName);
+        if (sceneFileBuild != null) {
+            fileId = sceneFileBuild.getFileId();
+            redisUtil.set(String.format(RedisConstants.FOLDER_FILEID_BUILD, folderName), fileId, 2 * 24 * 60 * 60);
+            return fileId;
+        }
+
+        // 加锁
+        boolean lock = redisLockUtil.lock(String.format(RedisConstants.FOLDER_LOCK_BUILD, folderName), 120);
+        if (!lock) {
+            throw new BusinessException(ErrorCode.FAILURE_CODE_5052);
+        }
+
+        // 查找场景表
+        LambdaQueryWrapper<ScenePro> proWrapper = new LambdaQueryWrapper<>();
+        proWrapper.like(ScenePro::getDataSource, "/" + folderName).eq(ScenePro::getRecStatus, 'A');
+        ScenePro pro = sceneProService.getOne(proWrapper);
+
+        String dataSource = null;
+
+        if (!org.springframework.util.ObjectUtils.isEmpty(pro)) {
+            dataSource = pro.getDataSource();
+        } else {
+            dataSource = scenePlusService.getDataSourceLikeUnicode("/" + folderName);
+        }
+
+        if (!org.springframework.util.ObjectUtils.isEmpty(dataSource)) {
+            log.info("从数据库中查到与 fileId:{} 匹配的路径为:{}", fileId, dataSource);
+            int n = dataSource.split("/").length;
+            if (n > 1) {
+                fileId = dataSource.split("/")[n - 2];
+            }
+        }
+
+        if (org.springframework.util.ObjectUtils.isEmpty(fileId)) {
+            throw new BusinessException(ErrorCode.PARAM_ERROR,"获取FileID 失败,请重新拍摄!");
+        }
+
+        sceneFileBuild = new SceneFileBuild();
+        sceneFileBuild.setChildName(dataSource.split("_")[0]);
+        sceneFileBuild.setFileId(fileId);
+        sceneFileBuild.setRecStatus("A");
+        sceneFileBuild.setUnicode(folderName);
+        sceneFileBuild.setCreateTime(new Date());
+        sceneFileBuildService.save(sceneFileBuild);
+        redisUtil.set(String.format(RedisConstants.FOLDER_FILEID_BUILD, folderName), fileId, 2 * 24 * 60 * 60);
+        redisUtil.set(String.format(RedisConstants.FILEID_FOLDER_BUILD, fileId), folderName, 2 * 24 * 60 * 60);
+        return fileId;
+    }
+
     @GetMapping("copyDataAndBuild")
     public ResultData copyDataAndBuild(@RequestParam(value = "dataSource") String dataSource,@RequestParam(value = "sceneVer") String sceneVer,
                                        @RequestParam(value = "sourceBucket",required = false) String sourceBucket) throws Exception {

+ 2 - 0
src/main/java/com/fdkankan/contro/service/ISceneFileBuildService.java

@@ -28,4 +28,6 @@ public interface ISceneFileBuildService extends IService<SceneFileBuild> {
     ResultData rebuildScene(String num,Boolean force,Boolean deleteExtras) throws IOException;
 
     ResultData copyDataAndBuild(String sourceBucet,String dataSource,String sceneVer) throws Exception;
+
+    SceneFileBuild findByUnicode(String folderName);
 }

+ 30 - 6
src/main/java/com/fdkankan/contro/service/impl/SceneFileBuildServiceImpl.java

@@ -31,6 +31,7 @@ import com.fdkankan.model.constants.ConstantFilePath;
 import com.fdkankan.model.constants.UploadFilePath;
 import com.fdkankan.rabbitmq.bean.BuildSceneCallMessage;
 import com.fdkankan.rabbitmq.util.RabbitMqProducer;
+import com.fdkankan.redis.util.RedisLockUtil;
 import com.fdkankan.redis.util.RedisUtil;
 import com.fdkankan.web.response.Result;
 import com.fdkankan.web.response.ResultData;
@@ -138,6 +139,9 @@ public class SceneFileBuildServiceImpl extends ServiceImpl<ISceneFileBuildMapper
     @Autowired
     private IFdkkLaserService fdkkLaserService;
 
+    @Autowired
+    private RedisLockUtil redisLockUtil;
+
     private RestTemplate restTemplate = new RestTemplate();
 
     @Override
@@ -206,16 +210,36 @@ public class SceneFileBuildServiceImpl extends ServiceImpl<ISceneFileBuildMapper
         }
 
         // 加锁
-        long incr = redisUtil.incr(String.format(RedisConstants.FOLDER_LOCK_BUILD, folderName), 1);
-        if (incr > 1) {
+        boolean lock = redisLockUtil.lock(String.format(RedisConstants.FOLDER_LOCK_BUILD, folderName), 120);
+        if (!lock) {
             throw new BusinessException(ErrorCode.FAILURE_CODE_5052);
         }
-        redisUtil.expire(String.format(RedisConstants.FOLDER_LOCK_BUILD, folderName), 120);
 
-        log.info("开始新生成build数据");
-        fileId = new SnowflakeIdGenerator(0, 0).nextId() + "";
-        log.info("新生成build数据,{}", fileId);
+        // 查找场景表
+        LambdaQueryWrapper<ScenePro> proWrapper = new LambdaQueryWrapper<>();
+        proWrapper.like(ScenePro::getDataSource, "/" + folderName).eq(ScenePro::getRecStatus, 'A');
+        ScenePro pro = sceneProService.getOne(proWrapper);
+
+        String dataSource = null;
+
+        if (!ObjectUtils.isEmpty(pro)) {
+            dataSource = pro.getDataSource();
+        } else {
+            dataSource = scenePlusService.getDataSourceLikeUnicode("/" + folderName);
+        }
+
+        if (!ObjectUtils.isEmpty(dataSource)) {
+            log.info("从数据库中查到与 fileId:{} 匹配的路径为:{}", fileId, dataSource);
+            int n = dataSource.split("/").length;
+            if (n > 1) {
+                fileId = dataSource.split("/")[n - 2];
+            }
+        }
 
+        if (ObjectUtils.isEmpty(fileId)) {
+            fileId = new SnowflakeIdGenerator(0, 0).nextId() + "";
+            log.info("新生成build数据,{}", fileId);
+        }
         sceneFileBuild = new SceneFileBuild();
         sceneFileBuild.setChildName(mac);
         sceneFileBuild.setFileId(fileId);