This commit is contained in:
chfei committed 2026-09-18 18:57:24 +08:00
1 parent f2caec6083
commit 75d0e85218
16 files changed
+316 -320

No files matched your search

@@ -1,12 +1,6 @@
using Store.Modules.CloudProduct.Entities;
using Store.Modules.CloudProduct.Entities;
using Store.Modules.CloudProduct.Enums;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.Json.Nodes;
using System.Threading.Tasks;
namespace Store.Modules.CloudProduct.ImportConverters
{
@@ -16,7 +10,7 @@ namespace Store.Modules.CloudProduct.ImportConverters
public interface IGoodsConverter
{
/// <summary>
/// 原始数据
/// 原始数据(JSON 原文)
/// </summary>
string RawData { get; }
@@ -29,11 +23,13 @@ namespace Store.Modules.CloudProduct.ImportConverters
/// 支持的平台类型
/// </summary>
PlatformType Platform { get; }
/// <summary>
/// 读取内容
/// </summary>
/// <param name="content"></param>
/// <param name="content">JSON 原文内容</param>
void ReadContent(string content);
/// <summary>
/// 判断是否匹配当前转换器
/// </summary>
@@ -43,37 +39,47 @@ namespace Store.Modules.CloudProduct.ImportConverters
/// <summary>
/// 转换为标准商品模型
/// </summary>
ProductEntity Convert();
GoodsEntity Convert();
}
public abstract class GoodsConverterBase : IGoodsConverter
/// <summary>
/// 转换器基类:统一实现读取、校验与转换骨架,子类只需实现匹配判定与字段映射。
/// </summary>
public abstract class GoodsConverter : IGoodsConverter
{
private bool _isMatch = false;
public string RawData { get; private set; }
public string RawData { get; private set; } = null!;
public abstract string ConverterName { get; }
public abstract PlatformType Platform { get; }
void IGoodsConverter.ReadContent(string filePath)
void IGoodsConverter.ReadContent(string content)
{
ReadContent(filePath);
}
protected virtual void ReadContent(string filePath)
{
RawData = filePath;
ReadContent(content);
}
protected virtual void ReadContent(string content)
{
RawData = content;
}
public virtual bool IsMatch()
{
_isMatch = true;
return IsMatchProvider();
return IsMatchCore();
}
protected abstract bool IsMatchProvider();
/// <summary>
/// 由子类实现的具体匹配判定
/// </summary>
protected abstract bool IsMatchCore();
/// <summary>
/// 带校验的转换(基类统一实现)
/// </summary>
public virtual ProductEntity Convert()
public virtual GoodsEntity Convert()
{
if (_isMatch == false) throw new Exception("未校验数据");
try
@@ -82,18 +88,20 @@ namespace Store.Modules.CloudProduct.ImportConverters
}
catch { throw new Exception("数据解析失败,不是有效的JSON格式"); }
var product = ConverterProvider();
if (product == null)
var goods = ConvertCore();
if (goods == null)
throw new Exception("转换失败");
// 基础校验
if (product.GoodsId == 0)
if (goods.GoodsId == 0)
throw new Exception("商品ID不能为空");
return product;
return goods;
}
protected abstract ProductEntity ConverterProvider();
/// <summary>
/// 由子类实现的具体字段映射
/// </summary>
protected abstract GoodsEntity ConvertCore();
}
}
@@ -1,27 +1,23 @@
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
public class GoodsConverterFactory
{
private static readonly List<IGoodsConverter> _converters = new List<IGoodsConverter>
{
new ConverterV1(),
new PddGoodsConverter(),
};
/// <summary>
/// 根据本地 JSON 文件路径转换。
/// </summary>
public static ProductEntity? Convert(string filePath)
public static GoodsEntity? Convert(string filePath)
{
if (string.IsNullOrWhiteSpace(filePath))
throw new ArgumentNullException(nameof(filePath));
@@ -29,24 +25,24 @@ namespace Store.Modules.CloudProduct.ImportConverters
if (!File.Exists(filePath))
throw new FileNotFoundException("找不到导入文件", filePath);
string content = File.ReadAllText(filePath);
var content = File.ReadAllText(filePath);
return ConvertContent(content);
}
/// <summary>
/// 根据 JSON 内容字符串转换。
/// </summary>
public static ProductEntity? ConvertContent(string content)
public static GoodsEntity? ConvertContent(string content)
{
if (string.IsNullOrWhiteSpace(content))
throw new ArgumentNullException(nameof(content));
foreach (IGoodsConverter item in _converters)
foreach (var converter in _converters)
{
item.ReadContent(content);
if (item.IsMatch())
converter.ReadContent(content);
if (converter.IsMatch())
{
var goods = item.Convert();
var goods = converter.Convert();
if (goods != null)
return goods;
}
@@ -55,4 +51,4 @@ namespace Store.Modules.CloudProduct.ImportConverters
return null;
}
}
}
}
@@ -1,9 +1,5 @@
using Store.Modules.CloudProduct.Entities;
using Store.Modules.CloudProduct.Enums;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.Json.Nodes;
namespace Store.Modules.CloudProduct.ImportConverters
@@ -11,13 +7,13 @@ namespace Store.Modules.CloudProduct.ImportConverters
/// <summary>
/// PDD 前端商品详情数据转换器(数据源格式见 data.txt)
/// </summary>
internal class ConverterV1 : GoodsConverterBase
internal class PddGoodsConverter : GoodsConverter
{
public override string ConverterName => "ConverterV1";
public override string ConverterName => nameof(PddGoodsConverter);
public override PlatformType Platform => PlatformType.PDD;
protected override bool IsMatchProvider()
protected override bool IsMatchCore()
{
try
{
@@ -40,7 +36,7 @@ namespace Store.Modules.CloudProduct.ImportConverters
}
}
protected override ProductEntity ConverterProvider()
protected override GoodsEntity ConvertCore()
{
if (JsonNode.Parse(RawData) is not JsonObject root)
throw new Exception("无法解析 PDD 商品数据");
@@ -48,7 +44,7 @@ namespace Store.Modules.CloudProduct.ImportConverters
var goodsNode = root["goods"] as JsonObject ?? root;
var priceNode = root["price"] as JsonObject;
var goods = new ProductEntity
var goods = new GoodsEntity
{
Platform = (byte)PlatformType.PDD,
GoodsId = GetLong(goodsNode, "goods_id"),
@@ -177,4 +173,4 @@ namespace Store.Modules.CloudProduct.ImportConverters
return (null, null);
}
}
}
}