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.

169 lines
7.1 KiB

2 years ago
  1. using Dapper;
  2. using Newtonsoft.Json.Converters;
  3. using Newtonsoft.Json;
  4. using Npgsql;
  5. using NPOI.SS.Formula.Functions;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Data.Common;
  9. using System.Linq;
  10. using System.Net.Http.Headers;
  11. using System.Text;
  12. using System.Threading.Tasks;
  13. using System.Text.RegularExpressions;
  14. namespace Shentun.Peis.PlugIns.Gem
  15. {
  16. public class ImportPatientRegisterPlugInsGem : ImportPatientRegisterPlugInsBase
  17. {
  18. private Guid _importCustomerOrgId;
  19. private string _hospitalId;
  20. private string _aesKEY;
  21. private string _year;
  22. public ImportPatientRegisterPlugInsGem(string parmValue) : base(parmValue)
  23. {
  24. }
  25. public override async Task ImportAsync()
  26. {
  27. await InitAsync();
  28. var qztlPatientRegisterFromInterface = await CallInterfaceServiceAsync();
  29. if (qztlPatientRegisterFromInterface == null || !qztlPatientRegisterFromInterface.plans.Any())
  30. {
  31. return;
  32. }
  33. foreach(var patient in qztlPatientRegisterFromInterface.plans)
  34. {
  35. }
  36. }
  37. public async Task InitAsync()
  38. {
  39. var customerOrgIdStr = InterfaceConfig.GetSection("Interface").GetSection("单位编号").Value;
  40. _hospitalId = InterfaceConfig.GetSection("Interface").GetSection("HospitalId").Value;
  41. _aesKEY = InterfaceConfig.GetSection("Interface").GetSection("aesKEY").Value;
  42. if (string.IsNullOrWhiteSpace(customerOrgIdStr))
  43. {
  44. return;
  45. }
  46. if (!Guid.TryParse(customerOrgIdStr, out _importCustomerOrgId))
  47. {
  48. throw new Exception("单位编号不正确");
  49. }
  50. if (string.IsNullOrWhiteSpace(_hospitalId))
  51. {
  52. throw new Exception("HospitalId参数不能为空");
  53. }
  54. if (string.IsNullOrWhiteSpace(_aesKEY))
  55. {
  56. throw new Exception("_aesKEY参数不能为空");
  57. }
  58. using (DbConnection conn = new NpgsqlConnection(AppConnctionStr))
  59. {
  60. string sql;
  61. sql = @"
  62. SELECT id as customer_org_id,
  63. display_name as customer_org_name,
  64. parent_id ,
  65. path_code
  66. from
  67. customer_org
  68. where id = @CustomerOrgId
  69. ";
  70. var customerOrgForPlugInss = (await conn.QueryAsync<CustomerOrgForPlugIns>(sql,
  71. new { CustomerOrgId = _importCustomerOrgId })).FirstOrDefault();
  72. if (customerOrgForPlugInss == null)
  73. {
  74. throw new Exception("单位编号不正确,找不到单位");
  75. }
  76. var year = customerOrgForPlugInss.CustomerOrgName.Substring(
  77. customerOrgForPlugInss.CustomerOrgName.Length - 5
  78. , 4);
  79. if (!int.TryParse(year, out var yearNumber))
  80. {
  81. throw new Exception("单位名称必须是以2000年这样的格式结尾");
  82. }
  83. if (yearNumber < 2024 || yearNumber > 2050)
  84. {
  85. throw new Exception("单位名称年限范围必须在2024-2050之间");
  86. }
  87. _year = year;
  88. }
  89. }
  90. public async Task<QztlPatientRegisterFromInterface?> CallInterfaceServiceAsync()
  91. {
  92. string baseAddress = InterfaceWebApiUrl;
  93. string ary = "HospitalId=" + _hospitalId + (Char)38 + "year=" + _year + (Char)38 + "AesKey=" + _aesKEY;
  94. baseAddress = baseAddress + ary;
  95. using (var httpClientHandler = new HttpClientHandler())
  96. {
  97. using (var httpClient = new HttpClient(httpClientHandler))
  98. {
  99. httpClient.BaseAddress = new Uri(baseAddress);
  100. httpClient.DefaultRequestHeaders.Accept.Add(
  101. new MediaTypeWithQualityHeaderValue("application/x-www-form-urlencoded"));//设置accept标头,告诉JSON是可接受的响应类型
  102. //httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", _accesToken);
  103. IsoDateTimeConverter timeFormat = new IsoDateTimeConverter();
  104. timeFormat.DateTimeFormat = "yyyy-MM-dd HH:mm:ss";
  105. //var sendData = JsonConvert.SerializeObject(data, Newtonsoft.Json.Formatting.Indented, timeFormat);
  106. using (HttpContent httpContent = new StringContent(""))
  107. {
  108. httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/x-www-form-urlencoded");
  109. HttpResponseMessage response = await httpClient.GetAsync("");
  110. string result;
  111. if (!response.IsSuccessStatusCode)
  112. {
  113. result = response.Content.ReadAsStringAsync().Result;
  114. throw new Exception("http通信错误:" + response.StatusCode + ",结果:" + result);
  115. }
  116. result = await response.Content.ReadAsStringAsync();
  117. result = result.Replace(":\"[{", ":[{").Replace("}]\"", "}]").Replace("\\", "");
  118. QztlPatientRegisterFromInterface? resultDto = JsonConvert.DeserializeObject<QztlPatientRegisterFromInterface>(result);
  119. if (resultDto != null)
  120. {
  121. if (resultDto.status != 0)
  122. {
  123. throw new Exception($"调用WebApi失败,返回错误,消息:" + resultDto.status + resultDto.errorMsg);
  124. }
  125. //插入数据库中备份
  126. using (DbConnection conn = new NpgsqlConnection(AppConnctionStr))
  127. {
  128. string sql;
  129. var qztlImportDataEntity = new QztlImportDataEntity()
  130. {
  131. Id = Guid.NewGuid(),
  132. Data = result,
  133. DataType = '0',
  134. IsComplete = 'Y',
  135. CreationTime = DateTime.Now,
  136. };
  137. sql = @"insert into qztl_import_data(id, data, data_type, is_complete,creation_time)
  138. values(@Id, @Data, @DataType, @IsComplete,@CreationTime)";
  139. conn.Execute(sql, qztlImportDataEntity);
  140. }
  141. return (QztlPatientRegisterFromInterface)resultDto;
  142. }
  143. return null;
  144. }
  145. }
  146. }
  147. }
  148. }
  149. }