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.

270 lines
10 KiB

1 month ago
  1. using Dapper;
  2. using Microsoft.Extensions.Configuration;
  3. using Microsoft.Identity.Client;
  4. using Newtonsoft.Json;
  5. using Npgsql;
  6. using ServiceReferenceDianLisReport;
  7. using ServiceReferenceDianLisResult;
  8. using Shentun.Peis.Enums;
  9. using Shentun.Peis.ImportLisResults;
  10. using Shentun.Peis.PlugIns.ColumnReferences;
  11. using Shentun.Peis.PlugIns.Extensions.ImportLisResults.Dian;
  12. using Shentun.Peis.PlugIns.ImportLisResults;
  13. using Shentun.Peis.PlugIns.ThirdPushs.Hty;
  14. using Shentun.Peis.PlugIns.WebAppoints;
  15. using Shentun.Peis.PrintReports;
  16. using Shentun.Peis.RegisterCheckPictures;
  17. using Shentun.Peis.ThirdInterfaces;
  18. using Spire.Pdf.Exporting.XPS.Schema;
  19. using System;
  20. using System.Collections.Generic;
  21. using System.Data;
  22. using System.Data.Common;
  23. using System.Linq;
  24. using System.ServiceModel;
  25. using System.Text;
  26. using System.Threading.Tasks;
  27. using Volo.Abp;
  28. using static OpenIddict.Abstractions.OpenIddictConstants;
  29. namespace Shentun.Peis.PlugIns.Extensions.ThirdPushs.Hty
  30. {
  31. public class PushPeisResultPlugInsHty : PushPeisResultPlugInsBase
  32. {
  33. public PushPeisResultPlugInsHty(Guid thirdInterfaceId) : base(thirdInterfaceId)
  34. {
  35. }
  36. public async Task DoWork()
  37. {
  38. List<Guid> patientRegisters = await GetRequestPatientRegistersHtyAsync();
  39. foreach (var patientRegisterId in patientRegisters)
  40. {
  41. await PushHtyPeisResultByPatientRegisterIdAsync(patientRegisterId);
  42. }
  43. }
  44. /// <summary>
  45. /// 根据id推送信息到海豚云
  46. /// </summary>
  47. /// <param name="patientRegisterId"></param>
  48. /// <returns></returns>
  49. /// <exception cref="UserFriendlyException"></exception>
  50. public async Task<string> PushHtyPeisResultByPatientRegisterIdAsync(Guid patientRegisterId)
  51. {
  52. var pushBaseApi = InterfaceConfig.GetValue("Interface:PushBaseApi", "");
  53. var columnReferenceId = InterfaceConfig.GetValue("Interface:ColumnReferenceId", "");
  54. using (DbConnection conn = new NpgsqlConnection(AppConnctionStr))
  55. {
  56. var sql_patient = $@" select b.patient_no,b.id_no,b.birth_date,a.medical_times,b.sex_id,a.patient_name,
  57. a.medical_start_date,a.patient_register_no,a.complete_flag from patient_register as a left join
  58. patient as b on a.patient_id=b.id where a.id=@patient_register_id ";
  59. var patientRegisterSqlEntity = (await conn.QueryFirstAsync(sql_patient,
  60. new { patient_register_id = patientRegisterId }));
  61. if (patientRegisterSqlEntity != null)
  62. {
  63. if (patientRegisterSqlEntity.complete_flag != "3")
  64. {
  65. throw new UserFriendlyException("人员为总检");
  66. }
  67. var pushRequest = new PushHtyDataInputDto();
  68. pushRequest.info = new PushHtyDataInputPatientDto
  69. {
  70. USERID = patientRegisterSqlEntity.patient_no.ToString(),
  71. CERTNO = patientRegisterSqlEntity.id_no.ToString(),
  72. BIRTHDATE = !string.IsNullOrWhiteSpace(patientRegisterSqlEntity.birth_date.ToString()) ? Convert.ToDateTime(patientRegisterSqlEntity.birth_date).ToString("yyyy-MM-dd") : "",
  73. USERTIMES = patientRegisterSqlEntity.medical_times.ToString(),
  74. GENDER = ConertSex(patientRegisterSqlEntity.sex_id.ToString()),
  75. NAME = patientRegisterSqlEntity.patient_name.ToString(),
  76. MEDICALDATE = Convert.ToDateTime(patientRegisterSqlEntity.medical_start_date).ToString("yyyy-MM-dd HH:mm:ss"),
  77. MEDICALCODE = patientRegisterSqlEntity.patient_register_no.ToString(),
  78. RORGNAME = ""
  79. };
  80. //查看项目
  81. var sql_item = $@" select c.display_name as item_name,b.* from register_check as a
  82. left join register_check_item as b on a.id=b.register_check_id
  83. left join item as c on b.item_id=c.id where a.patient_register_id=@patient_register_id ";
  84. var registerItemSqlEntity = await conn.QueryAsync(sql_item,
  85. new { patient_register_id = patientRegisterId });
  86. if (registerItemSqlEntity.Count() > 0)
  87. {
  88. #region 获取对照数据
  89. var sql_dz = $@" select a.code_value,b.interface_code_value from column_reference_code as a
  90. left join column_reference_interface as b on a.id=b.column_reference_code_id
  91. where a.column_reference_id=@column_reference_id ";
  92. var itemDZSqlEntity = (await conn.QueryAsync(sql_dz,
  93. new { column_reference_id = Guid.Parse(columnReferenceId) })).ToList();
  94. #endregion
  95. Dictionary<string, pushHtyDataItemDto> contents = new Dictionary<string, pushHtyDataItemDto>();
  96. foreach (var itemRow in registerItemSqlEntity)
  97. {
  98. var itemCode = itemDZSqlEntity.FirstOrDefault(f => f.code_value == itemRow.item_id.ToString());
  99. if (itemCode != null)
  100. {
  101. var constr = new pushHtyDataItemDto
  102. {
  103. dicValue = itemRow.reference_range_value.ToString(),
  104. ordinary = 1,
  105. p_value = itemRow.result.ToString(),
  106. sysCode = itemCode.interface_code_value,
  107. unit = itemRow.unit.ToString()
  108. };
  109. var dcIndex = contents.Count + 1;
  110. contents.Add("con" + dcIndex, constr);
  111. }
  112. }
  113. pushRequest.content = contents;
  114. }
  115. string pushRequestJsonString = JsonConvert.SerializeObject(pushRequest, Formatting.Indented);
  116. //推送信息
  117. var formContent = new MultipartFormDataContent();
  118. formContent.Add(new StringContent(_thirdToken), "token");
  119. formContent.Add(new StringContent("fbpc"), "urlFrom");
  120. formContent.Add(new StringContent("-1"), "tmId");
  121. formContent.Add(new StringContent(pushRequestJsonString, Encoding.UTF8, "application/json"), "pushData");
  122. var result = await HtyHttpPostHelper.PostFormDataAsync($"{pushBaseApi}/api/manageFrame/healthCheck/medicalInterfaceDataFetch.action", formContent, "PostmanRuntime-ApipostRuntime/1.1.0");
  123. var resultModel = JsonConvert.DeserializeObject<PushHtyDataResultDto>(result);
  124. if (resultModel.stateMessage == "suc")
  125. {
  126. #region 更新人员状态
  127. #endregion
  128. }
  129. return $"{pushRequest.info.NAME}_{pushRequest.info.MEDICALCODE}_推送结果:{resultModel.stateMessage}";
  130. }
  131. else
  132. {
  133. throw new UserFriendlyException("人员不存在");
  134. }
  135. }
  136. }
  137. /// <summary>
  138. /// 获取需要推送的人员id集合
  139. /// </summary>
  140. /// <returns></returns>
  141. public async Task<List<Guid>> GetRequestPatientRegistersHtyAsync()
  142. {
  143. var patientRegisterIds = new List<Guid>();
  144. var queryDaysStr = InterfaceConfig.GetValue("Interface:Scheduler:QueryDays", "");
  145. var isAuditWhere = InterfaceConfig.GetValue("Interface:Scheduler:IsAuditWhere", "");
  146. var uploadDateType = InterfaceConfig.GetValue("Interface:Scheduler:UploadDateType", "");
  147. var otherWhere = InterfaceConfig.GetValue("Interface:Scheduler:OtherWhere", "");
  148. var uploadCountLimit = InterfaceConfig.GetValue("Interface:Scheduler:UploadCountLimit", "");
  149. if (string.IsNullOrWhiteSpace(queryDaysStr))
  150. {
  151. queryDaysStr = "1";
  152. }
  153. if (!int.TryParse(queryDaysStr, out int days))
  154. {
  155. days = 1;
  156. }
  157. using (DbConnection conn = new NpgsqlConnection(AppConnctionStr))
  158. {
  159. string sql;
  160. sql = @"
  161. SELECT
  162. id
  163. from patient_register
  164. where complete_flag = @CompleteFlag ";
  165. if (uploadDateType == "1")
  166. {
  167. sql += " and summary_date>= @HandDate ";
  168. }
  169. else if (uploadDateType == "2")
  170. {
  171. sql += " and audit_date>= @HandDate ";
  172. }
  173. else
  174. {
  175. sql += " and last_modification_time>= @HandDate ";
  176. }
  177. if (isAuditWhere == "Y")
  178. {
  179. sql += " and is_audit='Y' ";
  180. }
  181. if (!string.IsNullOrWhiteSpace(otherWhere))
  182. {
  183. sql += otherWhere;
  184. }
  185. var parameters = new DynamicParameters();
  186. parameters.Add("HandDate", DateTime.Now.Date.AddDays(-days));
  187. parameters.Add("CompleteFlag", PatientRegisterCompleteFlag.SumCheck);
  188. sql += $" limit {uploadCountLimit} ";
  189. var patientRegisterSqlList = (await conn.QueryAsync<SyncPatientRegisterIdsDto>(sql, parameters)).ToList();
  190. patientRegisterIds = patientRegisterSqlList.Select(s => s.Id).ToList();
  191. }
  192. return patientRegisterIds;
  193. }
  194. private string ConertSex(string sexId)
  195. {
  196. string res = "";
  197. if (sexId == "M")
  198. {
  199. res = "男";
  200. }
  201. else if (sexId == "F")
  202. {
  203. res = "女";
  204. }
  205. else
  206. {
  207. res = "未知";
  208. }
  209. return res;
  210. }
  211. }
  212. }