Browse Source

需求:实现模型文件解析部分逻辑

lijie 3 years ago
parent
commit
c577e67fac

+ 32 - 0
adm-business/adm-server/src/main/java/com/persagy/adm/server/algorithm/constant/AdmModelFileStatusEnum.java

@@ -0,0 +1,32 @@
+package com.persagy.adm.server.algorithm.constant;
+/**
+ * 模型文件状态枚举
+ * @author : lijie
+ * Update By 2022/1/17 10:47
+ */
+public enum AdmModelFileStatusEnum {
+    /**上传完成*/
+    UPLOAD_COMPLETED("1", "上传完成"),
+    /**模型初步检查完成*/
+    FIRST_CHECK_COMPLETED("2", "模型初步检查完成"),
+    /**同步数据完成*/
+    SYNC_DATA_COMPLETED("3", "同步数据完成"),
+    /**模型正常结束*/
+    NORMAL_COMPLETED("4", "模型正常结束");
+
+    private String value;
+    private String desc;
+
+    AdmModelFileStatusEnum(String value, String desc) {
+        this.value = value;
+        this.desc = desc;
+    }
+
+    public String getValue() {
+        return value;
+    }
+
+    public String getDesc() {
+        return desc;
+    }
+}

+ 45 - 0
adm-business/adm-server/src/main/java/com/persagy/adm/server/algorithm/controller/AdmModelBackController.java

@@ -0,0 +1,45 @@
+package com.persagy.adm.server.algorithm.controller;
+
+
+import com.persagy.adm.server.algorithm.domain.ModelFileVo;
+import com.persagy.adm.server.algorithm.domain.QueryModelFileVo;
+import com.persagy.adm.server.algorithm.service.AdmModelBackService;
+import com.persagy.dmp.common.model.response.CommonResult;
+import com.persagy.dmp.common.utils.ResultHelper;
+import lombok.RequiredArgsConstructor;
+import org.springframework.web.bind.annotation.PostMapping;
+import org.springframework.web.bind.annotation.RequestBody;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RestController;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * 02-模型文件后台管理
+ * @author : lijie
+ * Update By 2022/1/17 10:30
+ */
+@RestController
+@RequestMapping("/model/back")
+@RequiredArgsConstructor
+public class AdmModelBackController {
+
+    private final AdmModelBackService admModelBackService;
+
+
+    /**
+     * 020101-模型文件后台管理-进行模型文件解析
+     * @return : com.persagy.dmp.common.model.response.CommonResult<java.util.List<com.persagy.adm.server.algorithm.entity.AdmModelFile>>
+     * @author : lijie
+     * Update By 2022/1/12 16:32
+     * @undone
+     */
+    @PostMapping(value = "/analysisModelsBack")
+    public CommonResult<List<Void>> analysisModelsBack() {
+        admModelBackService.analysisModelBack();
+
+        return ResultHelper.multi(new ArrayList<>());
+    }
+
+}

+ 16 - 0
adm-business/adm-server/src/main/java/com/persagy/adm/server/algorithm/service/AdmModelBackService.java

@@ -0,0 +1,16 @@
+package com.persagy.adm.server.algorithm.service;
+/**
+ * 模型解析后台逻辑处理接口
+ * @author : lijie
+ * Update By 2022/1/17 10:33
+ */
+public interface AdmModelBackService {
+    /**
+     * 后台解析模型文件
+     * return : void
+     * @author : lijie
+     * Update By 2022/1/17 10:35
+     */
+    void analysisModelBack();
+
+}

+ 13 - 2
adm-business/adm-server/src/main/java/com/persagy/adm/server/algorithm/service/AdmModelFileService.java

@@ -3,9 +3,20 @@ package com.persagy.adm.server.algorithm.service;
 import com.persagy.adm.server.algorithm.entity.AdmModelFile;
 import com.baomidou.mybatisplus.extension.service.IService;
 
+import java.util.List;
+
 /**
- *
+ * 模型文件逻辑处理接口
+ * @author : lijie
+ * Update By 2022/1/17 10:51
  */
 public interface AdmModelFileService extends IService<AdmModelFile> {
-
+    /**
+     * 根据状态查询模型文件列表
+     * @param status : {@see AdmModelStatusEnum#value}
+     * @return : java.util.List<com.persagy.adm.server.algorithm.entity.AdmModelFile>
+     * @author : lijie
+     * Update By 2022/1/17 10:52
+     */
+    List<AdmModelFile> listByStatus(String status);
 }

+ 117 - 0
adm-business/adm-server/src/main/java/com/persagy/adm/server/algorithm/service/impl/AdmModelBackServiceImpl.java

