DefaultDBScanner.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. using MediII.Adapter.ReceiveToScanModel;
  2. using MediII.Adapter.Scanner;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. using Microsoft.Practices.EnterpriseLibrary.Data;
  8. using MediII.Adapter.BizComponent.Base;
  9. using System.Data;
  10. using System.Diagnostics;
  11. using IL.Common;
  12. namespace MediII.Adapter.BizComponent.UE
  13. {
  14. public abstract class DefaultDBScanner : DBScaner
  15. {
  16. private int SearchNum
  17. {
  18. get
  19. {
  20. var searchNum = System.Configuration.ConfigurationManager.AppSettings["SearchNum"];
  21. int num = 0;
  22. if (!int.TryParse(searchNum, out num))
  23. {
  24. num = 10;
  25. }
  26. return num;
  27. }
  28. }
  29. protected override long OnDo(DateTime dtEventTime, Action<LogModel> setLog)
  30. {
  31. Stopwatch stopwatch = new Stopwatch();
  32. var type = GetMessageType();
  33. stopwatch.Start();
  34. try
  35. {
  36. if (setLog != null)
  37. setLog(new LogModel { Message = "开始一次消息处理扫描 消息类型:" + type });
  38. if (type == "MFN") //字典消息大于20个.一直解析
  39. {
  40. RunExec(type, setLog); //执行一次
  41. int num = 20;
  42. while (true)
  43. {
  44. var sql = @"SELECT top {0} ID FROM dbo.HL7_Receive WITH(NOLOCK) WHERE TaskStatus='1' and MessageID='MFN'";
  45. sql = string.Format(sql, num);
  46. var cmd = ctx.DBGetSqlStringCommand(sql);
  47. var dt = new DataTable();
  48. var dr = ctx.DBExecuteReader(cmd);
  49. dt.Load(dr);
  50. if (dt.Rows.Count >= num)
  51. {
  52. RunExec(type, setLog);
  53. }
  54. else
  55. {
  56. break;
  57. }
  58. }
  59. }
  60. else
  61. {
  62. RunExec(type, setLog);
  63. }
  64. }
  65. finally
  66. {
  67. stopwatch.Stop();
  68. //LogHelper.LogError(string.Format("{0}耗时{1}-->{2}", type, stopwatch.Elapsed, stopwatch.ElapsedTicks), LogCatagories.AdapterScan);
  69. }
  70. return 1;
  71. }
  72. private void RunExec(string type, Action<LogModel> setLog)
  73. {
  74. var num = SearchNum;
  75. var orderSeq = string.Empty;
  76. if (type == "MFN")
  77. {
  78. orderSeq = " Sequeue ASC,";
  79. }
  80. var sql = @"SELECT top {0} ID , SeqNo , SourceID ,MsgConID , MsgContent ,ReplyMsg , ReceivingTime , IP ,TaskStatus ,StartTime ,EndTime ,TaskIP , TaskMac ,TaskMsg ,MessageID ,MessageType ,Remark ,Sequeue FROM dbo.HL7_Receive WITH(NOLOCK) WHERE TaskStatus='1' and MessageID='{1}' ORDER BY {2} SeqNo ASC ";
  81. sql = string.Format(sql, num, type, orderSeq);
  82. try
  83. {
  84. var listRow = SqlDataAccess.ExecuteSqlStringAccessor<HL7_ReceiveEntity>(ctx, sql).Take(num).ToList();
  85. if (listRow.Count() > 0)
  86. {
  87. Process(listRow, setLog);
  88. }
  89. }
  90. catch (Exception ex)
  91. {
  92. LogHelper.LogError(ex, LogCatagories.AdapterScan);
  93. throw ex;
  94. }
  95. }
  96. private void Process(IEnumerable<HL7_ReceiveEntity> listRow, Action<LogModel> setLog)
  97. {
  98. listRow.ToList().ForEach(o =>
  99. {
  100. //验证
  101. var model = GetListRow(o.ID);
  102. if (null != model)
  103. {
  104. try
  105. {
  106. if (setLog != null)
  107. setLog(new LogModel { Message = o.MsgContent, MessageID = o.MessageID });
  108. SetStartBiz(o.ID);
  109. string mesStruct = o.MessageID;
  110. var behavior = BizComponentFactory.GetBizComponent(mesStruct);
  111. var errMsg = behavior.Process(o.MsgContent,o.MessageType);
  112. SetEndBiz(o.ID, errMsg);
  113. }
  114. catch (Exception ex)
  115. {
  116. if (setLog != null)
  117. setLog(new LogModel { Message = o.MsgContent, MessageID = o.MessageID, ErrorMsg = "消息处理错误 :" + ex.Message });
  118. SetEndBiz(o.ID, ex.Message);
  119. }
  120. }
  121. });
  122. }
  123. /// <summary>
  124. /// 获取消息类型
  125. /// </summary>
  126. /// <returns></returns>
  127. protected abstract string GetMessageType();
  128. /// <summary>
  129. /// 获取当前记录
  130. /// </summary>
  131. /// <param name="id">id</param>
  132. /// <returns></returns>
  133. protected HL7_ReceiveEntity GetListRow(string id)
  134. {
  135. var sql = "SELECT * FROM dbo.HL7_Receive WHERE TaskStatus='1' and StartTime is null and ID='" + id + "'";
  136. var model = SqlDataAccess.ExecuteSqlStringAccessor<HL7_ReceiveEntity>(ctx, sql).Take(1).FirstOrDefault();
  137. return model;
  138. }
  139. //开始时间
  140. protected int SetStartBiz(string id)
  141. {
  142. var sql = "UPDATE dbo.HL7_Receive SET StartTime=GETDATE() , TaskStatus='2',TaskIP=@TaskIP,TaskMac=@TaskMac WHERE ID=@ID";
  143. var cmd = SqlDataAccess.GetSqlStringCommand(ctx, sql);
  144. SqlDataAccess.AddInParameter(ctx, cmd, "ID", DbType.String, id);
  145. SqlDataAccess.AddInParameter(ctx, cmd, "TaskIP", DbType.String, NetHelper.GetIP());
  146. SqlDataAccess.AddInParameter(ctx, cmd, "TaskMac", DbType.String, NetHelper.GetMacAddress());
  147. return SqlDataAccess.ExecuteNonQuery(ctx, cmd);
  148. }
  149. //结束时间
  150. protected int SetEndBiz(string id, string errMsg)
  151. {
  152. var sql = "UPDATE dbo.HL7_Receive SET EndTime=GETDATE(),TaskStatus=@TaskStatus,TaskMsg=@TaskMsg WHERE ID=@ID";
  153. var cmd = SqlDataAccess.GetSqlStringCommand(ctx, sql);
  154. SqlDataAccess.AddInParameter(ctx, cmd, "ID", DbType.String, id);
  155. if (!string.IsNullOrEmpty(errMsg))
  156. {
  157. SqlDataAccess.AddInParameter(ctx, cmd, "TaskMsg", DbType.String, errMsg);
  158. SqlDataAccess.AddInParameter(ctx, cmd, "TaskStatus", DbType.Int32, 3);
  159. }
  160. else
  161. {
  162. SqlDataAccess.AddInParameter(ctx, cmd, "TaskMsg", DbType.String, DBNull.Value);
  163. SqlDataAccess.AddInParameter(ctx, cmd, "TaskStatus", DbType.Int32, 4);
  164. }
  165. return SqlDataAccess.ExecuteNonQuery(ctx, cmd);
  166. }
  167. public void DebugMsg(IEnumerable<HL7_ReceiveEntity> list)
  168. {
  169. Process(list, null);
  170. }
  171. }
  172. }