增加
自动上报违规商品 上货自动检测违规商品 自动下架违规商品
This commit is contained in:
1 parent
a319af0e91
commit
d5e38dd518
9 files changed
+308
-19
No files matched your search
@@ -8,6 +8,7 @@ using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Work.Api.Wx;
|
||||
|
||||
namespace Work.Services
|
||||
{
|
||||
@@ -48,28 +49,31 @@ namespace Work.Services
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 在后台解析微信处罚 JSON 并上报;无商品或处理失败时返回空列表,异常在内部记录。
|
||||
/// 在后台解析微信处罚 JSON、匹配店铺商品编码并上报,异常在内部记录。
|
||||
/// </summary>
|
||||
public static void ReportViolationsFromJson(string json)
|
||||
public static void ReportViolationsFromJson(string json, ShopLoginInfo shop)
|
||||
{
|
||||
try
|
||||
{
|
||||
Task.Run(() => ReportViolationsFromJsonCore(json)).ConfigureAwait(false);
|
||||
}
|
||||
catch (Exception ex)
|
||||
_ = Task.Run(async () =>
|
||||
{
|
||||
try
|
||||
{
|
||||
LogSave.AddFile("解析并上报违规商品失败:" + ex.Message);
|
||||
await ReportViolationsFromJsonCore(json, shop).ConfigureAwait(false);
|
||||
}
|
||||
catch
|
||||
catch (Exception ex)
|
||||
{
|
||||
// 日志失败也不能影响调用方。
|
||||
try
|
||||
{
|
||||
LogSave.AddFile("解析并上报违规商品失败:" + ex.Message);
|
||||
}
|
||||
catch
|
||||
{
|
||||
// 日志失败也不能影响调用方。
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private static async Task<List<ReportGoodsViolationResult>> ReportViolationsFromJsonCore(string json)
|
||||
private static async Task<List<ReportGoodsViolationResult>> ReportViolationsFromJsonCore(string json, ShopLoginInfo shop)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(json))
|
||||
{
|
||||
@@ -96,6 +100,8 @@ namespace Work.Services
|
||||
throw new ArgumentException("处罚数据缺少 record.items 数组。", nameof(json));
|
||||
}
|
||||
|
||||
if (records.Count == 0)
|
||||
return new List<ReportGoodsViolationResult>();
|
||||
var items = new List<ReportGoodsViolationItem>();
|
||||
foreach (var token in records)
|
||||
{
|
||||
@@ -138,12 +144,147 @@ namespace Work.Services
|
||||
}
|
||||
}
|
||||
|
||||
var reports = items.GroupBy(item => item.GoodsId).Select(group => new ReportGoodsViolationItem
|
||||
if (items.Count == 0)
|
||||
{
|
||||
return new List<ReportGoodsViolationResult>();
|
||||
}
|
||||
var productCodes = GetWxProductCodes(shop, items.Select(item => item.GoodsId));
|
||||
var mappedItems = new List<ReportGoodsViolationItem>();
|
||||
foreach (var item in items)
|
||||
{
|
||||
if (!productCodes.TryGetValue(item.GoodsId, out var code) || string.IsNullOrWhiteSpace(code))
|
||||
{
|
||||
LogSave.AddFile($"跳过违规商品 {item.GoodsId}:未找到对应的 spuCode。");
|
||||
continue;
|
||||
}
|
||||
if (!TryGetGoodsId(code, out var goodsId))
|
||||
{
|
||||
LogSave.AddFile($"跳过违规商品 {item.GoodsId}:spuCode 无法转换为有效商品编号。");
|
||||
continue;
|
||||
}
|
||||
mappedItems.Add(new ReportGoodsViolationItem { GoodsId = goodsId, Reasons = item.Reasons });
|
||||
}
|
||||
var reports = mappedItems.GroupBy(item => item.GoodsId).Select(group => new ReportGoodsViolationItem
|
||||
{
|
||||
GoodsId = group.Key,
|
||||
Reasons = string.Join(";", group.Select(item => item.Reasons).Distinct())
|
||||
}).ToList();
|
||||
return reports.Count == 0 ? new List<ReportGoodsViolationResult>() : await ReportViolations(reports);
|
||||
|
||||
return reports.Count == 0
|
||||
? new List<ReportGoodsViolationResult>() : await ReportViolations(reports);
|
||||
}
|
||||
|
||||
private static bool TryGetGoodsId(string code, out long goodsId)
|
||||
{
|
||||
code = code?.Trim() ?? string.Empty;
|
||||
if (code.StartsWith("P1-", StringComparison.Ordinal))
|
||||
{
|
||||
code = code.Substring(3);
|
||||
}
|
||||
else if (code.StartsWith("EC1", StringComparison.Ordinal) && code.EndsWith("A", StringComparison.Ordinal))
|
||||
{
|
||||
code = code.Substring(3, code.Length - 4);
|
||||
}
|
||||
return long.TryParse(code, out goodsId) && goodsId > 0;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 后台检查当前店铺所有在售商品,云端确认违规后立即下架;异常仅记录日志。
|
||||
/// </summary>
|
||||
public static void CheckAndOffShelfViolations(ShopLoginInfo shop)
|
||||
{
|
||||
_ = Task.Run(async () =>
|
||||
{
|
||||
try
|
||||
{
|
||||
await CheckAndOffShelfViolationsCore(shop).ConfigureAwait(false);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
try
|
||||
{
|
||||
LogSave.AddFile($"店铺 {shop?.ShopId} 在售商品违规检查失败:{ex.Message}");
|
||||
}
|
||||
catch { }
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private static async Task CheckAndOffShelfViolationsCore(ShopLoginInfo shop)
|
||||
{
|
||||
// 先读取完整列表,避免下架改变分页位置而漏查后面的商品。
|
||||
var products = GetWxProductCodes(shop, status: 5);
|
||||
var mappings = new Dictionary<long, long>();
|
||||
foreach (var product in products)
|
||||
{
|
||||
if (TryGetGoodsId(product.Value, out var goodsId))
|
||||
{
|
||||
mappings.Add(product.Key, goodsId);
|
||||
}
|
||||
else
|
||||
{
|
||||
LogSave.AddFile($"店铺 {shop.ShopId} 跳过商品 {product.Key}:spuCode 无法转换为有效商品编号。");
|
||||
}
|
||||
}
|
||||
var goodsIds = mappings.Values.Distinct().ToList();
|
||||
for (var offset = 0; offset < goodsIds.Count; offset += 50)
|
||||
{
|
||||
var batch = goodsIds.Skip(offset).Take(50).ToList();
|
||||
var statuses = await GetViolationStatus(batch).ConfigureAwait(false);
|
||||
var violations = new HashSet<long>(statuses.Where(item => item != null && item.IsViolation && batch.Contains(item.GoodsId))
|
||||
.Select(item => item.GoodsId));
|
||||
var productIds = mappings.Where(item => violations.Contains(item.Value)).Select(item => item.Key).ToList();
|
||||
// 同一云商品可能对应多个微信商品,每次下架最多 50 个。
|
||||
for (var index = 0; index < productIds.Count; index += 50)
|
||||
{
|
||||
var ids = string.Join(",", productIds.Skip(index).Take(50));
|
||||
var response = JObject.Parse(WxProductApi.DownProd(shop, ids));
|
||||
if (response["code"]?.ToString() != "0")
|
||||
{
|
||||
throw new Exception($"微信商品 {ids} 下架失败:{response}");
|
||||
}
|
||||
LogSave.AddFile($"店铺 {shop.ShopId} 云端检测违规,已下架微信商品:{ids}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static Dictionary<long, string> GetWxProductCodes(ShopLoginInfo shop, IEnumerable<long> productIds = null, int status = -1)
|
||||
{
|
||||
if (shop == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(shop));
|
||||
}
|
||||
var codes = new Dictionary<long, string>();
|
||||
var prodIds = productIds == null ? null : string.Join(",", productIds.Distinct());
|
||||
long received = 0;
|
||||
for (var page = 1; ; page++)
|
||||
{
|
||||
var response = JObject.Parse(WxProductApi.SearchProduct(shop, page, pageSize: 50, prodIds: prodIds, status: status));
|
||||
if (response["ret"]?.ToString() != "0" || response["success"]?.Type != JTokenType.Boolean
|
||||
|| !(bool)response["success"] || !(response["productList"] is JArray products)
|
||||
|| !long.TryParse(response["totalNum"]?.ToString(), out var total) || total < 0)
|
||||
{
|
||||
throw new Exception($"获取微信商品列表第 {page} 页失败。");
|
||||
}
|
||||
var previousCount = codes.Count;
|
||||
foreach (var product in products)
|
||||
{
|
||||
if (!(product is JObject item) || !long.TryParse(item["productId"]?.ToString(), out var id) || id <= 0)
|
||||
{
|
||||
throw new Exception("微信商品列表包含无效的 productId。");
|
||||
}
|
||||
codes[id] = item["spuCode"]?.Type == JTokenType.String ? (string)item["spuCode"] : null;
|
||||
}
|
||||
received += products.Count;
|
||||
if (received >= total)
|
||||
{
|
||||
return codes;
|
||||
}
|
||||
if (codes.Count == previousCount)
|
||||
{
|
||||
throw new Exception("微信商品列表分页未返回新商品,无法获取全部搜索结果。");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
Reference in new issue
Block a user