feat(mcp): 新增电池检查/故障汇总/现场维修记录三工具

- battery.inspection.list.query(m2 battery_inspection,老 /batteryInspection/pageList)
- repair.recording.list.query(m0 equipment_repair_recording,老 /equipmentRepair/recordingByPage)
- repair.handle.list.query(m0 equipment_repair_handle,老 repairRecordHandleList)
- 行权限复用 resolveRowScope 含直属下级口径(方案 B 拍板),battery 落 apply_agent_id,
  repair 三字段 OR(apply_agent_id/property_id 代管/agent_id)
- 修复点:富化与字典按页批量(修 N+1 与 accessory NPE)、chipCode 落 chip_code(修拼不存在列)、
  固定 id DESC 砍掉 sidx/sord 动态排序注入面、传 agentId 仍叠加行级 ACL
- REQ-20261106-0020,515 项测试全绿
This commit is contained in:
my_local_kimi_dev2
2026-11-07 09:08:39 +08:00
parent 2024e86573
commit 1a7a761a66
20 changed files with 2208 additions and 1 deletions
@@ -0,0 +1,45 @@
package me.iiv.agent.entity.m0;
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;
/** 备件(m0.accessory),逐字段复制自老系统 beans/b0/Accessory(老实体未标 @TableName,按约定落 accessory 表)。 */
@Getter
@Setter
@ToString
@TableName("accessory")
public class Accessory 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 stockNo;
/** 生成编号 */
private String productNo;
/** 显示状态 */
private Integer showStatus;
/** 物料id */
private Long materielId;
/** 设计寿命 */
private Integer lifeCount;
/** 备件名称 */
private String name;
/** 是否有编码 */
private Integer codeStatus;
/** 排序 */
private Integer orderBy;
}
@@ -0,0 +1,75 @@
package me.iiv.agent.entity.m0;
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;
/** 设备处理(现场维修记录,m0.equipment_repair_handle),逐字段复制自老系统 beans/b0/EquipmentRepairHandle。 */
@Getter
@Setter
@ToString
@TableName("equipment_repair_handle")
public class EquipmentRepairHandle 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;
/** 维修任务id(关联 equipment_repair_recording.id) */
private Long equipmentRepairId;
/** 维修人 */
private Long userId;
/** 添加时间 */
private Date addTime;
/** 记录描述 */
private String memo;
/** 维修状态 0:待解决;1:已解决;2:延迟解决 */
private Integer state;
/** 记录编号(列名拼写即 recordr_no,勿改) */
private String recordrNo;
/** 有效状态(老接口恒过滤 effective_state=1) */
private Integer effectiveState;
/** 设备所属代理ID */
private Long agentId;
private Long propertyId;
private Long districtId;
private Long buildingId;
private String floor;
private Long toiletId;
private Long positionId;
private String installAddress;
private String snCode;
/** 0:厕纸机;1:擦手纸机 */
private Integer deviceType;
/** 组件编号(fault_info.assembly_no,type=0 组件) */
private String assembly;
/** 零部件编号(fault_info.assembly_no,type=1 零部件) */
private String assemblyFault;
/** 备件ID */
private Long accessoryId;
/** 备件芯片编号 */
private String chipCode;
/** 旧芯片编号 */
private String oldChipCode;
/** 工单编号 */
private String workNo;
private Long maintenanceAgentId;
private String maintenanceAgentName;
private Long maintenancePropertyId;
private String maintenancePropertyName;
/** 记录人代理ID */
private Long applyAgentId;
/** 记录人代理 */
private String applyAgentName;
}
@@ -0,0 +1,74 @@
package me.iiv.agent.entity.m0;
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;
/** 设备维修(故障汇总,m0.equipment_repair_recording),逐字段复制自老系统 beans/b0/EquipmentRepairRecording。 */
@Getter
@Setter
@ToString
@TableName("equipment_repair_recording")
public class EquipmentRepairRecording 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;
/** 设备所属代理ID */
private Long agentId;
private Long propertyId;
private Long districtId;
private Long buildingId;
private String floor;
private Long toiletId;
private Long positionId;
private String installAddress;
/** 申报人 */
private Long userId;
/** 最近修改时间 */
private Date recordTime;
private String snCode;
/** 问题描述 */
private String problem;
/** 维修状态 0:待解决;1:已解决;2:延迟解决 */
private Integer state;
/** 维修编号 */
private String repairNo;
/** 有效状态(老接口恒过滤 effective_state=1) */
private Integer effectiveState;
/** 0:厕纸机;1:擦手纸机 */
private Integer deviceType;
/** 出纸次数 */
private Long paperCount;
/** 故障点计数 */
private Integer faultCount;
/** 来源类型 0:运维换机;1:客户换机;2:维护维修 */
private Integer sourceType;
/** 工单编号 */
private String workNo;
private Long maintenanceAgentId;
private String maintenanceAgentName;
private Long maintenancePropertyId;
private String maintenancePropertyName;
/** 维修人 */
private Long handUserId;
/** 维修人名称 */
private String handUserName;
/** 记录人代理ID */
private Long applyAgentId;
/** 记录人代理 */
private String applyAgentName;
}
@@ -0,0 +1,43 @@
package me.iiv.agent.entity.m0;
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;
/** 故障点字典(m0.fault_info),逐字段复制自老系统 beans/b0/FaultInfo。 */
@Getter
@Setter
@ToString
@TableName("fault_info")
public class FaultInfo 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;
/** 数据状态,0 正常,99 删除 */
private Integer softDelete;
/** 组件编号 */
private String assemblyNo;
/** 组件名称 */
private String assemblyName;
/** 排序 */
private Integer orderBy;
/** 上级编号 */
private String parentNo;
/** 故障类型 0:组件;1:零部件 */
private Integer type;
private String memo;
/** 当前零部件编号 */
private String currendNo;
}
@@ -0,0 +1,83 @@
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.time.LocalDate;
import java.util.Date;
/** 电池检测(m2.battery_inspection),逐字段复制自老系统 publicbeanandmap beans/b2/BatteryInspection。 */
@Getter
@Setter
@ToString
@TableName("battery_inspection")
public class BatteryInspection 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;
/** 无法充电(0:-;1:√) */
private Integer checkRecord1;
/** 电量异常(0:-;1:√) */
private Integer checkRecord2;
/** 电池内阻 */
private String checkRecord3;
/** 测量电压 */
private String checkRecord4;
/** 温度异常(0:-;1:√) */
private Integer checkRecord5;
/** 气味异常(0:-;1:√) */
private Integer checkRecord6;
/** 异物异响(0:-;1:√) */
private Integer checkRecord7;
/** 明显损伤(0:-;1:√) */
private Integer checkRecord8;
/** 鼓包变形(0:-;1:√) */
private Integer checkRecord9;
/** 明显变色(0:-;1:√) */
private Integer checkRecord10;
/** 氧化腐蚀(0:-;1:√) */
private Integer checkRecord11;
/** 异物粘连(0:-;1:√) */
private Integer checkRecord12;
/** 测量内阻是否处理触点(0:-;1:√) */
private Integer checkRecord13;
/** 是否返回(0:-;1:√) */
private Integer checkRecord14;
/** 电池编号 */
private String snCode;
/** 申请代理(行内冗余名称落库,老接口无富化) */
private Long applyAgentId;
private String applyAgentName;
private Long agentId;
private Long propertyId;
private Long districtId;
/** 公司名称(行内冗余) */
private String agentName;
/** 物业名称(行内冗余) */
private String propertyName;
/** 项目名称(行内冗余) */
private String districtName;
/** 生产日期 */
private LocalDate productionDate;
/** 检查时间 */
private Date checkTime;
private String memo;
/** 检查人ID */
private Long checkUserId;
/** 检查人 */
private String checkUserName;
/** 保养类型(0:小保养;1:大保养) */
private Integer checkType;
}
@@ -0,0 +1,7 @@
package me.iiv.agent.mapper.m0;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import me.iiv.agent.entity.m0.Accessory;
public interface AccessoryMapper extends BaseMapper<Accessory> {
}
@@ -0,0 +1,7 @@
package me.iiv.agent.mapper.m0;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import me.iiv.agent.entity.m0.EquipmentRepairHandle;
public interface EquipmentRepairHandleMapper extends BaseMapper<EquipmentRepairHandle> {
}
@@ -0,0 +1,7 @@
package me.iiv.agent.mapper.m0;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import me.iiv.agent.entity.m0.EquipmentRepairRecording;
public interface EquipmentRepairRecordingMapper extends BaseMapper<EquipmentRepairRecording> {
}
@@ -0,0 +1,7 @@
package me.iiv.agent.mapper.m0;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import me.iiv.agent.entity.m0.FaultInfo;
public interface FaultInfoMapper extends BaseMapper<FaultInfo> {
}
@@ -0,0 +1,7 @@
package me.iiv.agent.mapper.m2;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import me.iiv.agent.entity.m2.BatteryInspection;
public interface BatteryInspectionMapper extends BaseMapper<BatteryInspection> {
}
@@ -0,0 +1,251 @@
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.BatteryInspection;
import me.iiv.agent.mapper.m2.BatteryInspectionMapper;
import me.iiv.agent.mcp.McpErrorCode;
import me.iiv.agent.mcp.ToolCallException;
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.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.time.format.ResolverStyle;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
/**
* 电池检查记录查询工具,仿老系统 BatteryInspectionController /pageList
* (xiaoqu-franchisee controller/batteryInspection/BatteryInspectionController.java:62)。
* 名称(agent/property/district/applyAgent)行内冗余落库,无富化;
* 固定 id DESC(不搬老 sidx/sord 动态排序);checkTime 区间按老接口拼整天 00:00:00/23:59:59。
*/
@Component
public class BatteryInspectionTools implements ToolObject {
private static final int DEFAULT_ROWS = 20;
private static final int MAX_ROWS = 100;
private static final DateTimeFormatter DATE_FORMAT =
DateTimeFormatter.ofPattern("uuuu-MM-dd").withResolverStyle(ResolverStyle.STRICT);
/** checkRecord1-14 检查项文案(老 BatteryInspection 实体注释口径) */
private static final String[] CHECK_RECORD_LABELS = {
"无法充电", "电量异常", "电池内阻", "测量电压", "温度异常", "气味异常", "异物异响",
"明显损伤", "鼓包变形", "明显变色", "氧化腐蚀", "异物粘连", "测量内阻是否处理触点", "是否返回"};
final BatteryInspectionMapper mapper;
final PermissionService permissionService;
public BatteryInspectionTools(BatteryInspectionMapper mapper, PermissionService permissionService) {
this.mapper = mapper;
this.permissionService = permissionService;
}
/**
* 分页查询电池检查记录。硬过滤 soft_delete=0,固定 id DESC,
* selectCount + LIMIT 物理分页(项目未注册 MP 分页拦截器,禁用 selectPage)。
*/
@XqTool(category = "battery", kind = "read")
@Tool(name = "battery.inspection.list.query", description = "电池检查记录分页查询,对应老系统 /batteryInspection/pageList。"
+ "硬过滤 soft_delete=0,固定按 id 倒序。支持 snCode(模糊)、agentId/propertyId/districtId/"
+ "checkUserId(精确)、checkUserName(模糊)、checkType(0=小保养 1=大保养)、"
+ "checkRecord14(是否返回:0=否 1=是)筛选;productionDateStart/End 按生产日期过滤(纯日期);"
+ "checkTimeStart/End 按检查时间过滤,纯日期自动扩展为当天 00:00:00 至 23:59:59。"
+ "传了 agentId 也仍叠加当前身份的行级数据权限,无权数据返回空。"
+ "page 默认 1,rows 默认 20、上限 100(超出截断为 100 并在 notice 提示)。"
+ "每行返回全字段:checkRecord1-14 为检查项(0=未勾选 1=勾选,3=电池内阻与 4=测量电压为原始读数),"
+ "依次为 1无法充电 2电量异常 3电池内阻 4测量电压 5温度异常 6气味异常 7异物异响 8明显损伤 "
+ "9鼓包变形 10明显变色 11氧化腐蚀 12异物粘连 13测量内阻是否处理触点 14是否返回;"
+ "agent/property/district/applyAgent 名称为行内冗余字段。")
public Map<String, Object> batteryInspectionListQuery(
@ToolParam(required = false, description = "电池编号,模糊匹配") String snCode,
@ToolParam(required = false, description = "代理 id,正整数,精确匹配") Long agentId,
@ToolParam(required = false, description = "物业 id,正整数,精确匹配") Long propertyId,
@ToolParam(required = false, description = "站点 id,正整数,精确匹配") Long districtId,
@ToolParam(required = false, description = "保养类型:0=小保养 1=大保养") Integer checkType,
@ToolParam(required = false, description = "检查人 id,正整数,精确匹配") Long checkUserId,
@ToolParam(required = false, description = "检查人姓名,模糊匹配") String checkUserName,
@ToolParam(required = false, description = "生产日期起始,yyyy-MM-dd,含当天") String productionDateStart,
@ToolParam(required = false, description = "生产日期结束,yyyy-MM-dd,含当天") String productionDateEnd,
@ToolParam(required = false, description = "检查时间起始,yyyy-MM-dd,自动扩展为当天 00:00:00") String checkTimeStart,
@ToolParam(required = false, description = "检查时间结束,yyyy-MM-dd,自动扩展为当天 23:59:59") String checkTimeEnd,
@ToolParam(required = false, description = "是否返回:0=否 1=是") Integer checkRecord14,
@ToolParam(required = false, description = "页码,从 1 开始,默认 1") Integer page,
@ToolParam(required = false, description = "每页条数,默认 20,上限 100(超出截断为 100)") Integer rows) {
validatePositive(agentId, "agentId");
validatePositive(propertyId, "propertyId");
validatePositive(districtId, "districtId");
validatePositive(checkUserId, "checkUserId");
validateEnum(checkType, "checkType", 0, 1);
validateEnum(checkRecord14, "checkRecord14", 0, 1);
LocalDate productionStart = parseDate(productionDateStart, "productionDateStart");
LocalDate productionEnd = parseDate(productionDateEnd, "productionDateEnd");
if (productionStart != null && productionEnd != null && productionStart.isAfter(productionEnd)) {
throw invalid("productionDateStart 不能晚于 productionDateEnd");
}
LocalDate checkStart = parseDate(checkTimeStart, "checkTimeStart");
LocalDate checkEnd = parseDate(checkTimeEnd, "checkTimeEnd");
if (checkStart != null && checkEnd != null && checkStart.isAfter(checkEnd)) {
throw invalid("checkTimeStart 不能晚于 checkTimeEnd");
}
User user = AuthContextHolder.get().getUser();
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<BatteryInspection> wrapper = new LambdaQueryWrapper<BatteryInspection>()
.eq(BatteryInspection::getSoftDelete, 0)
.eq(agentId != null, BatteryInspection::getAgentId, agentId)
.eq(propertyId != null, BatteryInspection::getPropertyId, propertyId)
.eq(districtId != null, BatteryInspection::getDistrictId, districtId)
.eq(checkType != null, BatteryInspection::getCheckType, checkType)
.eq(checkUserId != null, BatteryInspection::getCheckUserId, checkUserId)
.eq(checkRecord14 != null, BatteryInspection::getCheckRecord14, checkRecord14);
if (StringUtils.hasText(snCode)) {
wrapper.like(BatteryInspection::getSnCode, snCode.trim());
}
if (StringUtils.hasText(checkUserName)) {
wrapper.like(BatteryInspection::getCheckUserName, checkUserName.trim());
}
if (productionStart != null) {
wrapper.ge(BatteryInspection::getProductionDate, productionStart);
}
if (productionEnd != null) {
wrapper.le(BatteryInspection::getProductionDate, productionEnd);
}
// 老接口语义:检查时间纯日期拼整天(:67-74)
if (checkStart != null) {
wrapper.ge(BatteryInspection::getCheckTime, checkStart.format(DATE_FORMAT) + " 00:00:00");
}
if (checkEnd != null) {
wrapper.le(BatteryInspection::getCheckTime, checkEnd.format(DATE_FORMAT) + " 23:59:59");
}
permissionService.applyBatteryInspectionRowFilter(wrapper, user);
Long total = mapper.selectCount(wrapper);
wrapper.orderByDesc(BatteryInspection::getId)
.last("LIMIT " + (long) (pageNo - 1) * pageSize + "," + pageSize);
List<Map<String, Object>> records = mapper.selectList(wrapper).stream()
.map(this::toRecord).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> toRecord(BatteryInspection bean) {
Map<String, Object> record = new LinkedHashMap<>();
record.put("id", bean.getId());
record.put("snCode", bean.getSnCode());
record.put("checkType", bean.getCheckType());
record.put("checkTypeText", checkTypeText(bean.getCheckType()));
putCheckRecord(record, 1, bean.getCheckRecord1());
putCheckRecord(record, 2, bean.getCheckRecord2());
record.put("checkRecord3", bean.getCheckRecord3());
record.put("checkRecord4", bean.getCheckRecord4());
putCheckRecord(record, 5, bean.getCheckRecord5());
putCheckRecord(record, 6, bean.getCheckRecord6());
putCheckRecord(record, 7, bean.getCheckRecord7());
putCheckRecord(record, 8, bean.getCheckRecord8());
putCheckRecord(record, 9, bean.getCheckRecord9());
putCheckRecord(record, 10, bean.getCheckRecord10());
putCheckRecord(record, 11, bean.getCheckRecord11());
putCheckRecord(record, 12, bean.getCheckRecord12());
putCheckRecord(record, 13, bean.getCheckRecord13());
putCheckRecord(record, 14, bean.getCheckRecord14());
record.put("productionDate", bean.getProductionDate() == null
? null : bean.getProductionDate().format(DATE_FORMAT));
record.put("checkTime", format(bean.getCheckTime()));
record.put("checkUserId", bean.getCheckUserId());
record.put("checkUserName", bean.getCheckUserName());
record.put("applyAgentId", bean.getApplyAgentId());
record.put("applyAgentName", bean.getApplyAgentName());
record.put("agentId", bean.getAgentId());
record.put("agentName", bean.getAgentName());
record.put("propertyId", bean.getPropertyId());
record.put("propertyName", bean.getPropertyName());
record.put("districtId", bean.getDistrictId());
record.put("districtName", bean.getDistrictName());
record.put("memo", bean.getMemo());
record.put("createTime", format(bean.getCreateTime()));
return record;
}
/** 0/1 检查项:勾选(1)时透出该检查项文案,未勾选/null 时为 null */
private static void putCheckRecord(Map<String, Object> record, int index, Integer value) {
record.put("checkRecord" + index, value);
record.put("checkRecord" + index + "Text",
value != null && value == 1 ? CHECK_RECORD_LABELS[index - 1] : null);
}
private static String checkTypeText(Integer value) {
if (value == null) {
return null;
}
return switch (value) {
case 0 -> "小保养";
case 1 -> "大保养";
default -> "未知(" + value + ")";
};
}
private static LocalDate parseDate(String value, String name) {
if (!StringUtils.hasText(value)) {
return null;
}
try {
return LocalDate.parse(value.trim(), DATE_FORMAT);
} catch (DateTimeParseException e) {
throw invalid(name + " 格式非法,要求 yyyy-MM-dd: " + value);
}
}
private static String format(Date value) {
return value == null ? null : new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(value);
}
private static void validatePositive(Long value, String name) {
if (value != null && value < 1) {
throw invalid(name + " 必须为正整数");
}
}
private static void validateEnum(Integer value, String name, int... allowed) {
if (value == null) {
return;
}
for (int candidate : allowed) {
if (value == candidate) {
return;
}
}
String options = Arrays.stream(allowed).mapToObj(String::valueOf)
.collect(Collectors.joining("、"));
throw invalid(name + " 仅支持 " + options);
}
/** 入参类错误透出具体文案给调用方,便于 LLM 自愈 */
private static ToolCallException invalid(String message) {
return new ToolCallException(McpErrorCode.INVALID_ARGUMENT, message);
}
}
@@ -0,0 +1,542 @@
package me.iiv.agent.mcp.tool;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import me.iiv.agent.entity.m0.Accessory;
import me.iiv.agent.entity.m0.EquipmentRepairHandle;
import me.iiv.agent.entity.m0.EquipmentRepairRecording;
import me.iiv.agent.entity.m0.FaultInfo;
import me.iiv.agent.entity.m0.User;
import me.iiv.agent.mapper.m0.AccessoryMapper;
import me.iiv.agent.mapper.m0.EquipmentRepairHandleMapper;
import me.iiv.agent.mapper.m0.EquipmentRepairRecordingMapper;
import me.iiv.agent.mapper.m0.FaultInfoMapper;
import me.iiv.agent.mapper.m0.UserMapper;
import me.iiv.agent.mcp.McpErrorCode;
import me.iiv.agent.mcp.ToolCallException;
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.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.time.format.ResolverStyle;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.function.Function;
import java.util.stream.Collectors;
/**
* 故障汇总 / 现场维修记录查询工具,仿老系统 EquipmentRepairController
* /recordingByPage(:117)与 /repairRecordHandleList(:183)。
* 修正老接口坑(设计文档 doc/battery-inspection-repair-mcp-design.md §4):
* 富化改按页批量(Redis multiGet + 按页 IN,老接口逐条 N+1 且 accessory byId 无 null 判断有 NPE 风险);
* handle 的 chip 参数落不存在的 chip 列 → 参数名 chipCode、落 chip_code 列;
* 不搬 webId/sidx/sord;图片不内嵌(老系统也是独立接口)。
*/
@Component
public class EquipmentRepairTools implements ToolObject {
private static final int DEFAULT_ROWS = 20;
private static final int MAX_ROWS = 100;
/** 老接口 Redis 字典 key(Properyt 为老拼写) */
private static final String NAME_AGENT_PREFIX = "name:Agent:";
private static final String NAME_PROPERTY_PREFIX = "name:Properyt:";
private static final String NAME_DISTRICT_PREFIX = "name:District:";
private static final DateTimeFormatter DATE_FORMAT =
DateTimeFormatter.ofPattern("uuuu-MM-dd").withResolverStyle(ResolverStyle.STRICT);
/** fault_info.type:0=组件 1=零部件 */
private static final int FAULT_TYPE_ASSEMBLY = 0;
private static final int FAULT_TYPE_ASSEMBLY_FAULT = 1;
final EquipmentRepairRecordingMapper recordingMapper;
final EquipmentRepairHandleMapper handleMapper;
final FaultInfoMapper faultInfoMapper;
final AccessoryMapper accessoryMapper;
final UserMapper userMapper;
final PermissionService permissionService;
final StringRedisTemplate redisTemplate;
public EquipmentRepairTools(EquipmentRepairRecordingMapper recordingMapper,
EquipmentRepairHandleMapper handleMapper,
FaultInfoMapper faultInfoMapper,
AccessoryMapper accessoryMapper,
UserMapper userMapper,
PermissionService permissionService,
StringRedisTemplate redisTemplate) {
this.recordingMapper = recordingMapper;
this.handleMapper = handleMapper;
this.faultInfoMapper = faultInfoMapper;
this.accessoryMapper = accessoryMapper;
this.userMapper = userMapper;
this.permissionService = permissionService;
this.redisTemplate = redisTemplate;
}
/**
* 故障汇总分页查询。硬过滤 soft_delete=0 + effective_state=1,固定 id DESC,
* selectCount + LIMIT 物理分页。名称/申报人富化按页批量,无 N+1。
*/
@XqTool(category = "repair", kind = "read")
@Tool(name = "repair.recording.list.query", description = "故障汇总(报修/故障记录)分页查询,对应老系统"
+ " /equipmentRepair/recordingByPage。仅返回有效记录(soft_delete=0 且 effective_state=1),"
+ "固定按 id 倒序。支持 id/snCode(模糊)/problem(模糊)/propertyId/districtId/state"
+ "(0=待解决 1=已解决 2=延迟解决)/userId(申报人)/sourceType(0=运维换机 1=客户换机 2=维护维修)"
+ "/deviceType(0=厕纸机 1=擦手纸机)筛选;agentId 为收窄语义"
+ "(申请代理或所属代理命中其一);createTimeStart/End 按记录创建时间过滤(纯日期,含边界日期值本身)。"
+ "传了筛选条件也仍叠加当前身份的行级数据权限,无权数据返回空。"
+ "page 默认 1,rows 默认 20、上限 100(超出截断为 100 并在 notice 提示)。"
+ "每行返回记录全字段 + agentName/propertyName/districtName/userName 富化(Redis/按页批量)"
+ "+ stateText/sourceTypeText/deviceTypeText 中文文案;图片不在列表内(老系统为独立接口)。")
public Map<String, Object> repairRecordingListQuery(
@ToolParam(required = false, description = "故障记录 id,正整数,精确匹配") Long id,
@ToolParam(required = false, description = "设备 SN,模糊匹配") String snCode,
@ToolParam(required = false, description = "代理 id,正整数;收窄为申请代理或所属代理命中其一,"
+ "且仍叠加行级数据权限") Long agentId,
@ToolParam(required = false, description = "物业 id,正整数,精确匹配") Long propertyId,
@ToolParam(required = false, description = "站点 id,正整数,精确匹配") Long districtId,
@ToolParam(required = false, description = "维修状态:0=待解决 1=已解决 2=延迟解决") Integer state,
@ToolParam(required = false, description = "申报人 id,正整数,精确匹配") Long userId,
@ToolParam(required = false, description = "来源类型:0=运维换机 1=客户换机 2=维护维修") Integer sourceType,
@ToolParam(required = false, description = "设备类型:0=厕纸机 1=擦手纸机") Integer deviceType,
@ToolParam(required = false, description = "问题描述,模糊匹配") String problem,
@ToolParam(required = false, description = "创建时间起始,yyyy-MM-dd,含边界值") String createTimeStart,
@ToolParam(required = false, description = "创建时间结束,yyyy-MM-dd,含边界值") String createTimeEnd,
@ToolParam(required = false, description = "页码,从 1 开始,默认 1") Integer page,
@ToolParam(required = false, description = "每页条数,默认 20,上限 100(超出截断为 100)") Integer rows) {
validatePositive(id, "id");
validatePositive(agentId, "agentId");
validatePositive(propertyId, "propertyId");
validatePositive(districtId, "districtId");
validatePositive(userId, "userId");
validateEnum(state, "state", 0, 1, 2);
validateEnum(sourceType, "sourceType", 0, 1, 2);
validateEnum(deviceType, "deviceType", 0, 1);
LocalDate createStart = parseDate(createTimeStart, "createTimeStart");
LocalDate createEnd = parseDate(createTimeEnd, "createTimeEnd");
if (createStart != null && createEnd != null && createStart.isAfter(createEnd)) {
throw invalid("createTimeStart 不能晚于 createTimeEnd");
}
User user = AuthContextHolder.get().getUser();
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<EquipmentRepairRecording> wrapper =
new LambdaQueryWrapper<EquipmentRepairRecording>()
.eq(EquipmentRepairRecording::getSoftDelete, 0)
.eq(EquipmentRepairRecording::getEffectiveState, 1)
.eq(id != null, EquipmentRepairRecording::getId, id)
.eq(propertyId != null, EquipmentRepairRecording::getPropertyId, propertyId)
.eq(districtId != null, EquipmentRepairRecording::getDistrictId, districtId)
.eq(state != null, EquipmentRepairRecording::getState, state)
.eq(userId != null, EquipmentRepairRecording::getUserId, userId)
.eq(sourceType != null, EquipmentRepairRecording::getSourceType, sourceType)
.eq(deviceType != null, EquipmentRepairRecording::getDeviceType, deviceType);
if (StringUtils.hasText(snCode)) {
wrapper.like(EquipmentRepairRecording::getSnCode, snCode.trim());
}
if (StringUtils.hasText(problem)) {
wrapper.like(EquipmentRepairRecording::getProblem, problem.trim());
}
// 老接口 agentId 收窄语义(:126-129):申请代理或所属代理命中其一
if (agentId != null) {
wrapper.and(w -> w.eq(EquipmentRepairRecording::getApplyAgentId, agentId)
.or().eq(EquipmentRepairRecording::getAgentId, agentId));
}
// 老接口语义:createTime 区间不拼时间(beanToWrapper 原样落值)
if (createStart != null) {
wrapper.ge(EquipmentRepairRecording::getCreateTime, createStart.format(DATE_FORMAT));
}
if (createEnd != null) {
wrapper.le(EquipmentRepairRecording::getCreateTime, createEnd.format(DATE_FORMAT));
}
permissionService.applyRepairRecordingRowFilter(wrapper, user);
Long total = recordingMapper.selectCount(wrapper);
wrapper.orderByDesc(EquipmentRepairRecording::getId)
.last("LIMIT " + (long) (pageNo - 1) * pageSize + "," + pageSize);
List<EquipmentRepairRecording> list = recordingMapper.selectList(wrapper);
NameEnrichment enrichment = enrichNames(list, EquipmentRepairRecording::getAgentId,
EquipmentRepairRecording::getPropertyId, EquipmentRepairRecording::getDistrictId,
EquipmentRepairRecording::getUserId);
List<Map<String, Object>> records = new ArrayList<>();
for (EquipmentRepairRecording row : list) {
records.add(toRecordingRecord(row, enrichment));
}
return pageResult(total, pageNo, pageSize, truncated, records);
}
/**
* 现场维修记录分页查询。硬过滤 soft_delete=0 + effective_state=1,固定 id DESC。
* 组件/零部件名按页批量查 fault_info(type 0/1 区分)、备件与维修人按页 IN 批量,无 N+1。
*/
@XqTool(category = "repair", kind = "read")
@Tool(name = "repair.handle.list.query", description = "现场维修记录(维修处理明细)分页查询,对应老系统"
+ " /equipmentRepair/repairRecordHandleList。仅返回有效记录(soft_delete=0 且 effective_state=1),"
+ "固定按 id 倒序。支持 id/equipmentRepairId(故障记录 id)/propertyId/districtId/state"
+ "(0=待解决 1=已解决 2=延迟解决)/userId(维修人)/accessoryId 精确匹配,"
+ "snCode/assembly(组件编号)/assemblyFault(零部件编号)/chipCode(备件芯片编号)模糊匹配;"
+ "agentId 为收窄语义(申请代理或所属代理命中其一),且仍叠加行级数据权限。"
+ "page 默认 1,rows 默认 20、上限 100(超出截断为 100 并在 notice 提示)。"
+ "每行返回记录全字段 + agentName/propertyName/districtName/userName(维修人)富化"
+ "+ assemblyName/assemblyFaultName(fault_info 字典,组件/零部件区分)"
+ "+ accessoryName(备件名,备件不存在时为 null)+ stateText/deviceTypeText 中文文案。")
public Map<String, Object> repairHandleListQuery(
@ToolParam(required = false, description = "维修处理记录 id,正整数,精确匹配") Long id,
@ToolParam(required = false, description = "故障记录 id(equipment_repair_recording.id),"
+ "正整数,精确匹配") Long equipmentRepairId,
@ToolParam(required = false, description = "设备 SN,模糊匹配") String snCode,
@ToolParam(required = false, description = "代理 id,正整数;收窄为申请代理或所属代理命中其一,"
+ "且仍叠加行级数据权限") Long agentId,
@ToolParam(required = false, description = "物业 id,正整数,精确匹配") Long propertyId,
@ToolParam(required = false, description = "站点 id,正整数,精确匹配") Long districtId,
@ToolParam(required = false, description = "维修状态:0=待解决 1=已解决 2=延迟解决") Integer state,
@ToolParam(required = false, description = "维修人 id,正整数,精确匹配") Long userId,
@ToolParam(required = false, description = "组件编号,模糊匹配") String assembly,
@ToolParam(required = false, description = "零部件编号,模糊匹配") String assemblyFault,
@ToolParam(required = false, description = "备件 id,正整数,精确匹配") Long accessoryId,
@ToolParam(required = false, description = "备件芯片编号,模糊匹配(落 chip_code 列)") String chipCode,
@ToolParam(required = false, description = "页码,从 1 开始,默认 1") Integer page,
@ToolParam(required = false, description = "每页条数,默认 20,上限 100(超出截断为 100)") Integer rows) {
validatePositive(id, "id");
validatePositive(equipmentRepairId, "equipmentRepairId");
validatePositive(agentId, "agentId");
validatePositive(propertyId, "propertyId");
validatePositive(districtId, "districtId");
validatePositive(userId, "userId");
validatePositive(accessoryId, "accessoryId");
validateEnum(state, "state", 0, 1, 2);
User user = AuthContextHolder.get().getUser();
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<EquipmentRepairHandle> wrapper = new LambdaQueryWrapper<EquipmentRepairHandle>()
.eq(EquipmentRepairHandle::getSoftDelete, 0)
.eq(EquipmentRepairHandle::getEffectiveState, 1)
.eq(id != null, EquipmentRepairHandle::getId, id)
.eq(equipmentRepairId != null, EquipmentRepairHandle::getEquipmentRepairId, equipmentRepairId)
.eq(propertyId != null, EquipmentRepairHandle::getPropertyId, propertyId)
.eq(districtId != null, EquipmentRepairHandle::getDistrictId, districtId)
.eq(state != null, EquipmentRepairHandle::getState, state)
.eq(userId != null, EquipmentRepairHandle::getUserId, userId)
.eq(accessoryId != null, EquipmentRepairHandle::getAccessoryId, accessoryId);
if (StringUtils.hasText(snCode)) {
wrapper.like(EquipmentRepairHandle::getSnCode, snCode.trim());
}
if (StringUtils.hasText(assembly)) {
wrapper.like(EquipmentRepairHandle::getAssembly, assembly.trim());
}
if (StringUtils.hasText(assemblyFault)) {
wrapper.like(EquipmentRepairHandle::getAssemblyFault, assemblyFault.trim());
}
if (StringUtils.hasText(chipCode)) {
wrapper.like(EquipmentRepairHandle::getChipCode, chipCode.trim());
}
if (agentId != null) {
wrapper.and(w -> w.eq(EquipmentRepairHandle::getApplyAgentId, agentId)
.or().eq(EquipmentRepairHandle::getAgentId, agentId));
}
permissionService.applyRepairHandleRowFilter(wrapper, user);
Long total = handleMapper.selectCount(wrapper);
wrapper.orderByDesc(EquipmentRepairHandle::getId)
.last("LIMIT " + (long) (pageNo - 1) * pageSize + "," + pageSize);
List<EquipmentRepairHandle> list = handleMapper.selectList(wrapper);
NameEnrichment enrichment = enrichNames(list, EquipmentRepairHandle::getAgentId,
EquipmentRepairHandle::getPropertyId, EquipmentRepairHandle::getDistrictId,
EquipmentRepairHandle::getUserId);
FaultNames faultNames = enrichFaultNames(list);
Map<Long, String> accessoryNames = enrichAccessoryNames(list);
List<Map<String, Object>> records = new ArrayList<>();
for (EquipmentRepairHandle row : list) {
records.add(toHandleRecord(row, enrichment, faultNames, accessoryNames));
}
return pageResult(total, pageNo, pageSize, truncated, records);
}
/** 批量富化上下文:Redis 名称 multiGet + 本页 user 批量 IN(修老接口逐条 N+1)。 */
private <T> NameEnrichment enrichNames(List<T> list, Function<T, Long> agentIdGetter,
Function<T, Long> propertyIdGetter,
Function<T, Long> districtIdGetter,
Function<T, Long> userIdGetter) {
NameEnrichment enrichment = new NameEnrichment();
if (list.isEmpty()) {
return enrichment;
}
enrichment.agentNames = redisNames(list, agentIdGetter, NAME_AGENT_PREFIX);
enrichment.propertyNames = redisNames(list, propertyIdGetter, NAME_PROPERTY_PREFIX);
enrichment.districtNames = redisNames(list, districtIdGetter, NAME_DISTRICT_PREFIX);
List<Long> userIds = list.stream().map(userIdGetter)
.filter(Objects::nonNull).distinct().collect(Collectors.toList());
if (!userIds.isEmpty()) {
enrichment.userNames = userMapper.selectBatchIds(userIds).stream()
.collect(Collectors.toMap(User::getId, User::getName, (a, b) -> a));
}
return enrichment;
}
private <T> Map<Long, String> redisNames(List<T> list, Function<T, Long> idGetter, String prefix) {
List<Long> ids = list.stream().map(idGetter)
.filter(Objects::nonNull).distinct().collect(Collectors.toList());
if (ids.isEmpty()) {
return Map.of();
}
List<String> keys = ids.stream().map(id -> prefix + id).collect(Collectors.toList());
List<String> values = redisTemplate.opsForValue().multiGet(keys);
Map<Long, String> names = new LinkedHashMap<>();
for (int i = 0; i < ids.size(); i++) {
names.put(ids.get(i), values == null || i >= values.size() ? null : values.get(i));
}
return names;
}
/**
* 组件/零部件名:按页收集 assembly/assemblyFault 号,带 type 区分批量查 fault_info
* (老接口全表加载字典且不分 type 的修正)。assembly 落 type=0 组件,assemblyFault 落 type=1 零部件。
*/
private FaultNames enrichFaultNames(List<EquipmentRepairHandle> list) {
FaultNames names = new FaultNames();
names.assemblyNames = faultNames(list, EquipmentRepairHandle::getAssembly, FAULT_TYPE_ASSEMBLY);
names.assemblyFaultNames = faultNames(list, EquipmentRepairHandle::getAssemblyFault,
FAULT_TYPE_ASSEMBLY_FAULT);
return names;
}
private Map<String, String> faultNames(List<EquipmentRepairHandle> list,
Function<EquipmentRepairHandle, String> noGetter, int type) {
List<String> nos = list.stream().map(noGetter)
.filter(StringUtils::hasText).map(String::trim).distinct().collect(Collectors.toList());
if (nos.isEmpty()) {
return Map.of();
}
return faultInfoMapper.selectList(new LambdaQueryWrapper<FaultInfo>()
.eq(FaultInfo::getSoftDelete, 0)
.eq(FaultInfo::getType, type)
.in(FaultInfo::getAssemblyNo, nos))
.stream().collect(Collectors.toMap(FaultInfo::getAssemblyNo,
FaultInfo::getAssemblyName, (a, b) -> a));
}
/** 备件名按页 IN 批量查(修老接口 byId 无 null 判断的 NPE 风险);查不到的 id 名称为 null。 */
private Map<Long, String> enrichAccessoryNames(List<EquipmentRepairHandle> list) {
List<Long> ids = list.stream().map(EquipmentRepairHandle::getAccessoryId)
.filter(Objects::nonNull).filter(id -> id > 0).distinct().collect(Collectors.toList());
if (ids.isEmpty()) {
return Map.of();
}
return accessoryMapper.selectBatchIds(ids).stream()
.filter(Objects::nonNull)
.collect(Collectors.toMap(Accessory::getId, Accessory::getName, (a, b) -> a));
}
private Map<String, Object> toRecordingRecord(EquipmentRepairRecording row, NameEnrichment enrichment) {
Map<String, Object> record = new LinkedHashMap<>();
record.put("id", row.getId());
record.put("repairNo", row.getRepairNo());
record.put("snCode", row.getSnCode());
record.put("deviceType", row.getDeviceType());
record.put("deviceTypeText", deviceTypeText(row.getDeviceType()));
record.put("state", row.getState());
record.put("stateText", stateText(row.getState()));
record.put("sourceType", row.getSourceType());
record.put("sourceTypeText", sourceTypeText(row.getSourceType()));
record.put("problem", row.getProblem());
record.put("paperCount", row.getPaperCount());
record.put("faultCount", row.getFaultCount());
record.put("recordTime", format(row.getRecordTime()));
record.put("userId", row.getUserId());
record.put("userName", mapGet(enrichment.userNames, row.getUserId()));
record.put("buildingId", row.getBuildingId());
record.put("floor", row.getFloor());
record.put("toiletId", row.getToiletId());
record.put("positionId", row.getPositionId());
record.put("installAddress", row.getInstallAddress());
record.put("agentId", row.getAgentId());
record.put("agentName", mapGet(enrichment.agentNames, row.getAgentId()));
record.put("applyAgentId", row.getApplyAgentId());
record.put("applyAgentName", row.getApplyAgentName());
record.put("propertyId", row.getPropertyId());
record.put("propertyName", mapGet(enrichment.propertyNames, row.getPropertyId()));
record.put("districtId", row.getDistrictId());
record.put("districtName", mapGet(enrichment.districtNames, row.getDistrictId()));
record.put("maintenanceAgentId", row.getMaintenanceAgentId());
record.put("maintenanceAgentName", row.getMaintenanceAgentName());
record.put("maintenancePropertyId", row.getMaintenancePropertyId());
record.put("maintenancePropertyName", row.getMaintenancePropertyName());
record.put("handUserId", row.getHandUserId());
record.put("handUserName", row.getHandUserName());
record.put("workNo", row.getWorkNo());
record.put("createTime", format(row.getCreateTime()));
return record;
}
private Map<String, Object> toHandleRecord(EquipmentRepairHandle row, NameEnrichment enrichment,
FaultNames faultNames, Map<Long, String> accessoryNames) {
Map<String, Object> record = new LinkedHashMap<>();
record.put("id", row.getId());
record.put("recordrNo", row.getRecordrNo());
record.put("equipmentRepairId", row.getEquipmentRepairId());
record.put("snCode", row.getSnCode());
record.put("deviceType", row.getDeviceType());
record.put("deviceTypeText", deviceTypeText(row.getDeviceType()));
record.put("state", row.getState());
record.put("stateText", stateText(row.getState()));
record.put("assembly", row.getAssembly());
record.put("assemblyName", faultNames.assemblyNames.get(row.getAssembly()));
record.put("assemblyFault", row.getAssemblyFault());
record.put("assemblyFaultName", faultNames.assemblyFaultNames.get(row.getAssemblyFault()));
record.put("accessoryId", row.getAccessoryId());
record.put("accessoryName", mapGet(accessoryNames, row.getAccessoryId()));
record.put("chipCode", row.getChipCode());
record.put("oldChipCode", row.getOldChipCode());
record.put("addTime", format(row.getAddTime()));
record.put("userId", row.getUserId());
record.put("userName", mapGet(enrichment.userNames, row.getUserId()));
record.put("memo", row.getMemo());
record.put("buildingId", row.getBuildingId());
record.put("floor", row.getFloor());
record.put("toiletId", row.getToiletId());
record.put("positionId", row.getPositionId());
record.put("installAddress", row.getInstallAddress());
record.put("agentId", row.getAgentId());
record.put("agentName", mapGet(enrichment.agentNames, row.getAgentId()));
record.put("applyAgentId", row.getApplyAgentId());
record.put("applyAgentName", row.getApplyAgentName());
record.put("propertyId", row.getPropertyId());
record.put("propertyName", mapGet(enrichment.propertyNames, row.getPropertyId()));
record.put("districtId", row.getDistrictId());
record.put("districtName", mapGet(enrichment.districtNames, row.getDistrictId()));
record.put("maintenanceAgentId", row.getMaintenanceAgentId());
record.put("maintenanceAgentName", row.getMaintenanceAgentName());
record.put("maintenancePropertyId", row.getMaintenancePropertyId());
record.put("maintenancePropertyName", row.getMaintenancePropertyName());
record.put("workNo", row.getWorkNo());
record.put("createTime", format(row.getCreateTime()));
return record;
}
private static Map<String, Object> pageResult(Long total, int pageNo, int pageSize,
boolean truncated, List<Map<String, Object>> records) {
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 static final class NameEnrichment {
Map<Long, String> agentNames = Map.of();
Map<Long, String> propertyNames = Map.of();
Map<Long, String> districtNames = Map.of();
Map<Long, String> userNames = Map.of();
}
private static final class FaultNames {
Map<String, String> assemblyNames = Map.of();
Map<String, String> assemblyFaultNames = Map.of();
}
private static <K, V> V mapGet(Map<K, V> map, K key) {
return key == null ? null : map.get(key);
}
private static String format(Date value) {
return value == null ? null : new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(value);
}
private static String stateText(Integer value) {
if (value == null) {
return null;
}
return switch (value) {
case 0 -> "待解决";
case 1 -> "已解决";
case 2 -> "延迟解决";
default -> "未知(" + value + ")";
};
}
private static String sourceTypeText(Integer value) {
if (value == null) {
return null;
}
return switch (value) {
case 0 -> "运维换机";
case 1 -> "客户换机";
case 2 -> "维护维修";
default -> "未知(" + value + ")";
};
}
private static String deviceTypeText(Integer value) {
if (value == null) {
return null;
}
return switch (value) {
case 0 -> "厕纸机";
case 1 -> "擦手纸机";
default -> "未知(" + value + ")";
};
}
private static LocalDate parseDate(String value, String name) {
if (!StringUtils.hasText(value)) {
return null;
}
try {
return LocalDate.parse(value.trim(), DATE_FORMAT);
} catch (DateTimeParseException e) {
throw invalid(name + " 格式非法,要求 yyyy-MM-dd: " + value);
}
}
private static void validatePositive(Long value, String name) {
if (value != null && value < 1) {
throw invalid(name + " 必须为正整数");
}
}
private static void validateEnum(Integer value, String name, int... allowed) {
if (value == null) {
return;
}
for (int candidate : allowed) {
if (value == candidate) {
return;
}
}
String options = Arrays.stream(allowed).mapToObj(String::valueOf)
.collect(Collectors.joining("、"));
throw invalid(name + " 仅支持 " + options);
}
/** 入参类错误透出具体文案给调用方,便于 LLM 自愈 */
private static ToolCallException invalid(String message) {
return new ToolCallException(McpErrorCode.INVALID_ARGUMENT, message);
}
}
@@ -1,12 +1,15 @@
package me.iiv.agent.permission;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import me.iiv.agent.entity.m0.Agent;
import me.iiv.agent.entity.m0.AgentManageDistrictAuth;
import me.iiv.agent.entity.m0.Authority;
import me.iiv.agent.entity.m0.BatteryFaultLog;
import me.iiv.agent.entity.m0.DeviceLifeCycle;
import me.iiv.agent.entity.m0.DistrictDistrictStrategy;
import me.iiv.agent.entity.m0.EquipmentRepairHandle;
import me.iiv.agent.entity.m0.EquipmentRepairRecording;
import me.iiv.agent.entity.m0.HardwareCleanLog;
import me.iiv.agent.entity.m0.HardwareView;
import me.iiv.agent.entity.m0.HardSockLife;
@@ -17,6 +20,7 @@ import me.iiv.agent.entity.m1.MallGoodsOrder;
import me.iiv.agent.entity.m1.SalerRepayment;
import me.iiv.agent.entity.m1.SalerRepaymentCustomer;
import me.iiv.agent.entity.m2.ContractNew;
import me.iiv.agent.entity.m2.BatteryInspection;
import me.iiv.agent.entity.m2.DeliveryOrder;
import me.iiv.agent.entity.m2.InstallOrder;
import me.iiv.agent.entity.m2.PickupOrder;
@@ -1097,4 +1101,59 @@ public class PermissionService {
}
wrapper.in(DeviceLifeCycle::getAgentId, scope.ids);
}
/**
* 电池检查(m2 battery_inspection)行权限(REQ-20261106-0020,方案 B 拍板复用 resolveRowScope 含直属下级
* 口径,对齐老 /batteryInspection/pageList 的 getByIsAgentNew propertyFlag=false 变体):
* 总部/服务号全量;销售非负责人按 property_id 集合;其余代理按 apply_agent_id IN ids
* (不加 managedPropertyIds OR——老 propertyFlag=false 分支本无物业 OR 段)。
*/
public void applyBatteryInspectionRowFilter(LambdaQueryWrapper<BatteryInspection> wrapper, User user) {
RowScope scope = resolveRowScope(user);
if (scope.headquarters) {
return;
}
if (scope.salesPropertyIds != null) {
wrapper.in(BatteryInspection::getPropertyId, scope.salesPropertyIds);
return;
}
wrapper.in(BatteryInspection::getApplyAgentId, scope.ids);
}
/** 故障汇总(m0 equipment_repair_recording)行权限,见 applyEquipmentRepairScope。 */
public void applyRepairRecordingRowFilter(LambdaQueryWrapper<EquipmentRepairRecording> wrapper, User user) {
applyEquipmentRepairScope(wrapper, user, EquipmentRepairRecording::getApplyAgentId,
EquipmentRepairRecording::getAgentId, EquipmentRepairRecording::getPropertyId);
}
/** 现场维修记录(m0 equipment_repair_handle)行权限,与故障汇总同口径。 */
public void applyRepairHandleRowFilter(LambdaQueryWrapper<EquipmentRepairHandle> wrapper, User user) {
applyEquipmentRepairScope(wrapper, user, EquipmentRepairHandle::getApplyAgentId,
EquipmentRepairHandle::getAgentId, EquipmentRepairHandle::getPropertyId);
}
/**
* 维修域共用行权限(REQ-20261106-0020,对齐老 getByIsAgentNew propertyFlag=true 变体的有效口径):
* 总部/服务号全量;销售非负责人按 property_id 集合;其余代理
* AND (apply_agent_id IN ids OR agent_id IN ids OR property_id IN 代管物业集合[非空时])。
*/
private <T> void applyEquipmentRepairScope(LambdaQueryWrapper<T> wrapper, User user,
SFunction<T, ?> applyAgentIdColumn,
SFunction<T, ?> agentIdColumn,
SFunction<T, ?> propertyIdColumn) {
RowScope scope = resolveRowScope(user);
if (scope.headquarters) {
return;
}
if (scope.salesPropertyIds != null) {
wrapper.in(propertyIdColumn, scope.salesPropertyIds);
return;
}
wrapper.and(w -> {
w.in(applyAgentIdColumn, scope.ids).or().in(agentIdColumn, scope.ids);
if (!scope.managedPropertyIds.isEmpty()) {
w.or().in(propertyIdColumn, scope.managedPropertyIds);
}
});
}
}