DefaultDBScanner.cs 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. using MediII.Adapter.ReceiveToKCModel;
  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 NHapi.Base.Parser;
  9. using MediII.Adapter.BizComponent.Base;
  10. using System.Data;
  11. using System.Diagnostics;
  12. using IL.Common;
  13. namespace MediII.Adapter.BizComponent.UE
  14. {
  15. public abstract class DefaultDBScanner : DBScaner
  16. {
  17. private int SearchNum
  18. {
  19. get
  20. {
  21. var searchNum = System.Configuration.ConfigurationManager.AppSettings["SearchNum"];
  22. int num = 0;
  23. if (!int.TryParse(searchNum, out num))
  24. {
  25. num = 10;
  26. }
  27. return num;
  28. }
  29. }
  30. /// <summary>
  31. /// 门诊定时类型,0=正常;>0 隔N天
  32. /// </summary>
  33. private int OutSearchType
  34. {
  35. get
  36. {
  37. var type = System.Configuration.ConfigurationManager.AppSettings["OutSearchType"];
  38. int rtn = 0;
  39. if (!int.TryParse(type, out rtn))
  40. {
  41. rtn = 0;
  42. }
  43. return rtn;
  44. }
  45. }
  46. private int DFTSearchType
  47. {
  48. get
  49. {
  50. var type = System.Configuration.ConfigurationManager.AppSettings["DFTSearchType"];
  51. int rtn = 30;
  52. if (!int.TryParse(type, out rtn))
  53. {
  54. rtn = 30;
  55. }
  56. return rtn;
  57. }
  58. }
  59. /// <summary>
  60. /// 住院定时类型,0=正常;>0 隔N天
  61. /// </summary>
  62. private int InSearchType
  63. {
  64. get
  65. {
  66. var type = System.Configuration.ConfigurationManager.AppSettings["InSearchType"];
  67. int rtn = 0;
  68. if (!int.TryParse(type, out rtn))
  69. {
  70. rtn = 0;
  71. }
  72. return rtn;
  73. }
  74. }
  75. protected override long OnDo(DateTime dtEventTime, Action<LogModel> setLog)
  76. {
  77. Stopwatch stopwatch = new Stopwatch();
  78. var type = GetMessageType();
  79. stopwatch.Start();
  80. try
  81. {
  82. if (setLog != null)
  83. setLog(new LogModel { Message = "开始一次消息处理扫描 消息类型:" + type });
  84. if (type == "DFT^B01") //字典解析
  85. {
  86. var sql = @" SELECT ID,SeqNo,ChartNo,VisitNo,OtherNo1,OtherNo2,OtherNo3,OtherName,TaskType,TaskStatus,
  87. UpdateUser,UpdateTime,Reserve1,Reserve2,Reserve3,IP,MAC,BeginTime,EndTime,Result,TaskMsg,ResultTime
  88. FROM dbo.HL7_Scan WITH(NOLOCK)
  89. WHERE TaskStatus=1 AND TaskType='{0}' AND OtherNo1 <=(GETDATE()-{1})
  90. ORDER BY SeqNo ASC";
  91. sql = string.Format(sql, type, DFTSearchType);
  92. try
  93. {
  94. var listRow = SqlDataAccess.ExecuteSqlStringAccessor<HL7_ScanEntity>(ctx, sql).Take(1).ToList();
  95. if (listRow.Count() > 0)
  96. {
  97. Process(listRow, setLog, type);
  98. }
  99. }
  100. catch (Exception ex)
  101. {
  102. LogHelper.LogError(ex, LogCatagories.AdapterScan);
  103. throw ex;
  104. }
  105. }
  106. else
  107. {
  108. RunExec(type, setLog);
  109. }
  110. }
  111. finally
  112. {
  113. stopwatch.Stop();
  114. //LogHelper.LogError(string.Format("{0}耗时{1}-->{2}", type, stopwatch.Elapsed, stopwatch.ElapsedTicks), LogCatagories.AdapterScan);
  115. }
  116. return 1;
  117. }
  118. private void RunExec(string type, Action<LogModel> setLog)
  119. {
  120. var num = SearchNum;
  121. var orderSeq = string.Empty;
  122. var timeSeq = string.Empty;
  123. if (type == "MFN")
  124. {
  125. orderSeq = " Sequeue ASC,";
  126. }
  127. if (type.Substring(0, 7) == "ADT^A01" || type.Substring(0, 7) == "ADT^A02")
  128. {
  129. if (InSearchType > 0)
  130. {
  131. timeSeq = string.Format(" AND OtherNo1 <=(GETDATE()-{0})", InSearchType);
  132. }
  133. }
  134. if (type.Substring(0, 7) == "ADT^A04" || type.Substring(0, 7) == "ADT^A05")
  135. {
  136. if (OutSearchType > 0)
  137. {
  138. timeSeq = string.Format(" AND OtherNo1 <=(GETDATE()-{0})", OutSearchType);
  139. }
  140. }
  141. //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 ";
  142. var sql = @" SELECT TOP {0} ID,SeqNo,ChartNo,VisitNo,OtherNo1,OtherNo2,OtherNo3,OtherName,TaskType,TaskStatus,
  143. UpdateUser,UpdateTime,Reserve1,Reserve2,Reserve3,IP,MAC,BeginTime,EndTime,Result,TaskMsg,ResultTime
  144. FROM dbo.HL7_Scan WITH(NOLOCK)
  145. WHERE TaskStatus=1 AND IP IS null AND MAC IS NULL AND TaskType='{1}' {3}
  146. ORDER BY {2} SeqNo ASC";
  147. sql = string.Format(sql, num, type, orderSeq, timeSeq);
  148. try
  149. {
  150. var listRow = SqlDataAccess.ExecuteSqlStringAccessor<HL7_ScanEntity>(ctx, sql).Take(num).ToList();
  151. if (listRow.Count() > 0)
  152. {
  153. Process(listRow, setLog, type);
  154. }
  155. }
  156. catch (Exception ex)
  157. {
  158. LogHelper.LogError(ex, LogCatagories.AdapterScan);
  159. throw ex;
  160. }
  161. }
  162. private void Process(IEnumerable<HL7_ScanEntity> listRow, Action<LogModel> setLog,string msgType)
  163. {
  164. listRow.ToList().ForEach(o =>
  165. {
  166. //验证
  167. var model = GetListRow(o.ID);
  168. if (null != model)
  169. {
  170. try
  171. {
  172. if (setLog != null)
  173. setLog(new LogModel { Message = o.VisitNo, MessageID = o.TaskType });
  174. SetStartBiz(o.ID);
  175. //PipeParser parser = new PipeParser();
  176. //string mesStruct = parser.GetMessageStructure(o.MsgContent).Substring(0, 3);
  177. //var behavior = BizComponentFactory.GetBizComponent(mesStruct);
  178. var behavior = BizComponentFactory.GetBizComponent(msgType.Substring(0,msgType.IndexOf('^')));
  179. var errMsg = behavior.Process(o.VisitNo, o.TaskType,o.ID);
  180. SetEndBiz(o.ID, errMsg);
  181. }
  182. catch (Exception ex)
  183. {
  184. if (setLog != null)
  185. setLog(new LogModel { Message = o.VisitNo, MessageID = o.TaskType, ErrorMsg = "消息处理错误 :" + ex.Message });
  186. SetEndBiz(o.ID, ex.Message);
  187. }
  188. }
  189. });
  190. }
  191. /// <summary>
  192. /// 获取消息类型
  193. /// </summary>
  194. /// <returns></returns>
  195. protected abstract string GetMessageType();
  196. /// <summary>
  197. /// 获取当前记录
  198. /// </summary>
  199. /// <param name="id">id</param>
  200. /// <returns></returns>
  201. protected HL7_ScanEntity GetListRow(string id)
  202. {
  203. var sql = "SELECT * FROM dbo.HL7_Scan WHERE TaskStatus='1' and ID='" + id + "'";
  204. var model = SqlDataAccess.ExecuteSqlStringAccessor<HL7_ScanEntity>(ctx, sql).Take(1).FirstOrDefault();
  205. return model;
  206. }
  207. //开始时间
  208. protected int SetStartBiz(string id)
  209. {
  210. var sql = "UPDATE dbo.HL7_Scan SET BeginTime=GETDATE() , TaskStatus='2',IP=@TaskIP,Mac=@TaskMac WHERE ID=@ID";
  211. var cmd = SqlDataAccess.GetSqlStringCommand(ctx, sql);
  212. SqlDataAccess.AddInParameter(ctx, cmd, "ID", DbType.String, id);
  213. SqlDataAccess.AddInParameter(ctx, cmd, "TaskIP", DbType.String, NetHelper.GetIP());
  214. SqlDataAccess.AddInParameter(ctx, cmd, "TaskMac", DbType.String, NetHelper.GetMacAddress());
  215. return SqlDataAccess.ExecuteNonQuery(ctx, cmd);
  216. }
  217. //结束时间
  218. protected int SetEndBiz(string id, string errMsg)
  219. {
  220. var sql = "UPDATE dbo.HL7_Scan SET EndTime=GETDATE(),TaskStatus=@TaskStatus,TaskMsg=@TaskMsg WHERE ID=@ID";
  221. var cmd = SqlDataAccess.GetSqlStringCommand(ctx, sql);
  222. SqlDataAccess.AddInParameter(ctx, cmd, "ID", DbType.String, id);
  223. if (!string.IsNullOrEmpty(errMsg))
  224. {
  225. SqlDataAccess.AddInParameter(ctx, cmd, "TaskMsg", DbType.String, errMsg);
  226. SqlDataAccess.AddInParameter(ctx, cmd, "TaskStatus", DbType.Int32, 3);
  227. }
  228. else
  229. {
  230. SqlDataAccess.AddInParameter(ctx, cmd, "TaskMsg", DbType.String, DBNull.Value);
  231. SqlDataAccess.AddInParameter(ctx, cmd, "TaskStatus", DbType.Int32, 4);
  232. }
  233. return SqlDataAccess.ExecuteNonQuery(ctx, cmd);
  234. }
  235. public void DebugMsg(IEnumerable<HL7_ReceiveEntity> list,string msgType)
  236. {
  237. //Process(list, null,msgType);
  238. }
  239. }
  240. }