zhou zhou
14 小时以前 ffbf67765d2ae447d62333eed85100a15685d781
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package com.vincent.rsf.server.ai.tool;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.vincent.rsf.framework.exception.CoolException;
import com.vincent.rsf.server.manager.entity.BasStation;
import com.vincent.rsf.server.manager.entity.Warehouse;
import com.vincent.rsf.server.manager.service.BasStationService;
import com.vincent.rsf.server.manager.service.WarehouseService;
import com.vincent.rsf.server.system.entity.DictData;
import com.vincent.rsf.server.system.service.DictDataService;
import lombok.RequiredArgsConstructor;
import org.springframework.ai.tool.annotation.Tool;
import org.springframework.ai.tool.annotation.ToolParam;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
 
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
 
@Component
@RequiredArgsConstructor
public class RsfWmsBaseTools {
 
    private final WarehouseService warehouseService;
    private final BasStationService basStationService;
    private final DictDataService dictDataService;
 
    @Tool(name = "rsf_query_warehouses", description = "只读查询工具。按仓库编码或名称查询仓库基础信息。")
    public List<Map<String, Object>> queryWarehouses(
            @ToolParam(description = "仓库编码,可选") String code,
            @ToolParam(description = "仓库名称,可选") String name,
            @ToolParam(description = "返回条数,默认 10,最大 50") Integer limit) {
        String normalizedCode = BuiltinToolGovernanceSupport.sanitizeQueryText(code, "仓库编码", 64);
        String normalizedName = BuiltinToolGovernanceSupport.sanitizeQueryText(name, "仓库名称", 100);
        BuiltinToolGovernanceSupport.requireAnyFilter("仓库查询至少需要提供仓库编码或名称", normalizedCode, normalizedName);
        LambdaQueryWrapper<Warehouse> queryWrapper = new LambdaQueryWrapper<>();
        int finalLimit = BuiltinToolGovernanceSupport.normalizeLimit(limit, 10, 50);
        if (StringUtils.hasText(normalizedCode)) {
            queryWrapper.like(Warehouse::getCode, normalizedCode);
        }
        if (StringUtils.hasText(normalizedName)) {
            queryWrapper.like(Warehouse::getName, normalizedName);
        }
        queryWrapper.orderByAsc(Warehouse::getCode).last("LIMIT " + finalLimit);
        List<Warehouse> warehouses = warehouseService.list(queryWrapper);
        List<Map<String, Object>> result = new ArrayList<>();
        for (Warehouse warehouse : warehouses) {
            Map<String, Object> item = new LinkedHashMap<>();
            item.put("id", warehouse.getId());
            item.put("code", warehouse.getCode());
            item.put("name", warehouse.getName());
            item.put("factory", warehouse.getFactory());
            item.put("address", warehouse.getAddress());
            item.put("status", warehouse.getStatus());
            item.put("statusLabel", warehouse.getStatus$());
            item.put("memo", warehouse.getMemo());
            result.add(item);
        }
        return result;
    }
 
