Files
Leantime/app/Domain/Bom/Tools/AddMasterItemTool.php

84 lines
3.1 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?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 AddMasterItemTool extends Tool
{
public function __construct(
private Bom $bomService,
) {}
public function name(): string
{
return 'addMasterItem';
}
public function description(): string
{
return '给主数据BOM/工艺文件/工具清单添加一条明细行。固定列seq(序号)、partNo(零件编号)、partName(零件名称)、partDrawingNo(零件图号)、material(材质)、spec(零件规格)、qtyPerUnit(单件用量)、unit(单位)、process(工序)、remark(备注);动态列用 key 传值。';
}
public function schema(ToolInputSchema $schema): ToolInputSchema
{
return $schema
->integer('id')->description('主数据 ID。')->required()
->integer('seq')->description('序号。')
->string('partNo')->description('零件编号。')
->string('partName')->description('零件名称。')
->string('partDrawingNo')->description('零件图号。')
->string('material')->description('材质。')
->string('spec')->description('零件规格。')
->string('qtyPerUnit')->description('单件用量。')
->string('unit')->description('单位。')
->string('process')->description('工序。')
->string('remark')->description('备注。')
->raw('dynamicColumns', [
'type' => 'object',
'description' => '动态列的值,键为列 key值为字符串。',
'additionalProperties' => ['type' => 'string'],
]);
}
public function handle(array $arguments): ToolResult
{
$id = (int) ($arguments['id'] ?? 0);
$values = [
'seq' => (int) ($arguments['seq'] ?? 0),
'partNo' => (string) ($arguments['partNo'] ?? ''),
'partName' => (string) ($arguments['partName'] ?? ''),
'partDrawingNo' => (string) ($arguments['partDrawingNo'] ?? ''),
'material' => (string) ($arguments['material'] ?? ''),
'spec' => (string) ($arguments['spec'] ?? ''),
'qtyPerUnit' => (string) ($arguments['qtyPerUnit'] ?? ''),
'unit' => (string) ($arguments['unit'] ?? ''),
'process' => (string) ($arguments['process'] ?? ''),
'remark' => (string) ($arguments['remark'] ?? ''),
];
// 动态列
$dynamic = $arguments['dynamicColumns'] ?? [];
if (is_array($dynamic)) {
foreach ($dynamic as $k => $v) {
$values[(string) $k] = (string) $v;
}
}
$itemId = $this->bomService->saveItem($id, $values);
if ($itemId > 0) {
return ToolResult::text("明细行添加成功itemId {$itemId}");
}
return ToolResult::error('添加明细行失败(可能无权限或主数据不存在)。');
}
}