SendOnlineOutTimeJob.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. using Dapper;
  2. using OfficeOpenXml;
  3. using OfficeOpenXml.Table;
  4. using Quartz;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.IO;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. using XYY.Core.Standard.Data.Infrastructure;
  12. using XYY.Common.Standard;
  13. namespace XYY.Tool.TimingTask.jobs
  14. {
  15. public class SendOnlineOutTimeJob : IJob
  16. {
  17. public async Task Execute(IJobExecutionContext context)
  18. {
  19. try
  20. {
  21. string sql = @"select
  22. c.TrackingNumber[运单号],
  23. a.ClearanceTime[清关时间],
  24. d.TargetTime[轨迹最后更新时间],
  25. d.LastGetTime[轨迹最后拉取时间],
  26. d.LastMessage[最后轨迹内容],
  27. a.LadingBillNumber[提单号],
  28. e.PublicName[服务渠道],
  29. g.Name[下单渠道],
  30. d.Status[状态]
  31. from Logistics_LadingBill(nolock)a
  32. join Logistics_Box(nolock)b on a.LadingBillNumber=b.LadingBillNumber
  33. join Logistics_BoxDetail(nolock)c on c.BoxId=b.Id
  34. join Logistics_Trace(Nolock)d on d.OrderId=c.OrderId
  35. join Logistics_Channel(nolock) e on e.Id = d.ChannelId
  36. join Logistics_Channel(nolock) f on f.id = e.PublicChanneId
  37. join Logistics_Public(nolock)g on g.Code=f.PublicCode
  38. where a.ClearanceTime is not null
  39. and d.OnlineTime is null and DATEDIFF(HOUR, a.ClearanceTime,getdate())>48
  40. and DATEDIFF(MONTH,a.ClearanceTime,getdate())<2
  41. ";
  42. string excelPath = System.IO.Path.Combine(
  43. System.AppContext.BaseDirectory, $"行运易上网异常监控-{DateTime.Now.ToString_yyyyMMdd()}.xlsx");
  44. using (var conn = ConnectionHelper.BuildDbConnection("Server=rm-wz96o72qd45w3d6mvyo.sqlserver.rds.aliyuncs.com,3433;Pooling=true;Max Pool Size=40000;Min Pool Size=0;initial catalog=xyy_wms;uid=xyy;pwd=SsAGn%SK*Ydjeibw;"))
  45. {
  46. conn.Open();
  47. ExcelPackage.LicenseContext = LicenseContext.NonCommercial;
  48. var data = conn.Query<OutModel>(sql, null);
  49. FileStream existingFile = new FileStream(excelPath, FileMode.Create);
  50. using (existingFile)
  51. {
  52. using (ExcelPackage package = new ExcelPackage(existingFile))
  53. {
  54. ExcelWorksheet worksheetIn = package.Workbook.Worksheets.Add("Sheet1");
  55. // 第二参数为true 则会把 lstData定义的属性名称作为excel标题
  56. worksheetIn.Cells.LoadFromCollection(data, true, TableStyles.Medium9);
  57. worksheetIn.Cells[worksheetIn.Dimension.Address].AutoFitColumns();
  58. package.Save(); //Save the workbook.
  59. }
  60. }
  61. }
  62. //发送文件
  63. var hook = new QYWXWebHook("https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=804910e8-c06b-46ec-a460-7e63cc1a9d89");
  64. string mid = await hook.PostFile(excelPath);
  65. hook.SendFile(mid);
  66. }
  67. catch (Exception ex)
  68. {
  69. Console.WriteLine(ex.Message);
  70. }
  71. }
  72. }
  73. public class OutModel
  74. {
  75. public string 下单渠道
  76. {
  77. get; set;
  78. }
  79. public string 状态
  80. {
  81. get; set;
  82. }
  83. public string 服务渠道
  84. {
  85. get; set;
  86. }
  87. public string 运单号
  88. {
  89. get; set;
  90. }
  91. public string 清关时间
  92. {
  93. get; set;
  94. }
  95. public string 轨迹最后更新时间
  96. {
  97. get; set;
  98. }
  99. public string 轨迹最后拉取时间
  100. {
  101. get; set;
  102. }
  103. public string 最后轨迹内容
  104. {
  105. get; set;
  106. }
  107. public string 提单号
  108. {
  109. get; set;
  110. }
  111. }
  112. }