Tools.cs 13 KB

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