    @Tool(name = "rsf_query_bas_stations", description = "只读查询工具。按站点编号、站点名称或使用状态查询基础站点。")
    public List<Map<String, Object>> queryBasStations(
            @ToolParam(description = "站点名称,可选") String stationName,
            @ToolParam(description = "站点编号,可选") String stationId,
            @ToolParam(description = "使用状态,可选") String useStatus,
            @ToolParam(description = "返回条数,默认 10,最大 50") Integer limit) {
        String normalizedStationName = BuiltinToolGovernanceSupport.sanitizeQueryText(stationName, "站点名称", 100);
        String normalizedStationId = BuiltinToolGovernanceSupport.sanitizeQueryText(stationId, "站点编号", 64);
        String normalizedUseStatus = BuiltinToolGovernanceSupport.sanitizeQueryText(useStatus, "使用状态", 32);
        BuiltinToolGovernanceSupport.requireAnyFilter("基础站点查询至少需要提供站点名称、站点编号或使用状态",
                normalizedStationName, normalizedStationId, normalizedUseStatus);
        LambdaQueryWrapper<BasStation> queryWrapper = new LambdaQueryWrapper<>();
        int finalLimit = BuiltinToolGovernanceSupport.normalizeLimit(limit, 10, 50);
        if (StringUtils.hasText(normalizedStationName)) {
            queryWrapper.like(BasStation::getStationName, normalizedStationName);
        }
        if (StringUtils.hasText(normalizedStationId)) {
            queryWrapper.like(BasStation::getStationId, normalizedStationId);
        }
        if (StringUtils.hasText(normalizedUseStatus)) {
            queryWrapper.eq(BasStation::getUseStatus, normalizedUseStatus);
        }
        queryWrapper.orderByAsc(BasStation::getStationName).last("LIMIT " + finalLimit);
        List<BasStation> stations = basStationService.list(queryWrapper);
        List<Map<String, Object>> result = new ArrayList<>();
        for (BasStation station : stations) {
            Map<String, Object> item = new LinkedHashMap<>();
            item.put("id", station.getId());
            item.put("stationName", station.getStationName());
            item.put("stationId", station.getStationId());
            item.put("type", station.getType());
            item.put("typeLabel", station.getType$());
            item.put("useStatus", station.getUseStatus());
            item.put("useStatusLabel", station.getUseStatus$());
            item.put("area", station.getArea());
            item.put("areaLabel", station.getArea$());
            item.put("isWcs", station.getIsWcs());
            item.put("inAble", station.getInAble());
            item.put("outAble", station.getOutAble());
            item.put("status", station.getStatus());
            result.add(item);
        }
        return result;
    }
 
    @Tool(name = "rsf_query_dict_data", description = "只读查询工具。根据字典类型编码查询字典数据,可按值或标签进一步过滤。")
    public List<Map<String, Object>> queryDictData(
            @ToolParam(required = true, description = "字典类型编码") String dictTypeCode,
            @ToolParam(description = "字典值,可选") String value,
            @ToolParam(description = "字典标签,可选") String label,
            @ToolParam(description = "返回条数,默认 20,最大 100") Integer limit) {
        String normalizedDictTypeCode = BuiltinToolGovernanceSupport.sanitizeQueryText(dictTypeCode, "字典类型编码", 64);
        String normalizedValue = BuiltinToolGovernanceSupport.sanitizeQueryText(value, "字典值", 64);
        String normalizedLabel = BuiltinToolGovernanceSupport.sanitizeQueryText(label, "字典标签", 100);
        if (!StringUtils.hasText(normalizedDictTypeCode)) {
            throw new CoolException("字典类型编码不能为空");
        }
        int finalLimit = BuiltinToolGovernanceSupport.normalizeLimit(limit, 20, 100);
        LambdaQueryWrapper<DictData> queryWrapper = new LambdaQueryWrapper<DictData>()
                .eq(DictData::getDictTypeCode, normalizedDictTypeCode);
        if (StringUtils.hasText(normalizedValue)) {
            queryWrapper.like(DictData::getValue, normalizedValue);
        }
        if (StringUtils.hasText(normalizedLabel)) {
            queryWrapper.like(DictData::getLabel, normalizedLabel);
        }
        queryWrapper.orderByAsc(DictData::getSort).last("LIMIT " + finalLimit);
        List<DictData> dictDataList = dictDataService.list(queryWrapper);
        List<Map<String, Object>> result = new ArrayList<>();
        for (DictData dictData : dictDataList) {
            Map<String, Object> item = new LinkedHashMap<>();
            item.put("id", dictData.getId());
            item.put("dictTypeCode", dictData.getDictTypeCode());
            item.put("value", dictData.getValue());
            item.put("label", dictData.getLabel());
            item.put("sort", dictData.getSort());
            item.put("color", dictData.getColor());
            item.put("group", dictData.getGroup());
            item.put("status", dictData.getStatus());
            item.put("statusLabel", dictData.getStatus$());
            result.add(item);
        }
        return result;
    }
 
}