@ -24,22 +24,8 @@ namespace DicomTool
private static readonly string DcmPath = Path . Combine ( AppDomain . CurrentDomain . BaseDirectory , "DicomFiles/" ) ;
private static readonly string DcmPath = Path . Combine ( AppDomain . CurrentDomain . BaseDirectory , "DicomFiles/" ) ;
private static readonly string EcgPath = Path . Combine ( AppDomain . CurrentDomain . BaseDirectory , "EcgFiles/" ) ;
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;" ;
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 )
private static void Main ( string [ ] args )
{
{
Console . WriteLine ( $"【启动】{DateTime.Now:yyyy-MM-dd HH:mm}" ) ;
Console . WriteLine ( $"【启动】{DateTime.Now:yyyy-MM-dd HH:mm}" ) ;
@ -47,8 +33,7 @@ namespace DicomTool
var timer = new Timer ( ) ;
var timer = new Timer ( ) ;
try
try
{
{
//DownEcgFtpImage();
DownEcgFtpImage ( ) ;
Execute ( ) ;
Console . ReadKey ( ) ;
Console . ReadKey ( ) ;
timer . Interval = intervalInMinutes * 6 0 * 1 0 0 0 ;
timer . Interval = intervalInMinutes * 6 0 * 1 0 0 0 ;
@ -82,203 +67,6 @@ namespace DicomTool
{
{
Console . WriteLine ( $@"执行-{DateTime.Now:yyyy-MM-dd HH:mm}" ) ;
Console . WriteLine ( $@"执行-{DateTime.Now:yyyy-MM-dd HH:mm}" ) ;
DownEcgFtpImage ( ) ;
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 ( )
public static void DownEcgFtpImage ( )
@ -296,8 +84,7 @@ namespace DicomTool
{
{
Directory . CreateDirectory ( EcgPath ) ;
Directory . CreateDirectory ( EcgPath ) ;
}
}
DownloadFtpFile ( files , name , password , EcgPath ) ;
FtpHelper . DownloadFtpFile ( files , name , password , EcgPath ) ;
UploadEcgImg ( EcgPath , files . Select ( s = > s . ID . ToString ( ) ) . ToList ( ) ) ;
UploadEcgImg ( EcgPath , files . Select ( s = > s . ID . ToString ( ) ) . ToList ( ) ) ;
Console . WriteLine ( "ECG 下载完毕" ) ;
Console . WriteLine ( "ECG 下载完毕" ) ;
Console . WriteLine ( $"【ECG-END】{DateTime.Now:yyyy-MM-dd HH:mm}" ) ;
Console . WriteLine ( $"【ECG-END】{DateTime.Now:yyyy-MM-dd HH:mm}" ) ;
@ -370,9 +157,58 @@ namespace DicomTool
Console . WriteLine ( $@"【上传成功】{i}" ) ;
Console . WriteLine ( $@"【上传成功】{i}" ) ;
}
}
public static void SyncPacsReport ( )
/// <summary>
/// 下载ECG图片
/// </summary>
/// <param name="ftpUriList"></param>
/// <param name="username"></param>
/// <param name="password"></param>
/// <param name="localFilePath"></param>
public static void DownloadFtpFile ( List < ReportEcg > ftpUriList , string username , string password , string localFilePath )
{
{
DAOHelp . ExecuteSql ( "EXEC sp_PacsRptInsert;" ) ;
// 确保本地文件路径是目录
if ( ! Directory . Exists ( localFilePath ) )
{
Directory . CreateDirectory ( localFilePath ) ;
}
foreach ( var reportEcg in ftpUriList )
{
// 创建 FtpWebRequest 对象
try
{
var request = ( FtpWebRequest ) WebRequest . Create ( reportEcg . ReportUrl ) ;
request . Method = WebRequestMethods . Ftp . DownloadFile ;
request . Credentials = new NetworkCredential ( username , password ) ;
// 获取响应并读取数据
var response = ( FtpWebResponse ) request . GetResponse ( ) ;
var responseStream = response . GetResponseStream ( ) ;
// 获取文件名
// string fileName = Path.GetFileName(new Uri(reportEcg.ReportUrl).LocalPath);
var fileName = reportEcg . ID + ".jpg" ;
Console . WriteLine ( "【下载】" + reportEcg . ID + ".jpg" ) ;
var localFileFullPath = Path . Combine ( localFilePath , fileName ) ;
// 将 FTP 文件内容写入本地文件
using ( FileStream fileStream = new FileStream ( localFileFullPath , FileMode . Create , FileAccess . Write ) )
{
byte [ ] buffer = new byte [ 4 0 9 6 ] ;
int bytesRead ;
while ( ( bytesRead = responseStream . Read ( buffer , 0 , buffer . Length ) ) ! = 0 )
{
fileStream . Write ( buffer , 0 , bytesRead ) ;
}
}
responseStream . Close ( ) ;
response . Close ( ) ;
}
catch ( WebException ex )
{
// 处理异常,例如打印错误信息
Console . WriteLine ( "Download failed for " + reportEcg + ": " + ex . Message ) ;
continue ;
}
}
}
}
}
}
}
}