外部导入pdd格式。增加 格式工厂和 IgoodsConverter转换器
This commit is contained in:
1 parent
99237fae41
commit
7a07e172cd
9 files changed
+989
-1
No files matched your search
@@ -0,0 +1,38 @@
|
||||
using AllBase.Model.Result.PDDResult;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using TemuKJD.BLL.ImportConverters;
|
||||
|
||||
namespace TemuKJD.BLL.ImportConverts
|
||||
{
|
||||
public class ConverterToPddFactory
|
||||
{
|
||||
private static List<IGoodsConverter> _converters = new List<IGoodsConverter>
|
||||
{
|
||||
new ConverterToPddV5(),
|
||||
};
|
||||
public static PDDFrontProductInfo5 Convert(string data)
|
||||
{
|
||||
|
||||
foreach (var item in _converters)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (item.IsMatch(data))
|
||||
{
|
||||
return item.Convert();
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw ex;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,315 @@
|
||||
using AllBase.Model.Result.PDDResult;
|
||||
using CefSharp.DevTools.CSS;
|
||||
using NaiBa.Util.Extension;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using TemuKJD.BLL.ImportConverters;
|
||||
using Work.GoodDetail;
|
||||
|
||||
namespace TemuKJD.BLL.ImportConverts
|
||||
{
|
||||
internal class ConverterToPddV5 : GoodsConverterBase
|
||||
{
|
||||
private JObject jsonRoot;
|
||||
|
||||
|
||||
public override string ConverterName => "To PDD V5";
|
||||
|
||||
public override PlatformType Platform => PlatformType.PDD;
|
||||
|
||||
protected override bool IsMatchProvider()
|
||||
{
|
||||
try
|
||||
{
|
||||
jsonRoot = JsonConvert.DeserializeObject<JObject>(RawData);
|
||||
return jsonRoot.SelectToken("$.store.initDataObj.goods") != null;
|
||||
}
|
||||
catch { return false; }
|
||||
}
|
||||
|
||||
protected override PDDFrontProductInfo5 ConverterProvider()
|
||||
{
|
||||
var goodsToken = jsonRoot.SelectToken("$.store.initDataObj.goods");
|
||||
var product = new PDDFrontProductInfo5();
|
||||
|
||||
// 逐个属性赋值,方便打断点调试
|
||||
product.GoodsId = goodsToken["goodsID"].Value<long>();
|
||||
|
||||
product.GoodsUrl = "";
|
||||
|
||||
product.BrandId = goodsToken["brandId"].ParseToLong();
|
||||
|
||||
product.Title = goodsToken["goodsName"]?.Value<string>();
|
||||
|
||||
product.Description = goodsToken["short_name"]?.Value<string>() ?? "";
|
||||
|
||||
product.ThumbUrl = goodsToken["thumbUrl"]?.Value<string>() ?? "";
|
||||
|
||||
product.CategoryId = goodsToken["catID"]?.Value<int>() ?? 0;
|
||||
|
||||
product.CategoryPath = GetCategoryPath(goodsToken);
|
||||
|
||||
product.MallId = goodsToken["mallID"]?.Value<int>() ?? 0;
|
||||
|
||||
product.Price = goodsToken["linePrice"]?.Value<decimal>() ?? 0;
|
||||
|
||||
product.Propertys = GetProperties(goodsToken);
|
||||
|
||||
product.MainImages = GetMainImages(goodsToken);
|
||||
|
||||
product.DetailImages = GetDetailImages(goodsToken);
|
||||
|
||||
product.Videos = GetVideos(goodsToken);
|
||||
|
||||
product.Specs = GetSpecs(goodsToken);
|
||||
|
||||
product.Skus = GetSkus(goodsToken);
|
||||
|
||||
return product;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取类目路径
|
||||
/// </summary>
|
||||
private List<int> GetCategoryPath(JToken goods)
|
||||
{
|
||||
var result = new List<int>();
|
||||
|
||||
// JSON 中有 catID1 ~ catID4
|
||||
for (int i = 1; i <= 4; i++)
|
||||
{
|
||||
var token = goods[$"catID{i}"];
|
||||
|
||||
if (token != null && token.Type != JTokenType.Null)
|
||||
{
|
||||
var value = token.Value<int>();
|
||||
|
||||
if (value > 0)
|
||||
result.Add(value);
|
||||
}
|
||||
}
|
||||
|
||||
// 如果没有,则至少加入当前类目
|
||||
if (result.Count == 0)
|
||||
{
|
||||
var categoryId = goods["catID"]?.Value<int>() ?? 0;
|
||||
|
||||
if (categoryId > 0)
|
||||
result.Add(categoryId);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
/// <summary>
|
||||
/// 商品属性
|
||||
/// </summary>
|
||||
private List<Property> GetProperties(JToken goods)
|
||||
{
|
||||
var result = new List<Property>();
|
||||
|
||||
var propertyArray = goods["goodsProperty"] as JArray;
|
||||
|
||||
if (propertyArray == null)
|
||||
return result;
|
||||
|
||||
foreach (var item in propertyArray)
|
||||
{
|
||||
var key = item["key"]?.Value<string>();
|
||||
|
||||
var values = item["values"] as JArray;
|
||||
|
||||
result.Add(new Property
|
||||
{
|
||||
Key = key ?? "",
|
||||
Value = string.Join(",", values?.Select(a => a))
|
||||
});
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
/// <summary>
|
||||
/// 主图
|
||||
/// </summary>
|
||||
private List<string> GetMainImages(JToken goods)
|
||||
{
|
||||
var result = new List<string>();
|
||||
|
||||
var images = goods["topGallery"] as JArray;
|
||||
|
||||
if (images != null)
|
||||
{
|
||||
foreach (var item in images)
|
||||
{
|
||||
var url = item["url"]?.Value<string>();
|
||||
|
||||
if (!string.IsNullOrEmpty(url))
|
||||
result.Add(url);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
images = goods["viewImageData"] as JArray;
|
||||
if (images != null)
|
||||
{
|
||||
foreach (var item in images)
|
||||
{
|
||||
var url = item.ToString();
|
||||
|
||||
if (!string.IsNullOrEmpty(url))
|
||||
result.Add(url);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
/// <summary>
|
||||
/// 详情图
|
||||
/// </summary>
|
||||
private List<string> GetDetailImages(JToken goods)
|
||||
{
|
||||
var result = new List<string>();
|
||||
|
||||
var images = goods["detailGallery"] as JArray;
|
||||
|
||||
if (images == null)
|
||||
return result;
|
||||
|
||||
foreach (var item in images)
|
||||
{
|
||||
var url = item["url"]?.Value<string>();
|
||||
|
||||
if (!string.IsNullOrEmpty(url))
|
||||
result.Add(url);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
/// <summary>
|
||||
/// 商品视频
|
||||
/// </summary>
|
||||
private List<string> GetVideos(JToken goods)
|
||||
{
|
||||
var result = new List<string>();
|
||||
|
||||
var videos = goods["videoGallery"] as JArray;
|
||||
|
||||
if (videos == null)
|
||||
return result;
|
||||
|
||||
foreach (var item in videos)
|
||||
{
|
||||
var url =
|
||||
item["videoUrl"]?.Value<string>() ??
|
||||
item["url"]?.Value<string>();
|
||||
|
||||
if (!string.IsNullOrEmpty(url))
|
||||
result.Add(url);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取规格
|
||||
/// </summary>
|
||||
private List<SpecItem> GetSpecs(JToken goods)
|
||||
{
|
||||
var result = new List<SpecItem>();
|
||||
|
||||
var skus = goods["skus"] as JArray;
|
||||
|
||||
if (skus == null)
|
||||
return result;
|
||||
|
||||
foreach (var sku in skus)
|
||||
{
|
||||
var specs = sku["specs"] as JArray;
|
||||
|
||||
if (specs == null)
|
||||
continue;
|
||||
|
||||
foreach (var spec in specs)
|
||||
{
|
||||
var specKey = spec["spec_key"]?.Value<string>() ?? "";
|
||||
var specValue = spec["spec_value"]?.Value<string>() ?? "";
|
||||
var specKeyId = spec["spec_key_id"]?.Value<long>() ?? 0;
|
||||
var specValueId = spec["spec_value_id"]?.Value<long>() ?? 0;
|
||||
|
||||
// 去重
|
||||
if (result.Any(x => x.SpecKey == specKey && x.SpecValue == specValue))
|
||||
continue;
|
||||
|
||||
result.Add(new SpecItem
|
||||
{
|
||||
SpecKey = specKey,
|
||||
SpecValue = specValue,
|
||||
SpecKeyId = specKeyId,
|
||||
SpecValueId = specValueId,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
/// <summary>
|
||||
/// SKU
|
||||
/// </summary>
|
||||
private List<SkuItem> GetSkus(JToken goods)
|
||||
{
|
||||
var result = new List<SkuItem>();
|
||||
|
||||
var skus = goods["skus"] as JArray;
|
||||
|
||||
if (skus == null)
|
||||
return result;
|
||||
|
||||
foreach (var sku in skus)
|
||||
{
|
||||
var specs = sku["specs"] as JArray;
|
||||
|
||||
var specValues = new List<string>();
|
||||
|
||||
if (specs != null)
|
||||
{
|
||||
foreach (var spec in specs)
|
||||
{
|
||||
var key = spec["spec_key"]?.Value<string>();
|
||||
var value = spec["spec_value"]?.Value<string>();
|
||||
|
||||
if (!string.IsNullOrEmpty(value))
|
||||
specValues.Add($"{key}:{value}");
|
||||
}
|
||||
}
|
||||
|
||||
// 优先使用 normalPrice
|
||||
var price = sku["normalPrice"].Value<decimal>();
|
||||
|
||||
result.Add(new SkuItem
|
||||
{
|
||||
SkuId = sku["skuId"]?.Value<string>() ?? "",
|
||||
|
||||
Spec = string.Join(";", specValues),
|
||||
|
||||
Price = price,
|
||||
|
||||
// oldGroupPrice 在这个 JSON 中是整数分价格,
|
||||
// 这里暂时转成字符串
|
||||
OriginalPrice = sku["oldGroupPrice"]?.Value<string>() ?? "",
|
||||
|
||||
Stock = sku["quantity"]?.Value<int>() ?? 0,
|
||||
|
||||
SpecImage = sku["thumbUrl"]?.Value<string>() ?? "",
|
||||
});
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,90 @@
|
||||
using AllBase.Model.Result.PDDResult;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
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>
|
||||
/// <returns>是否匹配</returns>
|
||||
bool IsMatch(string data);
|
||||
|
||||
/// <summary>
|
||||
/// 转换为标准商品模型
|
||||
/// </summary>
|
||||
PDDFrontProductInfo5 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; }
|
||||
|
||||
|
||||
public virtual bool IsMatch(string data)
|
||||
{
|
||||
_isMatch = true;
|
||||
RawData = data;
|
||||
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();
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user