修复算法预置数据中文乱码:启动时自动校正

SQL文件导入时编码不匹配导致中文字段为乱码,
添加@PostConstruct在启动时用Java硬编码的正确中文覆盖数据库中的预置算法数据。

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
2026-02-04 10:16:39 +08:00
parent 88da798cc0
commit 2b8fa3c1f2

View File

@@ -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<String, String[]> 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<String, String[]> 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<AiAlgorithm> queryAll() {
return algorithmMapper.queryAll();