@@ -0,0 +1,117 @@
+package com.persagy.adm.server.algorithm.service.impl;
+
+import cn.hutool.core.collection.CollUtil;
+import cn.hutool.core.io.IoUtil;
+import cn.hutool.core.map.MapUtil;
+import cn.hutool.core.util.NumberUtil;
+import cn.hutool.core.util.StrUtil;
+import cn.hutool.core.util.ZipUtil;
+import com.alibaba.fastjson.JSONObject;
+import com.persagy.adm.server.algorithm.constant.AdmModelFileStatusEnum;
+import com.persagy.adm.server.algorithm.entity.AdmModelFile;
+import com.persagy.adm.server.algorithm.service.AdmModelBackService;
+import com.persagy.adm.server.algorithm.service.AdmModelFileService;
+import com.persagy.dmp.file.utils.FileStorageHelper;
+import lombok.RequiredArgsConstructor;
+import lombok.SneakyThrows;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.stereotype.Service;
+
+import java.io.ByteArrayInputStream;
+import java.io.InputStream;
+import java.nio.charset.Charset;
+import java.nio.charset.StandardCharsets;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.zip.GZIPInputStream;
+import java.util.zip.ZipEntry;
+import java.util.zip.ZipInputStream;
+
+/**
+ * 模型解析后台逻辑处理接口
+ * @author : lijie
+ * Update By 2022/1/17 10:34
+ */
+@Service
+@RequiredArgsConstructor
+@Slf4j
+public class AdmModelBackServiceImpl implements AdmModelBackService {
+
+    private final AdmModelFileService admModelFileService;
+
+    /**
+     * 后台解析模型文件
+     * return : void
+     * @author : lijie
+     * Update By 2022/1/17 10:35
+     */
+    @Override
+    public void analysisModelBack() {
+        // 1.查询状态为3的文件
+        List<AdmModelFile> admModelFiles = admModelFileService
+                .listByStatus(AdmModelFileStatusEnum.SYNC_DATA_COMPLETED.getValue());
+        if (CollUtil.isEmpty(admModelFiles)){
+            return;
+        }
+        // 2.遍历文件
+        for (AdmModelFile admModelFile : admModelFiles) {
+            if (StrUtil.isBlank(admModelFile.getDataFileId())){
+                continue;
+            }
+            // 2.1 从文件服务下载jsonz文件
+            Map<String, JSONObject> gzipResultMap = unzipDataModelFile(FileStorageHelper.downloadStream(admModelFile.getDataFileId()));
+            if (MapUtil.isEmpty(gzipResultMap)){
+                admModelFile.setRemoved(Boolean.TRUE);
+                continue;
+            }
+
+
+
+        }
+
+    }
+    /**
+     * 解压数据文件流
+     * @param inputStream :数据文件流
+     * @return : java.util.Map<java.lang.String,com.alibaba.fastjson.JSONObject>
+     * @author : lijie
+     * Update By 2022/1/17 11:59
+     */
+    private Map<String, JSONObject> unzipDataModelFile(InputStream inputStream) {
+        Map<String, byte[]> byteMap = unzipDataModelFileToByteArray(inputStream);
+        Set<String> keySet = byteMap.keySet();
+        Map<String, JSONObject> resultMap = MapUtil.newHashMap(keySet.size());
+        for (String key : keySet) {
+            byte[] bytes = byteMap.get(key);
+            if (null==bytes || bytes.length<=0){
+                continue;
+            }
+            resultMap.put(key,JSONObject.parseObject(StrUtil.str(bytes, StandardCharsets.UTF_8)));
+        }
+        return resultMap;
+    }
+    /**
+     * 解压文件到字节流
+     * @param inputStream : 数据文件流
+     * @return : java.util.Map<java.lang.String,java.io.ByteArrayInputStream>  
+     * @author : lijie
+     * Update By 2022/1/17 12:02
+     */
+    @SneakyThrows
+    private Map<String, byte[]> unzipDataModelFileToByteArray(InputStream inputStream){
+        if (null==inputStream){
+            return new HashMap<>(0);
+        }
+        Map<String, byte[]> resultMap = MapUtil.newHashMap();
+        ZipInputStream zipInputStream = new ZipInputStream(inputStream,Charset.forName("GBK"));
+        ZipUtil.read(zipInputStream,zipEntry -> {
+            if (zipEntry.isDirectory()){
+                return;
+            }
+            resultMap.put(zipEntry.getName(),IoUtil.readBytes(zipInputStream,false));
+        });
+        return resultMap;
+    }
+}

+ 21 - 2
adm-business/adm-server/src/main/java/com/persagy/adm/server/algorithm/service/impl/AdmModelFileServiceImpl.java

@@ -1,17 +1,36 @@
 package com.persagy.adm.server.algorithm.service.impl;
 
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 import com.persagy.adm.server.algorithm.entity.AdmModelFile;
 import com.persagy.adm.server.algorithm.service.AdmModelFileService;
 import com.persagy.adm.server.algorithm.dao.AdmModelFileMapper;
 import org.springframework.stereotype.Service;
 
+import java.util.List;
+
 /**
- *
+ * 模型文件逻辑处理接口
+ * @author : lijie
+ * Update By 2022/1/17 10:54
  */
 @Service
 public class AdmModelFileServiceImpl extends ServiceImpl<AdmModelFileMapper, AdmModelFile> implements AdmModelFileService{
-
+    /**
+     * 根据状态查询模型文件列表
+     * @param status : {@see AdmModelStatusEnum#value}
+     * @return : java.util.List<com.persagy.adm.server.algorithm.entity.AdmModelFile>
+     * @author : lijie
+     * Update By 2022/1/17 10:52
+     */
+    @Override
+    public List<AdmModelFile> listByStatus(String status) {
+        LambdaQueryWrapper<AdmModelFile> queryWrapper = new LambdaQueryWrapper<>();
+        queryWrapper.eq(AdmModelFile::getStatus,status);
+        queryWrapper.eq(AdmModelFile::getRemoved,"0");
+        queryWrapper.eq(AdmModelFile::getValid,Boolean.TRUE);
+        return this.list(queryWrapper);
+    }
 }