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