From 0a2b23ad3f30dfb01c5d590fb98f39e93bfe1932 Mon Sep 17 00:00:00 2001
From: dongyukun <1208714201@qq.com>
Date: 星期二, 31 十二月 2024 18:35:17 +0800
Subject: [PATCH] Merge remote-tracking branch 'origin/master'

---
 iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mcs/pre/service/impl/MmItemResultServiceImpl.java            |   17 ++---
 iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mpk/common/utils/DllUtils.java                               |   43 ++++++++++++--
 iailab-module-model/iailab-module-model-biz/src/main/resources/mapper/mcs/MmItemResultDao.xml                                                  |    8 ++
 iailab-module-model/iailab-module-model-biz/db/mysql.sql                                                                                       |    4 
 iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mcs/sche/service/impl/StScheduleSchemeServiceImpl.java       |    4 
 iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mdk/schedule/impl/ScheduleModelHandlerImpl.java              |    8 --
 iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mcs/pre/dao/MmItemResultDao.java                             |    4 +
 iailab-module-model/iailab-module-model-api/src/main/java/com/iailab/module/model/api/mcs/McsApi.java                                          |    2 
 iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mcs/sche/service/impl/StScheduleRecordDetailServiceImpl.java |    2 
 iailab-module-model/iailab-module-model-api/src/main/java/com/iailab/module/model/enums/ScheduleTriggerMethodEnum.java                         |   35 +++++++++++
 iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/api/McsApiImpl.java                                          |    5 +
 iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/api/MdkApiImpl.java                                          |    2 
 12 files changed, 103 insertions(+), 31 deletions(-)

diff --git a/iailab-module-model/iailab-module-model-api/src/main/java/com/iailab/module/model/api/mcs/McsApi.java b/iailab-module-model/iailab-module-model-api/src/main/java/com/iailab/module/model/api/mcs/McsApi.java
index 638ca65..aa162cf 100644
--- a/iailab-module-model/iailab-module-model-api/src/main/java/com/iailab/module/model/api/mcs/McsApi.java
+++ b/iailab-module-model/iailab-module-model-api/src/main/java/com/iailab/module/model/api/mcs/McsApi.java
@@ -91,5 +91,5 @@
 
     @GetMapping(PREFIX + "/schedule-scheme/list")
     @Operation(summary = "获取调度方案列表")
-    List<StScheduleSchemeDTO> listScheduleScheme(@RequestParam Map<String, Object> params);
+    List<StScheduleSchemeDTO> listScheduleScheme(@RequestParam("triggerMethod") String triggerMethod, @RequestParam("triggerCondition") String triggerCondition);
 }
\ No newline at end of file
diff --git a/iailab-module-model/iailab-module-model-api/src/main/java/com/iailab/module/model/enums/ScheduleTriggerMethodEnum.java b/iailab-module-model/iailab-module-model-api/src/main/java/com/iailab/module/model/enums/ScheduleTriggerMethodEnum.java
new file mode 100644
index 0000000..637f592
--- /dev/null
+++ b/iailab-module-model/iailab-module-model-api/src/main/java/com/iailab/module/model/enums/ScheduleTriggerMethodEnum.java
@@ -0,0 +1,35 @@
+package com.iailab.module.model.enums;
+
+import lombok.AllArgsConstructor;
+import lombok.Getter;
+
+/**
+ * 调度模型触发方式
+ *
+ * @author PanZhibao
+ * @Description
+ * @createTime 2024年12月31日
+ */
+@Getter
+@AllArgsConstructor
+public enum ScheduleTriggerMethodEnum {
+
+    EVENT("1", "事件触发"),
+    JOB("2", "定时触发");
+
+    private String code;
+    private String desc;
+
+    public static ScheduleTriggerMethodEnum getEumByCode(String code) {
+        if (code == null) {
+            return null;
+        }
+
+        for (ScheduleTriggerMethodEnum statusEnum : ScheduleTriggerMethodEnum.values()) {
+            if (statusEnum.getCode().equals(code)) {
+                return statusEnum;
+            }
+        }
+        return null;
+    }
+}
\ No newline at end of file
diff --git a/iailab-module-model/iailab-module-model-biz/db/mysql.sql b/iailab-module-model/iailab-module-model-biz/db/mysql.sql
index d317c33..9ca9439 100644
--- a/iailab-module-model/iailab-module-model-biz/db/mysql.sql
+++ b/iailab-module-model/iailab-module-model-biz/db/mysql.sql
@@ -47,8 +47,8 @@
     datatime  datetime,
     datavalue decimal(19, 3),
     primary key (id),
