zhou zhou
17 小时以前 1d0ab9996661fdc66037870d4b98037f2dfa079a
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
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) {
        LambdaQueryWrapper<Warehouse> queryWrapper = new LambdaQueryWrapper<>();
        int finalLimit = normalizeLimit(limit, 10, 50);
        if (StringUtils.hasText(code)) {
            queryWrapper.like(Warehouse::getCode, code);
        }
        if (StringUtils.hasText(name)) {
            queryWrapper.like(Warehouse::getName, name);
        }
        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) {
        LambdaQueryWrapper<BasStation> queryWrapper = new LambdaQueryWrapper<>();
        int finalLimit = normalizeLimit(limit, 10, 50);
        if (StringUtils.hasText(stationName)) {
            queryWrapper.like(BasStation::getStationName, stationName);
        }
        if (StringUtils.hasText(stationId)) {
            queryWrapper.like(BasStation::getStationId, stationId);
        }
        if (StringUtils.hasText(useStatus)) {
            queryWrapper.eq(BasStation::getUseStatus, useStatus);
        }
        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) {
        if (!StringUtils.hasText(dictTypeCode)) {
            throw new CoolException("字典类型编码不能为空");
        }
        int finalLimit = normalizeLimit(limit, 20, 100);
        LambdaQueryWrapper<DictData> queryWrapper = new LambdaQueryWrapper<DictData>()
                .eq(DictData::getDictTypeCode, dictTypeCode);
        if (StringUtils.hasText(value)) {
            queryWrapper.like(DictData::getValue, value);
        }
        if (StringUtils.hasText(label)) {
            queryWrapper.like(DictData::getLabel, label);
        }
        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;
    }
 
    private int normalizeLimit(Integer limit, int defaultValue, int maxValue) {
        if (limit == null) {
            return defaultValue;
        }
        if (limit < 1 || limit > maxValue) {
            throw new CoolException("limit 必须在 1 到 " + maxValue + " 之间");
        }
        return limit;
    }
}