Files
StoreWeb/Plugins/CloudProduct/ImportConverters/GoodsConverterFactory.cs
T

55 lines
1.3 KiB
C#

using Store.Modules.CloudProduct.Entities;
using System.Collections.Generic;
using System.IO;
namespace Store.Modules.CloudProduct.ImportConverters
{
/// <summary>
/// 导入商品转换工厂:按顺序尝试各转换器,返回标准商品模型。
/// </summary>
public class GoodsConverterFactory
{
private static readonly List<IGoodsConverter> _converters = new List<IGoodsConverter>
{
new PddGoodsConverter(),
};
/// <summary>
/// 根据本地 JSON 文件路径转换。
/// </summary>
public static GoodsEntity? Convert(string filePath)
{
if (string.IsNullOrWhiteSpace(filePath))
throw new ArgumentNullException(nameof(filePath));
if (!File.Exists(filePath))
throw new FileNotFoundException("找不到导入文件", filePath);
var content = File.ReadAllText(filePath);
return ConvertContent(content);
}
/// <summary>
/// 根据 JSON 内容字符串转换。
/// </summary>
public static GoodsEntity? ConvertContent(string content)
{
if (string.IsNullOrWhiteSpace(content))
throw new ArgumentNullException(nameof(content));
foreach (var converter in _converters)
{
converter.ReadContent(content);
if (converter.IsMatch())
{
var goods = converter.Convert();
if (goods != null)
return goods;
}
}
return null;
}
}
}