package com.mes.core; import com.alibaba.fastjson2.JSON; import com.alibaba.fastjson2.JSONObject; import com.alibaba.fastjson2.JSONWriter; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.*; import java.nio.charset.StandardCharsets; import java.nio.file.*; import java.util.*; /** * 调试配置管理器 - 持久化调试规则到文件 */ public class DebugConfigManager { private static final Logger log = LoggerFactory.getLogger(DebugConfigManager.class); private static final String CONFIG_DIR = "config"; private static final String CONFIG_FILE = "debug_rules.json"; private final Path configPath; private final Map stationConfigs = new HashMap<>(); /** * 单个工位的调试配置 */ public static class StationDebugConfig { private String stationCode; private Map persistentRules = new HashMap<>(); private boolean enabled = false; public StationDebugConfig() {} public StationDebugConfig(String stationCode) { this.stationCode = stationCode; } public String getStationCode() { return stationCode; } public void setStationCode(String stationCode) { this.stationCode = stationCode; } public Map getPersistentRules() { return persistentRules; } public void setPersistentRules(Map rules) { this.persistentRules = rules; } public boolean isEnabled() { return enabled; } public void setEnabled(boolean enabled) { this.enabled = enabled; } public void addRule(String fieldName, Object value) { persistentRules.put(fieldName, value); } public void removeRule(String fieldName) { persistentRules.remove(fieldName); } } public DebugConfigManager() { // 配置文件路径:当前目录/config/debug_rules.json this.configPath = Paths.get(CONFIG_DIR, CONFIG_FILE); load(); } public DebugConfigManager(String configDir) { this.configPath = Paths.get(configDir, CONFIG_FILE); load(); } /** * 加载配置 */ public void load() { if (!Files.exists(configPath)) { log.info("[DebugConfig] 配置文件不存在,使用默认配置: {}", configPath); return; } try { String content = new String(Files.readAllBytes(configPath), StandardCharsets.UTF_8); JSONObject json = JSON.parseObject(content); if (json != null && json.containsKey("stations")) { JSONObject stations = json.getJSONObject("stations"); for (String stationCode : stations.keySet()) { JSONObject stationJson = stations.getJSONObject(stationCode); StationDebugConfig config = new StationDebugConfig(stationCode); config.setEnabled(stationJson.getBooleanValue("enabled")); JSONObject rules = stationJson.getJSONObject("rules"); if (rules != null) { for (String fieldName : rules.keySet()) { config.addRule(fieldName, rules.get(fieldName)); } } stationConfigs.put(stationCode, config); } } log.info("[DebugConfig] 加载配置成功,共 {} 个工位配置", stationConfigs.size()); } catch (Exception e) { log.error("[DebugConfig] 加载配置失败: {}", e.getMessage(), e); } } /** * 保存配置 */ public void save() { try { // 确保目录存在 Files.createDirectories(configPath.getParent()); JSONObject root = new JSONObject(); JSONObject stations = new JSONObject(); for (Map.Entry entry : stationConfigs.entrySet()) { StationDebugConfig config = entry.getValue(); JSONObject stationJson = new JSONObject(); stationJson.put("enabled", config.isEnabled()); stationJson.put("rules", config.getPersistentRules()); stations.put(entry.getKey(), stationJson); } root.put("stations", stations); root.put("lastModified", System.currentTimeMillis()); String content = JSON.toJSONString(root, JSONWriter.Feature.PrettyFormat); Files.write(configPath, content.getBytes(StandardCharsets.UTF_8)); log.info("[DebugConfig] 保存配置成功: {}", configPath); } catch (Exception e) { log.error("[DebugConfig] 保存配置失败: {}", e.getMessage(), e); } } /** * 获取工位配置 */ public StationDebugConfig getStationConfig(String stationCode) { return stationConfigs.computeIfAbsent(stationCode, StationDebugConfig::new); } /** * 添加规则 */ public void addRule(String stationCode, String fieldName, Object value) { getStationConfig(stationCode).addRule(fieldName, value); save(); } /** * 移除规则 */ public void removeRule(String stationCode, String fieldName) { getStationConfig(stationCode).removeRule(fieldName); save(); } /** * 设置工位调试启用状态 */ public void setEnabled(String stationCode, boolean enabled) { getStationConfig(stationCode).setEnabled(enabled); save(); } /** * 应用配置到拦截器 */ public void applyToInterceptor(String stationCode, ConditionInterceptor interceptor) { StationDebugConfig config = stationConfigs.get(stationCode); if (config != null && config.isEnabled()) { interceptor.clearRules(); for (Map.Entry rule : config.getPersistentRules().entrySet()) { interceptor.addRule(rule.getKey(), rule.getValue()); } log.info("[DebugConfig] 应用 {} 条规则到工位 {}", config.getPersistentRules().size(), stationCode); } } }