package com.zy.ai.service; import org.springframework.stereotype.Service; import java.text.SimpleDateFormat; import java.util.*; @Service public class WcsQueryService { // 这里注入你现有的各类 Service/Mapper:deviceService/taskService/logService/configService... public Map buildDiagnosisSnapshot(Map args) { Map focus = (Map) args.get("focus"); List deviceIds = focus == null ? Collections.emptyList() : (List) focus.get("deviceIds"); List taskIds = focus == null ? Collections.emptyList() : (List) focus.get("taskIds"); int timeWindowSec = getInt(args, "timeWindowSec", 600); int logLimit = getInt(args, "logLimit", 400); boolean includeConfigs = getBool(args, "includeConfigs", true); boolean includeDerived = getBool(args, "includeDerivedSignals", true); Date now = new Date(); Date from = new Date(now.getTime() - timeWindowSec * 1000L); // 1) devices List devices = new ArrayList<>(); for (String deviceId : safeList(deviceIds)) { Map a = new HashMap<>(); a.put("deviceId", deviceId); devices.add(getDeviceState(a)); } // 2) tasks Map taskArgs = new HashMap<>(); if (!safeList(taskIds).isEmpty()) { // 你可以让 listTasks 支持 taskIds 精确过滤 taskArgs.put("taskIds", taskIds); } else if (!safeList(deviceIds).isEmpty()) { taskArgs.put("deviceId", deviceIds.get(0)); // 或改成支持 deviceIds 数组 taskArgs.put("statuses", Arrays.asList("WAITING","RUNNING","SUSPENDED")); taskArgs.put("limit", 200); } Map taskResult = (Map) listTasks(taskArgs); List tasks = taskResult == null ? new ArrayList<>() : (List) taskResult.get("tasks"); // 3) logs Map logArgs = new HashMap<>(); logArgs.put("timeFrom", iso(from)); logArgs.put("timeTo", iso(now)); logArgs.put("limit", logLimit); if (!safeList(deviceIds).isEmpty()) logArgs.put("deviceId", deviceIds.get(0)); if (!safeList(taskIds).isEmpty()) logArgs.put("taskId", taskIds.get(0)); Map logResult = (Map) queryLogs(logArgs); List logs = logResult == null ? new ArrayList<>() : (List) logResult.get("logs"); // 4) configs Object configs = null; if (includeConfigs) { configs = getSystemConfig(new HashMap()); } // 5) derived Object derived = null; if (includeDerived) { derived = deriveSignals(devices, tasks, logs); } // output Map out = new LinkedHashMap<>(); Map meta = new LinkedHashMap<>(); meta.put("snapshotId", UUID.randomUUID().toString()); meta.put("generatedAt", iso(now)); meta.put("timeFrom", iso(from)); meta.put("timeTo", iso(now)); out.put("meta", meta); out.put("devices", devices); out.put("tasks", tasks); out.put("logs", logs); out.put("configs", configs); out.put("derivedSignals", derived); return out; } private Map deriveSignals(List devices, List tasks, List logs) { Map d = new LinkedHashMap<>(); List flags = new ArrayList<>(); List causes = new ArrayList<>(); List bottlenecks = new ArrayList<>(); // TODO:把你 WCS 领域规则塞这里(心跳超时、等待确认、命令无ACK、站台满、下游阻塞等) // 先给个示例: if (tasks != null && !tasks.isEmpty() && (devices == null || devices.isEmpty())) { flags.add("HAS_TASKS_BUT_NO_DEVICE_SNAPSHOT"); } d.put("anomalyFlags", flags); d.put("suspectedRootCauses", causes); d.put("suspectedBottlenecks", bottlenecks); return d; } // ======= 下面这些函数,你用现有 service 实现即可 ======= public Map getDeviceState(Map args) { /* ... */ return new HashMap<>(); } public Map listTasks(Map args) { /* ... */ return new HashMap<>(); } public Map queryLogs(Map args) { /* ... */ return new HashMap<>(); } public Object getSystemConfig(Map args) { /* ... */ return new HashMap<>(); } // helpers private int getInt(Map m, String k, int def) { Object v = m.get(k); if (v == null) return def; if (v instanceof Number) return ((Number) v).intValue(); return Integer.parseInt(String.valueOf(v)); } private boolean getBool(Map m, String k, boolean def) { Object v = m.get(k); if (v == null) return def; if (v instanceof Boolean) return (Boolean) v; return Boolean.parseBoolean(String.valueOf(v)); } private List safeList(List l) { return l == null ? Collections.emptyList() : l; } private String iso(Date d) { SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX"); f.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai")); return f.format(d); } }