Tools.cs 13 KB

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