feat: add warehouse stock query MCP tool
This commit is contained in:
committed by
my_local_kimi_dev2
parent
798075f47e
commit
487a41df74
@@ -0,0 +1,46 @@
|
||||
package me.iiv.agent.entity.m2;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.IdType;
|
||||
import com.baomidou.mybatisplus.annotation.TableId;
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import lombok.ToString;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.Date;
|
||||
|
||||
/** m2 库存管理表。 */
|
||||
@Getter
|
||||
@Setter
|
||||
@ToString
|
||||
@TableName("warehouse_stock_manage")
|
||||
public class WarehouseStockManage implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
@TableId(value = "id", type = IdType.AUTO)
|
||||
private Long id;
|
||||
private Long createId;
|
||||
private Date createTime;
|
||||
private Long modifyId;
|
||||
private Date modifyTime;
|
||||
private Integer softDelete;
|
||||
private String warehouseName;
|
||||
private Long warehouseId;
|
||||
private String warehouseNo;
|
||||
private Integer currentInventory;
|
||||
private Integer batteryInventory;
|
||||
private Long agentId;
|
||||
private String agentName;
|
||||
private Long materielDefinitionId;
|
||||
private String materialNo;
|
||||
private String materielFullName;
|
||||
private String materielSort;
|
||||
private Long materielIndexId;
|
||||
private String specifications;
|
||||
private Integer outOfStock;
|
||||
private String snCode;
|
||||
private Long warehouseOwnerId;
|
||||
private String warehouseOwnerName;
|
||||
}
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
package me.iiv.agent.mapper.m2;
|
||||
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import me.iiv.agent.entity.m2.WarehouseStockManage;
|
||||
|
||||
/** 库存管理 Mapper。 */
|
||||
public interface WarehouseStockManageMapper extends BaseMapper<WarehouseStockManage> {
|
||||
}
|
||||
@@ -0,0 +1,126 @@
|
||||
package me.iiv.agent.mcp.tool;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import me.iiv.agent.entity.m0.User;
|
||||
import me.iiv.agent.entity.m2.WarehouseStockManage;
|
||||
import me.iiv.agent.mapper.m2.WarehouseStockManageMapper;
|
||||
import me.iiv.agent.mcp.ToolObject;
|
||||
import me.iiv.agent.mcp.XqTool;
|
||||
import me.iiv.agent.permission.PermissionService;
|
||||
import me.iiv.agent.security.AuthContextHolder;
|
||||
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.time.ZoneId;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/** 库存物品查询工具,对齐老系统 /warehouseStatis/listByPage。 */
|
||||
@Component
|
||||
public class WarehouseStockTools implements ToolObject {
|
||||
|
||||
private static final int DEFAULT_ROWS = 20;
|
||||
private static final int MAX_ROWS = 100;
|
||||
private static final DateTimeFormatter TIME_FORMATTER =
|
||||
DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").withZone(ZoneId.of("Asia/Shanghai"));
|
||||
|
||||
final WarehouseStockManageMapper warehouseStockManageMapper;
|
||||
final PermissionService permissionService;
|
||||
|
||||
public WarehouseStockTools(WarehouseStockManageMapper warehouseStockManageMapper,
|
||||
PermissionService permissionService) {
|
||||
this.warehouseStockManageMapper = warehouseStockManageMapper;
|
||||
this.permissionService = permissionService;
|
||||
}
|
||||
|
||||
@XqTool(category = "warehouse", kind = "read")
|
||||
@Tool(name = "warehouse.stock.list.query", description = "分页查询库存物品,对应老系统 /warehouseStatis/listByPage。"
|
||||
+ "总部可看全部;代理仅可看本公司或 warehouse_agent_auth 授权代管公司作为管理公司/所属公司的库存。"
|
||||
+ "支持仓库、所属公司、物品名称/编号/分类/规格筛选;page 默认1,rows 默认20、上限100。"
|
||||
+ "固定按 current_inventory DESC、id DESC 排序。")
|
||||
public Map<String, Object> warehouseStockListQuery(
|
||||
@ToolParam(required = false, description = "仓库 id 集合") List<Long> warehouseIds,
|
||||
@ToolParam(required = false, description = "仓库所属公司 id") Long warehouseOwnerId,
|
||||
@ToolParam(required = false, description = "物品名称,模糊匹配") String materialName,
|
||||
@ToolParam(required = false, description = "物品编号,模糊匹配") String materialNo,
|
||||
@ToolParam(required = false, description = "物品分类 id 集合") List<Long> materialCategoryIds,
|
||||
@ToolParam(required = false, description = "规格型号,模糊匹配") String specifications,
|
||||
@ToolParam(required = false, description = "页码,从1开始,默认1") Integer page,
|
||||
@ToolParam(required = false, description = "每页条数,默认20、上限100") Integer rows) {
|
||||
int pageNo = page == null || page < 1 ? 1 : page;
|
||||
boolean truncated = rows != null && rows > MAX_ROWS;
|
||||
int pageSize = rows == null || rows < 1 ? DEFAULT_ROWS : Math.min(rows, MAX_ROWS);
|
||||
|
||||
LambdaQueryWrapper<WarehouseStockManage> wrapper = new LambdaQueryWrapper<WarehouseStockManage>()
|
||||
.eq(WarehouseStockManage::getSoftDelete, 0)
|
||||
.in(warehouseIds != null && !warehouseIds.isEmpty(), WarehouseStockManage::getWarehouseId, warehouseIds)
|
||||
.eq(warehouseOwnerId != null, WarehouseStockManage::getWarehouseOwnerId, warehouseOwnerId)
|
||||
.in(materialCategoryIds != null && !materialCategoryIds.isEmpty(),
|
||||
WarehouseStockManage::getMaterielIndexId, materialCategoryIds);
|
||||
if (StringUtils.hasText(materialName)) {
|
||||
wrapper.like(WarehouseStockManage::getMaterielFullName, materialName.trim());
|
||||
}
|
||||
if (StringUtils.hasText(materialNo)) {
|
||||
wrapper.like(WarehouseStockManage::getMaterialNo, materialNo.trim());
|
||||
}
|
||||
if (StringUtils.hasText(specifications)) {
|
||||
wrapper.like(WarehouseStockManage::getSpecifications, specifications.trim());
|
||||
}
|
||||
|
||||
User user = AuthContextHolder.get().getUser();
|
||||
permissionService.applyWarehouseStockRowFilter(wrapper, user);
|
||||
Long total = warehouseStockManageMapper.selectCount(wrapper);
|
||||
wrapper.orderByDesc(WarehouseStockManage::getCurrentInventory)
|
||||
.orderByDesc(WarehouseStockManage::getId)
|
||||
.last("LIMIT " + (long) (pageNo - 1) * pageSize + "," + pageSize);
|
||||
List<Map<String, Object>> records = warehouseStockManageMapper.selectList(wrapper).stream()
|
||||
.map(this::record).collect(Collectors.toList());
|
||||
|
||||
Map<String, Object> result = new LinkedHashMap<>();
|
||||
result.put("total", total);
|
||||
result.put("page", pageNo);
|
||||
result.put("rows", pageSize);
|
||||
if (truncated) {
|
||||
result.put("notice", "rows 超过上限 100,已截断为 100");
|
||||
}
|
||||
result.put("records", records);
|
||||
return result;
|
||||
}
|
||||
|
||||
private Map<String, Object> record(WarehouseStockManage stock) {
|
||||
Map<String, Object> row = new LinkedHashMap<>();
|
||||
row.put("id", stock.getId());
|
||||
row.put("warehouseId", stock.getWarehouseId());
|
||||
row.put("warehouseNo", stock.getWarehouseNo());
|
||||
row.put("warehouseName", stock.getWarehouseName());
|
||||
row.put("agentId", stock.getAgentId());
|
||||
row.put("agentName", stock.getAgentName());
|
||||
row.put("warehouseOwnerId", stock.getWarehouseOwnerId());
|
||||
row.put("warehouseOwnerName", stock.getWarehouseOwnerName());
|
||||
row.put("materialDefinitionId", stock.getMaterielDefinitionId());
|
||||
row.put("materialNo", stock.getMaterialNo());
|
||||
row.put("materialName", stock.getMaterielFullName());
|
||||
row.put("materialCategoryId", stock.getMaterielIndexId());
|
||||
row.put("materialCategory", stock.getMaterielSort());
|
||||
row.put("specifications", stock.getSpecifications());
|
||||
row.put("snCode", stock.getSnCode());
|
||||
int currentInventory = zeroIfNull(stock.getCurrentInventory());
|
||||
int outOfStock = zeroIfNull(stock.getOutOfStock());
|
||||
row.put("currentInventory", currentInventory);
|
||||
row.put("outOfStock", outOfStock);
|
||||
row.put("availableInventory", currentInventory - outOfStock);
|
||||
row.put("batteryInventory", zeroIfNull(stock.getBatteryInventory()));
|
||||
row.put("updatedAt", stock.getModifyTime() == null ? null
|
||||
: TIME_FORMATTER.format(stock.getModifyTime().toInstant()));
|
||||
return row;
|
||||
}
|
||||
|
||||
private static int zeroIfNull(Integer value) {
|
||||
return value == null ? 0 : value;
|
||||
}
|
||||
}
|
||||
@@ -13,6 +13,7 @@ import me.iiv.agent.entity.m2.ContractNew;
|
||||
import me.iiv.agent.entity.m2.WarehouseAgentAuth;
|
||||
import me.iiv.agent.entity.m2.WarehouseIn;
|
||||
import me.iiv.agent.entity.m2.WarehouseOut;
|
||||
import me.iiv.agent.entity.m2.WarehouseStockManage;
|
||||
import me.iiv.agent.mapper.m0.AgentManageDistrictAuthMapper;
|
||||
import me.iiv.agent.mapper.m0.AgentMapper;
|
||||
import me.iiv.agent.mapper.m0.AuthorityMapper;
|
||||
@@ -197,6 +198,26 @@ public class PermissionService {
|
||||
.collect(Collectors.joining(","));
|
||||
}
|
||||
|
||||
/**
|
||||
* 库存查询专用权限,1:1 对齐老系统 WrapperUtils.getWarehouseAuth:总部不加过滤;
|
||||
* 代理可看本公司及 warehouse_agent_auth 授权代管公司作为管理公司或所属公司的库存。
|
||||
*/
|
||||
public void applyWarehouseStockRowFilter(LambdaQueryWrapper<WarehouseStockManage> wrapper, User user) {
|
||||
if (!isAgentAuthority(user.getAuthorityId())) {
|
||||
return;
|
||||
}
|
||||
if (user.getAgentId() == null) {
|
||||
throw new IllegalStateException("代理商用户未关联代理商,拒绝访问业务数据");
|
||||
}
|
||||
Set<Long> ids = new LinkedHashSet<>();
|
||||
ids.add(user.getAgentId());
|
||||
ids.addAll(managedAuths(user.getId()).stream()
|
||||
.map(WarehouseAgentAuth::getManagerAgentId)
|
||||
.collect(Collectors.toList()));
|
||||
wrapper.and(w -> w.in(WarehouseStockManage::getAgentId, ids)
|
||||
.or().in(WarehouseStockManage::getWarehouseOwnerId, ids));
|
||||
}
|
||||
|
||||
/** 直属下级代理 id + 自己 agentId(老 :682-695,supervisor_id=自己,只取一层) */
|
||||
private List<Long> subordinateAndSelfIds(User user) {
|
||||
List<Long> ids = new ArrayList<>();
|
||||
|
||||
Reference in New Issue
Block a user