HL7_TimeOut_Scanner.cs 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. using MediII.Adapter.ReceiveToKCModel;
  2. using MediII.Adapter.Scanner;
  3. using System;
  4. using System.Configuration;
  5. using Microsoft.Practices.EnterpriseLibrary.Data;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Data;
  10. using IL.Common;
  11. namespace MediII.Adapter.BizComponent.UE
  12. {
  13. public class HL7_TimeTask_Scanner : DBScaner
  14. {
  15. //定时任务
  16. private int TaskRunTime = 5;
  17. public HL7_TimeTask_Scanner()
  18. {
  19. var tm = ConfigurationManager.AppSettings["TaskRunTime"];
  20. if (!int.TryParse(tm, out TaskRunTime))
  21. {
  22. TaskRunTime = 5;
  23. }
  24. }
  25. protected override long OnDo(DateTime dtEventTime, Action<LogModel> setLog)
  26. {
  27. if (setLog != null)
  28. setLog(new LogModel { Message = "开始一次数据库错误消息处理扫描 " });
  29. var sql = @"
  30. SELECT *
  31. FROM HL7_Platform.dbo.HL7_Scan with(nolock)
  32. WHERE 1 = 1
  33. AND ( TaskMsg LIKE '%Timeout 时间已到。%'
  34. OR TaskMsg LIKE '%此操作对该事务的状态无效%'
  35. OR TaskMsg LIKE '%请重新运行该事务。%' )
  36. AND (ErrNum<4 or ErrNum is null)
  37. AND TaskStatus = 3
  38. AND OtherNo1 > DATEADD(dy,-{0},getdate())
  39. ORDER BY SeqNo DESC ";
  40. sql = string.Format(sql, TaskRunTime);//操作中数据
  41. try
  42. {
  43. var listRow = SqlDataAccess.ExecuteSqlStringAccessor<HL7_ScanEntity>(ctx, sql).Take(100).ToList();
  44. if (listRow.Any())
  45. {
  46. process(listRow, setLog);
  47. }
  48. }
  49. catch (Exception ex)
  50. {
  51. if (setLog != null)
  52. setLog(new LogModel { Message = "重置数据库错误消息发生错误", ErrorMsg ="sql:"+ sql+"\r\n"+ ex.Message + "\r\n" + ex.StackTrace });
  53. }
  54. return 1;
  55. }
  56. private void process(IEnumerable<HL7_ScanEntity> listRow, Action<LogModel> setLog)
  57. {
  58. listRow.ToList().ForEach(o =>
  59. {
  60. try
  61. {
  62. if (setLog != null)
  63. setLog(new LogModel { Message = "重置数据库错误消息:" , MessageID = o.TaskType });
  64. var sql = @"UPDATE dbo.HL7_Scan SET TaskStatus=1,EndTime = NULL ,
  65. IP = NULL ,
  66. MAC = NULL ,
  67. ResultTime = NULL,ErrNum=ISNULL(ErrNum,0)+1 WHERE ID=@ID";
  68. var cmd = SqlDataAccess.GetSqlStringCommand(ctx, sql);
  69. SqlDataAccess.AddInParameter(ctx, cmd, "ID", DbType.String, o.ID);
  70. SqlDataAccess.ExecuteNonQuery(ctx, cmd);
  71. }
  72. catch (Exception ex)
  73. {
  74. if (setLog != null)
  75. setLog(new LogModel
  76. {
  77. Message = string.Format("重置数据库错误消息发生错误:HL7ReceiveID:{0}", o.ID),
  78. ErrorMsg = ex.Message + "\r\n" + ex.StackTrace
  79. });
  80. }
  81. });
  82. }
  83. }
  84. }