HL7_TimeOut_Scanner.cs 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. using MediII.Adapter.ReceiveToScanModel;
  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_TimeOut_Scanner : DBScaner
  14. {
  15. private int TaskRunTime = 5;
  16. public HL7_TimeOut_Scanner()
  17. {
  18. var tm = ConfigurationManager.AppSettings["TaskRunTime"];
  19. if (!int.TryParse(tm, out TaskRunTime))
  20. {
  21. TaskRunTime = 5;
  22. }
  23. }
  24. protected override long OnDo(DateTime dtEventTime, Action<LogModel> setLog)
  25. {
  26. if (setLog != null)
  27. setLog(new LogModel { Message = "开始一次数据库错误消息处理扫描 " });
  28. var sql = @"
  29. SELECT *
  30. FROM HL7_Platform.dbo.HL7_Receive with(nolock)
  31. WHERE 1 = 1
  32. AND ( TaskMsg LIKE '%Timeout 时间已到。%'
  33. OR TaskMsg LIKE '%此操作对该事务的状态无效%'
  34. OR TaskMsg LIKE '%请重新运行该事务。%' )
  35. AND (ErrNum<4 or ErrNum is null)
  36. AND TaskStatus = 3
  37. AND ReceivingTime > DATEADD(dy,-{0},getdate())
  38. ORDER BY SeqNo DESC ";
  39. sql = string.Format(sql, TaskRunTime);//操作中数据
  40. try
  41. {
  42. var listRow = SqlDataAccess.ExecuteSqlStringAccessor<HL7_ReceiveEntity>(ctx, sql).Take(100).ToList();
  43. if (listRow.Any())
  44. {
  45. process(listRow, setLog);
  46. }
  47. }
  48. catch (Exception ex)
  49. {
  50. if (setLog != null)
  51. setLog(new LogModel { Message = "重置数据库错误消息发生错误", ErrorMsg ="sql:"+ sql+"\r\n"+ ex.Message + "\r\n" + ex.StackTrace });
  52. }
  53. return 1;
  54. }
  55. private void process(IEnumerable<HL7_ReceiveEntity> listRow, Action<LogModel> setLog)
  56. {
  57. listRow.ToList().ForEach(o =>
  58. {
  59. try
  60. {
  61. if (setLog != null)
  62. setLog(new LogModel { Message = "重置数据库错误消息:" + o.MsgContent, MessageID = o.MessageID });
  63. var sql = "UPDATE dbo.HL7_Receive SET TaskStatus=1,StartTime=NULL,TaskIP=NULL,TaskMsg = null, TaskMac=NULL,ErrNum=ISNULL(ErrNum,0)+1 WHERE ID=@ID";
  64. var cmd = SqlDataAccess.GetSqlStringCommand(ctx, sql);
  65. SqlDataAccess.AddInParameter(ctx, cmd, "ID", DbType.String, o.ID);
  66. SqlDataAccess.ExecuteNonQuery(ctx, cmd);
  67. }
  68. catch (Exception ex)
  69. {
  70. if (setLog != null)
  71. setLog(new LogModel
  72. {
  73. Message = string.Format("重置数据库错误消息发生错误:HL7ReceiveID:{0}", o.ID),
  74. ErrorMsg = ex.Message + "\r\n" + ex.StackTrace
  75. });
  76. }
  77. });
  78. }
  79. }
  80. }