标准模板仅保留中文表头、必填标黄与枚举下拉;导入起始行改为第2行;保存自定义映射时可指定 standardTemplateKey。 Co-authored-by: Cursor <cursoragent@cursor.com>
79 lines
2.4 KiB
C#
79 lines
2.4 KiB
C#
using System.Text.Json;
|
|
using Fake.Application;
|
|
using Oms.Domain.OrdersAggregate.Entities;
|
|
|
|
namespace Oms.Application.Services.Orders.Templates;
|
|
|
|
/// <summary>
|
|
/// 解析模板导入时使用的列映射配置。
|
|
/// </summary>
|
|
public static class OrderTemplateImportMapping
|
|
{
|
|
public static void EnsureTemplateReadyForImport(OrderTemplate template)
|
|
{
|
|
if (!string.Equals(template.Type, "custom", StringComparison.OrdinalIgnoreCase))
|
|
{
|
|
return;
|
|
}
|
|
|
|
if (!string.Equals(template.MappingStatus, OrderTemplateMappingStatus.Ready, StringComparison.OrdinalIgnoreCase))
|
|
{
|
|
throw new BusinessException("自定义模板尚未完成字段映射,请先配置并保存映射");
|
|
}
|
|
|
|
if (string.IsNullOrWhiteSpace(template.ColumnMappingJson))
|
|
{
|
|
throw new BusinessException("自定义模板映射配置为空");
|
|
}
|
|
}
|
|
|
|
public static OrderTemplateColumnMappingDocument Resolve(OrderTemplate template)
|
|
{
|
|
var standardKey = template.StandardTemplateKey
|
|
?? template.TemplateKey
|
|
?? OrderTemplateKeys.DedicatedSmallParcel;
|
|
|
|
if (string.Equals(template.Type, "custom", StringComparison.OrdinalIgnoreCase))
|
|
{
|
|
return Deserialize(template.ColumnMappingJson)
|
|
?? throw new BusinessException("自定义模板映射配置无效");
|
|
}
|
|
|
|
return BuildStandard(standardKey);
|
|
}
|
|
|
|
public static OrderTemplateColumnMappingDocument BuildStandard(string templateKey)
|
|
{
|
|
var fields = OrderTemplateFieldRegistry.GetFields(templateKey);
|
|
return new OrderTemplateColumnMappingDocument
|
|
{
|
|
StandardTemplateKey = templateKey,
|
|
HeaderRowIndex = 1,
|
|
DataStartRowIndex = 2,
|
|
Mappings = fields.Select(field => new OrderTemplateColumnMappingItem
|
|
{
|
|
StandardFieldKey = field.FieldKey,
|
|
SourceColumn = field.Label,
|
|
Required = field.Required
|
|
}).ToList()
|
|
};
|
|
}
|
|
|
|
public static OrderTemplateColumnMappingDocument? Deserialize(string? json)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(json))
|
|
{
|
|
return null;
|
|
}
|
|
|
|
try
|
|
{
|
|
return JsonSerializer.Deserialize<OrderTemplateColumnMappingDocument>(json);
|
|
}
|
|
catch
|
|
{
|
|
return null;
|
|
}
|
|
}
|
|
}
|