diff --git a/db.sql b/db.sql
index c8867a0..5f981c5 100644
--- a/db.sql
+++ b/db.sql
@@ -1213,7 +1213,8 @@ CREATE TABLE `t_system_batch_email_record` (
`UpdateUserId` INT NULL COMMENT '更新人',
`UpdateTime` DATETIME NULL COMMENT '更新时间',
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='批量发送邮件记录';
CREATE TABLE `t_notice` (
diff --git a/sql/migrate_batch_email_record_dup_index_20261104.sql b/sql/migrate_batch_email_record_dup_index_20261104.sql
new file mode 100644
index 0000000..219cef6
--- /dev/null
+++ b/sql/migrate_batch_email_record_dup_index_20261104.sql
@@ -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;
diff --git a/src/Oms.Application/Dtos/System/BatchEmailDuplicateCheckInput.cs b/src/Oms.Application/Dtos/System/BatchEmailDuplicateCheckInput.cs
new file mode 100644
index 0000000..f6f1fba
--- /dev/null
+++ b/src/Oms.Application/Dtos/System/BatchEmailDuplicateCheckInput.cs
@@ -0,0 +1,33 @@
+namespace Oms.Application.Dtos.System;
+
+///
+/// 批量邮件近 N 天重复发送预检入参。
+///
+public class BatchEmailDuplicateCheckInput
+{
+ ///
+ /// 待发送的收件邮箱 + 主题对(主题应为占位符填充后的最终主题)。
+ ///
+ public List Pairs { get; set; } = new();
+
+ ///
+ /// 查重窗口天数,默认 30,服务端限制在 1–90。
+ ///
+ public int WithinDays { get; set; } = 30;
+}
+
+///
+/// 单条待查重的邮箱 + 主题。
+///
+public class BatchEmailDuplicatePairInput
+{
+ ///
+ /// 收件邮箱。
+ ///
+ public string To { get; set; } = string.Empty;
+
+ ///
+ /// 邮件主题(填充后)。
+ ///
+ public string Subject { get; set; } = string.Empty;
+}
diff --git a/src/Oms.Application/Dtos/System/BatchEmailDuplicateHitOutput.cs b/src/Oms.Application/Dtos/System/BatchEmailDuplicateHitOutput.cs
new file mode 100644
index 0000000..b0c3a4a
--- /dev/null
+++ b/src/Oms.Application/Dtos/System/BatchEmailDuplicateHitOutput.cs
@@ -0,0 +1,27 @@
+namespace Oms.Application.Dtos.System;
+
+///
+/// 近 N 天内已发送过同邮箱同主题的命中项。
+///
+public class BatchEmailDuplicateHitOutput
+{
+ ///
+ /// 收件邮箱(与历史记录中的写法一致)。
+ ///
+ public string ToEmail { get; set; } = string.Empty;
+
+ ///
+ /// 邮件主题。
+ ///
+ public string Subject { get; set; } = string.Empty;
+
+ ///
+ /// 最近一次发送时间。
+ ///
+ public DateTime SendTime { get; set; }
+
+ ///
+ /// 最近一次发送人。
+ ///
+ public string Sender { get; set; } = string.Empty;
+}
diff --git a/src/Oms.Application/Dtos/System/BatchEmailRecordListInput.cs b/src/Oms.Application/Dtos/System/BatchEmailRecordListInput.cs
index efb72de..9745011 100644
--- a/src/Oms.Application/Dtos/System/BatchEmailRecordListInput.cs
+++ b/src/Oms.Application/Dtos/System/BatchEmailRecordListInput.cs
@@ -1,8 +1,22 @@
namespace Oms.Application.Dtos.System;
+///
+/// 批量邮件发送记录分页查询入参。
+///
public class BatchEmailRecordListInput
{
+ ///
+ /// 页码,从 1 开始。
+ ///
public int Page { get; set; } = 1;
+ ///
+ /// 每页条数。
+ ///
public int PageSize { get; set; } = 30;
+
+ ///
+ /// 关键词:匹配收件邮箱 / 主题 / 正文 / 发送人(模糊)。
+ ///
+ public string? Keyword { get; set; }
}
diff --git a/src/Oms.Application/Services/System/SystemBatchEmailService.cs b/src/Oms.Application/Services/System/SystemBatchEmailService.cs
index d0b8dd0..e5061e8 100644
--- a/src/Oms.Application/Services/System/SystemBatchEmailService.cs
+++ b/src/Oms.Application/Services/System/SystemBatchEmailService.cs
@@ -27,7 +27,7 @@ public class SystemBatchEmailService : ApplicationService
LazyServiceProvider.GetRequiredService();
///
- /// 查询当前用户邮件发送记录(分页)。
+ /// 查询当前用户邮件发送记录(分页)。支持关键词匹配邮箱/主题/正文/发送人。
///
[HttpGet("/system/batch-email/records/list")]
public async Task> GetRecordListAsync([FromQuery] BatchEmailRecordListInput input)
@@ -35,13 +35,81 @@ public class SystemBatchEmailService : ApplicationService
var ownerUserId = GetCurrentUserId();
var pageIndex = OmsDocHelper.NormalizePage(input.Page);
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
{
Items = items.Select(MapRecord).ToList(),
Total = total
};
}
+
+ ///
+ /// 预检近 N 天内是否已发送过相同收件邮箱 + 相同主题(只读,不发信)。
+ /// 用于前端软拦:命中后可提示用户跳过或仍要发送。
+ ///
+ [HttpPost("/system/batch-email/check-duplicates")]
+ public async Task> CheckDuplicatesAsync(BatchEmailDuplicateCheckInput input)
+ {
+ var pairs = (input.Pairs ?? new List())
+ .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();
+ }
+
+ 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(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();
+ 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()}";
///
/// 查询邮件模板列表
///
diff --git a/src/Oms.Domain/SystemAggregate/Repositories/ISystemBatchEmailRecordRepository.cs b/src/Oms.Domain/SystemAggregate/Repositories/ISystemBatchEmailRecordRepository.cs
index 95eb3d2..f4b645f 100644
--- a/src/Oms.Domain/SystemAggregate/Repositories/ISystemBatchEmailRecordRepository.cs
+++ b/src/Oms.Domain/SystemAggregate/Repositories/ISystemBatchEmailRecordRepository.cs
@@ -5,9 +5,22 @@ namespace Oms.Domain.SystemAggregate.Repositories;
public interface ISystemBatchEmailRecordRepository : IRepository
{
+ ///
+ /// 分页查询当前用户发送记录,支持关键词模糊匹配。
+ ///
Task<(List Items, long Total)> GetPagedByOwnerAsync(
int ownerUserId,
int pageIndex,
int pageSize,
+ string? keyword = null,
+ CancellationToken cancellationToken = default);
+
+ ///
+ /// 在指定时间窗口内,按收件邮箱集合查出当前用户的发送记录(用于近 N 天同主题查重)。
+ ///
+ Task> FindRecentByOwnerAndEmailsAsync(
+ int ownerUserId,
+ IReadOnlyCollection emails,
+ DateTime since,
CancellationToken cancellationToken = default);
}
diff --git a/src/Oms.SqlSugarCore/System/SystemBatchEmailRecordRepository.cs b/src/Oms.SqlSugarCore/System/SystemBatchEmailRecordRepository.cs
index be225d5..5b35623 100644
--- a/src/Oms.SqlSugarCore/System/SystemBatchEmailRecordRepository.cs
+++ b/src/Oms.SqlSugarCore/System/SystemBatchEmailRecordRepository.cs
@@ -1,3 +1,4 @@
+using System.Linq.Expressions;
using Fake.DependencyInjection;
using Oms.Domain.SystemAggregate.Entities;
using Oms.Domain.SystemAggregate.Repositories;
@@ -10,15 +11,30 @@ public class SystemBatchEmailRecordRepository
ISystemBatchEmailRecordRepository,
IScopedDependency
{
+ private const int EmailBatchSize = 200;
+
public async Task<(List Items, long Total)> GetPagedByOwnerAsync(
int ownerUserId,
int pageIndex,
int pageSize,
+ string? keyword = null,
CancellationToken cancellationToken = default)
{
- var total = await CountAsync(x => x.OwnerUserId == ownerUserId, cancellationToken);
+ var kw = keyword?.Trim();
+ Expression> 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(
- x => x.OwnerUserId == ownerUserId,
+ predicate,
pageIndex,
pageSize,
new Dictionary
@@ -29,4 +45,37 @@ public class SystemBatchEmailRecordRepository
cancellationToken);
return (items, total);
}
+
+ public async Task> FindRecentByOwnerAndEmailsAsync(
+ int ownerUserId,
+ IReadOnlyCollection emails,
+ DateTime since,
+ CancellationToken cancellationToken = default)
+ {
+ if (emails.Count == 0)
+ {
+ return new List();
+ }
+
+ var result = new List();
+ 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;
+ }
}