123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401 |
- using Avalonia.Controls;
- using Emgu.CV.Cuda;
- using fis.Helpers;
- using fis.Log;
- using fis.Managers;
- using fis.Managers.Interfaces;
- using fis.Utilities;
- using fis.Utilities.FFMPEG;
- using fis.Win.Dev.Managers.Interfaces;
- using fis.Win.Dev.Managers.Modules.Storage;
- using fis.Win.Dev.Managers.RPC;
- using Newtonsoft.Json;
- using NPOI.POIFS.Crypt.Dsig;
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.IO;
- using System.Linq;
- using System.Security.Policy;
- using System.Text;
- using System.Threading.Tasks;
- using WingInterfaceLibrary.DTO.Common;
- using WingInterfaceLibrary.ResearchEdition;
- using Avalonia.Controls;
- using Avalonia.Interactivity;
- using Avalonia;
- namespace fis.Win.Dev.Managers
- {
- internal class ExportDataManager : IExportDataManager
- {
- string currentDataId = "";
- bool _isCancel = false;
- internal ExportDataManager()
- {
- HttpHelper.OnDownloadProgressChanged += OnDowloadProgressChanged;
- HttpHelper.DownloadFinished += OnDownloadFinished;
- }
- private void OnDownloadFinished(object? sender, bool e)
- {
- var args = new List<string>() {
- "1.0",
- ((int)ExportStatus.Finished).ToString(),
- currentDataId,
- };
- UpdateProgress(args);
- }
- private void OnDowloadProgressChanged(object? sender, double e)
- {
- var progress = e / 100;
- var args = new List<string>() {
- progress.ToString(),
- ((int)ExportStatus.Downloading).ToString(),
- currentDataId,
- };
- UpdateProgress(args);
- }
- private void UpdateProgress(List<string> args)
- {
- if (AppManager.IsVStation)
- {
- BrowserManager.SlaveBrowser.ExecuteJS(TargetMethodName.UpdateExportProgess, args);
- }
- else
- {
- BrowserManager.MainBrowser.ExecuteJS(TargetMethodName.UpdateExportProgess, args);
- }
- }
- public async void ExportDatas(List<ExportDataInfo> exportDataInfos, string name, string path)
- {
- _isCancel = false;
- var targetPath = Path.Combine(path, name);
- if (!Directory.Exists(targetPath))
- {
- Directory.CreateDirectory(targetPath);
- }
- foreach (var data in exportDataInfos)
- {
- if (_isCancel)
- {
- return;
- }
- currentDataId = data.Id;
- var dataName = HttpHelper.RemoveInvalidFileNameChars(data.Name);
- var fileName = Path.Combine(targetPath, dataName);
- if (File.Exists(fileName))
- {
- File.Delete(fileName);
- }
- await HttpHelper.DownloadFile(data.Url, new FileInfo(fileName));
- if (data.Url.EndsWith("vid") || fileName.EndsWith("vid"))
- {
- var converter = new VideoFFmpegHelper(fileName);
- var pathDest = Path.Combine(targetPath, GetFileNameWithoutExt(data.Url));
- converter.ConvertVidToMpeg4(pathDest);
- converter.Dispose();
- File.Delete(fileName);
- }
- }
- }
- public async Task ExportDatasAsync(ProjectSampleDataExportResult projectInfo, string path, string imageType, string videoType)
- {
- _isCancel = false;
- ///如果未指定文件夹名称,则由用户选择
- if (string.IsNullOrEmpty(path))
- {
- var dialog = new OpenFolderDialog();
- dialog.Directory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
- Window window;
- if (AppManager.IsVStation)
- {
- var manager = AppManager.Get<ISecondaryWindowManager>();
- window = manager.SlaveWindow;
- }
- else
- {
- window = AppManager.MainWindow;
- }
- var result = await dialog.ShowAsync(window);
- if (result != null)
- {
- path = result;
- }
- else
- {
- _CancelExportDatas(projectInfo);
- return;
- }
- }
- try
- {
- ///根目录文件夹名称
- var rootPathName = projectInfo.Name + "数据导出_" + DateTime.Now.ToString("yyyy-MM-dd-HHmmss");
- rootPathName = HttpHelper.RemoveInvalidFileNameChars(rootPathName);
- var rootPath = Path.Combine(path, rootPathName);
- if (!Directory.Exists(rootPath))
- {
- Directory.CreateDirectory(rootPath);
- }
- var resourcesManager = AppManager.Get<IResourceManager>();
- var reportFileStream = resourcesManager.GetManifestResourceStream("fis.Win.Resources.labReport.report.html");
- if (reportFileStream != null)
- {
- // 使用文件流将数据写入文件
- using (FileStream fileStream = new FileStream(Path.Combine(rootPath, "数据汇总预览报告.html"), FileMode.Create, FileAccess.Write))
- {
- await reportFileStream.CopyToAsync(fileStream);
- }
- }
- var exportReportStructure = new ExportReportStructure();
- exportReportStructure.ReportTitle = projectInfo.Name + DateTime.Now.ToString("yyyy-MM-dd");
- List<SampleDataInfo> sampleDataInfos = new List<SampleDataInfo>();
- var excelUrl = projectInfo.ExportExcelUrl;
- if (!string.IsNullOrEmpty(excelUrl))
- {
- Uri uri = new Uri(excelUrl);
- string absolutePath = uri.AbsolutePath;
- int lastSlashIndex = absolutePath.LastIndexOf('/');
- string fileNameWithExtension = absolutePath.Substring(lastSlashIndex + 1);
- int lastDotIndex = fileNameWithExtension.LastIndexOf('.');
- // 去掉文件后缀
- string fileName = (lastDotIndex == -1) ? fileNameWithExtension : fileNameWithExtension.Substring(0, lastDotIndex);
- currentDataId = fileName;
- await HttpHelper.DownloadFile(excelUrl, new FileInfo(Path.Combine(rootPath, "测量结果.xlsx")));
- }
- IList<SampleDetailInfo> sampleDetailList = projectInfo.SampleDetailList;
- foreach (SampleDetailInfo sampleDetail in sampleDetailList)
- {
- var sampleDataInfo = new SampleDataInfo();
- List<ExamDataInfo> ExamDataInfos = new List<ExamDataInfo>();
- if (sampleDetail.SampleRemedicalBasicList.Count == 0)
- {
- continue;
- }
- List<DataItemDTO> patientInfos = sampleDetail.PatientInfo;
- string patientPath = "";
- string patientName = "";
- if (patientInfos.Any((e) => e.Key == "Name") || patientInfos.Any((e) => e.Key == "AnimalInfoName"))
- {
- patientName = patientInfos.FirstOrDefault((e) => e.Key == "Name" || e.Key == "AnimalInfoName")?.Value ?? "";
- patientName = Base64Decryptor.DecryptBase64(patientName);
- patientName = HttpHelper.RemoveInvalidFileNameChars(patientName);
- patientPath = Path.Combine(rootPath, patientName);
- if (!Directory.Exists(patientPath))
- {
- Directory.CreateDirectory(patientPath);
- }
- sampleDataInfo.Title = patientName;
- }
- if (string.IsNullOrEmpty(patientName))
- {
- DateTime startDate = new DateTime(1971, 1, 1, 0, 0, 0, DateTimeKind.Utc);
- DateTime currentDate = DateTime.UtcNow;
- TimeSpan timeSpan = currentDate - startDate;
- double totalMilliseconds = timeSpan.TotalMilliseconds;
- patientName = "未命名" + totalMilliseconds.ToString();
- sampleDataInfo.Title = patientName;
- }
- IList<SampleRemedicalBasicInfo> sampleRemedicalBasicList = sampleDetail.SampleRemedicalBasicList;
- List<ExamDataInfo> examDataInfos = new List<ExamDataInfo>();
- foreach (SampleRemedicalBasicInfo sampleRemedicalBasicInfo in sampleRemedicalBasicList)
- {
- var examDataInfo = new ExamDataInfo();
- string examDate = sampleRemedicalBasicInfo.RemedicalName;
- examDataInfo.Title = examDate;
- List<string> imagePathList = new List<string>();
- List<string> measureDataList = new List<string>();
- List<string> measureDataPathList = new List<string>();
- string patientScanTypePath = Path.Combine(patientPath, examDate);
- if (!Directory.Exists(patientScanTypePath))
- {
- Directory.CreateDirectory(patientScanTypePath);
- }
- IList<SampleRemedicalInfoDTO> sampleRemedicalInfoList = sampleRemedicalBasicInfo.SampleRemedicalInfoList;
- int index = 1;
- foreach (SampleRemedicalInfoDTO sampleRemedicalInfoDTO in sampleRemedicalInfoList)
- {
- string fileExtentName = ".jpg";
- bool isVid = false;
- var fileDataType = sampleRemedicalInfoDTO.FileDataType;
- if (sampleRemedicalInfoDTO.IsAnalysisResult)
- {
- fileExtentName = ".png";
- }
- else
- {
- switch (fileDataType)
- {
- case WingInterfaceLibrary.Enum.SampleRemedicalFileDataTypeEnum.VinnoVidSingle:
- case WingInterfaceLibrary.Enum.SampleRemedicalFileDataTypeEnum.ThirdVidMovie:
- case WingInterfaceLibrary.Enum.SampleRemedicalFileDataTypeEnum.ThirdVidSingle:
- case WingInterfaceLibrary.Enum.SampleRemedicalFileDataTypeEnum.VinnoVidMovie:
- isVid = true;
- fileExtentName = ".vid";
- break;
- case WingInterfaceLibrary.Enum.SampleRemedicalFileDataTypeEnum.Image:
- case WingInterfaceLibrary.Enum.SampleRemedicalFileDataTypeEnum.AnalysisImage:
- case WingInterfaceLibrary.Enum.SampleRemedicalFileDataTypeEnum.MeasurementImage:
- fileExtentName = ".jpg";
- break;
- case WingInterfaceLibrary.Enum.SampleRemedicalFileDataTypeEnum.AnalysisVideo:
- fileExtentName = ".mp4";
- break;
- }
- }
- string fileNameNoExtName = examDate + "_" + index.ToString();
- string fileName = fileNameNoExtName + fileExtentName;
- string filePath = Path.Combine(patientScanTypePath, fileName);
- currentDataId = sampleRemedicalInfoDTO.BusinessCode;
- var imageUrl = sampleRemedicalInfoDTO.ImageUrl;
- if (sampleRemedicalInfoDTO.IsAnalysisResult)
- {
- imageUrl = sampleRemedicalInfoDTO.CoverImgUrl;
- }
- await HttpHelper.DownloadFile(imageUrl, new FileInfo(filePath));
- string prePath = patientName + "/" + examDate + "/";
- if (isVid)
- {
- bool isSingle = true;
- var converter = new VideoFFmpegHelper(filePath);
- if (converter.ImageCount > 1)
- {
- isSingle = false;
- }
- var pathDest = Path.Combine(rootPath,patientScanTypePath, fileNameNoExtName);
- converter.ConvertVidToImage(pathDest, imageType, videoType);
- if (videoType == "1")
- {
- converter.ConvertVidToMpeg4(pathDest);
- }
- converter.Dispose();
- File.Delete(filePath);
- imagePathList.Add(prePath + fileNameNoExtName + (isSingle ? (imageType == "0" ? ".jpg" : ".png") : (videoType == "0" ? ".mp4" : ".avi")));
- }
- else
- {
- imagePathList.Add(prePath + fileName);
- }
- measureDataList.Add("");
- measureDataPathList.Add("");
- var sampleRemedicalMeasuredList = sampleRemedicalInfoDTO.SampleRemedicalMeasuredList;
- if (sampleRemedicalMeasuredList != null && sampleRemedicalMeasuredList.Count > 0)
- {
- int measureIndex = 0;
- foreach (SampleRemedicalMeasuredInfoDTO sampleRemedicalMeasuredInfoDTO in sampleRemedicalMeasuredList)
- {
- measureIndex++;
- string measureImageUrl = sampleRemedicalMeasuredInfoDTO.MeasuredFileToken;
- string measuredData = sampleRemedicalMeasuredInfoDTO.MeasuredData;
- ///测量结果图像
- if (!string.IsNullOrEmpty(measureImageUrl))
- {
- var measureName = fileNameNoExtName + "_测量图像_" + measureIndex.ToString() + ".jpg";
- string fileMeasureImagePath = Path.Combine(patientScanTypePath, measureName);
- await HttpHelper.DownloadFile(measureImageUrl, new FileInfo(fileMeasureImagePath));
- imagePathList.Add(prePath + measureName);
- }
- ///测量结果字符串
- if (!string.IsNullOrEmpty(measuredData))
- {
- var measureResultName = fileNameNoExtName + "_测量结果_" + measureIndex.ToString() + ".txt";
- string fileMeasureResultPath = Path.Combine(patientScanTypePath, measureResultName);
- File.WriteAllText(fileMeasureResultPath, measuredData);
- measureDataPathList.Add(prePath + measureResultName);
- measureDataList.Add(measuredData);
- }
- }
- }
- index++;
- }
- examDataInfo.MeasureDataList = measureDataList;
- examDataInfo.ImagePathList = imagePathList;
- examDataInfo.MeasureDataPathList = measureDataPathList;
- examDataInfos.Add(examDataInfo);
- }
- sampleDataInfo.ExamDataInfos = examDataInfos;
- sampleDataInfos.Add(sampleDataInfo);
- }
- exportReportStructure.SampleData = sampleDataInfos;
- var json = JsonConvert.SerializeObject(exportReportStructure);
- if (json != null)
- {
- var content = "const exportStructurejsonData = " + json;
- File.WriteAllText(Path.Combine(rootPath, "ReportDataStructure.js"), content);
- }
- }
- catch (Exception e)
- {
- Logger.WriteShellLog($"ExportDatasAsync ex:{e}");
- }
- }
- private void _CancelExportDatas(ProjectSampleDataExportResult projectInfo)
- {
- var excelUrl = projectInfo.ExportExcelUrl;
- if (!string.IsNullOrEmpty(excelUrl))
- {
- Uri uri = new Uri(excelUrl);
- string absolutePath = uri.AbsolutePath;
- int lastSlashIndex = absolutePath.LastIndexOf('/');
- string fileNameWithExtension = absolutePath.Substring(lastSlashIndex + 1);
- int lastDotIndex = fileNameWithExtension.LastIndexOf('.');
- // 去掉文件后缀
- string fileName = (lastDotIndex == -1) ? fileNameWithExtension : fileNameWithExtension.Substring(0, lastDotIndex);
- var args = new List<string>() {
- "1.0",
- ((int)ExportStatus.Cancelled).ToString(),
- fileName,
- };
- UpdateProgress(args);
- }
- IList<SampleDetailInfo> sampleDetailList = projectInfo.SampleDetailList;
- foreach (var sampleDetail in sampleDetailList)
- {
- IList<SampleRemedicalBasicInfo> sampleRemedicalBasicList = sampleDetail.SampleRemedicalBasicList;
- foreach (var sampleRemedicalBasicInfo in sampleRemedicalBasicList)
- {
- IList<SampleRemedicalInfoDTO> sampleRemedicalInfoList = sampleRemedicalBasicInfo.SampleRemedicalInfoList;
- foreach (SampleRemedicalInfoDTO sampleRemedicalInfoDTO in sampleRemedicalInfoList)
- {
- var currentDataId = sampleRemedicalInfoDTO.BusinessCode;
- var args = new List<string>() {
- "1.0",
- ((int)ExportStatus.Cancelled).ToString(),
- currentDataId,
- };
- UpdateProgress(args);
- }
- }
- }
- }
- public void CancelExport()
- {
- _isCancel = true;
- }
- private string GetFileNameWithoutExt(string fileToken)
- {
- string[] fragments = fileToken.Split('/');
- string lastFragment = fragments.Last();
- return lastFragment.Split('.')[0];
- }
- public void Dispose()
- {
- }
- }
- }
|