-    INDEX     idx_outputid (outputid),
-    INDEX     idx_datatime (datatime)
+    UNIQUE KEY `unique_outputid_datatime` (`outputid`,`datatime`),
+    KEY `idx_outputid_datatime` (`outputid`,`datatime`) USING BTREE
 ) engine = innodb
   default character set utf8mb4 COMMENT = '预测结果表';
 
diff --git a/iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/api/McsApiImpl.java b/iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/api/McsApiImpl.java
index bd7dc00..129b8da 100644
--- a/iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/api/McsApiImpl.java
+++ b/iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/api/McsApiImpl.java
@@ -556,7 +556,10 @@
     }
 
     @Override
-    public List<StScheduleSchemeDTO> listScheduleScheme(Map<String, Object> params) {
+    public List<StScheduleSchemeDTO> listScheduleScheme(String triggerMethod, String triggerCondition) {
+        Map<String, Object> params = new HashMap<>();
+        params.put("triggerMethod", triggerMethod);
+        params.put("triggerCondition", triggerCondition);
         return stScheduleSchemeService.list(params);
     }
 
diff --git a/iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/api/MdkApiImpl.java b/iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/api/MdkApiImpl.java
index b7a549d..221133c 100644
--- a/iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/api/MdkApiImpl.java
+++ b/iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/api/MdkApiImpl.java
@@ -8,8 +8,6 @@
 import com.iailab.module.model.mcs.pre.service.MmPredictItemService;
 import com.iailab.module.model.mcs.sche.service.StScheduleRecordService;
 import com.iailab.module.model.mcs.sche.service.StScheduleSchemeService;
-import com.iailab.module.model.mdk.factory.ItemEntityFactory;
-import com.iailab.module.model.mdk.factory.PredictItemFactory;
 import com.iailab.module.model.mdk.predict.PredictModuleHandler;
 import com.iailab.module.model.mdk.predict.PredictResultHandler;
 import com.iailab.module.model.mdk.schedule.ScheduleModelHandler;
diff --git a/iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mcs/pre/dao/MmItemResultDao.java b/iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mcs/pre/dao/MmItemResultDao.java
index be97905..264c9f9 100644
--- a/iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mcs/pre/dao/MmItemResultDao.java
+++ b/iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mcs/pre/dao/MmItemResultDao.java
@@ -4,7 +4,9 @@
 import com.iailab.framework.tenant.core.db.dynamic.TenantDS;
 import com.iailab.module.model.mcs.pre.entity.MmItemResultEntity;
 import org.apache.ibatis.annotations.Mapper;
+import org.apache.ibatis.annotations.Param;
 
+import java.util.List;
 import java.util.Map;
 
 /**
@@ -19,5 +21,7 @@
 
     void savePredictValue(Map<String, Object> params);
 
+    void saveOrUpdateItemResult(@Param("list") List<MmItemResultEntity> list);
+
     void savePredictJsonValue(Map<String, Object> params);
 }
diff --git a/iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mcs/pre/service/impl/MmItemResultServiceImpl.java b/iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mcs/pre/service/impl/MmItemResultServiceImpl.java
index 26ec871..26f58e5 100644
--- a/iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mcs/pre/service/impl/MmItemResultServiceImpl.java
+++ b/iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mcs/pre/service/impl/MmItemResultServiceImpl.java
@@ -15,8 +15,6 @@
 import org.springframework.util.CollectionUtils;
 
 import java.math.BigDecimal;
-import java.text.ParseException;
-import java.text.SimpleDateFormat;
 import java.util.*;
 import java.util.stream.Collectors;
 
@@ -85,14 +83,15 @@
             map4.put("entity", resultJson);
             mmItemResultDao.savePredictJsonValue(map4);
 
-            Map<String, Object> params = new HashMap(4);
-            params.put("TABLENAME", T_MM_ITEM_RESULT);
-            params.put("OUTPUTID", entry.getKey());
-            params.put("STARTTIME", importList.get(0).getDatatime());
-            params.put("ENDTIME", importList.get(importList.size() - 1).getDatatime());
-            mmItemResultDao.deletePredictValue(params);
+//            Map<String, Object> params = new HashMap(4);
+//            params.put("TABLENAME", T_MM_ITEM_RESULT);
+//            params.put("OUTPUTID", entry.getKey());
+//            params.put("STARTTIME", importList.get(0).getDatatime());
+//            params.put("ENDTIME", importList.get(importList.size() - 1).getDatatime());
+//            mmItemResultDao.deletePredictValue(params);
         }
-        mmItemResultDao.insertBatch(importList,max_group_count);
+        // 存在则修改,不存在插入
+        mmItemResultDao.saveOrUpdateItemResult(importList);
 
         Map<String, Object> map3 = new HashMap<>(2);
         map3.put("TABLENAME", "T_MM_ITEM_RESULT_LAST_POINT");
diff --git a/iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mcs/sche/service/impl/StScheduleRecordDetailServiceImpl.java b/iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mcs/sche/service/impl/StScheduleRecordDetailServiceImpl.java
index dd6dd9b..8f68a9f 100644
--- a/iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mcs/sche/service/impl/StScheduleRecordDetailServiceImpl.java
+++ b/iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mcs/sche/service/impl/StScheduleRecordDetailServiceImpl.java
@@ -32,7 +32,7 @@
         for (Map.Entry<String, Object > entry: resultVO.getResult().entrySet()) {
             StScheduleRecordDetailEntity entity = new StScheduleRecordDetailEntity();
             entity.setId(UUID.randomUUID().toString());
-            entity.setId(recordId);
+            entity.setRecordId(recordId);
             entity.setSchemeId(resultVO.getSchemeId());
             entity.setModelId(resultVO.getModelId());
             entity.setScheduleTime(resultVO.getScheduleTime());
diff --git a/iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mcs/sche/service/impl/StScheduleSchemeServiceImpl.java b/iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mcs/sche/service/impl/StScheduleSchemeServiceImpl.java
index 623334c..59316ec 100644
--- a/iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mcs/sche/service/impl/StScheduleSchemeServiceImpl.java
+++ b/iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mcs/sche/service/impl/StScheduleSchemeServiceImpl.java
@@ -63,8 +63,8 @@
     @Override
     public List<StScheduleSchemeDTO> list(Map<String, Object> params) {
         QueryWrapper<StScheduleSchemeEntity> queryWrapper = new QueryWrapper<>();
-        queryWrapper.eq("trigger_method", params.get("trigger_method"));
-        queryWrapper.eq("trigger_condition", params.get("trigger_condition"));
+        queryWrapper.eq("trigger_method", params.get("triggerMethod"));
+        queryWrapper.eq("trigger_condition", params.get("triggerCondition"));
         List<StScheduleSchemeEntity> list = baseDao.selectList(queryWrapper);
         return ConvertUtils.sourceToTarget(list, StScheduleSchemeDTO.class);
     }
diff --git a/iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mdk/schedule/impl/ScheduleModelHandlerImpl.java b/iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mdk/schedule/impl/ScheduleModelHandlerImpl.java
index 104d7d1..e6b062d 100644
--- a/iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mdk/schedule/impl/ScheduleModelHandlerImpl.java
+++ b/iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mdk/schedule/impl/ScheduleModelHandlerImpl.java
@@ -89,21 +89,15 @@
             param2Values[portLength] = settings;
 
             log.info("#######################调度模型 " + scheduleModel.getModelName() + " ##########################");
-//            JSONObject jsonObjNewModelBean = new JSONObject();
-//            jsonObjNewModelBean.put("newModelBean", newModelBean);
-//            log.info(String.valueOf(jsonObjNewModelBean));
-//            JSONObject jsonObjParam2Values = new JSONObject();
-//            jsonObjParam2Values.put("param2Values", param2Values);
             log.info("参数: " + JSON.toJSONString(param2Values));
-
             //IAILMDK.run
             HashMap<String, Object> modelResult = DllUtils.run(newModelBean, param2Values, scheduleScheme.getMpkprojectid());
             if (!modelResult.containsKey(CommonConstant.MDK_STATUS_CODE) || !modelResult.containsKey(CommonConstant.MDK_RESULT) ||
                     !modelResult.get(CommonConstant.MDK_STATUS_CODE).toString().equals(CommonConstant.MDK_STATUS_100)) {
                 throw new RuntimeException("模型结果异常:" + modelResult);
             }
-            modelResult = (HashMap<String, Object>) modelResult.get(CommonConstant.MDK_RESULT);
             String statusCode = modelResult.get(CommonConstant.MDK_STATUS_CODE).toString();
+            modelResult = (HashMap<String, Object>) modelResult.get(CommonConstant.MDK_RESULT);
             //打印结果
             JSONObject jsonObjResult = new JSONObject();
             jsonObjResult.put("result", modelResult);
diff --git a/iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mpk/common/utils/DllUtils.java b/iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mpk/common/utils/DllUtils.java
index ae18b35..74df6bb 100644
--- a/iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mpk/common/utils/DllUtils.java
+++ b/iailab-module-model/iailab-module-model-biz/src/main/java/com/iailab/module/model/mpk/common/utils/DllUtils.java
@@ -12,14 +12,18 @@
 import java.lang.reflect.Method;
 import java.net.URL;
 import java.net.URLClassLoader;
+import java.util.Arrays;
 import java.util.HashMap;
 import java.util.Iterator;
 import java.util.Vector;
+import java.util.stream.Collectors;
 
 @Slf4j
 public class DllUtils {
 
     private static HashMap<String, URLClassLoader> classLoaderCache = new HashMap<>();
+    private static HashMap<String, Object> classCache = new HashMap<>();
+    private static HashMap<String, Method> classMethodCache = new HashMap<>();
 
     /**
      * @description: 加载dll到指定class下
@@ -147,6 +151,23 @@
             unloadDll(urlClassLoader);
             unloadJar(urlClassLoader);
             classLoaderCache.remove(projectId);
+            removeClassCache(projectId);
+            removeClassMethodCache(projectId);
+        }
+    }
+    public static synchronized void removeClassCache(String projectId) {
+        for (String key : classCache.keySet()) {
+            if (key.startsWith(projectId)) {
+                classCache.remove(key);
+            }
+        }
+    }
+
+    public static synchronized void removeClassMethodCache(String projectId) {
+        for (String key : classMethodCache.keySet()) {
+            if (key.startsWith(projectId)) {
+                classMethodCache.remove(key);
+            }
         }
     }
 
@@ -216,13 +237,23 @@
         } else if (model == null) {
             throw new RuntimeException("模型文件不能为空!");
         } else {
-            URLClassLoader classLoader = DllUtils.getClassLoader(projectId);
-            if (null == classLoader) {
-                throw new RuntimeException("dll未发布,classLoader为null");
+            String classCacheKey = projectId + "_" + model.getClassName();
+            String methodParams = Arrays.stream(model.getParamsArray()).map(e -> e.getName()).collect(Collectors.joining(","));
+            String classMethodCacheKey = classCacheKey + "." + model.getMethodName() + "(" + methodParams + ")";
+            if (classCache.containsKey(classCacheKey) && classMethodCache.containsKey(classMethodCacheKey)) {
+                return (HashMap)classMethodCache.get(classMethodCacheKey).invoke( classCache.get(classCacheKey), paramsValueArray);
+            }else {
+                URLClassLoader classLoader = DllUtils.getClassLoader(projectId);
+                if (null == classLoader) {
+                    throw new RuntimeException("dll未发布,classLoader为null");
+                }
+                Class<?> clazz = classLoader.loadClass(model.getClassName());
+                Object o = clazz.newInstance();
+                Method method = clazz.getMethod(model.getMethodName(), model.getParamsArray());
+                classCache.put(classCacheKey,o);
+                classMethodCache.put(classMethodCacheKey,method);
+                return (HashMap)method.invoke(o, paramsValueArray);
             }
-            Class<?> clazz = classLoader.loadClass(model.getClassName());
-            Method method = clazz.getMethod(model.getMethodName(), model.getParamsArray());
-            return (HashMap)method.invoke(clazz.newInstance(), paramsValueArray);
         }
     }
 }
diff --git a/iailab-module-model/iailab-module-model-biz/src/main/resources/mapper/mcs/MmItemResultDao.xml b/iailab-module-model/iailab-module-model-biz/src/main/resources/mapper/mcs/MmItemResultDao.xml
index ec7a065..a0a6838 100644
--- a/iailab-module-model/iailab-module-model-biz/src/main/resources/mapper/mcs/MmItemResultDao.xml
+++ b/iailab-module-model/iailab-module-model-biz/src/main/resources/mapper/mcs/MmItemResultDao.xml
@@ -28,5 +28,13 @@
         VALUES (#{entity.id},#{entity.outputid},#{entity.predicttime},#{entity.jsonvalue},#{entity.cumulant})
     </insert>
 
+    <insert id="saveOrUpdateItemResult" parameterType="com.iailab.module.model.mcs.pre.entity.MmItemResultEntity">
+        INSERT INTO t_mm_item_result (id,outputid, datatime, datavalue) VALUES
+        <foreach item="item" collection="list" separator=",">
+            (#{item.id},#{item.outputid},#{item.datatime},#{item.datavalue})
+        </foreach>
+            ON DUPLICATE KEY UPDATE datavalue = VALUES(datavalue)
+    </insert>
+
 
 </mapper>
\ No newline at end of file

--
Gitblit v1.9.3