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
{
///
/// 导入商品数据转换器
///
public interface IGoodsConverter
{
///
/// 原始数据
///
string RawData { get; }
///
/// 获取转换器名称
///
string ConverterName { get; }
///
/// 支持的平台类型
///
PlatformType Platform { get; }
///
/// 读取内容
///
///
void ReadContent(string content);
///
/// 判断是否匹配当前转换器
///
/// 是否匹配
bool IsMatch();
///
/// 转换为标准商品模型
///
ProductEntity Convert();
}
public abstract class GoodsConverterBase : IGoodsConverter
{
private bool _isMatch = false;
public string RawData { get; private set; }
public abstract string ConverterName { get; }
public abstract PlatformType Platform { get; }
void IGoodsConverter.ReadContent(string filePath)
{
ReadContent(filePath);
}
protected virtual void ReadContent(string filePath)
{
RawData = filePath;
}
public virtual bool IsMatch()
{
_isMatch = true;
return IsMatchProvider();
}
protected abstract bool IsMatchProvider();
///
/// 带校验的转换(基类统一实现)
///
public virtual ProductEntity Convert()
{
if (_isMatch == false) throw new Exception("未校验数据");
try
{
JsonNode.Parse(RawData);
}
catch { throw new Exception("数据解析失败,不是有效的JSON格式"); }
var product = ConverterProvider();
if (product == null)
throw new Exception("转换失败");
// 基础校验
if (product.GoodsId == 0)
throw new Exception("商品ID不能为空");
return product;
}
protected abstract ProductEntity ConverterProvider();
}
}