userIds, FlowNotification notification)
{
if (userIds.Count == 0) return FlowNotifyPushResult.Ok(0);
var users = await _userRep.AsQueryable()
.Where(u => userIds.Contains(u.Id) && !string.IsNullOrEmpty(u.Email))
.Select(u => new { u.Id, u.Email, u.RealName })
.ToListAsync();
if (users.Count == 0) return FlowNotifyPushResult.Ok(0);
var html = $"{System.Net.WebUtility.HtmlEncode(notification.Content)}
" +
$"
该邮件由系统自动发送,请勿直接回复。实例 Id:{notification.InstanceId}
";
var errors = new List();
var successCount = 0;
foreach (var user in users)
{
try
{
await _emailService.SendEmail(html, notification.Title, user.Email);
successCount++;
}
catch (Exception ex)
{
errors.Add($"[{user.Id}]{ex.Message}");
}
}
if (successCount > 0 && errors.Count == 0)
return FlowNotifyPushResult.Ok(successCount);
if (successCount > 0)
return new FlowNotifyPushResult
{
Success = true,
ActualTargetCount = successCount,
ErrorMessage = $"部分失败: {string.Join("; ", errors)}"
};
return FlowNotifyPushResult.Fail(string.Join("; ", errors));
}
}