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 { /// /// 导入商品转换工厂:按顺序尝试各转换器,返回标准商品模型。 /// public class ConverterToGoodsFactory { private static readonly List _converters = new List { new ConverterV1(), }; /// /// 根据本地 JSON 文件路径转换。 /// 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); } /// /// 根据 JSON 内容字符串转换。 /// 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; } } }