From 3e18d4bfbf2c657b08b21512c2d884cc9d59df7b Mon Sep 17 00:00:00 2001
From: dengzedong <dengzedong@email>
Date: 星期一, 14 十月 2024 16:29:49 +0800
Subject: [PATCH] 模型运行

---
 iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mpk/controller/admin/MdkController.java |   52 ++++++++++++++++++++++++++++++++++++++++++++++++----
 1 files changed, 48 insertions(+), 4 deletions(-)

diff --git a/iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mpk/controller/admin/MdkController.java b/iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mpk/controller/admin/MdkController.java
index 81ae60c..8d75179 100644
--- a/iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mpk/controller/admin/MdkController.java
+++ b/iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mpk/controller/admin/MdkController.java
@@ -2,12 +2,15 @@
 
 import com.alibaba.fastjson.JSON;
 import com.alibaba.fastjson.JSONArray;
+import com.iail.utils.RSAUtils;
 import com.iailab.framework.common.exception.enums.GlobalErrorCodeConstants;
 import com.iailab.framework.common.pojo.CommonResult;
+import com.iailab.framework.tenant.core.context.TenantContextHolder;
 import com.iailab.module.model.mpk.common.MdkConstant;
 import com.iailab.module.model.mpk.common.utils.DllUtils;
 import com.iailab.module.model.mpk.common.utils.Readtxt;
 import com.iailab.module.model.mpk.dto.MdkDTO;
+import com.iailab.module.model.mpk.dto.MdkRunDTO;
 import io.swagger.v3.oas.annotations.Operation;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Value;
@@ -16,6 +19,7 @@
 import org.springframework.web.multipart.MultipartFile;
 
 import java.io.File;
+import java.lang.reflect.Method;
 import java.net.URLClassLoader;
 import java.util.ArrayList;
 import java.util.HashMap;
@@ -37,16 +41,25 @@
     private String mpkBakFilePath;
 
 
-    @PostMapping("run")
-    public CommonResult<String> run(@RequestBody MdkDTO dto) {
+    /**
+     * @description: 模型测试运行
+     * @author: dzd
+     * @date: 2024/10/14 15:26
+     **/
+    @PostMapping("test")
+    public CommonResult<String> test(@RequestBody MdkDTO dto) {
+        Long tenantId = TenantContextHolder.getTenantId();
+        // 备份文件 租户隔离
+        String mpkTenantBakFilePath = mpkBakFilePath + File.separator + tenantId;
+
         Class<?> clazz;
         URLClassLoader classLoader;
         try {
-            File jarFile = new File(mpkBakFilePath + File.separator + MdkConstant.JAR + File.separator + dto.getPyName() + ".jar");
+            File jarFile = new File(mpkTenantBakFilePath + File.separator + MdkConstant.JAR + File.separator + dto.getPyName() + ".jar");
             if (!jarFile.exists()) {
                 throw new RuntimeException("jar包不存在,请先生成代码。jarPath:" + jarFile.getAbsolutePath());
             }
-            File dllFile = new File(mpkBakFilePath + File.separator + MdkConstant.DLL + File.separator + dto.getPyName() + ".dll");
+            File dllFile = new File(mpkTenantBakFilePath + File.separator + MdkConstant.DLL + File.separator + dto.getPyName() + ".dll");
             if (!dllFile.exists()) {
                 throw new RuntimeException("dll文件不存在,请先生成代码。dllPath:" + dllFile.getAbsolutePath());
             }
@@ -111,6 +124,37 @@
         }
     }
 
+    /**
+     * @description: 模型运行
+     * @author: dzd
+     * @date: 2024/10/14 15:26
+     **/
+    @PostMapping("run")
+    public CommonResult<String> run(@RequestBody MdkRunDTO dto) {
+        if (RSAUtils.checkLisenceBean().getCode() != 1) {
+            log.error("Lisence 不可用!");
+            return CommonResult.error(GlobalErrorCodeConstants.INTERNAL_SERVER_ERROR.getCode(),"Lisence 不可用!");
+        } else {
+            try {
+                URLClassLoader classLoader = DllUtils.getClassLoader(dto.getProjectId());
+                if (null == classLoader) {
+                    return CommonResult.error(GlobalErrorCodeConstants.ERROR_CONFIGURATION.getCode(),"请先发布项目!");
+                }
+
+                Class<?> clazz = classLoader.loadClass(dto.getClassName());
+                Method method = clazz.getMethod(dto.getMethodName(), dto.getParamsClassArray());
+                HashMap invoke = (HashMap) method.invoke(clazz.newInstance(), dto.getParamsValueArray());
+
+                // todo 将结果存入数据库
+
+            } catch (Exception e) {
+                log.error("模型运行失败",e);
+                return CommonResult.error(GlobalErrorCodeConstants.INTERNAL_SERVER_ERROR.getCode(),"模型运行失败!");
+            }
+        }
+        return CommonResult.success();
+    }
+
     @PostMapping("/import")
     @Operation(summary = "导入参数")
     public CommonResult<List<String>> importExcel(@RequestParam("file") MultipartFile file) throws Exception {

--
Gitblit v1.9.3