云端数据 增加违规检测日志

This commit is contained in:
chfei committed 2026-11-02 17:19:49 +08:00
1 parent 1fa91a9cfb
commit 207696d417
7 files changed
+173 -10

No files matched your search

@@ -2,6 +2,7 @@ using MAX.Core.DependencyInjection;
using MAX.Framework.Commons;
using MAX.Framework.Dtos;
using Microsoft.Extensions.Logging;
using Store.Modules.CloudProduct.Entities;
using Store.Modules.CloudProduct.Dtos;
using Store.Modules.CloudProduct.Repositorys;
@@ -56,13 +57,53 @@ public class GoodsViolationService(
}).ToList();
}
/// <summary>整批校验后统一写入日志,每个事件保留一条记录。</summary>
public async Task RecordLogsAsync(RecordViolationLogRequest[]? items)
{
if (items == null || items.Length is < 1 or > 1000)
throw new AppException("每次请提供1-1000条违规日志", ResultCode.ValidationError);
var logs = new GoodsViolationLogEntity[items.Length];
var now = DateTime.Now;
for (var i = 0; i < items.Length; i++)
{
var item = items[i];
if (item == null || string.IsNullOrWhiteSpace(item.ShopId) || item.ShopId.Trim().Length > 64
|| item.GoodsId <= 0 || item.ReasonId <= 0
|| item.Action is not (ViolationLogAction.Report or ViolationLogAction.AutoListingExcluded or ViolationLogAction.AutoOffShelf))
throw new AppException($"第{i + 1}条日志须提供有效店铺编号、商品编号、违规类型和动作(1、2或3)", ResultCode.ValidationError);
logs[i] = new GoodsViolationLogEntity
{
ShopId = item.ShopId.Trim(), GoodsId = item.GoodsId, ReasonId = item.ReasonId,
Action = item.Action, CreatedTime = now
};
}
var reasonIds = logs.Select(x => (uint)x.ReasonId).Distinct().ToArray();
if (await repository.CountReasonsAsync(reasonIds) != reasonIds.Length)
throw new AppException("违规类型不存在,请从违规字典中选择", ResultCode.ValidationError);
await repository.InsertLogsAsync(logs);
}
/// <summary>分页读取违规事件,beforeId 为上一页最后一条日志编号。</summary>
public Task<List<GoodsViolationLogEntity>> GetLogsAsync(string[]? shopIds, long? goodsId, long beforeId, int size)
{
if (shopIds == null || shopIds.Length is < 1 or > 1000
|| shopIds.Any(id => string.IsNullOrWhiteSpace(id) || id.Trim().Length > 64)
|| goodsId <= 0 || beforeId < 0 || size is < 1 or > 200)
throw new AppException("请提供1-1000个有效商铺编号、有效的商品及分页参数,每页允许1-200条", ResultCode.ValidationError);
return repository.GetLogsAsync(shopIds.Select(id => id.Trim()).Distinct().ToArray(), goodsId, beforeId, size);
}
public async Task<List<GoodsViolationStatus>> GetStatusAsync(long[]? ids)
{
if (ids == null || ids.Length is < 1 or > 1000 || ids.Any(x => x <= 0))
throw new AppException("请提供1-1000个正数商品编号", ResultCode.ValidationError);
var distinct = ids.Distinct().ToArray();
var violations = (await repository.GetViolationIdsAsync(distinct)).ToHashSet();
return distinct.Select(x => new GoodsViolationStatus { GoodsId = x, IsViolation = violations.Contains(x) }).ToList();
var reasons = await repository.GetReasonIdsByGoodsAsync(distinct);
return distinct.Select(id => new GoodsViolationStatus
{
GoodsId = id, IsViolation = reasons.Contains(id), ReasonIds = reasons[id].ToArray()
}).ToList();
}
public async Task<RemoveGoodsViolationsResult> RemoveAsync(long[]? ids)