| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442 |
- using Newtonsoft.Json.Linq;
- using PTMedicalInsurance.Business;
- using PTMedicalInsurance.Common;
- using PTMedicalInsurance.Forms.BasicDatas.services;
- using PTMedicalInsurance.Helper;
- using PTMedicalInsurance.Variables;
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Globalization;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- namespace PTMedicalInsurance.Forms.BasicDatas
- {
- class DirectoryDownloadService
- {
- //设置实例
- CenterBusiness cBus = new CenterBusiness();
- HisMainBusiness hBus = new HisMainBusiness();
- HisIrisServices hIS = new HisIrisServices();
- MIIrisServices mIS = new MIIrisServices();
- InvokeHelper invoker = new InvokeHelper();
- public event EventHandler<string> VerChangeEvent;
- private static readonly Dictionary<DirectoryTypeEnum, TradeEnum> TradeMap = new Dictionary<DirectoryTypeEnum, TradeEnum>
- {
- { DirectoryTypeEnum.Drug, TradeEnum.CMDirectory },
- { DirectoryTypeEnum.MedicalService, TradeEnum.MedicalServiceDirectory },
- { DirectoryTypeEnum.Consumables, TradeEnum.MaterialDirectory },
- { DirectoryTypeEnum.DiseaseDiagnosis, TradeEnum.DiseaseDiagDirectory },
- { DirectoryTypeEnum.Surgery, TradeEnum.OperationDirectory },
- { DirectoryTypeEnum.OutpatientChronicDiseases, TradeEnum.OPSDiseaseDirectory },
- { DirectoryTypeEnum.Drgs, TradeEnum.DRGDiseaseDirectory },
- { DirectoryTypeEnum.DaySurgery, TradeEnum.DayOperationDiseaseDirectory },
- { DirectoryTypeEnum.Dictionary, TradeEnum.DictionaryDownload }, // 不变
- { DirectoryTypeEnum.ChineseHerbalPieces, TradeEnum.CMPiecesDirectory },
- { DirectoryTypeEnum.HospMadeMedicines, TradeEnum.MedOrgSelfDrug },
- { DirectoryTypeEnum.TumorMorphology, TradeEnum.TumorDirectory },
- { DirectoryTypeEnum.TcmDisease, TradeEnum.CMDiseaseDirectory },
- { DirectoryTypeEnum.TcmSyndrome, TradeEnum.CMSyndromeDirectory }
- };
- #region 字典下载
- /// <summary>
- /// 字典下载,通过判断dicCode判断是单字典值下载还是全量下载
- /// </summary>
- /// <param name="joData"></param>
- /// <param name="dictCode"></param>
- /// <param name="errMsg"></param>
- /// <returns></returns>
- public int DownloadDictionary(JObject joData, string dictCode, out string errMsg)
- {
- errMsg = string.Empty;
- JObject joRtn = cBus.DownDictionay(joData);
- string listJson = JsonHelper.getDestValue(joRtn, "output.list");
- if (JsonHelper.parseCenterRtnValue(joRtn, out errMsg) != 0)
- {
- return -1;
- }
- JArray jaList = JArray.Parse(listJson);
- JObject joImportRtn = ImportDictionary(jaList, dictCode);
- if (JsonHelper.parseIrisRtnValue(joImportRtn, out errMsg) == 0)
- {
- return 0;
- }
- errMsg = "导入数据出现错误,请查看日志!" + errMsg;
- return -1;
- }
- /// <summary>
- /// 插入字典主表和明细表
- /// </summary>
- /// <param name="jaList"></param>
- /// <param name="type"></param>
- /// <returns></returns>
- public JObject ImportDictionary(JArray jaList, string type)
- {
- string errorMsg = string.Empty;
- int errorCount = 0;
- // 插入主表获取字典主键
- if (!TryInsertDictionary(type, out string hbDictionaryDr, out string mainErrorMsg))
- {
- return JsonHelper.setExceptionJson(-1, "insertDictionary", mainErrorMsg);
- }
- try
- {
- // 批量插入明细表
- JArray batch = new JArray();
- for (int i = 0; i < jaList.Count; i++)
- {
- var item = jaList[i];
- dynamic detail = new JObject();
- detail.ID = "";
- detail.updateUserID = Global.user.ID;
- detail.HospitalDr = Global.inf.hospitalDr;
- detail.InterfaceDr = Global.inf.interfaceDr;
- detail.HBDictionaryDr = hbDictionaryDr;
- detail.Code = item["value"]?.ToString();
- detail.Descripts = item["label"]?.ToString();
- batch.Add(detail);
- // 每50条提交一次
- if ((i + 1) % 50 == 0 || i == jaList.Count - 1)
- {
- JObject result = mIS.insertDictionaryDataDetail(batch);
- if (result["errorCode"]?.ToString() != "0")
- {
- errorCount++;
- errorMsg += $"\r\n{result["errorMessage"]}";
- }
- batch = new JArray(); // 重置批次
- }
- }
- return JsonHelper.setExceptionJson(0 - errorCount, errorMsg, null);
- }
- catch (Exception ex)
- {
- return JsonHelper.setExceptionJson(-1, "ImportDictionary", ex.Message);
- }
- }
- /// <summary>
- /// 插入主表并获取主键ID
- /// </summary>
- /// <param name="type"></param>
- /// <param name="hbDictionaryDr"></param>
- /// <param name="errorMsg"></param>
- /// <returns></returns>
- private bool TryInsertDictionary(string type, out string hbDictionaryDr, out string errorMsg)
- {
- hbDictionaryDr = string.Empty;
- errorMsg = string.Empty;
- try
- {
- JObject joData = new JObject
- {
- { "HospitalDr", Global.inf.hospitalDr },
- { "InterfaceDr", Global.inf.interfaceDr },
- { "InsuCode", type }
- };
- JObject joRtn = mIS.insertDictionary(joData);
- if (JsonHelper.parseIrisRtnValue(joRtn, out errorMsg) != 0)
- {
- return false;
- }
- hbDictionaryDr = JsonHelper.getDestValue(joRtn, "data.HBDictionaryDr");
- if (string.IsNullOrEmpty(hbDictionaryDr))
- {
- errorMsg = "HBDictionaryDr返回值为空";
- return false;
- }
- return true;
- }
- catch (Exception ex)
- {
- errorMsg = ex.Message;
- return false;
- }
- }
- #endregion
- #region 目录下载
- /// <summary>
- /// 下载指定类型的医保目录并导入数据库
- /// </summary>
- /// <param name="version">版本号</param>
- /// <param name="category">目录类型(使用枚举)</param>
- /// <param name="progressBar">进度条控件(可选)</param>
- /// <param name="errorMessage">输出错误信息</param>
- /// <returns>成功返回0,失败返回-1</returns>
- //public int SingleDownload(string version, DirectoryTypeEnum category,Form frm , out string errorMessage)
- //{
- // try
- // {
- // errorMessage = string.Empty;
- // // 获取对应的交易码
- // if (!TradeMap.TryGetValue(category, out var trade))
- // {
- // errorMessage = "未找到对应的目录类型";
- // return -1;
- // }
- // // 下载目录数据
- // JObject joRtn = hBus.DownloadDirectory(trade.GetCode(), version);
- // if (JsonHelper.parseIrisRtnValue(joRtn, out errorMessage) != 0)
- // {
- // return -1;
- // }
- // string filePath = joRtn["filePath"]?.ToString();
- // if (string.IsNullOrEmpty(filePath))
- // {
- // errorMessage = "文件路径为空";
- // return -1;
- // }
- // // 导入数据
- // DataImoport dataImport = new DataImoport();
- // ProgressWrapper _dataLoader;
- // _dataLoader = new ProgressWrapper(dataImport.importDataToIrisByTxtNew, frm);
- // _dataLoader.SetProgressBar(ProgressBarStyle.Continuous, "正在下载...", 50);
- // DirectoryDownloadContext directoryDownloadContext = new DirectoryDownloadContext();
- // directoryDownloadContext.txtPath = filePath;
- // directoryDownloadContext.size = 30;
- // directoryDownloadContext.directoryType = (int)category;
- // directoryDownloadContext.startVersion = version;
- // object[] o = new object[] { directoryDownloadContext};
- // _dataLoader.Start(o, (result, errMsg) =>
- // {
- // if (result == 0)
- // {
- // MessageBox.Show("下载成功");
- // }
- // else
- // {
- // MessageBox.Show(errMsg);
- // }
- // return ;
- // });
- // return 0;
- // }
- // catch (Exception ex)
- // {
- // errorMessage = $"SingleDownload 出现异常:{ex.Message}";
- // Global.writeLog(errorMessage);
- // return -1;
- // }
- //}
- public int SingleDownload(string version, DirectoryTypeEnum category, Form frm, out string errorMessage)
- {
- try
- {
- errorMessage = string.Empty;
- // 导入数据
- DataImoport dataImport = new DataImoport();
- ProgressWrapper _dataLoader;
- _dataLoader = new ProgressWrapper(SingleDownload, frm);
- _dataLoader.SetProgressBar(ProgressBarStyle.Continuous, "正在下载...", 50);
- DirectoryDownloadContext directoryDownloadContext = new DirectoryDownloadContext();
- directoryDownloadContext.size = 30;
- directoryDownloadContext.category = category;
- directoryDownloadContext.startVersion = version;
- object[] o = new object[] { directoryDownloadContext };
- _dataLoader.Start(o, (result, errMsg) =>
- {
- if (result == 0)
- {
- MessageBox.Show("下载成功");
- }
- else
- {
- MessageBox.Show(errMsg);
- }
- return;
- });
- return 0;
- }
- catch (Exception ex)
- {
- errorMessage = $"SingleDownload 出现异常:{ex.Message}";
- Global.writeLog(errorMessage);
- return -1;
- }
- }
- public int SingleDownload(DataLoaderContext context, out string err)
- {
- err = string.Empty;
- object[] args = context.args;
- DirectoryDownloadContext directoryDownloadContext = (DirectoryDownloadContext)args[0];
- ////设置标题
- //string header = $"正在下载zip文件,当前版本名称【{directoryDownloadContext.startVersion}】";
- //if (context.setProgressHeaderText != null)
- //{
- // context.setProgressHeaderText(ProgressBarStyle.Marquee, header, directoryDownloadContext.speed);
- //}
- if (directoryDownloadContext.category == DirectoryTypeEnum.None)
- {
- err = "未选择有效大类!";
- return -1;
- }
- // 获取对应的交易码
- if (!TradeMap.TryGetValue(directoryDownloadContext.category, out var trade))
- {
- err = "未找到对应的目录类型";
- return -1;
- }
- // 检查是否应取消操作
- if (context.Worker.CancellationPending)
- {
- err = "操作取消!";
- return -1; // 或者其他表示取消的返回值
- }
- // 下载目录数据
- JObject joRtn = hBus.DownloadDirectory(trade, directoryDownloadContext.startVersion);
- if (JsonHelper.parseIrisRtnValue(joRtn, out err) != 0)
- {
- return -1;
- }
- directoryDownloadContext.txtPath = joRtn["filePath"]?.ToString();
- if (string.IsNullOrEmpty(directoryDownloadContext.txtPath))
- {
- err = "文件路径为空";
- return -1;
- }
- // 检查是否应取消操作
- if (context.Worker.CancellationPending)
- {
- err = "操作取消!";
- return -1; // 或者其他表示取消的返回值
- }
- // 导入数据
- DataImoport dataImport = new DataImoport();
- return dataImport.importDataToIrisByTxtNew(context,out err);
- }
- /// <summary>
- /// 循环下载
- /// </summary>
- /// <param name="startVer"></param>
- /// <param name="err"></param>
- /// <returns></returns>
- public int LoopDownload(DataLoaderContext context, out string err)
- {
- err = "";
- bool bStop = true;
- object[] args = context.args;
- DirectoryDownloadContext directoryDownloadContext = (DirectoryDownloadContext)args[0];
- while (bStop)
- {
- if (SingleDownload(context, out err) != 0)
- {
- Global.writeLog("err:" + err);
- bStop = false;
- return -1;
- }
- }
- return 0;
- }
- public int LoopDownload(string version, DirectoryTypeEnum category, Form frm, out string errorMessage)
- {
- try
- {
- errorMessage = string.Empty;
- // 导入数据
- DataImoport dataImport = new DataImoport();
- ProgressWrapper _dataLoader;
- _dataLoader = new ProgressWrapper(LoopDownload, frm);
- _dataLoader.SetProgressBar(ProgressBarStyle.Continuous, "正在下载...", 50);
- DirectoryDownloadContext directoryDownloadContext = new DirectoryDownloadContext();
- directoryDownloadContext.size = 30;
- directoryDownloadContext.category = category;
- directoryDownloadContext.startVersion = version;
- object[] o = new object[] { directoryDownloadContext };
- _dataLoader.Start(o, (result, errMsg) =>
- {
- if (result == 0)
- {
- MessageBox.Show("下载成功");
- }
- else
- {
- MessageBox.Show(errMsg);
- }
- return;
- });
- return 0;
- }
- catch (Exception ex)
- {
- errorMessage = $"SingleDownload 出现异常:{ex.Message}";
- Global.writeLog(errorMessage);
- return -1;
- }
- }
- #endregion
- }
- class DirectoryDownloadContext
- {
- public int size = 50;
- //public int directoryType = 1;
- public string txtPath = "";
- public string startVersion = "";
- public DirectoryTypeEnum category = DirectoryTypeEnum.None;
- public ProgressBarStyle progressBarStyle = ProgressBarStyle.Continuous;
- public string header = "";
- public int speed = 50;
- }
- }
|