From 2b8fa3c1f2306a5d2bc009560ddb664c31b43114 Mon Sep 17 00:00:00 2001 From: 16337 <1633794139@qq.com> Date: Wed, 4 Feb 2026 10:16:39 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E7=AE=97=E6=B3=95=E9=A2=84?= =?UTF-8?q?=E7=BD=AE=E6=95=B0=E6=8D=AE=E4=B8=AD=E6=96=87=E4=B9=B1=E7=A0=81?= =?UTF-8?q?=EF=BC=9A=E5=90=AF=E5=8A=A8=E6=97=B6=E8=87=AA=E5=8A=A8=E6=A0=A1?= =?UTF-8?q?=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit SQL文件导入时编码不匹配导致中文字段为乱码, 添加@PostConstruct在启动时用Java硬编码的正确中文覆盖数据库中的预置算法数据。 Co-Authored-By: Claude Opus 4.5 --- .../service/impl/AiAlgorithmServiceImpl.java | 53 +++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/src/main/java/com/genersoft/iot/vmp/aiot/service/impl/AiAlgorithmServiceImpl.java b/src/main/java/com/genersoft/iot/vmp/aiot/service/impl/AiAlgorithmServiceImpl.java index 109f8678d..7a1360f07 100644 --- a/src/main/java/com/genersoft/iot/vmp/aiot/service/impl/AiAlgorithmServiceImpl.java +++ b/src/main/java/com/genersoft/iot/vmp/aiot/service/impl/AiAlgorithmServiceImpl.java @@ -5,6 +5,7 @@ import com.genersoft.iot.vmp.aiot.config.AiServiceConfig; import com.genersoft.iot.vmp.aiot.dao.AiAlgorithmMapper; import com.genersoft.iot.vmp.aiot.service.IAiAlgorithmService; import com.genersoft.iot.vmp.aiot.service.IAiConfigLogService; +import jakarta.annotation.PostConstruct; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @@ -12,6 +13,7 @@ import org.springframework.web.client.RestTemplate; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; +import java.util.HashMap; import java.util.List; import java.util.Map; @@ -30,6 +32,57 @@ public class AiAlgorithmServiceImpl implements IAiAlgorithmService { private static final DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); + /** + * 预置算法定义,启动时自动校正数据库中的乱码数据 + */ + private static final Map PRESET_ALGORITHMS = new HashMap<>(); + static { + // algoCode -> {algoName, targetClass, description, paramSchema} + PRESET_ALGORITHMS.put("leave_post", new String[]{ + "离岗检测", "person", "检测人员是否在岗,支持工作时间段配置", + "{\"confirm_on_duty_sec\":{\"type\":\"int\",\"default\":10,\"min\":1},\"confirm_leave_sec\":{\"type\":\"int\",\"default\":10,\"min\":1},\"cooldown_sec\":{\"type\":\"int\",\"default\":300,\"min\":0},\"working_hours\":{\"type\":\"list\",\"default\":[]}}" + }); + PRESET_ALGORITHMS.put("intrusion", new String[]{ + "周界入侵检测", "person", "检测人员进入指定区域", + "{\"cooldown_seconds\":{\"type\":\"int\",\"default\":120,\"min\":0},\"confirm_seconds\":{\"type\":\"int\",\"default\":5,\"min\":1}}" + }); + PRESET_ALGORITHMS.put("crowd_detection", new String[]{ + "人群聚集检测", "person", "检测区域内人员数量是否超过阈值", + "{\"max_count\":{\"type\":\"int\",\"default\":10,\"min\":1},\"cooldown_seconds\":{\"type\":\"int\",\"default\":300,\"min\":0}}" + }); + } + + @PostConstruct + public void initPresetAlgorithms() { + String now = LocalDateTime.now().format(FORMATTER); + for (Map.Entry entry : PRESET_ALGORITHMS.entrySet()) { + String code = entry.getKey(); + String[] vals = entry.getValue(); + AiAlgorithm existing = algorithmMapper.queryByCode(code); + if (existing == null) { + AiAlgorithm algo = new AiAlgorithm(); + algo.setAlgoCode(code); + algo.setAlgoName(vals[0]); + algo.setTargetClass(vals[1]); + algo.setDescription(vals[2]); + algo.setParamSchema(vals[3]); + algo.setIsActive(1); + algo.setCreateTime(now); + algo.setUpdateTime(now); + algorithmMapper.add(algo); + log.info("[AI算法] 初始化预置算法: {}", code); + } else { + existing.setAlgoName(vals[0]); + existing.setTargetClass(vals[1]); + existing.setDescription(vals[2]); + existing.setParamSchema(vals[3]); + existing.setUpdateTime(now); + algorithmMapper.updateByCode(existing); + log.info("[AI算法] 校正预置算法数据: {}", code); + } + } + } + @Override public List queryAll() { return algorithmMapper.queryAll();