feat(mcp): 新增设备转固定资产写工具 device.lifecycle.fixed_asset.update(REQ-20261117-0003)

按 SN 精确匹配把 device_life_cycle.asset_state 转为固定资产(1),memo 写来源状态文案;
已是固定资产/已报废守卫拒绝,null 按库存商品处理;只写 asset_state/memo/modify_id/
modify_time 四列,不联动资产卡片/金蝶;零行级权限,prod 只绑研发专用 scope 6。
DeviceLifeCycle 实体补 memo 字段映射(DB 列早已存在,老系统从未映射)。
This commit is contained in:
my_local_kimi_dev2
2026-11-17 09:41:24 +08:00
parent e042f62c34
commit 37ce5a3443
6 changed files with 449 additions and 0 deletions
@@ -53,6 +53,8 @@ public class DeviceLifeCycle implements Serializable {
private String lastLatitudeAndLongitude;
/** 资产属性(0:库存商品;1:固定资产;2:出租;3:售出;4:报废;5:费用化) */
private Integer assetState;
/** 备注(DB 列真实存在但老系统 bean 未映射,历史基本为空;设备转固定资产工具首次写入) */
private String memo;
private Long agentId;
private String agentName;
private Long assetOwnerId;
@@ -0,0 +1,126 @@
package me.iiv.agent.mcp.tool;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import me.iiv.agent.entity.m0.DeviceLifeCycle;
import me.iiv.agent.entity.m0.User;
import me.iiv.agent.mapper.m0.DeviceLifeCycleMapper;
import me.iiv.agent.mcp.McpCallContextHolder;
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.security.AuthContextHolder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
/**
* 设备转固定资产工具(REQ-20261117-0003,设计文档 doc/device-lifecycle-fixed-asset-mcp-design.md)。
* 按 SN 精确匹配把 device_life_cycle.asset_state 改为 1(固定资产),memo 写来源状态文案;
* 目标状态固定为 1,天然满足「不允许从固定资产变成其他」;报废(4)按拍板结论守卫拒绝。
* 零行级权限,门禁靠「研发专用」scope;必须用户身份 Token(操作人落 modify_id + 审计)。
* 只改 device_life_cycle 一表,不联动 m2 asset_info 资产卡片/金蝶同步,不动 last_op_* 流转字段。
*/
@Component
public class DeviceLifecycleAdminTools implements ToolObject {
private static final Logger log = LoggerFactory.getLogger(DeviceLifecycleAdminTools.class);
private static final int ASSET_STATE_FIXED = 1;
private static final int ASSET_STATE_DISCARDED = 4;
private static final Map<Integer, String> ASSET_STATE_TEXT = Map.of(
0, "库存商品", 1, "固定资产", 2, "出租", 3, "售出", 4, "报废", 5, "费用化");
private static final Map<Integer, String> DEVICE_TYPE_TEXT = Map.of(
0, "出纸机", 1, "空感", 2, "电池", 3, "充电箱", 4, "泡沫机", 5, "蓝牙网关");
final DeviceLifeCycleMapper deviceLifeCycleMapper;
public DeviceLifecycleAdminTools(DeviceLifeCycleMapper deviceLifeCycleMapper) {
this.deviceLifeCycleMapper = deviceLifeCycleMapper;
}
@XqTool(category = "admin", kind = "write")
@Tool(name = "device.lifecycle.fixed_asset.update", description = "【写操作·研发/运维专用】按 SN 精确匹配(不模糊)"
+ "把设备生命周期记录 device_life_cycle.asset_state 转为固定资产(1),"
+ "memo 自动写「从<原状态>变成固定资产」。允许来源状态:库存商品(0)/出租(2)/售出(3)/费用化(5)/空;"
+ "已是固定资产(1)报错拒绝(本工具目标状态固定为 1,任何情况下都不会把固定资产改成其他状态);"
+ "已报废(4)报错拒绝。只更新 asset_state/memo/modify_id/modify_time 四列,"
+ "不动流转状态、不生成资产卡片、不触发金蝶同步。"
+ "无行级权限校验,访问控制靠 scope 绑定;必须用户身份 Token,每次调用写审计日志。"
+ "重复 SN 取 id 最早一条(与老系统口径一致)。")
public Map<String, Object> fixedAssetUpdate(
@ToolParam(description = "设备 SN,精确匹配 sn_code,不模糊") String snCode) {
if (!StringUtils.hasText(snCode)) {
throw new ToolCallException(McpErrorCode.INVALID_ARGUMENT, "snCode 不能为空");
}
String sn = snCode.trim();
User caller = AuthContextHolder.get().getUser();
if (caller == null) {
throw new ToolCallException(McpErrorCode.PERMISSION_DENIED,
"该工具要求用户身份 Token 调用(操作人需可追溯),服务身份 Token 不可用");
}
List<DeviceLifeCycle> rows = deviceLifeCycleMapper.selectList(
new LambdaQueryWrapper<DeviceLifeCycle>()
.eq(DeviceLifeCycle::getSoftDelete, 0)
.eq(DeviceLifeCycle::getSnCode, sn)
.orderByAsc(DeviceLifeCycle::getId)
.last("LIMIT 1"));
if (rows.isEmpty()) {
throw new ToolCallException(McpErrorCode.NOT_FOUND_OR_FORBIDDEN,
"设备生命周期记录不存在(sn_code=" + sn + ")");
}
DeviceLifeCycle record = rows.get(0);
Integer oldState = record.getAssetState() == null ? 0 : record.getAssetState();
if (oldState == ASSET_STATE_FIXED) {
throw new ToolCallException(McpErrorCode.INVALID_ARGUMENT,
"设备 " + sn + " 当前已是固定资产,不允许重复转换");
}
if (oldState == ASSET_STATE_DISCARDED) {
throw new ToolCallException(McpErrorCode.INVALID_ARGUMENT,
"设备 " + sn + " 已报废,不允许转固定资产");
}
String oldStateText = ASSET_STATE_TEXT.getOrDefault(oldState, "未知");
String memo = "从" + oldStateText + "变成固定资产";
Date now = new Date();
int updated = deviceLifeCycleMapper.update(null, new LambdaUpdateWrapper<DeviceLifeCycle>()
.eq(DeviceLifeCycle::getId, record.getId())
.set(DeviceLifeCycle::getAssetState, ASSET_STATE_FIXED)
.set(DeviceLifeCycle::getMemo, memo)
.set(DeviceLifeCycle::getModifyId, caller.getId())
.set(DeviceLifeCycle::getModifyTime, now));
if (updated != 1) {
throw new ToolCallException(McpErrorCode.DATA_INTEGRITY_ERROR,
"更新影响行数=" + updated + ",预期 1,请携带 request_id 联系运维排查");
}
String requestId = McpCallContextHolder.currentOrCreateRequestId();
log.info("device.lifecycle.fixed_asset.update 完成 requestId={} sn={} id={} {}→1 memo={} caller={}",
requestId, sn, record.getId(), oldState, memo, caller.getId());
Map<String, Object> result = new LinkedHashMap<>();
result.put("snCode", sn);
result.put("deviceType", record.getDeviceType());
result.put("deviceTypeText", DEVICE_TYPE_TEXT.get(record.getDeviceType()));
result.put("oldAssetState", oldState);
result.put("oldAssetStateText", oldStateText);
result.put("newAssetState", ASSET_STATE_FIXED);
result.put("newAssetStateText", ASSET_STATE_TEXT.get(ASSET_STATE_FIXED));
result.put("memo", memo);
result.put("modifyTime", now);
result.put("requestId", requestId);
return result;
}
}
@@ -0,0 +1,177 @@
package me.iiv.agent.mcp.tool;
import com.baomidou.mybatisplus.core.MybatisConfiguration;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.TableInfoHelper;
import me.iiv.agent.entity.m0.DeviceLifeCycle;
import me.iiv.agent.entity.m0.User;
import me.iiv.agent.mapper.m0.DeviceLifeCycleMapper;
import me.iiv.agent.mcp.McpErrorCode;
import me.iiv.agent.mcp.ToolCallException;
import me.iiv.agent.security.AuthContext;
import me.iiv.agent.security.AuthContextHolder;
import org.apache.ibatis.builder.MapperBuilderAssistant;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import java.util.List;
import java.util.Map;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.isNull;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@ExtendWith(MockitoExtension.class)
class DeviceLifecycleAdminToolsTest {
@Mock DeviceLifeCycleMapper deviceLifeCycleMapper;
DeviceLifecycleAdminTools tools;
@BeforeEach
void setUp() {
MapperBuilderAssistant assistant = new MapperBuilderAssistant(new MybatisConfiguration(), "");
TableInfoHelper.initTableInfo(assistant, DeviceLifeCycle.class);
tools = new DeviceLifecycleAdminTools(deviceLifeCycleMapper);
User caller = new User();
caller.setId(812L);
AuthContextHolder.set(new AuthContext(null, caller));
}
@AfterEach
void tearDown() {
AuthContextHolder.clear();
}
@Test
void rejectsBlankSnCode() {
ToolCallException e1 = assertThrows(ToolCallException.class,
() -> tools.fixedAssetUpdate(null));
assertEquals(McpErrorCode.INVALID_ARGUMENT, e1.getCode());
ToolCallException e2 = assertThrows(ToolCallException.class,
() -> tools.fixedAssetUpdate(" "));
assertEquals(McpErrorCode.INVALID_ARGUMENT, e2.getCode());
verify(deviceLifeCycleMapper, never()).update(isNull(), any());
}
@Test
void deniesServiceTokenWithoutUser() {
AuthContextHolder.set(new AuthContext(null, null));
ToolCallException e = assertThrows(ToolCallException.class,
() -> tools.fixedAssetUpdate("XQ-1"));
assertEquals(McpErrorCode.PERMISSION_DENIED, e.getCode());
verify(deviceLifeCycleMapper, never()).update(isNull(), any());
}
@Test
void notFoundWhenNoRow() {
when(deviceLifeCycleMapper.selectList(any())).thenReturn(List.of());
ToolCallException e = assertThrows(ToolCallException.class,
() -> tools.fixedAssetUpdate("XQ-NONE"));
assertEquals(McpErrorCode.NOT_FOUND_OR_FORBIDDEN, e.getCode());
verify(deviceLifeCycleMapper, never()).update(isNull(), any());
}
@Test
void alreadyFixedAssetRejected() {
when(deviceLifeCycleMapper.selectList(any())).thenReturn(List.of(record(1L, 1)));
ToolCallException e = assertThrows(ToolCallException.class,
() -> tools.fixedAssetUpdate("XQ-1"));
assertEquals(McpErrorCode.INVALID_ARGUMENT, e.getCode());
assertTrue(e.getRawMessage().contains("已是固定资产"));
verify(deviceLifeCycleMapper, never()).update(isNull(), any());
}
@Test
void discardedRejected() {
when(deviceLifeCycleMapper.selectList(any())).thenReturn(List.of(record(1L, 4)));
ToolCallException e = assertThrows(ToolCallException.class,
() -> tools.fixedAssetUpdate("XQ-1"));
assertEquals(McpErrorCode.INVALID_ARGUMENT, e.getCode());
assertTrue(e.getRawMessage().contains("报废"));
verify(deviceLifeCycleMapper, never()).update(isNull(), any());
}
@Test
void convertsFromStockAndSetsOnlyFourColumns() {
when(deviceLifeCycleMapper.selectList(any())).thenReturn(List.of(record(10L, 0)));
when(deviceLifeCycleMapper.update(isNull(), any())).thenReturn(1);
Map<String, Object> result = tools.fixedAssetUpdate("XQ-1");
assertEquals(0, result.get("oldAssetState"));
assertEquals("库存商品", result.get("oldAssetStateText"));
assertEquals(1, result.get("newAssetState"));
assertEquals("从库存商品变成固定资产", result.get("memo"));
@SuppressWarnings("unchecked")
ArgumentCaptor<LambdaUpdateWrapper<DeviceLifeCycle>> captor =
ArgumentCaptor.forClass(LambdaUpdateWrapper.class);
verify(deviceLifeCycleMapper).update(isNull(), captor.capture());
String sqlSet = captor.getValue().getSqlSet();
assertTrue(sqlSet.contains("asset_state"));
assertTrue(sqlSet.contains("memo"));
assertTrue(sqlSet.contains("modify_id"));
assertTrue(sqlSet.contains("modify_time"));
assertFalse(sqlSet.contains("last_op_user"));
assertFalse(sqlSet.contains("last_op_date"));
assertFalse(sqlSet.contains("exchange_state"));
assertFalse(sqlSet.contains("jindie_sync_state"));
}
@Test
void convertsFromRentSoldAndExpensed() {
assertMemoFor(2, "从出租变成固定资产");
assertMemoFor(3, "从售出变成固定资产");
assertMemoFor(5, "从费用化变成固定资产");
}
@Test
void nullAssetStateTreatedAsStock() {
when(deviceLifeCycleMapper.selectList(any())).thenReturn(List.of(record(11L, null)));
when(deviceLifeCycleMapper.update(isNull(), any())).thenReturn(1);
Map<String, Object> result = tools.fixedAssetUpdate("XQ-NULL");
assertEquals(0, result.get("oldAssetState"));
assertEquals("库存商品", result.get("oldAssetStateText"));
assertEquals("从库存商品变成固定资产", result.get("memo"));
}
@Test
void updateRowCountNotOneIsDataIntegrityError() {
when(deviceLifeCycleMapper.selectList(any())).thenReturn(List.of(record(12L, 0)));
when(deviceLifeCycleMapper.update(isNull(), any())).thenReturn(0);
ToolCallException e = assertThrows(ToolCallException.class,
() -> tools.fixedAssetUpdate("XQ-1"));
assertEquals(McpErrorCode.DATA_INTEGRITY_ERROR, e.getCode());
}
private void assertMemoFor(int oldState, String expectedMemo) {
when(deviceLifeCycleMapper.selectList(any())).thenReturn(List.of(record(99L, oldState)));
when(deviceLifeCycleMapper.update(isNull(), any())).thenReturn(1);
Map<String, Object> result = tools.fixedAssetUpdate("XQ-" + oldState);
assertEquals(expectedMemo, result.get("memo"));
}
private static DeviceLifeCycle record(Long id, Integer assetState) {
DeviceLifeCycle record = new DeviceLifeCycle();
record.setId(id);
record.setSnCode("XQ-1");
record.setDeviceType(0);
record.setAssetState(assetState);
return record;
}
}