DirectoryDownloadService.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. using Newtonsoft.Json.Linq;
  2. using PTMedicalInsurance.Business;
  3. using PTMedicalInsurance.Common;
  4. using PTMedicalInsurance.Forms.BasicDatas.services;
  5. using PTMedicalInsurance.Helper;
  6. using PTMedicalInsurance.Variables;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.ComponentModel;
  10. using System.Globalization;
  11. using System.Linq;
  12. using System.Text;
  13. using System.Threading.Tasks;
  14. using System.Windows.Forms;
  15. namespace PTMedicalInsurance.Forms.BasicDatas
  16. {
  17. class DirectoryDownloadService
  18. {
  19. //设置实例
  20. CenterBusiness cBus = new CenterBusiness();
  21. HisMainBusiness hBus = new HisMainBusiness();
  22. HisIrisServices hIS = new HisIrisServices();
  23. MIIrisServices mIS = new MIIrisServices();
  24. InvokeHelper invoker = new InvokeHelper();
  25. public event EventHandler<string> VerChangeEvent;
  26. private static readonly Dictionary<DirectoryTypeEnum, TradeEnum> TradeMap = new Dictionary<DirectoryTypeEnum, TradeEnum>
  27. {
  28. { DirectoryTypeEnum.Drug, TradeEnum.CMDirectory },
  29. { DirectoryTypeEnum.MedicalService, TradeEnum.MedicalServiceDirectory },
  30. { DirectoryTypeEnum.Consumables, TradeEnum.MaterialDirectory },
  31. { DirectoryTypeEnum.DiseaseDiagnosis, TradeEnum.DiseaseDiagDirectory },
  32. { DirectoryTypeEnum.Surgery, TradeEnum.OperationDirectory },
  33. { DirectoryTypeEnum.OutpatientChronicDiseases, TradeEnum.OPSDiseaseDirectory },
  34. { DirectoryTypeEnum.Drgs, TradeEnum.DRGDiseaseDirectory },
  35. { DirectoryTypeEnum.DaySurgery, TradeEnum.DayOperationDiseaseDirectory },
  36. { DirectoryTypeEnum.Dictionary, TradeEnum.DictionaryDownload }, // 不变
  37. { DirectoryTypeEnum.ChineseHerbalPieces, TradeEnum.CMPiecesDirectory },
  38. { DirectoryTypeEnum.HospMadeMedicines, TradeEnum.MedOrgSelfDrug },
  39. { DirectoryTypeEnum.TumorMorphology, TradeEnum.TumorDirectory },
  40. { DirectoryTypeEnum.TcmDisease, TradeEnum.CMDiseaseDirectory },
  41. { DirectoryTypeEnum.TcmSyndrome, TradeEnum.CMSyndromeDirectory }
  42. };
  43. #region 字典下载
  44. /// <summary>
  45. /// 字典下载,通过判断dicCode判断是单字典值下载还是全量下载
  46. /// </summary>
  47. /// <param name="joData"></param>
  48. /// <param name="dictCode"></param>
  49. /// <param name="errMsg"></param>
  50. /// <returns></returns>
  51. public int DownloadDictionary(JObject joData, string dictCode, out string errMsg)
  52. {
  53. errMsg = string.Empty;
  54. JObject joRtn = cBus.DownDictionay(joData);
  55. string listJson = JsonHelper.getDestValue(joRtn, "output.list");
  56. if (JsonHelper.parseCenterRtnValue(joRtn, out errMsg) != 0)
  57. {
  58. return -1;
  59. }
  60. JArray jaList = JArray.Parse(listJson);
  61. JObject joImportRtn = ImportDictionary(jaList, dictCode);
  62. if (JsonHelper.parseIrisRtnValue(joImportRtn, out errMsg) == 0)
  63. {
  64. return 0;
  65. }
  66. errMsg = "导入数据出现错误,请查看日志!" + errMsg;
  67. return -1;
  68. }
  69. /// <summary>
  70. /// 插入字典主表和明细表
  71. /// </summary>
  72. /// <param name="jaList"></param>
  73. /// <param name="type"></param>
  74. /// <returns></returns>
  75. public JObject ImportDictionary(JArray jaList, string type)
  76. {
  77. string errorMsg = string.Empty;
  78. int errorCount = 0;
  79. // 插入主表获取字典主键
  80. if (!TryInsertDictionary(type, out string hbDictionaryDr, out string mainErrorMsg))
  81. {
  82. return JsonHelper.setExceptionJson(-1, "insertDictionary", mainErrorMsg);
  83. }
  84. try
  85. {
  86. // 批量插入明细表
  87. JArray batch = new JArray();
  88. for (int i = 0; i < jaList.Count; i++)
  89. {
  90. var item = jaList[i];
  91. dynamic detail = new JObject();
  92. detail.ID = "";
  93. detail.updateUserID = Global.user.ID;
  94. detail.HospitalDr = Global.inf.hospitalDr;
  95. detail.InterfaceDr = Global.inf.interfaceDr;
  96. detail.HBDictionaryDr = hbDictionaryDr;
  97. detail.Code = item["value"]?.ToString();
  98. detail.Descripts = item["label"]?.ToString();
  99. batch.Add(detail);
  100. // 每50条提交一次
  101. if ((i + 1) % 50 == 0 || i == jaList.Count - 1)
  102. {
  103. JObject result = mIS.insertDictionaryDataDetail(batch);
  104. if (result["errorCode"]?.ToString() != "0")
  105. {
  106. errorCount++;
  107. errorMsg += $"\r\n{result["errorMessage"]}";
  108. }
  109. batch = new JArray(); // 重置批次
  110. }
  111. }
  112. return JsonHelper.setExceptionJson(0 - errorCount, errorMsg, null);
  113. }
  114. catch (Exception ex)
  115. {
  116. return JsonHelper.setExceptionJson(-1, "ImportDictionary", ex.Message);
  117. }
  118. }
  119. /// <summary>
  120. /// 插入主表并获取主键ID
  121. /// </summary>
  122. /// <param name="type"></param>
  123. /// <param name="hbDictionaryDr"></param>
  124. /// <param name="errorMsg"></param>
  125. /// <returns></returns>
  126. private bool TryInsertDictionary(string type, out string hbDictionaryDr, out string errorMsg)
  127. {
  128. hbDictionaryDr = string.Empty;
  129. errorMsg = string.Empty;
  130. try
  131. {
  132. JObject joData = new JObject
  133. {
  134. { "HospitalDr", Global.inf.hospitalDr },
  135. { "InterfaceDr", Global.inf.interfaceDr },
  136. { "InsuCode", type }
  137. };
  138. JObject joRtn = mIS.insertDictionary(joData);
  139. if (JsonHelper.parseIrisRtnValue(joRtn, out errorMsg) != 0)
  140. {
  141. return false;
  142. }
  143. hbDictionaryDr = JsonHelper.getDestValue(joRtn, "data.HBDictionaryDr");
  144. if (string.IsNullOrEmpty(hbDictionaryDr))
  145. {
  146. errorMsg = "HBDictionaryDr返回值为空";
  147. return false;
  148. }
  149. return true;
  150. }
  151. catch (Exception ex)
  152. {
  153. errorMsg = ex.Message;
  154. return false;
  155. }
  156. }
  157. #endregion
  158. #region 目录下载
  159. /// <summary>
  160. /// 下载指定类型的医保目录并导入数据库
  161. /// </summary>
  162. /// <param name="version">版本号</param>
  163. /// <param name="category">目录类型(使用枚举)</param>
  164. /// <param name="progressBar">进度条控件(可选)</param>
  165. /// <param name="errorMessage">输出错误信息</param>
  166. /// <returns>成功返回0,失败返回-1</returns>
  167. //public int SingleDownload(string version, DirectoryTypeEnum category,Form frm , out string errorMessage)
  168. //{
  169. // try
  170. // {
  171. // errorMessage = string.Empty;
  172. // // 获取对应的交易码
  173. // if (!TradeMap.TryGetValue(category, out var trade))
  174. // {
  175. // errorMessage = "未找到对应的目录类型";
  176. // return -1;
  177. // }
  178. // // 下载目录数据
  179. // JObject joRtn = hBus.DownloadDirectory(trade.GetCode(), version);
  180. // if (JsonHelper.parseIrisRtnValue(joRtn, out errorMessage) != 0)
  181. // {
  182. // return -1;
  183. // }
  184. // string filePath = joRtn["filePath"]?.ToString();
  185. // if (string.IsNullOrEmpty(filePath))
  186. // {
  187. // errorMessage = "文件路径为空";
  188. // return -1;
  189. // }
  190. // // 导入数据
  191. // DataImoport dataImport = new DataImoport();
  192. // ProgressWrapper _dataLoader;
  193. // _dataLoader = new ProgressWrapper(dataImport.importDataToIrisByTxtNew, frm);
  194. // _dataLoader.SetProgressBar(ProgressBarStyle.Continuous, "正在下载...", 50);
  195. // DirectoryDownloadContext directoryDownloadContext = new DirectoryDownloadContext();
  196. // directoryDownloadContext.txtPath = filePath;
  197. // directoryDownloadContext.size = 30;
  198. // directoryDownloadContext.directoryType = (int)category;
  199. // directoryDownloadContext.startVersion = version;
  200. // object[] o = new object[] { directoryDownloadContext};
  201. // _dataLoader.Start(o, (result, errMsg) =>
  202. // {
  203. // if (result == 0)
  204. // {
  205. // MessageBox.Show("下载成功");
  206. // }
  207. // else
  208. // {
  209. // MessageBox.Show(errMsg);
  210. // }
  211. // return ;
  212. // });
  213. // return 0;
  214. // }
  215. // catch (Exception ex)
  216. // {
  217. // errorMessage = $"SingleDownload 出现异常:{ex.Message}";
  218. // Global.writeLog(errorMessage);
  219. // return -1;
  220. // }
  221. //}
  222. public int SingleDownload(string version, DirectoryTypeEnum category, Form frm, out string errorMessage)
  223. {
  224. try
  225. {
  226. errorMessage = string.Empty;
  227. // 导入数据
  228. DataImoport dataImport = new DataImoport();
  229. ProgressWrapper _dataLoader;
  230. _dataLoader = new ProgressWrapper(SingleDownload, frm);
  231. _dataLoader.SetProgressBar(ProgressBarStyle.Continuous, "正在下载...", 50);
  232. DirectoryDownloadContext directoryDownloadContext = new DirectoryDownloadContext();
  233. directoryDownloadContext.size = 30;
  234. directoryDownloadContext.category = category;
  235. directoryDownloadContext.startVersion = version;
  236. object[] o = new object[] { directoryDownloadContext };
  237. _dataLoader.Start(o, (result, errMsg) =>
  238. {
  239. if (result == 0)
  240. {
  241. MessageBox.Show("下载成功");
  242. }
  243. else
  244. {
  245. MessageBox.Show(errMsg);
  246. }
  247. return;
  248. });
  249. return 0;
  250. }
  251. catch (Exception ex)
  252. {
  253. errorMessage = $"SingleDownload 出现异常:{ex.Message}";
  254. Global.writeLog(errorMessage);
  255. return -1;
  256. }
  257. }
  258. public int SingleDownload(DataLoaderContext context, out string err)
  259. {
  260. err = string.Empty;
  261. object[] args = context.args;
  262. DirectoryDownloadContext directoryDownloadContext = (DirectoryDownloadContext)args[0];
  263. ////设置标题
  264. //string header = $"正在下载zip文件,当前版本名称【{directoryDownloadContext.startVersion}】";
  265. //if (context.setProgressHeaderText != null)
  266. //{
  267. // context.setProgressHeaderText(ProgressBarStyle.Marquee, header, directoryDownloadContext.speed);
  268. //}
  269. if (directoryDownloadContext.category == DirectoryTypeEnum.None)
  270. {
  271. err = "未选择有效大类!";
  272. return -1;
  273. }
  274. // 获取对应的交易码
  275. if (!TradeMap.TryGetValue(directoryDownloadContext.category, out var trade))
  276. {
  277. err = "未找到对应的目录类型";
  278. return -1;
  279. }
  280. // 检查是否应取消操作
  281. if (context.Worker.CancellationPending)
  282. {
  283. err = "操作取消!";
  284. return -1; // 或者其他表示取消的返回值
  285. }
  286. // 下载目录数据
  287. JObject joRtn = hBus.DownloadDirectory(trade, directoryDownloadContext.startVersion);
  288. if (JsonHelper.parseIrisRtnValue(joRtn, out err) != 0)
  289. {
  290. return -1;
  291. }
  292. directoryDownloadContext.txtPath = joRtn["filePath"]?.ToString();
  293. if (string.IsNullOrEmpty(directoryDownloadContext.txtPath))
  294. {
  295. err = "文件路径为空";
  296. return -1;
  297. }
  298. // 检查是否应取消操作
  299. if (context.Worker.CancellationPending)
  300. {
  301. err = "操作取消!";
  302. return -1; // 或者其他表示取消的返回值
  303. }
  304. // 导入数据
  305. DataImoport dataImport = new DataImoport();
  306. return dataImport.importDataToIrisByTxtNew(context,out err);
  307. }
  308. /// <summary>
  309. /// 循环下载
  310. /// </summary>
  311. /// <param name="startVer"></param>
  312. /// <param name="err"></param>
  313. /// <returns></returns>
  314. public int LoopDownload(DataLoaderContext context, out string err)
  315. {
  316. err = "";
  317. bool bStop = true;
  318. object[] args = context.args;
  319. DirectoryDownloadContext directoryDownloadContext = (DirectoryDownloadContext)args[0];
  320. while (bStop)
  321. {
  322. if (SingleDownload(context, out err) != 0)
  323. {
  324. Global.writeLog("err:" + err);
  325. bStop = false;
  326. return -1;
  327. }
  328. }
  329. return 0;
  330. }
  331. public int LoopDownload(string version, DirectoryTypeEnum category, Form frm, out string errorMessage)
  332. {
  333. try
  334. {
  335. errorMessage = string.Empty;
  336. // 导入数据
  337. DataImoport dataImport = new DataImoport();
  338. ProgressWrapper _dataLoader;
  339. _dataLoader = new ProgressWrapper(LoopDownload, frm);
  340. _dataLoader.SetProgressBar(ProgressBarStyle.Continuous, "正在下载...", 50);
  341. DirectoryDownloadContext directoryDownloadContext = new DirectoryDownloadContext();
  342. directoryDownloadContext.size = 30;
  343. directoryDownloadContext.category = category;
  344. directoryDownloadContext.startVersion = version;
  345. object[] o = new object[] { directoryDownloadContext };
  346. _dataLoader.Start(o, (result, errMsg) =>
  347. {
  348. if (result == 0)
  349. {
  350. MessageBox.Show("下载成功");
  351. }
  352. else
  353. {
  354. MessageBox.Show(errMsg);
  355. }
  356. return;
  357. });
  358. return 0;
  359. }
  360. catch (Exception ex)
  361. {
  362. errorMessage = $"SingleDownload 出现异常:{ex.Message}";
  363. Global.writeLog(errorMessage);
  364. return -1;
  365. }
  366. }
  367. #endregion
  368. }
  369. class DirectoryDownloadContext
  370. {
  371. public int size = 50;
  372. //public int directoryType = 1;
  373. public string txtPath = "";
  374. public string startVersion = "";
  375. public DirectoryTypeEnum category = DirectoryTypeEnum.None;
  376. public ProgressBarStyle progressBarStyle = ProgressBarStyle.Continuous;
  377. public string header = "";
  378. public int speed = 50;
  379. }
  380. }