122 lines
3.0 KiB
C#
122 lines
3.0 KiB
C#
using AllBase.Model.Result;
|
|
using AllBase.Model.Result.PDDResult;
|
|
using Newtonsoft.Json;
|
|
using Newtonsoft.Json.Linq;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
namespace TemuKJD.BLL.ImportConverters
|
|
{
|
|
/// <summary>
|
|
/// 导入商品数据转换器
|
|
/// </summary>
|
|
public interface IGoodsConverter
|
|
{
|
|
/// <summary>
|
|
/// 原始数据
|
|
/// </summary>
|
|
string RawData { get; }
|
|
|
|
/// <summary>
|
|
/// 获取转换器名称
|
|
/// </summary>
|
|
string ConverterName { get; }
|
|
|
|
/// <summary>
|
|
/// 支持的平台类型
|
|
/// </summary>
|
|
PlatformType Platform { get; }
|
|
/// <summary>
|
|
/// 读取内容
|
|
/// </summary>
|
|
/// <param name="filePath"></param>
|
|
void ReadContent(string filePath);
|
|
/// <summary>
|
|
/// 直接赋值内容
|
|
/// </summary>
|
|
/// <param name="filePath"></param>
|
|
void SetContent(string content);
|
|
/// <summary>
|
|
/// 判断是否匹配当前转换器
|
|
/// </summary>
|
|
/// <returns>是否匹配</returns>
|
|
bool IsMatch();
|
|
|
|
/// <summary>
|
|
/// 转换为标准商品模型
|
|
/// </summary>
|
|
PDDFrontProductInfo5 Convert();
|
|
}
|
|
|
|
public abstract class GoodsConverterBase : IGoodsConverter
|
|
{
|
|
private bool _isMatch = false;
|
|
public string FilePath { get; private set; }
|
|
public string RawData { get; private set; }
|
|
public abstract string ConverterName { get; }
|
|
public abstract PlatformType Platform { get; }
|
|
|
|
void IGoodsConverter.ReadContent(string filePath)
|
|
{
|
|
ReadContent(filePath);
|
|
}
|
|
void IGoodsConverter.SetContent(string content)
|
|
{
|
|
SetContent(content);
|
|
}
|
|
protected virtual void ReadContent(string filePath)
|
|
{
|
|
if (File.Exists(filePath) == false) throw new Exception("文件不存在");
|
|
FilePath = filePath;
|
|
using (StreamReader sr = new StreamReader(filePath, Ude.CharsetDetector.GetEncoding(filePath)))
|
|
{
|
|
RawData = sr.ReadToEnd();
|
|
}
|
|
}
|
|
protected virtual void SetContent(string content)
|
|
{
|
|
RawData = content;
|
|
}
|
|
|
|
public virtual bool IsMatch()
|
|
{
|
|
_isMatch = true;
|
|
return IsMatchProvider();
|
|
}
|
|
protected abstract bool IsMatchProvider();
|
|
|
|
/// <summary>
|
|
/// 带校验的转换(基类统一实现)
|
|
/// </summary>
|
|
public virtual PDDFrontProductInfo5 Convert()
|
|
{
|
|
if (_isMatch == false) throw new Exception("未校验数据");
|
|
try
|
|
{
|
|
JObject.Parse(RawData);
|
|
}
|
|
catch { throw new Exception("数据解析失败,不是有效的JSON格式"); }
|
|
|
|
|
|
var product = ConverterProvider();
|
|
if (product == null)
|
|
throw new Exception("转换失败");
|
|
// 基础校验
|
|
if (product.GoodsId == 0)
|
|
throw new Exception("商品ID不能为空");
|
|
|
|
if (product.Skus == null || !product.Skus.Any())
|
|
throw new Exception("至少需要一个SKU");
|
|
|
|
return product;
|
|
}
|
|
|
|
protected abstract PDDFrontProductInfo5 ConverterProvider();
|
|
}
|
|
}
|