From e1a6b6a5507c101d58c1ae6183361df18daef1da Mon Sep 17 00:00:00 2001
From: zhou zhou <3272660260@qq.com>
Date: 星期六, 21 三月 2026 10:38:27 +0800
Subject: [PATCH] #ai rsf_query_task
---
rsf-server/src/main/java/com/vincent/rsf/server/ai/tool/RsfWmsTaskTools.java | 275 ++++++++++++++++++++++++++++++++++++++++++------------
1 files changed, 211 insertions(+), 64 deletions(-)
diff --git a/rsf-server/src/main/java/com/vincent/rsf/server/ai/tool/RsfWmsTaskTools.java b/rsf-server/src/main/java/com/vincent/rsf/server/ai/tool/RsfWmsTaskTools.java
index 377a73b..93ebf7d 100644
--- a/rsf-server/src/main/java/com/vincent/rsf/server/ai/tool/RsfWmsTaskTools.java
+++ b/rsf-server/src/main/java/com/vincent/rsf/server/ai/tool/RsfWmsTaskTools.java
@@ -2,7 +2,10 @@
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.vincent.rsf.framework.exception.CoolException;
+import com.vincent.rsf.server.common.utils.FieldsUtils;
import com.vincent.rsf.server.manager.entity.Task;
+import com.vincent.rsf.server.manager.entity.TaskItem;
+import com.vincent.rsf.server.manager.service.TaskItemService;
import com.vincent.rsf.server.manager.service.TaskService;
import lombok.RequiredArgsConstructor;
import org.springframework.ai.tool.annotation.Tool;
@@ -14,36 +17,104 @@
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
+import java.util.Objects;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
@Component
@RequiredArgsConstructor
public class RsfWmsTaskTools {
+ private static final Pattern TASK_CODE_PATTERN = Pattern.compile("[A-Za-z]{1,8}\\d{6,}");
+ private static final Pattern TASK_CODE_DIGIT_PATTERN = Pattern.compile("\\d{8,}");
+
private final TaskService taskService;
+ private final TaskItemService taskItemService;
/**
- * 鏌ヨ浠诲姟鍒楄〃銆�
- * 鏂规硶瑕佹眰鑷冲皯甯︿竴涓繃婊ゆ潯浠讹紝閬垮厤妯″瀷鎶婁换鍔¤〃褰撲綔鍙洿鎺ラ亶鍘嗙殑鏁版嵁婧愩��
+ * 缁熶竴浠诲姟鏌ヨ銆�
+ * 浼犵簿纭� taskCode 鏃惰繑鍥炰换鍔℃眹鎬诲拰鏄庣粏锛涘惁鍒欐寜鏉′欢杩斿洖浠诲姟鍒楄〃銆�
*/
- @Tool(name = "rsf_query_task_list", description = "鍙鏌ヨ宸ュ叿銆傛寜浠诲姟鍙枫�佺姸鎬併�佷换鍔$被鍨嬨�佹簮绔欑偣銆佺洰鏍囩珯鐐圭瓑鏉′欢鏌ヨ浠诲姟鍒楄〃銆�")
- public List<Map<String, Object>> queryTaskList(
- @ToolParam(description = "浠诲姟鍙凤紝鍙ā绯婃煡璇�") String taskCode,
- @ToolParam(description = "浠诲姟鐘舵�侊紝鍙��") Integer taskStatus,
- @ToolParam(description = "浠诲姟绫诲瀷锛屽彲閫�") Integer taskType,
+ @Tool(name = "rsf_query_task", description = "鍙鏌ヨ宸ュ叿銆傛敮鎸佹寜浠诲姟鍙枫�佺姸鎬併�佺被鍨嬨�佹簮绔欑偣銆佺洰鏍囩珯鐐规煡璇换鍔★紱浼犵簿纭换鍔″彿鏃惰繑鍥炰换鍔℃槑缁嗭紝涓嶄紶杩囨护鏉′欢鏃惰繑鍥炴渶杩戜换鍔°��")
+ public Map<String, Object> queryTask(
+ @ToolParam(description = "浠诲姟鍙凤紝鍙ā绯婃煡璇紱鏀寔鐩存帴浼犲叆鑷劧璇█锛屽伐鍏蜂細鑷姩鎻愬彇浠诲姟鍙�") String taskCode,
+ @ToolParam(description = "浠诲姟鐘舵�侊紝鍙�夛紱绌哄瓧绗︿覆鎴� 0 瑙嗕负鏈紶") String taskStatus,
+ @ToolParam(description = "浠诲姟绫诲瀷锛屽彲閫夛紱绌哄瓧绗︿覆鎴� 0 瑙嗕负鏈紶") String taskType,
@ToolParam(description = "婧愮珯鐐癸紝鍙��") String orgSite,
@ToolParam(description = "鐩爣绔欑偣锛屽彲閫�") String targSite,
- @ToolParam(description = "杩斿洖鏉℃暟锛岄粯璁� 10锛屾渶澶� 50") Integer limit) {
- String normalizedTaskCode = BuiltinToolGovernanceSupport.sanitizeQueryText(taskCode, "浠诲姟鍙�", 64);
+ @ToolParam(description = "杩斿洖鏉℃暟锛岄粯璁� 10锛屾渶澶� 50锛涚┖瀛楃涓叉垨 0 瑙嗕负榛樿鍊�") String limit) {
+ String normalizedTaskCode = normalizeTaskCode(taskCode);
+ Integer normalizedTaskStatus = normalizeOptionalFilterNumber(taskStatus, "浠诲姟鐘舵��");
+ Integer normalizedTaskType = normalizeOptionalFilterNumber(taskType, "浠诲姟绫诲瀷");
String normalizedOrgSite = BuiltinToolGovernanceSupport.sanitizeQueryText(orgSite, "婧愮珯鐐�", 64);
String normalizedTargSite = BuiltinToolGovernanceSupport.sanitizeQueryText(targSite, "鐩爣绔欑偣", 64);
- BuiltinToolGovernanceSupport.requireAnyFilter("浠诲姟鍒楄〃鏌ヨ鑷冲皯闇�瑕佹彁渚涗竴涓繃婊ゆ潯浠�",
- normalizedTaskCode, normalizedOrgSite, normalizedTargSite,
- taskStatus == null ? null : String.valueOf(taskStatus),
- taskType == null ? null : String.valueOf(taskType));
+ int finalLimit = normalizeLimit(limit, 10, 50);
+
+ Task exactMatchedTask = findExactMatchedTask(normalizedTaskCode, normalizedTaskStatus, normalizedTaskType,
+ normalizedOrgSite, normalizedTargSite);
+ if (exactMatchedTask != null) {
+ Map<String, Object> response = new LinkedHashMap<>();
+ response.put("queryMode", "detail");
+ response.put("taskCount", 1);
+ response.put("limit", finalLimit);
+ response.put("resolvedTaskCode", exactMatchedTask.getTaskCode());
+ response.put("tasks", List.of(buildTaskDetail(exactMatchedTask)));
+ return response;
+ }
+
LambdaQueryWrapper<Task> queryWrapper = new LambdaQueryWrapper<>();
- int finalLimit = BuiltinToolGovernanceSupport.normalizeLimit(limit, 10, 50);
- if (StringUtils.hasText(normalizedTaskCode)) {
- queryWrapper.like(Task::getTaskCode, normalizedTaskCode);
+ applyCommonFilters(queryWrapper, normalizedTaskCode, normalizedTaskStatus, normalizedTaskType,
+ normalizedOrgSite, normalizedTargSite, true);
+ queryWrapper.orderByDesc(Task::getCreateTime).last("LIMIT " + finalLimit);
+ List<Task> tasks = taskService.list(queryWrapper);
+ List<Map<String, Object>> result = new ArrayList<>();
+ for (Task task : tasks) {
+ result.add(buildTaskSummary(task));
+ }
+ Map<String, Object> response = new LinkedHashMap<>();
+ response.put("queryMode", "list");
+ response.put("taskCount", result.size());
+ response.put("limit", finalLimit);
+ response.put("resolvedTaskCode", normalizedTaskCode);
+ response.put("tasks", result);
+ return response;
+ }
+
+ private Task findExactMatchedTask(String taskCode, Integer taskStatus, Integer taskType, String orgSite, String targSite) {
+ if (!StringUtils.hasText(taskCode)) {
+ return null;
+ }
+ Task exactTask = findUniqueTask(taskCode, taskStatus, taskType, orgSite, targSite, false);
+ if (exactTask != null) {
+ return exactTask;
+ }
+ String digitCandidate = extractDigitCandidate(taskCode);
+ if (!StringUtils.hasText(digitCandidate) || digitCandidate.equals(taskCode)) {
+ return null;
+ }
+ return findUniqueTask(digitCandidate, taskStatus, taskType, orgSite, targSite, true);
+ }
+
+ private Task findUniqueTask(String taskCode, Integer taskStatus, Integer taskType, String orgSite,
+ String targSite, boolean fuzzyTaskCode) {
+ LambdaQueryWrapper<Task> queryWrapper = new LambdaQueryWrapper<>();
+ applyCommonFilters(queryWrapper, taskCode, taskStatus, taskType, orgSite, targSite, fuzzyTaskCode);
+ queryWrapper.orderByDesc(Task::getCreateTime).last("LIMIT 2");
+ List<Task> tasks = taskService.list(queryWrapper);
+ if (tasks.size() == 1) {
+ return tasks.get(0);
+ }
+ return null;
+ }
+
+ private void applyCommonFilters(LambdaQueryWrapper<Task> queryWrapper, String taskCode, Integer taskStatus,
+ Integer taskType, String orgSite, String targSite, boolean fuzzyTaskCode) {
+ if (StringUtils.hasText(taskCode)) {
+ if (fuzzyTaskCode) {
+ queryWrapper.like(Task::getTaskCode, taskCode);
+ } else {
+ queryWrapper.eq(Task::getTaskCode, taskCode);
+ }
}
if (taskStatus != null) {
queryWrapper.eq(Task::getTaskStatus, taskStatus);
@@ -51,61 +122,68 @@
if (taskType != null) {
queryWrapper.eq(Task::getTaskType, taskType);
}
- if (StringUtils.hasText(normalizedOrgSite)) {
- queryWrapper.eq(Task::getOrgSite, normalizedOrgSite);
+ if (StringUtils.hasText(orgSite)) {
+ queryWrapper.eq(Task::getOrgSite, orgSite);
}
- if (StringUtils.hasText(normalizedTargSite)) {
- queryWrapper.eq(Task::getTargSite, normalizedTargSite);
+ if (StringUtils.hasText(targSite)) {
+ queryWrapper.eq(Task::getTargSite, targSite);
}
- queryWrapper.orderByDesc(Task::getCreateTime).last("LIMIT " + finalLimit);
- List<Task> tasks = taskService.list(queryWrapper);
- List<Map<String, Object>> result = new ArrayList<>();
- for (Task task : tasks) {
- result.add(buildTaskSummary(task));
- }
- return result;
}
- /**
- * 鏌ヨ鍗曚釜浠诲姟璇︽儏銆�
- * 涓庡垪琛ㄦ煡璇笉鍚岋紝杩欓噷鍏佽杩斿洖鏇翠赴瀵岀殑瀛楁锛屼絾浠嶇劧瑕佹眰璋冪敤鏂归�氳繃浠诲姟 ID 鎴栦换鍔″彿鍋氱簿纭畾浣嶃��
- */
- @Tool(name = "rsf_query_task_detail", description = "鍙鏌ヨ宸ュ叿銆傛牴鎹换鍔� ID 鎴栦换鍔″彿鏌ヨ浠诲姟璇︽儏銆�")
- public Map<String, Object> queryTaskDetail(
- @ToolParam(description = "浠诲姟 ID") Long taskId,
- @ToolParam(description = "浠诲姟鍙�") String taskCode) {
- String normalizedTaskCode = BuiltinToolGovernanceSupport.sanitizeQueryText(taskCode, "浠诲姟鍙�", 64);
- if (taskId == null && !StringUtils.hasText(normalizedTaskCode)) {
- throw new CoolException("浠诲姟 ID 鍜屼换鍔″彿鑷冲皯闇�瑕佹彁渚涗竴涓�");
+ private String normalizeTaskCode(String taskCode) {
+ String normalized = BuiltinToolGovernanceSupport.sanitizeQueryText(taskCode, "浠诲姟鍙�", 128);
+ if (!StringUtils.hasText(normalized)) {
+ return null;
}
- Task task;
- if (taskId != null) {
- task = taskService.getById(taskId);
- } else {
- task = taskService.getOne(new LambdaQueryWrapper<Task>().eq(Task::getTaskCode, normalizedTaskCode));
+ Matcher taskCodeMatcher = TASK_CODE_PATTERN.matcher(normalized);
+ if (taskCodeMatcher.find()) {
+ return taskCodeMatcher.group().toUpperCase();
}
- if (task == null) {
- throw new CoolException("鏈煡璇㈠埌浠诲姟");
+ Matcher digitMatcher = TASK_CODE_DIGIT_PATTERN.matcher(normalized);
+ if (digitMatcher.find()) {
+ return digitMatcher.group();
}
- Map<String, Object> result = buildTaskSummary(task);
- result.put("resource", task.getResource());
- result.put("exceStatus", task.getExceStatus());
- result.put("orgLoc", task.getOrgLoc());
- result.put("targLoc", task.getTargLoc());
- result.put("orgSite", task.getOrgSite());
- result.put("orgSiteLabel", task.getOrgSite$());
- result.put("targSite", task.getTargSite());
- result.put("targSiteLabel", task.getTargSite$());
- result.put("barcode", task.getBarcode());
- result.put("robotCode", task.getRobotCode());
- result.put("memo", task.getMemo());
- result.put("expCode", task.getExpCode());
- result.put("expDesc", task.getExpDesc());
- result.put("startTime", task.getStartTime$());
- result.put("endTime", task.getEndTime$());
- result.put("createTime", task.getCreateTime$());
- result.put("updateTime", task.getUpdateTime$());
- return result;
+ return normalized.replace("*", "").trim();
+ }
+
+ private String extractDigitCandidate(String taskCode) {
+ if (!StringUtils.hasText(taskCode)) {
+ return null;
+ }
+ Matcher digitMatcher = TASK_CODE_DIGIT_PATTERN.matcher(taskCode);
+ if (digitMatcher.find()) {
+ return digitMatcher.group();
+ }
+ return null;
+ }
+
+ private Integer normalizeOptionalFilterNumber(String value, String fieldLabel) {
+ String normalized = BuiltinToolGovernanceSupport.sanitizeQueryText(value, fieldLabel, 32);
+ if (!StringUtils.hasText(normalized)) {
+ return null;
+ }
+ try {
+ int parsed = Integer.parseInt(normalized);
+ return parsed <= 0 ? null : parsed;
+ } catch (NumberFormatException e) {
+ throw new CoolException(fieldLabel + "蹇呴』鏄暣鏁�");
+ }
+ }
+
+ private int normalizeLimit(String value, int defaultValue, int maxValue) {
+ String normalized = BuiltinToolGovernanceSupport.sanitizeQueryText(value, "limit", 16);
+ if (!StringUtils.hasText(normalized)) {
+ return defaultValue;
+ }
+ try {
+ int parsed = Integer.parseInt(normalized);
+ if (parsed <= 0) {
+ return defaultValue;
+ }
+ return BuiltinToolGovernanceSupport.normalizeLimit(parsed, defaultValue, maxValue);
+ } catch (NumberFormatException e) {
+ throw new CoolException("limit 蹇呴』鏄暣鏁�");
+ }
}
private Map<String, Object> buildTaskSummary(Task task) {
@@ -128,4 +206,73 @@
return item;
}
+ private Map<String, Object> buildTaskDetail(Task task) {
+ Map<String, Object> result = buildTaskSummary(task);
+ List<TaskItem> taskItems = taskItemService.list(new LambdaQueryWrapper<TaskItem>()
+ .eq(TaskItem::getTaskId, task.getId()));
+ result.put("itemCount", taskItems.size());
+
+ double totalAnfme = 0D;
+ double totalWorkQty = 0D;
+ double totalQty = 0D;
+ List<Map<String, Object>> items = new ArrayList<>();
+ for (TaskItem taskItem : taskItems) {
+ totalAnfme += taskItem.getAnfme() == null ? 0D : taskItem.getAnfme();
+ totalWorkQty += taskItem.getWorkQty() == null ? 0D : taskItem.getWorkQty();
+ totalQty += taskItem.getQty() == null ? 0D : taskItem.getQty();
+ items.add(buildTaskItemRow(taskItem));
+ }
+
+ result.put("totalAnfme", totalAnfme);
+ result.put("totalWorkQty", totalWorkQty);
+ result.put("totalQty", totalQty);
+ result.put("items", items);
+ return result;
+ }
+
+ private Map<String, Object> buildTaskItemRow(TaskItem taskItem) {
+ if (!Objects.isNull(taskItem.getFieldsIndex())) {
+ taskItem.setExtendFields(FieldsUtils.getFields(taskItem.getFieldsIndex()));
+ }
+ Map<String, Object> item = new LinkedHashMap<>();
+ item.put("id", taskItem.getId());
+ item.put("taskId", taskItem.getTaskId());
+ item.put("matnrId", taskItem.getMatnrId());
+ item.put("matnrCode", taskItem.getMatnrCode());
+ item.put("maktx", taskItem.getMaktx());
+ item.put("trackCode", taskItem.getTrackCode());
+ item.put("splrBatch", taskItem.getSplrBatch());
+ item.put("batch", taskItem.getBatch());
+ item.put("spec", taskItem.getSpec());
+ item.put("model", taskItem.getModel());
+ item.put("unit", taskItem.getUnit());
+ item.put("anfme", taskItem.getAnfme());
+ item.put("workQty", taskItem.getWorkQty());
+ item.put("qty", taskItem.getQty());
+ item.put("ableQty", taskItem.getAbleQty());
+ item.put("source", taskItem.getSource());
+ item.put("sourceId", taskItem.getSourceId());
+ item.put("sourceCode", taskItem.getSourceCode());
+ item.put("orderId", taskItem.getOrderId());
+ item.put("orderItemId", taskItem.getOrderItemId());
+ item.put("platItemId", taskItem.getPlatItemId());
+ item.put("platOrderCode", taskItem.getPlatOrderCode());
+ item.put("platWorkCode", taskItem.getPlatWorkCode());
+ item.put("projectCode", taskItem.getProjectCode());
+ item.put("orderType", taskItem.getOrderType());
+ item.put("orderTypeLabel", taskItem.getOrderType$());
+ item.put("wkType", taskItem.getWkType());
+ item.put("wkTypeLabel", taskItem.getWkType$());
+ item.put("isptResult", taskItem.getIsptResult());
+ item.put("isptResultLabel", taskItem.getIsptResult$());
+ item.put("fieldsIndex", taskItem.getFieldsIndex());
+ item.put("extendFields", taskItem.getExtendFields());
+ item.put("status", taskItem.getStatus());
+ item.put("statusLabel", taskItem.getStatus$());
+ item.put("memo", taskItem.getMemo());
+ item.put("createTime", taskItem.getCreateTime$());
+ item.put("updateTime", taskItem.getUpdateTime$());
+ return item;
+ }
+
}
--
Gitblit v1.9.1