AML 操作方法文档

AML 操作方法文档

一、AML 基本格式

1.1 标准格式

<AML>
  <Item type="[对象类型]" action="[操作类型]" [属性]>
    <!-- 对象属性 -->
  </Item>
</AML>

1.2 操作类型

| 操作 | 说明 | 示例 |

|------|------|------|

| get | 查询 | 获取对象列表或单个对象 |

| add | 创建 | 创建新对象 |

| edit | 编辑 | 修改现有对象 |

| delete | 删除 | 删除对象 |

二、AML 操作模板

2.1 查询操作 (get)

文件位置/app/lib/aml/queries.ts

// 查询对象列表
export const queryTemplates = {
  // ECR查询
  ecrList: (status?: string) => `
    <AML>
      <Item type="ECR" action="get" select="id,change_number,title,description,status,created_by,created_on">
        ${status ? `<status>${status}</status>` : ''}
      </Item>
    </AML>
  `,

  // ECR单个查询
  ecrById: (id: string) => `
    <AML>
      <Item type="ECR" action="get" id="${id}" select="id,change_number,title,description,status,created_by,created_on">
      </Item>
    </AML>
  `,

  // Part查询
  partList: (partNumber?: string) => `
    <AML>
      <Item type="Part" action="get" select="id,part_number,name,description,unit_cost,weight">
        ${partNumber ? `<part_number>${partNumber}</part_number>` : ''}
      </Item>
    </AML>
  `,

  // Vendor查询
  vendorList: () => `
    <AML>
      <Item type="Vendor" action="get" select="id,name,contact,phone,email,address">
      </Item>
    </AML>
  `,

  // BOM查询
  bomList: (partId?: string) => `
    <AML>
      <Item type="BOM" action="get" select="id,part_id,item_id,quantity,unit">
        ${partId ? `<part_id>${partId}</part_id>` : ''}
      </Item>
    </AML>
  `,

  // Inventory查询
  inventoryList: () => `
    <AML>
      <Item type="Inventory" action="get" select="id,part_id,quantity,location,warehouse">
      </Item>
    </AML>
  `
};

2.2 创建操作 (add)

// 文件位置:/app/lib/aml/creates.ts

export const createTemplates = {
  // 创建ECR
  ecr: (data: { changeNumber: string; title: string; description: string }) => `
    <AML>
      <Item type="ECR" action="add">
        <change_number>${data.changeNumber}</change_number>
        <title><![CDATA[${data.title}]]></title>
        <description><![CDATA[${data.description}]]></description>
        <status>new</status>
      </Item>
    </AML>
  `,

  // 创建Part
  part: (data: { partNumber: string; name: string; description?: string; unitCost?: number; weight?: number }) => `
    <AML>
      <Item type="Part" action="add">
        <part_number>${data.partNumber}</part_number>
        <name><![CDATA[${data.name}]]></name>
        ${data.description ? `<description><![CDATA[${data.description}]]></description>` : ''}
        ${data.unitCost ? `<unit_cost>${data.unitCost}</unit_cost>` : ''}
        ${data.weight ? `<weight>${data.weight}</weight>` : ''}
      </Item>
    </AML>
  `,

  // 创建Vendor
  vendor: (data: { name: string; contact?: string; phone?: string; email?: string; address?: string }) => `
    <AML>
      <Item type="Vendor" action="add">
        <name><![CDATA[${data.name}]]></name>
        ${data.contact ? `<contact><![CDATA[${data.contact}]]></contact>` : ''}
        ${data.phone ? `<phone>${data.phone}</phone>` : ''}
        ${data.email ? `<email>${data.email}</email>` : ''}
        ${data.address ? `<address><![CDATA[${data.address}]]></address>` : ''}
      </Item>
    </AML>
  `,

  // 创建BOM
  bom: (data: { partId: string; itemId: string; quantity: number; unit?: string }) => `
    <AML>
      <Item type="BOM" action="add">
        <part_id>${data.partId}</part_id>
        <item_id>${data.itemId}</item_id>
        <quantity>${data.quantity}</quantity>
        ${data.unit ? `<unit>${data.unit}</unit>` : ''}
      </Item>
    </AML>
  `
};

2.3 编辑操作 (edit)

// 文件位置:/app/lib/aml/edits.ts

export const editTemplates = {
  // 更新ECR
  ecr: (id: string, data: { status?: string; description?: string }) => `
    <AML>
      <Item type="ECR" action="edit" id="${id}">
        ${data.status ? `<status>${data.status}</status>` : ''}
        ${data.description ? `<description><![CDATA[${data.description}]]></description>` : ''}
      </Item>
    </AML>
  `,

  // 更新Part
  part: (id: string, data: { name?: string; description?: string; unitCost?: number; weight?: number }) => `
    <AML>
      <Item type="Part" action="edit" id="${id}">
        ${data.name ? `<name><![CDATA[${data.name}]]></name>` : ''}
        ${data.description ? `<description><![CDATA[${data.description}]]></description>` : ''}
        ${data.unitCost ? `<unit_cost>${data.unitCost}</unit_cost>` : ''}
        ${data.weight ? `<weight>${data.weight}</weight>` : ''}
      </Item>
    </AML>
  `,

  // 更新Vendor
  vendor: (id: string, data: { name?: string; contact?: string; phone?: string; email?: string; address?: string }) => `
    <AML>
      <Item type="Vendor" action="edit" id="${id}">
        ${data.name ? `<name><![CDATA[${data.name}]]></name>` : ''}
        ${data.contact ? `<contact><![CDATA[${data.contact}]]></contact>` : ''}
        ${data.phone ? `<phone>${data.phone}</phone>` : ''}
        ${data.email ? `<email>${data.email}</email>` : ''}
        ${data.address ? `<address><![CDATA[${data.address}]]></address>` : ''}
      </Item>
    </AML>
  `
};

