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