WeeklyFridayJob.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. using Quartz;
  2. using OASystem.API.OAMethodLib.QiYeWeChatAPI;
  3. using OASystem.Domain.ViewModels.QiYeWeChat;
  4. using Newtonsoft.Json;
  5. using Newtonsoft.Json.Linq;
  6. using Microsoft.AspNetCore.Http;
  7. namespace OASystem.API.OAMethodLib.Quartz.Jobs
  8. {
  9. /// <summary>
  10. /// 每周五下午4点执行的定时任务
  11. /// </summary>
  12. public class WeeklyFridayJob : IJob
  13. {
  14. private readonly ILogger<WeeklyFridayJob> _logger;
  15. private readonly SqlSugarClient _sqlSugar;
  16. private readonly IQiYeWeChatApiService _qiYeWeChatApiService;
  17. private readonly HttpClient _httpClient;
  18. public WeeklyFridayJob(ILogger<WeeklyFridayJob> logger, SqlSugarClient sqlSugar
  19. , IHttpClientFactory httpClientFactory, IQiYeWeChatApiService qiYeWeChatApiService)
  20. {
  21. _logger = logger;
  22. _sqlSugar = sqlSugar;
  23. _httpClient = httpClientFactory.CreateClient();
  24. _qiYeWeChatApiService = qiYeWeChatApiService;
  25. _httpClient.Timeout = TimeSpan.FromMinutes(10);
  26. }
  27. public async Task Execute(IJobExecutionContext context)
  28. {
  29. var jobName = context.JobDetail.Key.Name;
  30. _logger.LogInformation($"开始执行任务 {jobName},时间:{DateTime.Now:yyyy-MM-dd HH:mm:ss}");
  31. try
  32. {
  33. var startDate = DateTime.Today.AddDays(-4);
  34. var endDate = DateTime.Today.AddDays(1);
  35. // startDate = new DateTime(2026, 3, 16);
  36. // endDate = new DateTime(2026, 3, 21);
  37. string baseUrl = "http://132.232.92.186:8888";//"http://localhost:5256";
  38. var createUserId = 4;
  39. var sqSetting = _sqlSugar.Queryable<Sys_SetData>().First(x => x.Id == 1417);
  40. if (sqSetting != null)
  41. {
  42. Dictionary<int, List<int>> result = JsonConvert.DeserializeObject<Dictionary<int, List<int>>>(sqSetting.Remark);
  43. result.Add(235, new List<int> { 258 });
  44. foreach (var item in result.Keys)
  45. {
  46. var keyUser = await _sqlSugar.Queryable<Sys_Users>()
  47. .FirstAsync(x => x.Id == item);
  48. if (keyUser == null || string.IsNullOrEmpty(keyUser.Email))
  49. {
  50. _logger.LogWarning($"管理者(ID:{item})不存在或未配置邮箱,跳过发送汇总邮件。");
  51. continue;
  52. }
  53. var users = await _sqlSugar.Queryable<Sys_Users>()
  54. .LeftJoin<Sys_Department>((u, d) => u.DepId == d.Id)
  55. .LeftJoin<Sys_JobPost>((u, d, jp) => u.JobPostId == jp.Id)
  56. .Where((u, d, jp) => u.IsDel == 0 && result[item].Contains(u.Id) && u.Id != item)
  57. .Select((u, d, jp) => new { u.Id, u.CnName, DepName = d.DepName ?? "", JobName = jp.JobName ?? "" })
  58. .ToListAsync();
  59. // 用于收集该管理者下所有员工的附件
  60. var attachments = new List<IFormFile>();
  61. foreach (var user in users)
  62. {
  63. // 构建API URL
  64. string apiUrl = $"{baseUrl}/api/PersonnelModule/AiPerformanceAnalysis_AllDepartment";
  65. // 构建查询参数
  66. var queryParams = new Dictionary<string, string>
  67. {
  68. { "userId", user.Id.ToString() },
  69. { "start", startDate.ToString("yyyy-MM-dd HH:mm:ss") },
  70. { "end", endDate.ToString("yyyy-MM-dd HH:mm:ss") },
  71. { "createUserId", createUserId.ToString() },
  72. { "isMonthData", "true" }
  73. };
  74. // 构建完整URL
  75. var queryString = string.Join("&", queryParams.Select(kvp => $"{kvp.Key}={Uri.EscapeDataString(kvp.Value)}"));
  76. string fullUrl = $"{apiUrl}?{queryString}";
  77. _logger.LogInformation($"正在为用户 {user.CnName}(ID:{user.Id}, 部门:{user.DepName}) 生成绩效数据...");
  78. // 发送HTTP GET请求
  79. var response = await _httpClient.GetAsync(fullUrl);
  80. var responseContent = await response.Content.ReadAsStringAsync();
  81. if (response.IsSuccessStatusCode)
  82. {
  83. try
  84. {
  85. var resultJson = JsonConvert.DeserializeObject<JsonView>(responseContent);
  86. if (resultJson != null && resultJson.Code == 200)
  87. {
  88. _logger.LogInformation($"用户 {user.CnName}(ID:{user.Id}, 部门:{user.DepName}) 绩效数据生成成功");
  89. //生成PDF
  90. var url = $"{baseUrl}/api/PersonnelModule/AiPerformanceAnalysis_JobMarketingFileDown";
  91. var queryParamsPdf = new Dictionary<string, string>
  92. {
  93. { "userId", user.Id.ToString() },
  94. { "start", startDate.ToString("yyyy-MM-dd") },
  95. { "end", endDate.ToString("yyyy-MM-dd") },
  96. };
  97. var queryStringPdf = string.Join("&", queryParamsPdf.Select(kvp => $"{kvp.Key}={Uri.EscapeDataString(kvp.Value)}"));
  98. url = $"{url}?{queryStringPdf}";
  99. _logger.LogInformation($"用户 {user.CnName}(ID:{user.Id}, 部门:{user.DepName}) 开始生成PDF:{url}");
  100. var pdfUrl = await _httpClient.PostAsync(url, new StringContent(queryStringPdf));
  101. if (pdfUrl.IsSuccessStatusCode)
  102. {
  103. var pdfContent = await pdfUrl.Content.ReadAsStringAsync();
  104. var pdfContentJson = JObject.Parse(pdfContent);
  105. var pdfUrlString = pdfContentJson["data"]["url"].ToString();
  106. var pdfUrlSeed = new Uri(pdfUrlString);
  107. var pdfBytes = await _httpClient.GetByteArrayAsync(pdfUrlSeed);
  108. // 提取文件名并重命名以包含员工姓名,方便管理者识别
  109. var originalFileName = pdfUrlString.Split('/').Last();
  110. var fileName = $"{originalFileName}";
  111. var pdfFile = new FormFile(new MemoryStream(pdfBytes), 0, pdfBytes.Length, fileName, fileName);
  112. _logger.LogInformation($"用户 {user.CnName}(ID:{user.Id}) PDF生成成功,大小:{pdfFile.Length},已加入待发送列表");
  113. // 将附件添加到列表
  114. attachments.Add(pdfFile);
  115. }
  116. else
  117. {
  118. _logger.LogWarning($"用户 {user.CnName}(ID:{user.Id}) 生成PDF失败:{pdfUrl.StatusCode}");
  119. }
  120. }
  121. else
  122. {
  123. _logger.LogWarning($"用户 {user.CnName}(ID:{user.Id}) 绩效数据生成失败:{resultJson?.Msg ?? "未知错误"}");
  124. }
  125. }
  126. catch (Exception ex)
  127. {
  128. _logger.LogError(ex, $"处理用户 {user.CnName}(ID:{user.Id}) 时发生异常");
  129. }
  130. }
  131. else
  132. {
  133. _logger.LogError($"用户 {user.CnName}(ID:{user.Id}) 绩效数据生成API调用失败,状态码: {response.StatusCode}");
  134. }
  135. // 添加延迟,避免请求过于频繁
  136. await Task.Delay(3000);
  137. }
  138. // 如果该管理者有附件,则一次性发送一封汇总邮件
  139. if (attachments.Any())
  140. {
  141. _logger.LogInformation($"正在为管理者 {keyUser.CnName} 发送汇总邮件,附件数量:{attachments.Count}");
  142. var resultEmail = await _qiYeWeChatApiService.EmailSendAsync(new EmailRequestDto
  143. {
  144. ToEmails = new List<string> { keyUser.Email },
  145. Subject = $"员工绩效数据汇总({startDate:yyyy-MM-dd} 至 {endDate:yyyy-MM-dd})",
  146. Body = $"您好,附件中包含您负责的 {attachments.Count} 位员工的周绩效报告,请查收。",
  147. Files = attachments.ToArray()
  148. });
  149. _logger.LogInformation($"管理者 {keyUser.CnName} 汇总邮件发送结果:{resultEmail.errcode}, {resultEmail.errmsg}");
  150. }
  151. else
  152. {
  153. _logger.LogWarning($"管理者 {keyUser.CnName} 没有生成的绩效附件,跳过发送邮件。");
  154. }
  155. }
  156. }
  157. }
  158. catch (Exception ex)
  159. {
  160. _logger.LogError(ex, $"任务 {jobName} 执行失败");
  161. }
  162. }
  163. }
  164. }