Tools.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. using ADODB;
  2. using System;
  3. using System.Net;
  4. using System.Net.Sockets;
  5. using System.Security.Cryptography;
  6. using System.Windows.Forms;
  7. using ZLPlugin_LisPacs_MR.Domain.Units;
  8. using ZLPlugin_LisPacs_MR.Model;
  9. namespace ZLPlugin_LisPacs_MR
  10. {
  11. //工具类
  12. public class Tools
  13. {
  14. //生成 uuid
  15. public static string Generate16CharUUID()
  16. {
  17. byte[] buffer = new byte[8];
  18. using (RandomNumberGenerator rng = RandomNumberGenerator.Create())
  19. {
  20. rng.GetBytes(buffer);
  21. }
  22. long longValue = BitConverter.ToInt64(buffer, 0);
  23. Log.Info("longValue" + longValue);
  24. return longValue.ToString("X").Substring(0, 16);
  25. }
  26. //获取ip地址
  27. public static string GetHostIp()
  28. {
  29. string hostName = Dns.GetHostName();
  30. IPHostEntry ipHostInfo = Dns.GetHostEntry(hostName);
  31. IPAddress[] ipAddresses = ipHostInfo.AddressList;
  32. foreach (IPAddress ip in ipAddresses)
  33. {
  34. if (ip.AddressFamily == AddressFamily.InterNetwork)
  35. {
  36. Console.WriteLine("本机 IP 地址:" + ip);
  37. return ip.ToString();
  38. }
  39. }
  40. return hostName;
  41. }
  42. public static string FormatStr(string str)
  43. {
  44. return str.Replace("\n", "").Replace("\t", "").Replace("\r", "");
  45. }
  46. public static string ReplaceString(string str)
  47. {
  48. return str.Replace("\\", "\\\\").Replace("\"", "\\\"");
  49. }
  50. public static Connection Connection;
  51. /// <summary>
  52. /// 查询
  53. /// </summary>
  54. /// <param name="sql"></param>
  55. /// <param name="rs"></param>
  56. public static void QueryTable(string sql, out Recordset rs)
  57. {
  58. rs = new Recordset();
  59. try
  60. {
  61. rs.Open(sql, Connection, CursorTypeEnum.adOpenKeyset, LockTypeEnum.adLockOptimistic, (int)CommandTypeEnum.adCmdText);
  62. }
  63. catch (Exception ex)
  64. {
  65. Log.Info(ex.Message, true);
  66. Log.Info("出错sql为:" + sql, true);
  67. }
  68. }
  69. public static int ExecuteSql(string sql)
  70. {
  71. int result = 1;
  72. try
  73. {
  74. object missing = System.Reflection.Missing.Value;
  75. Command Command = new Command();
  76. Command.ActiveConnection = Connection;
  77. Command.CommandText = sql;
  78. Command.Execute(out missing, ref missing, (int)CommandTypeEnum.adCmdText);
  79. }
  80. catch (Exception ex)
  81. {
  82. result = 0;
  83. Log.Info("ex.Message=" + ex.Message);
  84. Log.Info("ex.StackTrace=" + ex.StackTrace);
  85. Connection.Close();
  86. }
  87. return result;
  88. }
  89. public static int ExecuteStoredProc(long 病人id, long 主医嘱id, string json, int lis, int pacs)
  90. {
  91. int result = 1;
  92. try
  93. {
  94. int? isNull = null;
  95. object missing = System.Reflection.Missing.Value;
  96. Command Command = new Command();
  97. Command.ActiveConnection = Connection;
  98. Command.CommandText = "Zl_医嘱作废_一张纸互认";
  99. Command.CommandType = CommandTypeEnum.adCmdStoredProc;
  100. Parameter par_br = Command.CreateParameter("病人id_In", DataTypeEnum.adBigInt, ParameterDirectionEnum.adParamInput, 18, 病人id);
  101. Command.Parameters.Append(par_br);
  102. Parameter par_yz = Command.CreateParameter("主医嘱id_In", DataTypeEnum.adBigInt, ParameterDirectionEnum.adParamInput, 18, 主医嘱id);
  103. Command.Parameters.Append(par_yz);
  104. Parameter par_json = Command.CreateParameter("Json_In", DataTypeEnum.adVarWChar, ParameterDirectionEnum.adParamInput, 2000, json);
  105. Command.Parameters.Append(par_json);
  106. Parameter par_pacs = Command.CreateParameter("检查消息_In", DataTypeEnum.adInteger, ParameterDirectionEnum.adParamInput, 1, pacs == 1 ? 1 : isNull);
  107. Command.Parameters.Append(par_pacs);
  108. Parameter par_lis = Command.CreateParameter("检验消息_In", DataTypeEnum.adInteger, ParameterDirectionEnum.adParamInput, 1, lis == 1 ? 1 : isNull);
  109. Command.Parameters.Append(par_lis);
  110. Parameter par_state = Command.CreateParameter("state", DataTypeEnum.adInteger, ParameterDirectionEnum.adParamOutput, 1);
  111. Command.Parameters.Append(par_state);
  112. Command.Execute(out missing, ref missing, (int)CommandTypeEnum.adCmdStoredProc);
  113. }
  114. catch (Exception ex)
  115. {
  116. result = 0;
  117. Connection.Close();
  118. Log.Info(ex.Message, true);
  119. Log.Info(ex.StackTrace, true);
  120. }
  121. return result;
  122. }
  123. /// <summary>
  124. /// 得到json,无子数组
  125. /// </summary>
  126. /// <param name="rd"></param>
  127. /// <returns></returns>
  128. public static string GetJsonData(Recordset rd)
  129. {
  130. string value = "{";
  131. for (var i = 0; i < rd.RecordCount; i++)
  132. {
  133. for (var j = 0; j < rd.Fields.Count; j++)
  134. {
  135. object obj = rd.Fields[j].Value;
  136. string val = obj.ToString().Trim();
  137. if (val == "")
  138. {
  139. value += "\"" + rd.Fields[j].Name + "\":null,";
  140. }
  141. else
  142. {
  143. value += "\"" + rd.Fields[j].Name + "\":\"" + rd.Fields[j].Value + "\",";
  144. }
  145. }
  146. rd.MoveNext();
  147. }
  148. value = value.Substring(0, value.Length - 1);
  149. value += "}";
  150. return value;
  151. }
  152. /// <summary>
  153. /// 得到保存前的医嘱json
  154. /// </summary>
  155. /// <param name="rd"></param>
  156. /// <returns></returns>
  157. public static string GetAdviceSaveData(Recordset rd)
  158. {
  159. string value = "";
  160. if (rd.RecordCount > 0)
  161. {
  162. value = "[";
  163. for (var i = 0; i < rd.RecordCount; i++)
  164. {
  165. value += "{";
  166. for (var j = 0; j < rd.Fields.Count; j++)
  167. {
  168. object obj = rd.Fields[j].Value;
  169. string val = obj.ToString().Trim();
  170. if (val == "")
  171. {
  172. value += "\"" + rd.Fields[j].Name + "\":null,";
  173. }
  174. else
  175. {
  176. if (rd.Fields[j].Name == "医嘱内容")
  177. {
  178. value += "\"" + rd.Fields[j].Name + "\":\"" + val.Replace("\"", " ") + "\",";
  179. }
  180. else if (rd.Fields[j].Name== "附项")
  181. {
  182. value += "\"" + rd.Fields[j].Name + "\":null,";
  183. }
  184. else
  185. {
  186. value += "\"" + rd.Fields[j].Name + "\":\"" + rd.Fields[j].Value + "\",";
  187. }
  188. }
  189. }
  190. value = value.Substring(0, value.Length - 1);
  191. value += "},";
  192. rd.MoveNext();
  193. }
  194. value = value.Substring(0, value.Length - 1);
  195. value += "]";
  196. }
  197. return value;
  198. }
  199. public static string RecordsetToJson(Recordset rd)
  200. {
  201. string value = "";
  202. if (rd.RecordCount > 0)
  203. {
  204. //value = "[";
  205. for (var i = 0; i < rd.RecordCount; i++)
  206. {
  207. value += "{";
  208. for (var j = 0; j < rd.Fields.Count; j++)
  209. {
  210. Log.Info("\"" + rd.Fields[j].Name + "\":\"" + rd.Fields[j].Value + "\",");
  211. value += "\"" + rd.Fields[j].Name + "\":\"" + rd.Fields[j].Value + "\",";
  212. //object obj = rd.Fields[j].Value;
  213. //string val = obj.ToString().Trim();
  214. //if (val == "")
  215. //{
  216. // value += "\"" + rd.Fields[j].Name + "\":null,";
  217. //}
  218. //else
  219. //{
  220. // value += "\"" + rd.Fields[j].Name + "\":\"" + rd.Fields[j].Value + "\",";
  221. //}
  222. }
  223. value = value.Substring(0, value.Length - 1);
  224. value += "},";
  225. //rd.MoveNext();
  226. }
  227. value = value.Substring(0, value.Length - 1);
  228. //value += "]";
  229. }
  230. return value;
  231. }
  232. /// <summary>
  233. /// 防止卡顿,程序等待
  234. /// </summary>
  235. /// <param name="mm"></param>
  236. public static void Delay(int mm, int sate = 1)
  237. {
  238. DateTime current = DateTime.Now;
  239. while (current.AddMilliseconds(mm) > DateTime.Now)
  240. {
  241. if (!string.IsNullOrWhiteSpace(SocketClient.Message))
  242. {
  243. return;
  244. }
  245. if (sate == 1)
  246. {
  247. Application.DoEvents();
  248. }
  249. }
  250. }
  251. public static bool MutualRecognition(Results result, out string message)
  252. {
  253. message = "";
  254. if (result.ResultText[0].State == "0")
  255. {
  256. return true;
  257. }
  258. else//互认
  259. {
  260. var LIS = result.ResultText[0].LIS;
  261. var PACS = result.ResultText[0].PACS;
  262. if (LIS != null && PACS == null)
  263. {
  264. message = "已互认检验结果[";
  265. for (int i = 0; i < result.ResultText[0].LIS.Count; i++)
  266. {
  267. message += result.ResultText[0].LIS[i].ReportName + ",";
  268. }
  269. message = message.Substring(0, message.Length - 1);
  270. message += "],不允许重复检验,请删除或调整相关项目后再保存!";
  271. }
  272. else if (PACS != null && LIS == null)
  273. {
  274. message = "已互认检查结果[";
  275. for (int i = 0; i < result.ResultText[0].PACS.Count; i++)
  276. {
  277. message += result.ResultText[0].PACS[i].ItemName + ",";
  278. }
  279. message = message.Substring(0, message.Length - 1);
  280. message += "],不允许重复检查,请删除或调整相关项目后再保存!";
  281. }
  282. else if (PACS != null && LIS != null)
  283. {
  284. message = "已互认检验结果[";
  285. for (int i = 0; i < result.ResultText[0].LIS.Count; i++)
  286. {
  287. message += result.ResultText[0].LIS[i].ReportName + ",";
  288. }
  289. message = message.Substring(0, message.Length - 1);
  290. message += "],检查结果[";
  291. for (int i = 0; i < result.ResultText[0].PACS.Count; i++)
  292. {
  293. message += result.ResultText[0].PACS[i].ItemName + ",";
  294. }
  295. message = message.Substring(0, message.Length - 1);
  296. message += "],不允许重复检验检查,请删除或调整相关项目后再保存!";
  297. }
  298. return false;
  299. }
  300. }
  301. }
  302. }