using ADODB;
using System;
using System.Net;
using System.Net.Sockets;
using System.Security.Cryptography;
using System.Windows.Forms;
using ZLPlugin_LisPacs_MR.Domain.Units;
using ZLPlugin_LisPacs_MR.Model;
namespace ZLPlugin_LisPacs_MR
{
//工具类
public class Tools
{
//生成 uuid
public static string Generate16CharUUID()
{
byte[] buffer = new byte[8];
using (RandomNumberGenerator rng = RandomNumberGenerator.Create())
{
rng.GetBytes(buffer);
}
long longValue = BitConverter.ToInt64(buffer, 0);
Log.Info("longValue" + longValue);
return longValue.ToString("X").Substring(0, 16);
}
public static string Generate16CharGUID()
{
// 生成Guid
Guid guid = Guid.NewGuid();
// 将Guid转换为字符串并截取前16位
string randomString = guid.ToString().Substring(0, 16);
Log.Info("Generate16CharGUID=" + randomString);
return randomString;
}
//获取ip地址
public static string GetHostIp()
{
string hostName = Dns.GetHostName();
IPHostEntry ipHostInfo = Dns.GetHostEntry(hostName);
IPAddress[] ipAddresses = ipHostInfo.AddressList;
foreach (IPAddress ip in ipAddresses)
{
if (ip.AddressFamily == AddressFamily.InterNetwork)
{
Console.WriteLine("本机 IP 地址:" + ip);
return ip.ToString();
}
}
return hostName;
}
public static string FormatStr(string str)
{
return str.Replace("\n", "").Replace("\t", "").Replace("\r", "");
}
public static string ReplaceString(string str)
{
return str.Replace("\\", "\\\\").Replace("\"", "\\\"");
}
public static Connection Connection;
///
/// 查询
///
///
///
public static void QueryTable(string sql, out Recordset rs)
{
rs = new Recordset();
try
{
rs.Open(sql, Connection, CursorTypeEnum.adOpenKeyset, LockTypeEnum.adLockOptimistic, (int)CommandTypeEnum.adCmdText);
}
catch (Exception ex)
{
Log.Info(ex.Message, true);
Log.Info("出错sql为:" + sql, true);
}
}
public static int ExecuteSql(string sql)
{
int result = 1;
try
{
object missing = System.Reflection.Missing.Value;
Command Command = new Command();
Command.ActiveConnection = Connection;
Command.CommandText = sql;
Command.Execute(out missing, ref missing, (int)CommandTypeEnum.adCmdText);
}
catch (Exception ex)
{
result = 0;
Log.Info("ex.Message=" + ex.Message);
Log.Info("ex.StackTrace=" + ex.StackTrace);
Connection.Close();
}
return result;
}
public static int ExecuteStoredProc(long 病人id, long 主医嘱id, string json, int lis, int pacs)
{
int result = 1;
try
{
int? isNull = null;
object missing = System.Reflection.Missing.Value;
Command Command = new Command();
Command.ActiveConnection = Connection;
Command.CommandText = "Zl_医嘱作废_一张纸互认";
Command.CommandType = CommandTypeEnum.adCmdStoredProc;
Parameter par_br = Command.CreateParameter("病人id_In", DataTypeEnum.adBigInt, ParameterDirectionEnum.adParamInput, 18, 病人id);
Command.Parameters.Append(par_br);
Parameter par_yz = Command.CreateParameter("主医嘱id_In", DataTypeEnum.adBigInt, ParameterDirectionEnum.adParamInput, 18, 主医嘱id);
Command.Parameters.Append(par_yz);
Parameter par_json = Command.CreateParameter("Json_In", DataTypeEnum.adVarWChar, ParameterDirectionEnum.adParamInput, 2000, json);
Command.Parameters.Append(par_json);
Parameter par_pacs = Command.CreateParameter("检查消息_In", DataTypeEnum.adInteger, ParameterDirectionEnum.adParamInput, 1, pacs == 1 ? 1 : isNull);
Command.Parameters.Append(par_pacs);
Parameter par_lis = Command.CreateParameter("检验消息_In", DataTypeEnum.adInteger, ParameterDirectionEnum.adParamInput, 1, lis == 1 ? 1 : isNull);
Command.Parameters.Append(par_lis);
Parameter par_state = Command.CreateParameter("state", DataTypeEnum.adInteger, ParameterDirectionEnum.adParamOutput, 1);
Command.Parameters.Append(par_state);
Command.Execute(out missing, ref missing, (int)CommandTypeEnum.adCmdStoredProc);
}
catch (Exception ex)
{
result = 0;
Connection.Close();
Log.Info(ex.Message, true);
Log.Info(ex.StackTrace, true);
}
return result;
}
///
/// 得到json,无子数组
///
///
///
public static string GetJsonData(Recordset rd)
{
string value = "{";
for (var i = 0; i < rd.RecordCount; i++)
{
for (var j = 0; j < rd.Fields.Count; j++)
{
object obj = rd.Fields[j].Value;
string val = obj.ToString().Trim();
if (val == "")
{
value += "\"" + rd.Fields[j].Name + "\":null,";
}
else
{
//value += "\"" + rd.Fields[j].Name + "\":\"" + rd.Fields[j].Value + "\",";
if (rd.Fields[j].Name == "附项")
{
value += "\"" + rd.Fields[j].Name + "\":null,";
}
else
{
value += "\"" + rd.Fields[j].Name + "\":\"" + rd.Fields[j].Value + "\",";
}
}
}
rd.MoveNext();
}
value = value.Substring(0, value.Length - 1);
value += "}";
return value;
}
///
/// 得到保存前的医嘱json
///
///
///
public static string GetAdviceSaveData(Recordset rd)
{
string value = "";
if (rd.RecordCount > 0)
{
value = "[";
for (var i = 0; i < rd.RecordCount; i++)
{
value += "{";
for (var j = 0; j < rd.Fields.Count; j++)
{
object obj = rd.Fields[j].Value;
string val = obj.ToString().Trim();
if (val == "")
{
value += "\"" + rd.Fields[j].Name + "\":null,";
}
else
{
if (rd.Fields[j].Name == "医嘱内容")
{
value += "\"" + rd.Fields[j].Name + "\":\"" + val.Replace("\"", " ") + "\",";
}else if (rd.Fields[j].Name == "附项")
{
value += "\"" + rd.Fields[j].Name + "\":null,";
}
else
{
value += "\"" + rd.Fields[j].Name + "\":\"" + rd.Fields[j].Value + "\",";
}
}
}
value = value.Substring(0, value.Length - 1);
value += "},";
rd.MoveNext();
}
value = value.Substring(0, value.Length - 1);
value += "]";
}
return value;
}
public static string RecordsetToJson(Recordset rd)
{
string value = "";
if (rd.RecordCount > 0)
{
//value = "[";
for (var i = 0; i < rd.RecordCount; i++)
{
value += "{";
for (var j = 0; j < rd.Fields.Count; j++)
{
Log.Info("\"" + rd.Fields[j].Name + "\":\"" + rd.Fields[j].Value + "\",");
value += "\"" + rd.Fields[j].Name + "\":\"" + rd.Fields[j].Value + "\",";
//object obj = rd.Fields[j].Value;
//string val = obj.ToString().Trim();
//if (val == "")
//{
// value += "\"" + rd.Fields[j].Name + "\":null,";
//}
//else
//{
// value += "\"" + rd.Fields[j].Name + "\":\"" + rd.Fields[j].Value + "\",";
//}
}
value = value.Substring(0, value.Length - 1);
value += "},";
//rd.MoveNext();
}
value = value.Substring(0, value.Length - 1);
//value += "]";
}
return value;
}
///
/// 防止卡顿,程序等待
///
///
public static void Delay(int mm, int sate = 1)
{
DateTime current = DateTime.Now;
while (current.AddMilliseconds(mm) > DateTime.Now)
{
if (!string.IsNullOrWhiteSpace(SocketClient.Message))
{
return;
}
if (sate == 1)
{
Application.DoEvents();
}
}
}
public static bool MutualRecognition(Results result, out string message)
{
message = "";
if (result.ResultText[0].State == "0")
{
return true;
}
else//互认
{
var LIS = result.ResultText[0].LIS;
var PACS = result.ResultText[0].PACS;
if (LIS != null && PACS == null)
{
message = "已互认检验结果[";
for (int i = 0; i < result.ResultText[0].LIS.Count; i++)
{
message += result.ResultText[0].LIS[i].ReportName + ",";
}
message = message.Substring(0, message.Length - 1);
message += "],不允许重复检验,请删除或调整相关项目后再保存!";
}
else if (PACS != null && LIS == null)
{
message = "已互认检查结果[";
for (int i = 0; i < result.ResultText[0].PACS.Count; i++)
{
message += result.ResultText[0].PACS[i].ItemName + ",";
}
message = message.Substring(0, message.Length - 1);
message += "],不允许重复检查,请删除或调整相关项目后再保存!";
}
else if (PACS != null && LIS != null)
{
message = "已互认检验结果[";
for (int i = 0; i < result.ResultText[0].LIS.Count; i++)
{
message += result.ResultText[0].LIS[i].ReportName + ",";
}
message = message.Substring(0, message.Length - 1);
message += "],检查结果[";
for (int i = 0; i < result.ResultText[0].PACS.Count; i++)
{
message += result.ResultText[0].PACS[i].ItemName + ",";
}
message = message.Substring(0, message.Length - 1);
message += "],不允许重复检验检查,请删除或调整相关项目后再保存!";
}
return false;
}
}
}
}