|
|
using System;
|
|
|
using System.Collections.Generic;
|
|
|
using System.Data.SqlClient;
|
|
|
using System.Drawing;
|
|
|
using System.Drawing.Imaging;
|
|
|
using System.IO;
|
|
|
using System.Linq;
|
|
|
using System.Net;
|
|
|
using System.Timers;
|
|
|
using Dicom.Imaging;
|
|
|
using DicomTool.Model;
|
|
|
using DicomTool.Utils;
|
|
|
|
|
|
namespace DicomTool
|
|
|
{
|
|
|
internal class Program
|
|
|
{
|
|
|
// 设置循环间隔时间为10分钟
|
|
|
private const int intervalInMinutes = 60;
|
|
|
// PACS 共享文件访问
|
|
|
private const string UserName = "XBDLISUser";
|
|
|
private const string Password = "BlueFlag.Lis!@#";
|
|
|
// DCM文件下载存放路径
|
|
|
private static readonly string DcmPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "DicomFiles/");
|
|
|
private static readonly string EcgPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "EcgFiles/");
|
|
|
|
|
|
// 188
|
|
|
//private const string connectionString = "Data Source=192.168.12.188;Initial Catalog=peisdb;User ID=sa;Password=000626;";
|
|
|
// 盈江妇幼
|
|
|
// private const string ConnectionString = @"Data Source=172.172.100.12;Initial Catalog=peisdb;User ID=sa;Password=xbdLis!@#77911;";
|
|
|
// 芒市妇幼
|
|
|
// private const string ConnectionString = @"Data Source=192.168.11.5;Initial Catalog=peisdb;User ID=XBDLISUser;Password=BlueFlag.Lis!@#;";
|
|
|
// 德宏中医
|
|
|
private const string ConnectionString = @"Data Source=200.200.200.71;Initial Catalog=peisdb;User ID=sa;Password=wVJeC28@eY*&F#5NGL^eYC3m;";
|
|
|
|
|
|
/// <summary>
|
|
|
/// PACS报告中已选的图片
|
|
|
/// </summary>
|
|
|
private const string InsertExamPacsImage =
|
|
|
@"INSERT INTO Exam_PacsImage (Image,EID,ReportNo,InTime) VALUES (@ImageData,@EID,@ReportNo,@InTime);
|
|
|
INSERT INTO Report_Pacs(Image, EID, ReportNo, InTime) VALUES(@ImageData, @EID, @ReportNo, @InTime);";
|
|
|
|
|
|
private static void Main(string[] args)
|
|
|
{
|
|
|
Console.WriteLine($"【启动】{DateTime.Now:yyyy-MM-dd HH:mm}");
|
|
|
// 创建一个 Timer 实例
|
|
|
var timer = new Timer();
|
|
|
try
|
|
|
{
|
|
|
//DownEcgFtpImage();
|
|
|
Execute();
|
|
|
|
|
|
Console.ReadKey();
|
|
|
timer.Interval = intervalInMinutes * 60 * 1000;
|
|
|
// 设置为 true,使得 Timer 每隔设定的间隔时间自动触发一次 Elapsed 事件
|
|
|
timer.AutoReset = true;
|
|
|
// 绑定 Elapsed 事件处理程序
|
|
|
timer.Elapsed += Timer_Elapsed;
|
|
|
// 启动 Timer
|
|
|
timer.Start();
|
|
|
// 阻止控制台程序退出
|
|
|
Console.ReadKey();
|
|
|
// 停止 Timer
|
|
|
timer.Stop();
|
|
|
}
|
|
|
catch (Exception e)
|
|
|
{
|
|
|
Console.WriteLine(e.Message);
|
|
|
timer.Stop();
|
|
|
}
|
|
|
|
|
|
Console.WriteLine($"【停止】{DateTime.Now:yyyy-MM-dd HH:mm}");
|
|
|
Console.ReadKey();
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 定时执行任务
|
|
|
/// </summary>
|
|
|
/// <param name="sender"></param>
|
|
|
/// <param name="e"></param>
|
|
|
private static void Timer_Elapsed(object sender, ElapsedEventArgs e)
|
|
|
{
|
|
|
Console.WriteLine($@"执行-{DateTime.Now:yyyy-MM-dd HH:mm}");
|
|
|
DownEcgFtpImage();
|
|
|
Execute();
|
|
|
SyncPacsReport();
|
|
|
//DelDownFiles();
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 下载选择的
|
|
|
/// </summary>
|
|
|
private static void Execute()
|
|
|
{
|
|
|
Console.WriteLine($"【开始同步PACS报告图片】{DateTime.Now:yyyy-MM-dd HH:mm}------------");
|
|
|
//--将PACS检查报告同步到体检系统中
|
|
|
if (!Directory.Exists(DcmPath))
|
|
|
{
|
|
|
Directory.CreateDirectory(DcmPath);
|
|
|
}
|
|
|
var reportList = PacsSqlHelper.GetPacsReportList();
|
|
|
var fileNameList = new List<string>();
|
|
|
int i = 1;
|
|
|
foreach (var report in reportList)
|
|
|
{
|
|
|
try
|
|
|
{
|
|
|
// 已选图片UID
|
|
|
var selectedList = PacsSqlHelper.GetReportUidList(report.AccessionNumber);
|
|
|
if (selectedList.Count <= 0) continue;
|
|
|
// DCM图片路径
|
|
|
var imageFiles = PacsSqlHelper.GetPacsImageFile(report.PatientCode,report.ExamFeeitem_Code);
|
|
|
if (string.IsNullOrEmpty(imageFiles)) continue;
|
|
|
// 得到DCM共享文件地址
|
|
|
var dcmPaths = imageFiles.Split(';');
|
|
|
// 路径为空
|
|
|
if (!(dcmPaths?.Length > 0)) continue;
|
|
|
|
|
|
//获取远程共享文件
|
|
|
using (var client = new WebClient())
|
|
|
{
|
|
|
// 配置授权账户密码
|
|
|
var credentials = new NetworkCredential(UserName, Password);
|
|
|
client.Credentials = credentials;
|
|
|
selectedList.ForEach(selected =>
|
|
|
{
|
|
|
var file = selected + ".DCM";
|
|
|
var dcmPacsDict = dcmPaths[0].Substring(dcmPaths[0].Length , file.Length);
|
|
|
string downPath = dcmPaths[0].Replace(dcmPacsDict, file);
|
|
|
if (Path.HasExtension(downPath))
|
|
|
{
|
|
|
//下载远程文件
|
|
|
var buffer = client.DownloadData(downPath);
|
|
|
// 保存本地
|
|
|
var name = $"{report.PatientCode?.Trim()}-{report.ExamFeeitem_Code?.Trim()}-" + i;
|
|
|
Bytes2File(buffer, DcmPath, $"{name}.DCM");
|
|
|
fileNameList.Add($"{name}.DCM");
|
|
|
Console.WriteLine($"下载:{name}.DCM");
|
|
|
i++;
|
|
|
};
|
|
|
});
|
|
|
// 循环路径
|
|
|
for (var index = 0; index < dcmPaths.Count(); index++)
|
|
|
{
|
|
|
try
|
|
|
{
|
|
|
// 当前路径
|
|
|
var dcmPath = dcmPaths[index];
|
|
|
if (string.IsNullOrEmpty(dcmPath)) continue;
|
|
|
if (!Path.HasExtension(dcmPath) ) continue;
|
|
|
selectedList.ForEach(selected =>
|
|
|
{
|
|
|
if (dcmPath.Contains(selected))
|
|
|
{
|
|
|
//下载远程文件
|
|
|
var buffer = client.DownloadData(dcmPath);
|
|
|
// 保存本地
|
|
|
var name = $"{report.PatientCode?.Trim()}-{report.ExamFeeitem_Code?.Trim()}-" + i;
|
|
|
Bytes2File(buffer, DcmPath, $"{name}.DCM");
|
|
|
fileNameList.Add($"{name}.DCM");
|
|
|
Console.WriteLine($"下载:{name}.DCM");
|
|
|
i++;
|
|
|
}
|
|
|
});
|
|
|
}
|
|
|
catch (Exception e)
|
|
|
{
|
|
|
Console.WriteLine($"01." + e.Message);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
catch (Exception e)
|
|
|
{
|
|
|
Console.WriteLine($"02." + e.Message);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
Console.WriteLine($@"【下载】{fileNameList.Count}");
|
|
|
if (fileNameList.Count > 0)
|
|
|
// 保存到本地后上传到服务器
|
|
|
UploadDcmImg(DcmPath, fileNameList, InsertExamPacsImage);
|
|
|
|
|
|
Console.WriteLine($@"【等待】{intervalInMinutes}min");
|
|
|
Console.WriteLine($"------------------------------------------");
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 1、将本地文件PACS影像图片发送到服务器
|
|
|
/// </summary>
|
|
|
public static void UploadDcmImg(string folderPath, List<string> fileNameList, string insertSql)
|
|
|
{
|
|
|
var time = DateTime.Now;
|
|
|
// 获取文件夹下所有文件的路径
|
|
|
var files = Directory.GetFiles(folderPath);
|
|
|
var i = 0;
|
|
|
// 建立数据库连接
|
|
|
using (var connection = new SqlConnection(ConnectionString))
|
|
|
{
|
|
|
// 遍历文件路径并输出
|
|
|
foreach (var filePath in files)
|
|
|
{
|
|
|
try
|
|
|
{
|
|
|
var fileName = System.IO.Path.GetFileName(filePath);
|
|
|
if (!fileNameList.Contains(fileName)) continue;
|
|
|
var eid = Convert.ToInt64(fileName.Split('-')[0]);
|
|
|
var reportNo = fileName.Split('-')[1];
|
|
|
var image = new DicomImage(filePath);
|
|
|
//image.NumberOfFrames 如果有多帧图片需要将每帧都转成jpeg
|
|
|
//DCM转Bitmap
|
|
|
var bitmap = image.RenderImage().AsBitmap();
|
|
|
using (var streamImg = new MemoryStream())
|
|
|
{
|
|
|
//Bitmap To byte[]
|
|
|
bitmap.Save(streamImg, System.Drawing.Imaging.ImageFormat.Jpeg);
|
|
|
var imgBytes = streamImg.ToArray();
|
|
|
// File.WriteAllBytes(Path.Combine(DcmPath, i+".jpeg"), imgBytes);
|
|
|
//上传
|
|
|
{
|
|
|
connection.Open();
|
|
|
// 创建插入记录的 SQL 查询
|
|
|
// 创建命令对象
|
|
|
using (var command = new SqlCommand(insertSql, connection))
|
|
|
{
|
|
|
// 设置参数值
|
|
|
command.Parameters.AddWithValue("@ImageData", imgBytes);
|
|
|
command.Parameters.AddWithValue("@EID", eid);
|
|
|
command.Parameters.AddWithValue("@ReportNo", reportNo);
|
|
|
command.Parameters.AddWithValue("@InTime", time);
|
|
|
|
|
|
Console.WriteLine($@"【上传】{fileName}");
|
|
|
// 执行插入操作
|
|
|
command.ExecuteNonQuery();
|
|
|
i++;
|
|
|
}
|
|
|
|
|
|
connection.Close();
|
|
|
}
|
|
|
}
|
|
|
// 删除上传成功的文件
|
|
|
File.Delete(filePath);
|
|
|
}
|
|
|
catch (Exception e)
|
|
|
{
|
|
|
Console.WriteLine(System.IO.Path.GetFileName(filePath));
|
|
|
Console.WriteLine(e.Message);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
Console.WriteLine($@"【上传成功】{i}");
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 将byte数组转换为文件并保存到指定地址
|
|
|
/// </summary>
|
|
|
/// <param name="buff">byte数组</param>
|
|
|
/// <param name="path">保存地址</param>
|
|
|
/// <param name="fileName"></param>
|
|
|
public static void Bytes2File(byte[] buff, string path, string fileName)
|
|
|
{
|
|
|
try
|
|
|
{
|
|
|
//如果不存在就创建Enclosure文件夹
|
|
|
if (Directory.Exists(path) == false)
|
|
|
Directory.CreateDirectory(path);
|
|
|
|
|
|
if (File.Exists(path + fileName))
|
|
|
File.Delete(path + fileName);
|
|
|
|
|
|
var fs = new FileStream(path + fileName, FileMode.CreateNew);
|
|
|
var bw = new BinaryWriter(fs);
|
|
|
bw.Write(buff, 0, buff.Length);
|
|
|
bw.Close();
|
|
|
fs.Close();
|
|
|
}
|
|
|
catch (Exception e)
|
|
|
{
|
|
|
Console.WriteLine("【下载失败】" + fileName + "--" + e.Message);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
public static void DownEcgFtpImage()
|
|
|
{
|
|
|
Console.WriteLine("-----------------------------------");
|
|
|
Console.WriteLine($"【ECG-START】{DateTime.Now:yyyy-MM-dd HH:mm}");
|
|
|
var files = DAOHelp.GetDataBySQL<ReportEcg>
|
|
|
($@"SELECT id,ReportUrl FROM Report_GSEXD WHERE ReportUrl is not null and ReportData is null");
|
|
|
Console.WriteLine("[ECG-ReportCount]" + files.Count);
|
|
|
if (files.Count == 0)
|
|
|
return;
|
|
|
var name = "TJ_HXECG";
|
|
|
var password = "123456";
|
|
|
if (!Directory.Exists(EcgPath))
|
|
|
{
|
|
|
Directory.CreateDirectory(EcgPath);
|
|
|
}
|
|
|
|
|
|
FtpHelper.DownloadFtpFile(files, name, password, EcgPath);
|
|
|
UploadEcgImg(EcgPath, files.Select(s => s.ID.ToString()).ToList());
|
|
|
Console.WriteLine("ECG 下载完毕");
|
|
|
Console.WriteLine($"【ECG-END】{DateTime.Now:yyyy-MM-dd HH:mm}");
|
|
|
Console.WriteLine("-----------------------------------");
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 1、将ecg图片发送到服务器
|
|
|
/// </summary>
|
|
|
public static void UploadEcgImg(string folderPath, List<string> idList)
|
|
|
{
|
|
|
var i = 0;
|
|
|
// 建立数据库连接
|
|
|
using (var connection = new SqlConnection(ConnectionString))
|
|
|
{
|
|
|
// 遍历文件路径并输出
|
|
|
foreach (var id in idList)
|
|
|
{
|
|
|
var jpgFilePath = Path.Combine(folderPath, id + ".jpg");
|
|
|
try
|
|
|
{
|
|
|
if (!File.Exists(jpgFilePath))
|
|
|
{
|
|
|
Console.WriteLine(id + "NotFound" + jpgFilePath);
|
|
|
continue;
|
|
|
}
|
|
|
|
|
|
// 加载JPG图片
|
|
|
using (var image = Image.FromFile(jpgFilePath))
|
|
|
{
|
|
|
// 创建内存流
|
|
|
using (MemoryStream memoryStream = new MemoryStream())
|
|
|
{
|
|
|
// 将图像保存到内存流中,指定图像格式为JPEG
|
|
|
image.Save(memoryStream, ImageFormat.Jpeg);
|
|
|
// 将内存流中的数据复制到字节数组
|
|
|
var imgBytes = memoryStream.ToArray();
|
|
|
//上传
|
|
|
{
|
|
|
connection.Open();
|
|
|
// 创建插入记录的 SQL 查询
|
|
|
string updateEcgRpt =
|
|
|
@"update Report_GSEXD set ReportData=@ReportData where id=@ID";
|
|
|
// 创建命令对象
|
|
|
using (var command = new SqlCommand(updateEcgRpt, connection))
|
|
|
{
|
|
|
// 设置参数值
|
|
|
command.Parameters.AddWithValue("@ReportData", imgBytes);
|
|
|
command.Parameters.AddWithValue("@ID", id);
|
|
|
Console.WriteLine($@"【上传】{id}.jpg");
|
|
|
// 执行插入操作
|
|
|
command.ExecuteNonQuery();
|
|
|
i++;
|
|
|
}
|
|
|
|
|
|
connection.Close();
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
File.Delete(jpgFilePath);
|
|
|
}
|
|
|
catch (Exception e)
|
|
|
{
|
|
|
Console.WriteLine(id + "-" + e.Message);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
Console.WriteLine($@"【上传成功】{i}");
|
|
|
}
|
|
|
|
|
|
public static void SyncPacsReport()
|
|
|
{
|
|
|
DAOHelp.ExecuteSql("EXEC sp_PacsRptInsert;");
|
|
|
}
|
|
|
}
|
|
|
} |