using Dapper; using Microsoft.Extensions.Configuration; using Microsoft.Identity.Client; using Newtonsoft.Json; using Npgsql; using ServiceReferenceDianLisReport; using ServiceReferenceDianLisResult; using Shentun.Peis.Enums; using Shentun.Peis.ImportLisResults; using Shentun.Peis.PlugIns.ColumnReferences; using Shentun.Peis.PlugIns.Extensions.ImportLisResults.Dian; using Shentun.Peis.PlugIns.ImportLisResults; using Shentun.Peis.PlugIns.ThirdPushs.Hty; using Shentun.Peis.PlugIns.WebAppoints; using Shentun.Peis.PrintReports; using Shentun.Peis.RegisterCheckPictures; using Shentun.Peis.ThirdInterfaces; using Spire.Pdf.Exporting.XPS.Schema; using System; using System.Collections.Generic; using System.Data; using System.Data.Common; using System.Linq; using System.ServiceModel; using System.Text; using System.Threading.Tasks; using Volo.Abp; using static OpenIddict.Abstractions.OpenIddictConstants; namespace Shentun.Peis.PlugIns.Extensions.ThirdPushs.Hty { public class PushPeisResultPlugInsHty : PushPeisResultPlugInsBase { public PushPeisResultPlugInsHty(Guid thirdInterfaceId) : base(thirdInterfaceId) { } public async Task DoWork() { List patientRegisters = await GetRequestPatientRegistersHtyAsync(); foreach (var patientRegisterId in patientRegisters) { await PushHtyPeisResultByPatientRegisterIdAsync(patientRegisterId); } } /// /// 根据id推送信息到海豚云 /// /// /// /// public async Task PushHtyPeisResultByPatientRegisterIdAsync(Guid patientRegisterId) { var pushBaseApi = InterfaceConfig.GetValue("Interface:PushBaseApi", ""); var columnReferenceId = InterfaceConfig.GetValue("Interface:ColumnReferenceId", ""); var token = GetThirdToken(); if (string.IsNullOrWhiteSpace(token)) { throw new UserFriendlyException("获取token失败"); } using (DbConnection conn = new NpgsqlConnection(AppConnctionStr)) { var sql_patient = $@" select b.patient_no,b.id_no,b.birth_date,a.medical_times,b.sex_id,a.patient_name,b.address,b.mobile_telephone,b.telephone, a.medical_start_date,a.patient_register_no,a.complete_flag from patient_register as a left join patient as b on a.patient_id=b.id where a.id=@patient_register_id "; var patientRegisterSqlEntity = await conn.QueryFirstAsync(sql_patient, new { patient_register_id = patientRegisterId }); if (patientRegisterSqlEntity != null) { if (patientRegisterSqlEntity.complete_flag != "3") { throw new UserFriendlyException("人员为总检"); } #region 单位信息 string sql_cusomter = @"select c.display_name from patient_register as a left join customer_org_register as b on a.customer_org_register_id=b.id left join customer_org as c on b.customer_org_id=c.id where a.id=@patient_register_id "; var customerOrgName = conn.QuerySingleOrDefault(sql_cusomter, new { patient_register_id = patientRegisterId }); #endregion var pushRequest = new PushHtyDataInputDto(); pushRequest.info = new PushHtyDataInputPatientDto { USERID = patientRegisterSqlEntity.patient_no.ToString(), CERTNO = patientRegisterSqlEntity.id_no?.ToString() ?? "", BIRTHDATE = !string.IsNullOrWhiteSpace(patientRegisterSqlEntity.birth_date?.ToString()) ? Convert.ToDateTime(patientRegisterSqlEntity.birth_date).ToString("yyyy-MM-dd") : "", USERTIMES = patientRegisterSqlEntity.medical_times.ToString(), GENDER = ConertSex(patientRegisterSqlEntity.sex_id.ToString()), NAME = patientRegisterSqlEntity.patient_name.ToString(), MEDICALDATE = Convert.ToDateTime(patientRegisterSqlEntity.medical_start_date).ToString("yyyy-MM-dd HH:mm:ss"), MEDICALCODE = patientRegisterSqlEntity.patient_register_no.ToString(), RORGNAME = customerOrgName, ADDRESS = patientRegisterSqlEntity.address?.ToString() ?? "", MOBILE = patientRegisterSqlEntity.mobile_telephone?.ToString() ?? "", TEL = patientRegisterSqlEntity.telephone?.ToString() ?? "", }; //查看项目 var sql_item = $@" select c.display_name as item_name,b.* from register_check as a left join register_check_item as b on a.id=b.register_check_id left join item as c on b.item_id=c.id where a.complete_flag='1' and a.patient_register_id=@patient_register_id "; var registerItemSqlEntity = await conn.QueryAsync(sql_item, new { patient_register_id = patientRegisterId }); if (registerItemSqlEntity.Count() > 0) { #region 获取对照数据 var sql_dz = $@" select a.code_value,b.interface_code_value from column_reference_code as a left join column_reference_interface as b on a.id=b.column_reference_code_id where a.column_reference_id=@column_reference_id "; var itemDZSqlEntity = (await conn.QueryAsync(sql_dz, new { column_reference_id = Guid.Parse(columnReferenceId) })).ToList(); #endregion Dictionary contents = new Dictionary(); foreach (var itemRow in registerItemSqlEntity) { var itemCode = itemDZSqlEntity.FirstOrDefault(f => f.code_value == itemRow.item_id.ToString()); if (itemCode != null) { var constr = new pushHtyDataItemDto { dicValue = itemRow.reference_range_value?.ToString() ?? "", ordinary = 1, p_value = itemRow.result?.ToString() ?? "", sysCode = itemCode.interface_code_value, unit = itemRow.unit?.ToString() ?? "" }; var dcIndex = contents.Count + 1; contents.Add("con" + dcIndex, constr); } } pushRequest.content = contents; } string pushRequestJsonString = JsonConvert.SerializeObject(pushRequest, Formatting.Indented); //推送信息 var formContent = new MultipartFormDataContent(); formContent.Add(new StringContent(token), "token"); formContent.Add(new StringContent("fbpc"), "urlFrom"); formContent.Add(new StringContent("-1"), "tmId"); formContent.Add(new StringContent(pushRequestJsonString, Encoding.UTF8, "application/json"), "pushData"); var result = await HtyHttpPostHelper.PostFormDataAsync($"{pushBaseApi}/api/manageFrame/healthCheck/medicalInterfaceDataFetch.action", formContent, "PostmanRuntime-ApipostRuntime/1.1.0"); var resultModel = JsonConvert.DeserializeObject(result); if (resultModel.stateMessage == "suc") { #region 更新人员状态 conn.Execute("update patient_register set is_push_third_result='Y' where id=@patient_register_id", new { patient_register_id = patientRegisterId }); #endregion } return $"{pushRequest.info.NAME}_{pushRequest.info.MEDICALCODE}_推送结果:{resultModel.stateMessage}"; } else { throw new UserFriendlyException("人员不存在"); } } } /// /// 获取需要推送的人员id集合 /// /// public async Task> GetRequestPatientRegistersHtyAsync() { var patientRegisterIds = new List(); var queryDaysStr = InterfaceConfig.GetValue("Interface:Scheduler:QueryDays", ""); var isAuditWhere = InterfaceConfig.GetValue("Interface:Scheduler:IsAuditWhere", ""); var uploadDateType = InterfaceConfig.GetValue("Interface:Scheduler:UploadDateType", ""); var otherWhere = InterfaceConfig.GetValue("Interface:Scheduler:OtherWhere", ""); var uploadCountLimit = InterfaceConfig.GetValue("Interface:Scheduler:UploadCountLimit", ""); if (string.IsNullOrWhiteSpace(queryDaysStr)) { queryDaysStr = "1"; } if (!int.TryParse(queryDaysStr, out int days)) { days = 1; } using (DbConnection conn = new NpgsqlConnection(AppConnctionStr)) { string sql; sql = @" SELECT id from patient_register where complete_flag = @CompleteFlag "; if (uploadDateType == "1") { sql += " and summary_date>= @HandDate "; } else if (uploadDateType == "2") { sql += " and audit_date>= @HandDate "; } else { sql += " and last_modification_time>= @HandDate "; } if (isAuditWhere == "Y") { sql += " and is_audit='Y' "; } if (!string.IsNullOrWhiteSpace(otherWhere)) { sql += otherWhere; } var parameters = new DynamicParameters(); parameters.Add("HandDate", DateTime.Now.Date.AddDays(-days)); parameters.Add("CompleteFlag", PatientRegisterCompleteFlag.SumCheck); sql += $" limit {uploadCountLimit} "; var patientRegisterSqlList = (await conn.QueryAsync(sql, parameters)).ToList(); patientRegisterIds = patientRegisterSqlList.Select(s => s.Id).ToList(); } return patientRegisterIds; } private string ConertSex(string sexId) { string res = ""; if (sexId == "M") { res = "男"; } else if (sexId == "F") { res = "女"; } else { res = "未知"; } return res; } } }