You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

331 lines
17 KiB

6 months ago
4 weeks ago
6 months ago
4 weeks ago
6 months ago
4 months ago
6 months ago
5 months ago
6 months ago
6 months ago
6 months ago
5 months ago
6 months ago
3 months ago
6 months ago
3 months ago
6 months ago
6 months ago
6 months ago
  1. using Dapper;
  2. using Npgsql;
  3. using ServiceReferenceDianLisReport;
  4. using ServiceReferenceDianLisResult;
  5. using ServiceReferenceHzcyHis;
  6. using Shentun.Peis.ImportLisResults;
  7. using Shentun.Peis.PlugIns.ChargeRequests;
  8. using Shentun.Peis.PlugIns.Extensions.ChargeRequests.Hzcy;
  9. using Shentun.Peis.PlugIns.ImportLisResults;
  10. using Shentun.Peis.PlugIns.LisRequests;
  11. using Shentun.Peis.PlugIns.PatientRegisters;
  12. using Shentun.Peis.PrintReports;
  13. using Shentun.Peis.RegisterCheckPictures;
  14. using System;
  15. using System.Collections.Generic;
  16. using System.Data.Common;
  17. using System.Linq;
  18. using System.Net.Http;
  19. using System.ServiceModel;
  20. using System.Text;
  21. using System.Threading.Tasks;
  22. using Volo.Abp.Application.Dtos;
  23. namespace Shentun.Peis.PlugIns.Extensions.ImportLisResults.Dian
  24. {
  25. public class ImportLisResultPlugInsDian : ImportLisResultPlugInsBase
  26. {
  27. private readonly string _clientID;
  28. private readonly string _clientGUID;
  29. public ImportLisResultPlugInsDian(Guid thirdInterfaceId) : base(thirdInterfaceId)
  30. {
  31. _clientID = InterfaceConfig.GetSection("Interface").GetSection("ClientID").Value;
  32. _clientGUID = InterfaceConfig.GetSection("Interface").GetSection("ClientGUID").Value;
  33. }
  34. public ImportLisResultPlugInsDian(string parmValue) : base(parmValue)
  35. {
  36. _clientID = InterfaceConfig.GetSection("Interface").GetSection("ClientID").Value;
  37. _clientGUID = InterfaceConfig.GetSection("Interface").GetSection("ClientGUID").Value;
  38. }
  39. public override Task<ImportLisResultPlugInsOut> ImportResultByPatientRegisterIdAsync(Guid patientRegisterId)
  40. {
  41. return base.ImportResultByPatientRegisterIdAsync(patientRegisterId);
  42. }
  43. public override async Task DoWork()
  44. {
  45. var queryDaysStr = InterfaceConfig.GetSection("Interface").GetSection("Scheduler").GetSection("QueryDays").Value;
  46. if (string.IsNullOrWhiteSpace(queryDaysStr))
  47. {
  48. queryDaysStr = "1";
  49. }
  50. if (!int.TryParse(queryDaysStr, out int days))
  51. {
  52. days = 1;
  53. }
  54. var patientRegisters = GetRequestPatientRegistersDianAsync(days).Result;
  55. foreach (var patientRegister in patientRegisters)
  56. {
  57. await ImportOueSendResultByPatientRegisterIdAsync(patientRegister.PatientRegisterId);
  58. }
  59. }
  60. public async Task<ImportLisResultPlugInsOut> ImportOueSendResultByPatientRegisterIdAsync(Guid patientRegisterId)
  61. {
  62. var lisResultAddress = InterfaceConfig.GetSection("Interface").GetSection("LisResultAddress").Value;
  63. var lisReportAddress = InterfaceConfig.GetSection("Interface").GetSection("LisReportAddress").Value;
  64. using (var client = CreateClient(lisResultAddress))
  65. {
  66. // 设置超时时间
  67. client.InnerChannel.OperationTimeout = TimeSpan.FromMinutes(2);
  68. var execOrganizationUnitIdStr = InterfaceConfig.GetSection("Interface").GetSection("ExecOrganizationUnitId").Value;
  69. var defaultCheckDoctorName = InterfaceConfig.GetSection("Interface").GetSection("DefaultCheckDoctorName").Value;
  70. var barcodeMode = InterfaceConfig.GetSection("Interface").GetSection("BarcodeMode").Value; //0-人员条码 1-检验条码
  71. #region 导入数据
  72. List<CommonTableOut> itemCommonTableOuts = new List<CommonTableOut>();
  73. List<CommonTableOut> asbitemCommonTableOuts = new List<CommonTableOut>();
  74. string sql_item = $"select data_code as dian_code,display_name as item_id from common_table " +
  75. $" where common_table_type_id='{InterfaceConfig.GetSection("Interface").GetSection("ItemCommonTableTypeId").Value}' ";
  76. string sql_asbitem = $"select data_code as dian_code,display_name as item_id from common_table " +
  77. $" where common_table_type_id='{InterfaceConfig.GetSection("Interface").GetSection("AsbitemCommonTableTypeId").Value}' ";
  78. var result = new ImportLisResultPlugInsOut();
  79. using (DbConnection conn = new NpgsqlConnection(AppConnctionStr))
  80. {
  81. itemCommonTableOuts = (await conn.QueryAsync<CommonTableOut>(sql_item)).ToList();
  82. asbitemCommonTableOuts = (await conn.QueryAsync<CommonTableOut>(sql_asbitem)).ToList();
  83. try
  84. {
  85. var createImportLisResultDtos = new List<CreateImportLisResultDto>();
  86. //设置结果,生成小结
  87. string register_item_sql = $"select d.lis_request_no,f.item_id::TEXT as item_id,g.display_name as item_name,patient_register_no" +
  88. " from patient_register as a " +
  89. " left join register_check as b on a.id=b.patient_register_id " +
  90. " left join register_check_asbitem as c on b.id=c.register_check_id " +
  91. " left join lis_request as d on c.lis_request_id=d.id " +
  92. " left join asbitem as e on c.asbitem_id=e.id " +
  93. " left join register_check_item as f on b.id=f.register_check_id " +
  94. " left join item as g on f.item_id=g.id " +
  95. $" where a.id='{patientRegisterId}' and e.is_outsend='Y' and b.is_audit='N' ";
  96. var lisResultFromInterfaces = (await conn.QueryAsync<LisResultFromImportInterface>(register_item_sql)).ToList();
  97. string barCode = lisResultFromInterfaces.FirstOrDefault().PatientRegisterNo;
  98. string barCodeDian = "";
  99. //常规结果获取
  100. var result_cg = await client.GetDetailByHospCodeAsync(_clientID, _clientGUID, barCode, 0);
  101. var resultObj_cg = XmlHelper.DeserializeXmlAddRoot<GetDetailByHospCodeNormalOut>(result_cg.Body.GetDetailByHospCodeResult);
  102. if (resultObj_cg.ResultsDataSet != null && resultObj_cg.ResultsDataSet.Tables.Count > 0)
  103. {
  104. barCodeDian = resultObj_cg.ResultsDataSet.Tables.FirstOrDefault().Barcode;
  105. }
  106. //病理结果
  107. var result_bl = await client.GetDetailByHospCodeAsync(_clientID, _clientGUID, barCode, 1);
  108. var resultObj_bl = XmlHelper.DeserializeXmlAddRoot<GetDetailByHospCodePathologyOut>(result_bl.Body.GetDetailByHospCodeResult);
  109. if (resultObj_bl.ResultsDataSet != null && resultObj_bl.ResultsDataSet.Tables.Count > 0 && string.IsNullOrWhiteSpace(barCodeDian))
  110. {
  111. barCodeDian = resultObj_bl.ResultsDataSet.Tables.FirstOrDefault().Barcode;
  112. }
  113. foreach (var li in lisResultFromInterfaces)
  114. {
  115. var dian_code = itemCommonTableOuts.FirstOrDefault(f => f.ItemId == li.ItemId);
  116. if (dian_code != null)
  117. {
  118. if (resultObj_cg.ResultsDataSet != null)
  119. {
  120. var dianResult = resultObj_cg.ResultsDataSet.Tables.FirstOrDefault(f => f.S == dian_code.DianCode);
  121. if (dianResult != null)
  122. {
  123. var createImportLisResultDto = new CreateImportLisResultDto()
  124. {
  125. LisRequestNo = li.LisRequestNo,
  126. ItemId = Guid.Parse(li.ItemId),
  127. ItemName = li.ItemName,
  128. Result = dianResult.FinalResult,
  129. // Unit = lisResult.Unit,
  130. // ReferenceRangeValue = lisResult.ReferenceRangeValue,
  131. // CriticalRangeValue = lisResult.CriticalRangeValue,
  132. // ResultStatusId = lisResult.ResultStatusId,
  133. // ReportPrompt = lisResult.ReportPrompt,
  134. CheckDoctorName = defaultCheckDoctorName,
  135. //CheckDate = dianResult.ApproveDate,
  136. CheckDate = dianResult.CollectDate,
  137. ExecOrganizationUnitId = Guid.Parse(execOrganizationUnitIdStr)
  138. };
  139. createImportLisResultDtos.Add(createImportLisResultDto);
  140. }
  141. }
  142. if (resultObj_bl.ResultsDataSet != null)
  143. {
  144. var dianResult = resultObj_bl.ResultsDataSet.Tables.FirstOrDefault(f => f.TestCode == dian_code.DianCode);
  145. if (dianResult != null)
  146. {
  147. var createImportLisResultDto = new CreateImportLisResultDto()
  148. {
  149. LisRequestNo = li.LisRequestNo,
  150. ItemId = Guid.Parse(li.ItemId),
  151. ItemName = li.ItemName,
  152. Result = dianResult.Repidea,
  153. // Unit = lisResult.Unit,
  154. // ReferenceRangeValue = lisResult.ReferenceRangeValue,
  155. // CriticalRangeValue = lisResult.CriticalRangeValue,
  156. // ResultStatusId = lisResult.ResultStatusId,
  157. // ReportPrompt = lisResult.ReportPrompt,
  158. CheckDoctorName = defaultCheckDoctorName,
  159. // CheckDate = dianResult.ReportDate,
  160. CheckDate = dianResult.CollectDate,
  161. ExecOrganizationUnitId = Guid.Parse(execOrganizationUnitIdStr)
  162. };
  163. createImportLisResultDtos.Add(createImportLisResultDto);
  164. }
  165. }
  166. }
  167. }
  168. if (createImportLisResultDtos.Any())
  169. {
  170. var callResult = await CallAppServiceAsync<List<CreateImportLisResultDto>, object>("api/app/ImportLisResult/ImportResult", createImportLisResultDtos);
  171. }
  172. //导入图片
  173. using (var clientReport = CreateReportClient(lisReportAddress))
  174. {
  175. var result_report = await clientReport.GetReportInfoAsync(_clientID, _clientGUID, barCodeDian, "0");
  176. var resultObj_report = XmlHelper.DeserializeXmlAddRoot<GetReportInfoOut>(result_report.Body.GetReportInfoResult);
  177. if (resultObj_report.Msg.Code == "1" && resultObj_report.ResultsDataSetReport != null && resultObj_report.ResultsDataSetReport.Tables.Any())
  178. {
  179. var ImportReportInputs = new List<UploadRegisterCheckPictureManyDto>();
  180. foreach (var itemReport in resultObj_report.ResultsDataSetReport.Tables)
  181. {
  182. var base64str = ToBase64(itemReport.PicReportUrl);
  183. var asbitemIds = asbitemCommonTableOuts.Where(m => m.DianCode == itemReport.TestCode).ToList();
  184. if (asbitemIds.Any())
  185. {
  186. string sql_register_check_id = "select distinct b.id as register_check_id from patient_register as a " +
  187. " left join register_check as b on a.id=b.patient_register_id " +
  188. " left join register_check_asbitem as c on b.id=c.register_check_id " +
  189. $" where a.id='{patientRegisterId}' and c.asbitem_id=ANY(@asbitemIds) ";
  190. var registerCheckIds = (await conn.QueryAsync<RegisterCheckIdInputDto>(sql_register_check_id, new
  191. {
  192. asbitemIds = asbitemIds.Select(s => Guid.Parse(s.ItemId)).ToArray()
  193. })).ToList();
  194. foreach (var registerCheckId in registerCheckIds)
  195. {
  196. List<UploadRegisterCheckPictureManyPictureBaseStrsDto> pictureBaseStrs = new List<UploadRegisterCheckPictureManyPictureBaseStrsDto>();
  197. pictureBaseStrs.Add(new UploadRegisterCheckPictureManyPictureBaseStrsDto
  198. {
  199. PictureBaseStr = base64str,
  200. FileName = itemReport.TestCode,
  201. IsPrint = 'Y'
  202. });
  203. ImportReportInputs.Add(new UploadRegisterCheckPictureManyDto
  204. {
  205. PictureBaseStrs = pictureBaseStrs,
  206. PictureFileType = '0',
  207. RegisterCheckId = registerCheckId.RegisterCheckId
  208. });
  209. }
  210. }
  211. }
  212. foreach (var importReportInput in ImportReportInputs)
  213. {
  214. var callResult = await CallAppServiceAsync<UploadRegisterCheckPictureManyDto, object>("api/app/registercheckpicture/uploadregistercheckpicturemany", importReportInput);
  215. }
  216. }
  217. }
  218. }
  219. catch (Exception ex)
  220. { }
  221. }
  222. #endregion
  223. }
  224. return new ImportLisResultPlugInsOut();
  225. }
  226. private RasClientDetailSoapClient CreateClient(string url)
  227. {
  228. return new RasClientDetailSoapClient(new BasicHttpBinding(),
  229. new EndpointAddress(url));
  230. }
  231. private ReportServiceSoapClient CreateReportClient(string url)
  232. {
  233. return new ReportServiceSoapClient(new BasicHttpBinding(),
  234. new EndpointAddress(url));
  235. }
  236. private string ToBase64(string filename)
  237. {
  238. Stream stream;
  239. string fileBase64;
  240. if (filename.StartsWith("http"))
  241. {
  242. using (var httpClient = new HttpClient())
  243. {
  244. var response = httpClient.GetAsync(filename).Result;
  245. if (response.IsSuccessStatusCode)
  246. {
  247. var contentStream = response.Content.ReadAsStreamAsync().Result;
  248. using var memoryStream = new MemoryStream();
  249. contentStream.CopyTo(memoryStream);
  250. var fileBytes = memoryStream.ToArray();
  251. return Convert.ToBase64String(fileBytes);
  252. }
  253. else
  254. {
  255. throw new Exception("转换base64时,返回错误:" + response.StatusCode);
  256. }
  257. }
  258. }
  259. else
  260. {
  261. using (stream = new FileStream(filename, FileMode.Open, FileAccess.Read))
  262. {
  263. byte[] byteData = new byte[stream.Length];
  264. stream.Read(byteData, 0, byteData.Length);
  265. fileBase64 = Convert.ToBase64String(byteData);
  266. stream.Close();
  267. }
  268. return fileBase64;
  269. }
  270. }
  271. }
  272. }