ProcessAndNotifySummary.cs 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. using OASystem.API.OAMethodLib.DeepSeekAPI;
  2. using OASystem.API.OAMethodLib.Hotmail;
  3. using OASystem.API.OAMethodLib.QiYeWeChatAPI;
  4. using OASystem.Domain.ViewModels.QiYeWeChat;
  5. using System.Text.Json;
  6. using System.Text.Json.Serialization;
  7. using System.Web;
  8. using static OASystem.API.OAMethodLib.Hotmail.HotmailService;
  9. namespace OASystem.API.OAMethodLib.Quartz.Business
  10. {
  11. /// <summary>
  12. /// 获取hotmail邮件,自动总结邮件内容发送至企业微信邮件
  13. /// </summary>
  14. public static class ProcessAndNotifySummary
  15. {
  16. private static readonly SqlSugarClient _sqlSugar = AutofacIocManager.Instance.GetService<SqlSugarClient>();
  17. private static readonly IQiYeWeChatApiService _qiYeWeChatApiService = AutofacIocManager.Instance.GetService<IQiYeWeChatApiService>();
  18. private static readonly HotmailService _hotmailService = AutofacIocManager.Instance.GetService<HotmailService>();
  19. private static readonly IDeepSeekService _deepSeekService = AutofacIocManager.Instance.GetService<IDeepSeekService>();
  20. /// <summary>
  21. /// hotmail 邮件 汇总 发送企微邮件
  22. /// 时间范围 昨天
  23. /// </summary>
  24. public static async void ProcessAndNotifySummaryAsync()
  25. {
  26. var hotmailConfigs = await _hotmailService.GetUserMailConfigListAsync();
  27. if (hotmailConfigs == null || !hotmailConfigs.Any()) return;
  28. var hotmails = hotmailConfigs.Select(x => x.UserName).ToList();
  29. var userIds = hotmailConfigs.Select(x => x.UserId).ToList();
  30. var cstZone = CommonFun.GetCstZone();
  31. var nowInCst = TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow, cstZone);
  32. var yesterdayStart = nowInCst.Date.AddDays(-1);
  33. var yesterdayEnd = yesterdayStart.AddDays(1).AddTicks(-1);
  34. // 获取邮件信息
  35. var emailInfos = await _hotmailService.GetMergedMessagesAsync(hotmails, yesterdayStart, yesterdayEnd);
  36. // 处理无邮件情况
  37. if (emailInfos == null || !emailInfos.Any())
  38. {
  39. await NotifyEmptyEmails(userIds);
  40. return;
  41. }
  42. //// 预处理:限制每封邮件正文长度,防止 Token 溢出
  43. //foreach (var mail in emailInfos)
  44. //{
  45. // mail.Content = CleanHtmlToPlainText(mail.Content);
  46. //}
  47. // 调用 AI
  48. var question = BuildMailSummaryPrompt(emailInfos);
  49. var res = await _deepSeekService.ChatAsync(question);
  50. if (res.Success)
  51. {
  52. // 清洗 AI 可能带出的 Markdown 格式符
  53. string cleanJson = res.Answer.Trim();
  54. if (cleanJson.StartsWith("```json")) cleanJson = cleanJson.Replace("```json", "").Replace("```", "").Trim();
  55. try
  56. {
  57. var aiSummaryResults = JsonConvert.DeserializeObject<List<AiSummaryResult>>(cleanJson);
  58. var users = await _sqlSugar.Queryable<Sys_Users>().Where(x => x.IsDel == 0 && userIds.Contains(x.Id)).Select(x => new { x.Id, x.Email }).ToListAsync();
  59. foreach (var hotmailConfig in hotmailConfigs)
  60. {
  61. var qwEmail = users.FirstOrDefault(x => x.Id == hotmailConfig.UserId)?.Email;
  62. if (string.IsNullOrEmpty(qwEmail)) continue;
  63. if (hotmailConfig.UserName.Equals("925554512@qq.com") || hotmailConfig.UserName.Equals("Roy.Lei.Atom@hotmail.com"))
  64. {
  65. if (aiSummaryResults.Any(x => x.Recipient.Equals("925554512@qq.com")))
  66. {
  67. hotmailConfig.UserName = "925554512@qq.com";
  68. }
  69. if (aiSummaryResults.Any(x => x.Recipient.Equals("Roy.Lei.Atom@hotmail.com")))
  70. {
  71. hotmailConfig.UserName = "Roy.Lei.Atom@hotmail.com";
  72. }
  73. }
  74. // 获取 AI 为该账号生成的摘要
  75. var summary = aiSummaryResults?.FirstOrDefault(x => x.Recipient.Equals(hotmailConfig.UserName, StringComparison.OrdinalIgnoreCase));
  76. string finalSubject = $"{DateTime.Now:yyyy-MM-dd} - 邮件汇总";
  77. string finalBody = "未能获取到hotmail邮件。";
  78. if (summary != null)
  79. {
  80. finalSubject = $"[AI摘要] {summary.EmailSubject}";
  81. finalBody = summary.TextBody;
  82. }
  83. // 测试阶段默认发送在我的邮箱
  84. string defualtEmail1 = "johnny.yang@pan-american-intl.com";
  85. string defualtEmail2 = "Roy.lei@pan-american-intl.com";
  86. await _qiYeWeChatApiService.EmailSendAsync(new EmailRequestDto
  87. {
  88. ToEmails = new List<string> {
  89. qwEmail,
  90. defualtEmail1,
  91. defualtEmail2
  92. },
  93. Subject = finalSubject,
  94. Body = finalBody,
  95. });
  96. }
  97. }
  98. catch (Exception ex)
  99. {
  100. // 记录解析 JSON 失败日志
  101. }
  102. }
  103. }
  104. /// <summary>
  105. /// 纯正则实现:剔除 HTML 标签、样式和脚本,保留核心文本
  106. /// </summary>
  107. private static string CleanHtmlToPlainText(string? html)
  108. {
  109. if (string.IsNullOrEmpty(html)) return string.Empty;
  110. // 1. 剔除脚本 (Script) 和 样式 (Style) 及其内部内容
  111. html = Regex.Replace(html, @"<(script|style)[^>]*?>.*?</\1>", "", RegexOptions.IgnoreCase | RegexOptions.Singleline);
  112. // 2. 剔除所有 HTML 标签
  113. html = Regex.Replace(html, @"<[^>]*>", " ");
  114. // 3. 将 HTML 实体字符转换回普通字符 (例如 &nbsp; -> 空格, &lt; -> <)
  115. html = HttpUtility.HtmlDecode(html);
  116. // 4. 清理多余的空白字符和重复换行
  117. html = Regex.Replace(html, @"\s+", " "); // 将多个空格/换行合并为一个空格
  118. html = Regex.Replace(html, @"(\n\s*){2,}", "\n"); // 压缩重复换行
  119. return html.Trim();
  120. }
  121. public static string BuildMailSummaryPrompt(List<MailDto> mailList)
  122. {
  123. var rawDataJson = System.Text.Json.JsonSerializer.Serialize(mailList, new JsonSerializerOptions { WriteIndented = false });
  124. return $@"
  125. # Role: .NET 邮件情报分析引擎 (JSON-ONLY Mode)
  126. ## Task
  127. 解析以下 `rawDataJson` 数据,按 `Recipient` (收件人) 分组并生成深度分析简报。
  128. ## Constraints (Strict)
  129. 1. **Output Format**: 只输出标准的 JSON 数组代码块。严禁包含任何开场白、结尾问候、Markdown 解释文字或非 JSON 字符。
  130. 2. **HTML Rule**: `TextBody` 字段内仅允许使用 `<strong>` 和 `<br />`。严禁使用 `\n`、`<div>` 或其他标签。
  131. 3. **Naming Convention**:
  132. - 字典 Key/属性名: 必须使用 **PascalCase** (如: Recipient, EmailSubject, TextBody)。
  133. - 内部逻辑变量: 使用 **camelCase**。
  134. 4. **Logic**:
  135. - 按请求中的 `to` 字段进行分组。
  136. - 分析每组邮件的业务关联性,生成 [当日概览]、[详情摘要]、[分析结论]。
  137. ## Output Schema
  138. [
  139. {{
  140. ""Recipient"": ""string"",
  141. ""EmailSubject"": ""每日情报分析报告"",
  142. ""TextBody"": ""<strong>[收件人:...]</strong><br /><br /><strong>[核心概览]</strong><br />...分析内容...<br /><br /><strong>[分析结论]</strong><br />...""
  143. }}
  144. ]
  145. ## Input Data
  146. {rawDataJson}
  147. ## Execution
  148. Now, output the JSON array based on the logic above. No prose, no chat, just the JSON block.";
  149. }
  150. private static async Task NotifyEmptyEmails(List<int> userIds)
  151. {
  152. var userEmails = await _sqlSugar.Queryable<Sys_Users>().Where(x => x.IsDel == 0 && userIds.Contains(x.Id)).Select(x => x.Email).ToListAsync();
  153. if (userEmails.Any())
  154. {
  155. await _qiYeWeChatApiService.EmailSendAsync(new EmailRequestDto
  156. {
  157. ToEmails = userEmails,
  158. Subject = $"{DateTime.Now:yyyy-MM-dd} - 邮件汇总",
  159. Body = "昨日暂未收到有效邮件。"
  160. });
  161. }
  162. }
  163. public class AiSummaryResult
  164. {
  165. public string Recipient { get; set; } = string.Empty;
  166. public string EmailSubject { get; set; } = string.Empty;
  167. public string TextBody { get; set; } = string.Empty;
  168. }
  169. }
  170. }