using Store.Modules.CloudProduct.Entities; using System.Collections.Generic; using System.IO; namespace Store.Modules.CloudProduct.ImportConverters { /// /// 导入商品转换工厂:按顺序尝试各转换器,返回标准商品模型。 /// public class GoodsConverterFactory { private static readonly List _converters = new List { new PddGoodsConverter(), }; /// /// 根据本地 JSON 文件路径转换。 /// 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); } /// /// 根据 JSON 内容字符串转换。 /// 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; } } }