2.4 删除操作 (delete)

// 文件位置:/app/lib/aml/deletes.ts

export const deleteTemplates = {
  // 删除ECR
  ecr: (id: string) => `
    <AML>
      <Item type="ECR" action="delete" id="${id}">
      </Item>
    </AML>
  `,

  // 删除Part
  part: (id: string) => `
    <AML>
      <Item type="Part" action="delete" id="${id}">
      </Item>
    </AML>
  `,

  // 删除Vendor
  vendor: (id: string) => `
    <AML>
      <Item type="Vendor" action="delete" id="${id}">
      </Item>
    </AML>
  `
};

三、AML 调用封装

文件位置/app/lib/aml/index.ts

import { executeAmlWithFallback } from '../SCSAI/fallback';
import { queryTemplates } from './queries';
import { createTemplates } from './creates';
import { editTemplates } from './edits';
import { deleteTemplates } from './deletes';

// 查询对象列表
export async function queryItems(itemType: string, status?: string): Promise<any> {
  let aml: string;

  switch (itemType) {
    case 'ECR':
      aml = queryTemplates.ecrList(status);
      break;
    case 'Part':
      aml = queryTemplates.partList();
      break;
    case 'Vendor':
      aml = queryTemplates.vendorList();
      break;
    case 'BOM':
      aml = queryTemplates.bomList();
      break;
    case 'Inventory':
      aml = queryTemplates.inventoryList();
      break;
    default:
      throw new Error(`Unsupported item type: ${itemType}`);
  }

  return await executeAmlWithFallback(aml, itemType);
}

// 创建对象
export async function createItem(itemType: string, data: any): Promise<any> {
  let aml: string;

  switch (itemType) {
    case 'ECR':
      aml = createTemplates.ecr(data);
      break;
    case 'Part':
      aml = createTemplates.part(data);
      break;
    case 'Vendor':
      aml = createTemplates.vendor(data);
      break;
    case 'BOM':
      aml = createTemplates.bom(data);
      break;
    default:
      throw new Error(`Unsupported item type: ${itemType}`);
  }

  return await executeAmlWithFallback(aml, itemType);
}

// 更新对象
export async function updateItem(itemType: string, id: string, data: any): Promise<any> {
  let aml: string;

  switch (itemType) {
    case 'ECR':
      aml = editTemplates.ecr(id, data);
      break;
    case 'Part':
      aml = editTemplates.part(id, data);
      break;
    case 'Vendor':
      aml = editTemplates.vendor(id, data);
      break;
    default:
      throw new Error(`Unsupported item type: ${itemType}`);
  }

  return await executeAmlWithFallback(aml, itemType);
}

// 删除对象
export async function deleteItem(itemType: string, id: string): Promise<any> {
  let aml: string;

  switch (itemType) {
    case 'ECR':
      aml = deleteTemplates.ecr(id);
      break;
    case 'Part':
      aml = deleteTemplates.part(id);
      break;
    case 'Vendor':
      aml = deleteTemplates.vendor(id);
      break;
    default:
      throw new Error(`Unsupported item type: ${itemType}`);
  }

  return await executeAmlWithFallback(aml, itemType);
}

四、常用对象类型

4.1 对象类型列表

| 对象类型 | 说明 | 主要字段 |

|----------|------|----------|

| ECR | 工程变更请求 | change_number, title, description, status |

| ECO | 工程变更指令 | change_number, title, affected_items, status |

| Part | 零件 | part_number, name, description, unit_cost, weight |

| BOM | 物料清单 | part_id, item_id, quantity, unit |

| Vendor | 供应商 | name, contact, phone, email, address |

| Inventory | 库存 | part_id, quantity, location, warehouse |

4.2 状态流转

ECR状态

  • new → pending_approval → approved → in_progress → completed
  • new → pending_approval → rejected

ECO状态

  • new → pending_approval → approved → in_progress → completed
  • new → pending_approval → rejected

五、快速使用示例

// 文件位置:/app/api/objects/route.ts

import { queryItems, createItem, updateItem, deleteItem } from '@/app/lib/aml';

// GET /api/objects?type=ECR&status=pending_approval
export async function GET(request: Request) {
  const { searchParams } = new URL(request.url);
  const type = searchParams.get('type') || 'ECR';
  const status = searchParams.get('status');

  const result = await queryItems(type, status);
  return Response.json(result);
}

// POST /api/objects
export async function POST(request: Request) {
  const { type, data } = await request.json();
  const result = await createItem(type, data);
  return Response.json(result);
}

// PUT /api/objects
export async function PUT(request: Request) {
  const { type, id, data } = await request.json();
  const result = await updateItem(type, id, data);
  return Response.json(result);
}

// DELETE /api/objects
export async function DELETE(request: Request) {
  const { searchParams } = new URL(request.url);
  const type = searchParams.get('type');
  const id = searchParams.get('id');

  if (!type || !id) {
    return Response.json({ success: false, error: 'Missing type or id' }, { status: 400 });
  }

  const result = await deleteItem(type, id);
  return Response.json(result);
}

六、文件位置总结

| 文件 | 位置 | 用途 |

|------|------|------|

| queries.ts | /app/lib/aml/queries.ts | 查询模板 |

| creates.ts | /app/lib/aml/creates.ts | 创建模板 |

| edits.ts | /app/lib/aml/edits.ts | 编辑模板 |

| deletes.ts | /app/lib/aml/deletes.ts | 删除模板 |

| index.ts | /app/lib/aml/index.ts | 统一导出和调用封装 |

← 返回案例列表
分享:
🤖 Try Now →
🤖
🎁