从外部MES系统导入订单时,如何在MORYX中创建未知产品?
问题描述
我从外部MES系统获取订单数据,尝试在MORYX中创建对应订单。创建订单的大致代码如下:
var orderContext = new OrderCreationContext { Number = externalOrder.Number, }; var operationContext = new OperationCreationContext { Order = orderContext, TotalAmount = externalOrder.Amount, Number = externalOrder.OperationNumber, ProductIdentifier = externalOrder.ProductNumber, }; orderContext.Operations.Add(operationContext); _orderManagement.AddOperation(operationContext);
订单可成功创建,但当提供的产品在MORYX中不存在时,会出现报错:
It was not possible to assign the product!
我希望能动态创建该产品,但发现ProductManager似乎没有提供对应的方法,请问如何将未知产品导入MORYX?
解决方案
在MORYX中动态导入未知产品,可通过以下两种方案实现:
1. 使用IProductImporter接口(推荐)
MORYX框架内置了IProductImporter接口,专门用于外部产品数据的导入。你可以通过依赖注入获取该接口实例,构建产品导入DTO后执行创建操作:
代码示例
// 注入IProductImporter与IProductManagement接口 private readonly IProductImporter _productImporter; private readonly IProductManagement _productManagement; // 构造函数初始化依赖 public YourService(IProductImporter productImporter, IProductManagement productManagement) { _productImporter = productImporter; _productManagement = productManagement; } // 检查并创建缺失产品的方法 private async Task EnsureProductExists(string productNumber) { // 先检查产品是否已存在 var existingProduct = _productManagement.GetProduct(productNumber); if (existingProduct != null) return; // 构建产品导入DTO var productDto = new ProductImportDto { Identifier = productNumber, Name = productNumber, // 可根据外部数据替换为实际产品名称 Version = "1.0", // 按需补充其他属性,如单位、描述、工艺路线等 }; // 执行产品导入 var importResult = await _productImporter.ImportAsync(new[] { productDto }); if (!importResult.Success) { throw new InvalidOperationException($"产品创建失败:{importResult.Message}"); } }
集成到订单创建流程
在执行原订单创建逻辑前,先调用上述方法确保产品存在:
// 先确保产品已创建 await EnsureProductExists(externalOrder.ProductNumber); // 原订单创建逻辑 var orderContext = new OrderCreationContext { Number = externalOrder.Number }; var operationContext = new OperationCreationContext { Order = orderContext, TotalAmount = externalOrder.Amount, Number = externalOrder.OperationNumber, ProductIdentifier = externalOrder.ProductNumber, }; orderContext.Operations.Add(operationContext); _orderManagement.AddOperation(operationContext);
2. 直接通过ProductManagement创建产品
如果IProductImporter的默认逻辑不满足业务需求,可直接使用ProductManagement的底层方法创建产品,但需提前配置好对应的产品类型(ProductType):
代码示例
// 需注入IProductManagement与IProductTypeManagement private readonly IProductManagement _productManagement; private readonly IProductTypeManagement _productTypeManagement; public YourService(IProductManagement productManagement, IProductTypeManagement productTypeManagement) { _productManagement = productManagement; _productTypeManagement = productTypeManagement; } private IProduct CreateProduct(string productNumber) { // 获取预设的产品类型(需确保该类型已在系统中配置) var productType = _productTypeManagement.GetProductType("DefaultProduct"); if (productType == null) { throw new InvalidOperationException("未找到预设的产品类型"); } // 创建产品实例 var product = _productManagement.CreateProduct(productType, productNumber); // 设置产品基础属性 product.Name = productNumber; product.Version = "1.0"; // 保存产品到系统 _productManagement.SaveProduct(product); return product; }
注意事项
- 产品标识符(
Identifier)必须与外部系统的产品编号完全一致,否则订单创建时仍会出现匹配失败。 - 建议在创建前先检查产品是否存在,避免重复创建导致的数据冗余。
- 若需自定义产品属性,可扩展
ProductImportDto或产品实体类,添加业务所需字段。
内容的提问来源于stack exchange,提问作者SevenEleven
相关产品推荐
相关产品推荐

