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

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

@@ -13,13 +13,19 @@ public class GoodsViolationRepository(
public Task<List<ViolationReasonDto>> GetReasonsAsync() => Orm.Select<ViolationReasonEntity>()
.OrderBy(x => x.Id).ToListAsync(x => new ViolationReasonDto
{
Name = x.Name,
Id = x.Id, Name = x.Name,
IsViolation = x.IsViolation
});
public Task<List<long>> GetViolationIdsAsync(long[] ids) => Select
.Where(x => ids.Contains(x.GoodsId)).Distinct().ToListAsync(x => x.GoodsId);
public async Task<ILookup<long, int>> GetReasonIdsByGoodsAsync(long[] ids)
{
var rows = await Select.Where(x => ids.Contains(x.GoodsId)).ToListAsync(x => new { x.GoodsId, x.ReasonId });
return rows.ToLookup(x => x.GoodsId, x => checked((int)x.ReasonId));
}
public async Task<List<long>> GetOffShelfGoodsIdsAsync(long[] ids)
{
var records = await Select.Where(x => ids.Contains(x.GoodsId)).ToListAsync();
@@ -54,25 +60,25 @@ public class GoodsViolationRepository(
using var unit = Orm.CreateUnitOfWork();
var repository = unit.GetRepository<GoodsViolationEntity>();
var reasonRepository = unit.GetRepository<ViolationReasonEntity>();
var reasonIds = new Dictionary<string, uint>(StringComparer.Ordinal);
var dictionary = new Dictionary<string, ViolationReasonEntity>(StringComparer.Ordinal);
foreach (var name in items.Values.SelectMany(x => x).Distinct(StringComparer.Ordinal))
{
// 让数据库按字典表的排序规则匹配名称,与唯一索引保持一致。
var reason = await reasonRepository.Select.Where(x => x.Name == name).FirstAsync();
// 新类型只收录到字典,绝不因首次上报自动启用商品封禁。
reason ??= await reasonRepository.InsertAsync(new ViolationReasonEntity { Name = name, IsViolation = false });
// 数据库排序规则可能忽略部分字符差异;判定时仍要求名称完整相等。
if (reason.IsViolation && string.Equals(reason.Name, name, StringComparison.Ordinal))
reasonIds.Add(name, reason.Id);
dictionary.Add(name, reason);
}
var ids = items.Keys.ToArray();
var existing = (await repository.Select.Where(x => ids.Contains(x.GoodsId))
.ToListAsync()).ToLookup(x => x.GoodsId);
var now = DateTime.UtcNow;
var now = DateTime.Now;
var recordedIds = new List<long>();
foreach (var (goodsId, reasons) in items)
{
var accepted = reasons.Where(reasonIds.ContainsKey).Select(x => reasonIds[x]).Distinct().ToArray();
// 判定要求名称完整匹配。
var accepted = reasons.Where(name => dictionary[name].IsViolation && dictionary[name].Name == name)
.Select(name => dictionary[name].Id).Distinct().ToArray();
// 忽略原因仅记录字典,不新增或改变该商品已有的违规状态。
if (accepted.Length == 0) continue;
var rows = existing[goodsId].ToArray();
@@ -101,9 +107,26 @@ public class GoodsViolationRepository(
return recordedIds.ToArray();
}
public Task<long> CountReasonsAsync(uint[] ids) => Orm.Select<ViolationReasonEntity>()
.Where(x => ids.Contains(x.Id)).CountAsync();
public async Task InsertLogsAsync(GoodsViolationLogEntity[] logs)
{
// 即使 ORM 拆分 INSERT,各批次也在同一事务内提交。
using var unit = Orm.CreateUnitOfWork();
await unit.GetRepository<GoodsViolationLogEntity>().InsertAsync(logs);
unit.Commit();
}
// 游标按自增主键倒序翻页,同一时间的事件也有确定顺序。
public Task<List<GoodsViolationLogEntity>> GetLogsAsync(string[] shopIds, long? goodsId, long beforeId, int size)
=> Orm.Select<GoodsViolationLogEntity>().Where(x => shopIds.Contains(x.ShopId!))
.WhereIf(goodsId.HasValue, x => x.GoodsId == goodsId)
.WhereIf(beforeId > 0, x => x.Id < beforeId).OrderByDescending(x => x.Id).Limit(size).ToListAsync();
public Task<int> MarkDeletedAsync(long[] ids) => Orm.Update<GoodsViolationEntity>()
.Set(x => x.IsIndexDeleted, true)
.Set(x => x.UpdatedTime, DateTime.UtcNow)
.Set(x => x.UpdatedTime, DateTime.Now)
.Where(x => ids.Contains(x.GoodsId)).ExecuteAffrowsAsync();
public Task<List<long>> GetBatchAsync(long afterId, int size, bool pendingOnly) => Select