feat: 批量邮件记录关键词查询与近30天查重预检 #19
3
db.sql
3
db.sql
@ -1213,7 +1213,8 @@ CREATE TABLE `t_system_batch_email_record` (
|
|||||||
`UpdateUserId` INT NULL COMMENT '更新人',
|
`UpdateUserId` INT NULL COMMENT '更新人',
|
||||||
`UpdateTime` DATETIME NULL COMMENT '更新时间',
|
`UpdateTime` DATETIME NULL COMMENT '更新时间',
|
||||||
PRIMARY KEY (`Id`),
|
PRIMARY KEY (`Id`),
|
||||||
KEY `idx_batch_email_record_owner_time` (`OwnerUserId`, `SendTime`, `Id`)
|
KEY `idx_batch_email_record_owner_time` (`OwnerUserId`, `SendTime`, `Id`),
|
||||||
|
KEY `idx_batch_email_record_owner_email_subject_time` (`OwnerUserId`, `ToEmail`(128), `Subject`(191), `SendTime`)
|
||||||
) COMMENT='批量发送邮件记录';
|
) COMMENT='批量发送邮件记录';
|
||||||
|
|
||||||
CREATE TABLE `t_notice` (
|
CREATE TABLE `t_notice` (
|
||||||
|
|||||||
20
sql/migrate_batch_email_record_dup_index_20261104.sql
Normal file
20
sql/migrate_batch_email_record_dup_index_20261104.sql
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
-- 批量邮件发送记录:近 N 天同邮箱同主题查重用普通索引(禁止 UNIQUE)
|
||||||
|
-- 可重复执行
|
||||||
|
|
||||||
|
SET @idx_exists := (
|
||||||
|
SELECT COUNT(1)
|
||||||
|
FROM information_schema.statistics
|
||||||
|
WHERE table_schema = DATABASE()
|
||||||
|
AND table_name = 't_system_batch_email_record'
|
||||||
|
AND index_name = 'idx_batch_email_record_owner_email_subject_time'
|
||||||
|
);
|
||||||
|
|
||||||
|
SET @sql := IF(
|
||||||
|
@idx_exists = 0,
|
||||||
|
'ALTER TABLE `t_system_batch_email_record` ADD KEY `idx_batch_email_record_owner_email_subject_time` (`OwnerUserId`, `ToEmail`(128), `Subject`(191), `SendTime`)',
|
||||||
|
'SELECT 1'
|
||||||
|
);
|
||||||
|
|
||||||
|
PREPARE stmt FROM @sql;
|
||||||
|
EXECUTE stmt;
|
||||||
|
DEALLOCATE PREPARE stmt;
|
||||||
@ -0,0 +1,33 @@
|
|||||||
|
namespace Oms.Application.Dtos.System;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 批量邮件近 N 天重复发送预检入参。
|
||||||
|
/// </summary>
|
||||||
|
public class BatchEmailDuplicateCheckInput
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 待发送的收件邮箱 + 主题对(主题应为占位符填充后的最终主题)。
|
||||||
|
/// </summary>
|
||||||
|
public List<BatchEmailDuplicatePairInput> Pairs { get; set; } = new();
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 查重窗口天数,默认 30,服务端限制在 1–90。
|
||||||
|
/// </summary>
|
||||||
|
public int WithinDays { get; set; } = 30;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 单条待查重的邮箱 + 主题。
|
||||||
|
/// </summary>
|
||||||
|
public class BatchEmailDuplicatePairInput
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 收件邮箱。
|
||||||
|
/// </summary>
|
||||||
|
public string To { get; set; } = string.Empty;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 邮件主题(填充后)。
|
||||||
|
/// </summary>
|
||||||
|
public string Subject { get; set; } = string.Empty;
|
||||||
|
}
|
||||||
@ -0,0 +1,27 @@
|
|||||||
|
namespace Oms.Application.Dtos.System;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 近 N 天内已发送过同邮箱同主题的命中项。
|
||||||
|
/// </summary>
|
||||||
|
public class BatchEmailDuplicateHitOutput
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 收件邮箱(与历史记录中的写法一致)。
|
||||||
|
/// </summary>
|
||||||
|
public string ToEmail { get; set; } = string.Empty;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 邮件主题。
|
||||||
|
/// </summary>
|
||||||
|
public string Subject { get; set; } = string.Empty;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 最近一次发送时间。
|
||||||
|
/// </summary>
|
||||||
|
public DateTime SendTime { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 最近一次发送人。
|
||||||
|
/// </summary>
|
||||||
|
public string Sender { get; set; } = string.Empty;
|
||||||
|
}
|
||||||
@ -1,8 +1,22 @@
|
|||||||
namespace Oms.Application.Dtos.System;
|
namespace Oms.Application.Dtos.System;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 批量邮件发送记录分页查询入参。
|
||||||
|
/// </summary>
|
||||||
public class BatchEmailRecordListInput
|
public class BatchEmailRecordListInput
|
||||||
{
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 页码,从 1 开始。
|
||||||
|
/// </summary>
|
||||||
public int Page { get; set; } = 1;
|
public int Page { get; set; } = 1;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 每页条数。
|
||||||
|
/// </summary>
|
||||||
public int PageSize { get; set; } = 30;
|
public int PageSize { get; set; } = 30;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 关键词:匹配收件邮箱 / 主题 / 正文 / 发送人(模糊)。
|
||||||
|
/// </summary>
|
||||||
|
public string? Keyword { get; set; }
|
||||||
}
|
}
|
||||||
|
|||||||
@ -27,7 +27,7 @@ public class SystemBatchEmailService : ApplicationService
|
|||||||
LazyServiceProvider.GetRequiredService<ISystemBatchEmailRecordRepository>();
|
LazyServiceProvider.GetRequiredService<ISystemBatchEmailRecordRepository>();
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 查询当前用户邮件发送记录(分页)。
|
/// 查询当前用户邮件发送记录(分页)。支持关键词匹配邮箱/主题/正文/发送人。
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[HttpGet("/system/batch-email/records/list")]
|
[HttpGet("/system/batch-email/records/list")]
|
||||||
public async Task<OmsPageResult<BatchEmailRecordOutput>> GetRecordListAsync([FromQuery] BatchEmailRecordListInput input)
|
public async Task<OmsPageResult<BatchEmailRecordOutput>> GetRecordListAsync([FromQuery] BatchEmailRecordListInput input)
|
||||||
@ -35,13 +35,81 @@ public class SystemBatchEmailService : ApplicationService
|
|||||||
var ownerUserId = GetCurrentUserId();
|
var ownerUserId = GetCurrentUserId();
|
||||||
var pageIndex = OmsDocHelper.NormalizePage(input.Page);
|
var pageIndex = OmsDocHelper.NormalizePage(input.Page);
|
||||||
var pageSize = OmsDocHelper.NormalizePageSize(input.PageSize);
|
var pageSize = OmsDocHelper.NormalizePageSize(input.PageSize);
|
||||||
var (items, total) = await RecordRepository.GetPagedByOwnerAsync(ownerUserId, pageIndex, pageSize);
|
var (items, total) = await RecordRepository.GetPagedByOwnerAsync(
|
||||||
|
ownerUserId,
|
||||||
|
pageIndex,
|
||||||
|
pageSize,
|
||||||
|
input.Keyword);
|
||||||
return new OmsPageResult<BatchEmailRecordOutput>
|
return new OmsPageResult<BatchEmailRecordOutput>
|
||||||
{
|
{
|
||||||
Items = items.Select(MapRecord).ToList(),
|
Items = items.Select(MapRecord).ToList(),
|
||||||
Total = total
|
Total = total
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 预检近 N 天内是否已发送过相同收件邮箱 + 相同主题(只读,不发信)。
|
||||||
|
/// 用于前端软拦:命中后可提示用户跳过或仍要发送。
|
||||||
|
/// </summary>
|
||||||
|
[HttpPost("/system/batch-email/check-duplicates")]
|
||||||
|
public async Task<List<BatchEmailDuplicateHitOutput>> CheckDuplicatesAsync(BatchEmailDuplicateCheckInput input)
|
||||||
|
{
|
||||||
|
var pairs = (input.Pairs ?? new List<BatchEmailDuplicatePairInput>())
|
||||||
|
.Select(p => new
|
||||||
|
{
|
||||||
|
Email = (p.To ?? string.Empty).Trim(),
|
||||||
|
Subject = (p.Subject ?? string.Empty).Trim()
|
||||||
|
})
|
||||||
|
.Where(p => !string.IsNullOrWhiteSpace(p.Email) && p.Email.Contains('@'))
|
||||||
|
.GroupBy(p => $"{p.Email.ToLowerInvariant()}\u0001{p.Subject}")
|
||||||
|
.Select(g => g.First())
|
||||||
|
.ToList();
|
||||||
|
|
||||||
|
if (pairs.Count == 0)
|
||||||
|
{
|
||||||
|
return new List<BatchEmailDuplicateHitOutput>();
|
||||||
|
}
|
||||||
|
|
||||||
|
var withinDays = input.WithinDays <= 0 ? 30 : Math.Clamp(input.WithinDays, 1, 90);
|
||||||
|
var since = DateTime.Now.AddDays(-withinDays);
|
||||||
|
var ownerUserId = GetCurrentUserId();
|
||||||
|
var emails = pairs.Select(p => p.Email).Distinct(StringComparer.OrdinalIgnoreCase).ToList();
|
||||||
|
var recent = await RecordRepository.FindRecentByOwnerAndEmailsAsync(ownerUserId, emails, since);
|
||||||
|
|
||||||
|
// key: lower(email) + subject(trim) -> 最近一条
|
||||||
|
var latestByKey = new Dictionary<string, SystemBatchEmailRecord>(StringComparer.Ordinal);
|
||||||
|
foreach (var row in recent)
|
||||||
|
{
|
||||||
|
var key = BuildDuplicateKey(row.ToEmail, row.Subject);
|
||||||
|
if (!latestByKey.TryGetValue(key, out var existing) || row.SendTime > existing.SendTime)
|
||||||
|
{
|
||||||
|
latestByKey[key] = row;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var hits = new List<BatchEmailDuplicateHitOutput>();
|
||||||
|
foreach (var pair in pairs)
|
||||||
|
{
|
||||||
|
var key = BuildDuplicateKey(pair.Email, pair.Subject);
|
||||||
|
if (!latestByKey.TryGetValue(key, out var row))
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
hits.Add(new BatchEmailDuplicateHitOutput
|
||||||
|
{
|
||||||
|
ToEmail = row.ToEmail,
|
||||||
|
Subject = row.Subject,
|
||||||
|
SendTime = row.SendTime,
|
||||||
|
Sender = row.Sender
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return hits;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static string BuildDuplicateKey(string email, string subject) =>
|
||||||
|
$"{(email ?? string.Empty).Trim().ToLowerInvariant()}\u0001{(subject ?? string.Empty).Trim()}";
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 查询邮件模板列表
|
/// 查询邮件模板列表
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
@ -5,9 +5,22 @@ namespace Oms.Domain.SystemAggregate.Repositories;
|
|||||||
|
|
||||||
public interface ISystemBatchEmailRecordRepository : IRepository<SystemBatchEmailRecord>
|
public interface ISystemBatchEmailRecordRepository : IRepository<SystemBatchEmailRecord>
|
||||||
{
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 分页查询当前用户发送记录,支持关键词模糊匹配。
|
||||||
|
/// </summary>
|
||||||
Task<(List<SystemBatchEmailRecord> Items, long Total)> GetPagedByOwnerAsync(
|
Task<(List<SystemBatchEmailRecord> Items, long Total)> GetPagedByOwnerAsync(
|
||||||
int ownerUserId,
|
int ownerUserId,
|
||||||
int pageIndex,
|
int pageIndex,
|
||||||
int pageSize,
|
int pageSize,
|
||||||
|
string? keyword = null,
|
||||||
|
CancellationToken cancellationToken = default);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 在指定时间窗口内,按收件邮箱集合查出当前用户的发送记录(用于近 N 天同主题查重)。
|
||||||
|
/// </summary>
|
||||||
|
Task<List<SystemBatchEmailRecord>> FindRecentByOwnerAndEmailsAsync(
|
||||||
|
int ownerUserId,
|
||||||
|
IReadOnlyCollection<string> emails,
|
||||||
|
DateTime since,
|
||||||
CancellationToken cancellationToken = default);
|
CancellationToken cancellationToken = default);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,3 +1,4 @@
|
|||||||
|
using System.Linq.Expressions;
|
||||||
using Fake.DependencyInjection;
|
using Fake.DependencyInjection;
|
||||||
using Oms.Domain.SystemAggregate.Entities;
|
using Oms.Domain.SystemAggregate.Entities;
|
||||||
using Oms.Domain.SystemAggregate.Repositories;
|
using Oms.Domain.SystemAggregate.Repositories;
|
||||||
@ -10,15 +11,30 @@ public class SystemBatchEmailRecordRepository
|
|||||||
ISystemBatchEmailRecordRepository,
|
ISystemBatchEmailRecordRepository,
|
||||||
IScopedDependency
|
IScopedDependency
|
||||||
{
|
{
|
||||||
|
private const int EmailBatchSize = 200;
|
||||||
|
|
||||||
public async Task<(List<SystemBatchEmailRecord> Items, long Total)> GetPagedByOwnerAsync(
|
public async Task<(List<SystemBatchEmailRecord> Items, long Total)> GetPagedByOwnerAsync(
|
||||||
int ownerUserId,
|
int ownerUserId,
|
||||||
int pageIndex,
|
int pageIndex,
|
||||||
int pageSize,
|
int pageSize,
|
||||||
|
string? keyword = null,
|
||||||
CancellationToken cancellationToken = default)
|
CancellationToken cancellationToken = default)
|
||||||
{
|
{
|
||||||
var total = await CountAsync(x => x.OwnerUserId == ownerUserId, cancellationToken);
|
var kw = keyword?.Trim();
|
||||||
|
Expression<Func<SystemBatchEmailRecord, bool>> predicate = string.IsNullOrWhiteSpace(kw)
|
||||||
|
? x => x.OwnerUserId == ownerUserId
|
||||||
|
: x =>
|
||||||
|
x.OwnerUserId == ownerUserId
|
||||||
|
&& (
|
||||||
|
x.ToEmail.Contains(kw)
|
||||||
|
|| x.Subject.Contains(kw)
|
||||||
|
|| (x.Content ?? string.Empty).Contains(kw)
|
||||||
|
|| x.Sender.Contains(kw)
|
||||||
|
);
|
||||||
|
|
||||||
|
var total = await CountAsync(predicate, cancellationToken);
|
||||||
var items = await GetPagedListAsync(
|
var items = await GetPagedListAsync(
|
||||||
x => x.OwnerUserId == ownerUserId,
|
predicate,
|
||||||
pageIndex,
|
pageIndex,
|
||||||
pageSize,
|
pageSize,
|
||||||
new Dictionary<string, bool>
|
new Dictionary<string, bool>
|
||||||
@ -29,4 +45,37 @@ public class SystemBatchEmailRecordRepository
|
|||||||
cancellationToken);
|
cancellationToken);
|
||||||
return (items, total);
|
return (items, total);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public async Task<List<SystemBatchEmailRecord>> FindRecentByOwnerAndEmailsAsync(
|
||||||
|
int ownerUserId,
|
||||||
|
IReadOnlyCollection<string> emails,
|
||||||
|
DateTime since,
|
||||||
|
CancellationToken cancellationToken = default)
|
||||||
|
{
|
||||||
|
if (emails.Count == 0)
|
||||||
|
{
|
||||||
|
return new List<SystemBatchEmailRecord>();
|
||||||
|
}
|
||||||
|
|
||||||
|
var result = new List<SystemBatchEmailRecord>();
|
||||||
|
var distinct = emails
|
||||||
|
.Where(e => !string.IsNullOrWhiteSpace(e))
|
||||||
|
.Select(e => e.Trim())
|
||||||
|
.Distinct(StringComparer.OrdinalIgnoreCase)
|
||||||
|
.ToList();
|
||||||
|
|
||||||
|
for (var i = 0; i < distinct.Count; i += EmailBatchSize)
|
||||||
|
{
|
||||||
|
var batch = distinct.Skip(i).Take(EmailBatchSize).ToList();
|
||||||
|
var rows = await GetListAsync(
|
||||||
|
x => x.OwnerUserId == ownerUserId && x.SendTime >= since && batch.Contains(x.ToEmail),
|
||||||
|
cancellationToken: cancellationToken);
|
||||||
|
if (rows.Count > 0)
|
||||||
|
{
|
||||||
|
result.AddRange(rows);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user