56 lines
1.7 KiB
PHP
56 lines
1.7 KiB
PHP
<?php
|
||
|
||
namespace Leantime\Domain\Bom\Tools;
|
||
|
||
use Laravel\Mcp\Server\Tool;
|
||
use Laravel\Mcp\Server\Tools\ToolInputSchema;
|
||
use Laravel\Mcp\Server\Tools\ToolResult;
|
||
use Leantime\Domain\Bom\Services\Bom;
|
||
|
||
/**
|
||
* 给主数据(BOM/工艺文件/工具清单)添加一个动态列。
|
||
*/
|
||
class AddMasterColumnTool extends Tool
|
||
{
|
||
public function __construct(
|
||
private Bom $bomService,
|
||
) {}
|
||
|
||
public function name(): string
|
||
{
|
||
return 'addMasterColumn';
|
||
}
|
||
|
||
public function description(): string
|
||
{
|
||
return '给主数据(BOM/工艺文件/工具清单)添加一个动态列(自定义字段)。key 为存储标识(英文或中文),label 为显示名。';
|
||
}
|
||
|
||
public function schema(ToolInputSchema $schema): ToolInputSchema
|
||
{
|
||
return $schema
|
||
->integer('id')->description('主数据 ID。')->required()
|
||
->string('key')->description('列标识(英文或中文,用于存储)。')->required()
|
||
->string('label')->description('列显示名,默认同 key。');
|
||
}
|
||
|
||
public function handle(array $arguments): ToolResult
|
||
{
|
||
$id = (int) ($arguments['id'] ?? 0);
|
||
$key = (string) ($arguments['key'] ?? '');
|
||
$label = (string) ($arguments['label'] ?? $key);
|
||
|
||
if ($key === '') {
|
||
return ToolResult::error('key 不能为空');
|
||
}
|
||
|
||
$columnId = $this->bomService->addColumn($id, $key, $label);
|
||
|
||
if ($columnId > 0) {
|
||
return ToolResult::text("动态列添加成功:{$key}(columnId {$columnId})。");
|
||
}
|
||
|
||
return ToolResult::error('添加列失败(可能无权限、key 冲突固定列、或主数据不存在)。');
|
||
}
|
||
}
|