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.

559 lines
23 KiB

using Azure.Core;
using Dapper;
using Microsoft.Extensions.Configuration;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using Npgsql;
using Shentun.Peis.ImportLisResults;
using Shentun.Peis.PlugIns.Extensions.ChargeRequests.YinHai;
using Shentun.Peis.PlugIns.Extensions.ChargeRequests.YinHai.FeeBacks;
using Shentun.Peis.PlugIns.Extensions.ImportLisResults.YinHai.ResultCalls;
using Shentun.Peis.PlugIns.Extensions.LisRequests.YinHai;
using Shentun.Peis.ThirdInterfaces;
using Shentun.Utilities;
using System;
using System.Collections.Generic;
using System.Data.Common;
using System.IdentityModel.Tokens.Jwt;
using System.Linq;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using Volo.Abp.Application.Dtos;
namespace Shentun.Peis.PlugIns.Extensions.ImportLisResults.YinHai
{
public class ImportLisResultPlugInsYinHai
{
protected IConfiguration? AppConfig;
private readonly string AppConnctionStr;
private readonly IConfiguration? InterfaceConfig;
private readonly ThirdInterfaceDto? _thirdInterfaceDto;
private readonly PluginLogger _logger; // 添加日志实例
private string? _appBaseAddress;
private static string? _accesToken;
protected string? AppUser;
protected string? AppPassword;
static ImportLisResultPlugInsYinHai()
{
Dapper.DefaultTypeMap.MatchNamesWithUnderscores = true;
}
public ImportLisResultPlugInsYinHai(Guid thirdInterfaceId)
{
_logger = new PluginLogger();
AppConfig = new ConfigurationBuilder()
.SetBasePath(DirectoryHelper.GetAppDirectory()) // 设置基础路径为当前目录
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.Build();
AppConnctionStr = AppConfig.GetSection("ConnectionStrings")
.GetSection("Default").Value;
using (DbConnection conn = new NpgsqlConnection(AppConnctionStr))
{
string sql;
sql = @"
SELECT *
from third_interface
where id =@ThirdInterfaceId
";
_thirdInterfaceDto = (conn.Query<ThirdInterfaceDto>(sql,
new { ThirdInterfaceId = thirdInterfaceId })).Single();
}
var configurationBuilder = new ConfigurationBuilder()
.AddJsonStream(new MemoryStream(System.Text.Encoding.UTF8.GetBytes(_thirdInterfaceDto.ParmValue)));
InterfaceConfig = configurationBuilder.Build();
_appBaseAddress = AppConfig.GetSection("App")
.GetSection("SelfUrl").Value;
AppUser = AppConfig.GetSection("App")
.GetSection("SelfUser").Value;
AppPassword = AppConfig.GetSection("App")
.GetSection("SelfPassword").Value;
}
public ImportLisResultPlugInsYinHai(string parmValue)
{
_logger = new PluginLogger();
AppConfig = new ConfigurationBuilder()
.SetBasePath(DirectoryHelper.GetAppDirectory()) // 设置基础路径为当前目录
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.Build();
AppConnctionStr = AppConfig.GetSection("ConnectionStrings")
.GetSection("Default").Value;
var configurationBuilder = new ConfigurationBuilder()
.AddJsonStream(new MemoryStream(System.Text.Encoding.UTF8.GetBytes(parmValue)));
InterfaceConfig = configurationBuilder.Build();
_appBaseAddress = AppConfig.GetSection("App")
.GetSection("SelfUrl").Value;
AppUser = AppConfig.GetSection("App")
.GetSection("SelfUser").Value;
AppPassword = AppConfig.GetSection("App")
.GetSection("SelfPassword").Value;
}
/// <summary>
/// 接收lis检查结果
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
/// <exception cref="Exception"></exception>
public async Task<CallBussinessResponseDetailDto> SendResultRequestAsync(InferfaceXmlDataDto input)
{
var res = new CallBussinessResponseDetailDto
{
msg = "失败",
status_code = "-1"
};
#region 解析xml
var lisResultCallRequestData = GetLisResultCallRequestData(input.message);
#endregion
_logger.LogResult("银海接收lis检查结果", $"请求数据:{input.message}");
try
{
var execDoctorUser = InterfaceConfig.GetValue("Interface:ExecDoctorUser", "");
var commonTableTypeId = InterfaceConfig.GetValue("Interface:CommonTableTypeId", "");
var execOrganizationUnitId = InterfaceConfig.GetValue("Interface:ExecOrganizationUnitId", "");
var checkDoctorName = InterfaceConfig.GetValue("Interface:CheckDoctorName", "");
string actionName = "result_call";
string codeName = "lis_server";
var bodyData = lisResultCallRequestData.body.data.request_details;
var createImportLisResultDtos = new List<CreateImportLisResultDto>();
if (bodyData.detail.Any())
{
var lisItemCommonTableList = await GetCommonTableByTypeId(commonTableTypeId);
foreach (var item in bodyData.detail)
{
var dzFirst = lisItemCommonTableList.FirstOrDefault(f => f.ThirdCode == item.examine_code.ToString());
if (dzFirst == null)
{
_logger.LogResult("银海接收lis检查结果", $"Lis明细项目【{item.examine_name},编码:{item.examine_code}】没有对照");
// throw new Exception($"Lis明细项目【{item.examine_name},编码:{item.examine_code}】没有对照");
}
else
{
var createImportLisResultDto = new CreateImportLisResultDto()
{
LisRequestNo = item.request_no,
ItemId = Guid.Parse(dzFirst.DisplayName),
ItemName = item.examine_name,
Result = item.examine_result,
Unit = item.computing_unit,
ReferenceRangeValue = item.reference_range,
//CriticalRangeValue = "",
//ResultStatusId = "",
//ReportPrompt = "",
CheckDoctorName = checkDoctorName,
//CheckDate = lisResult.CheckDate,
ExecOrganizationUnitId = Guid.Parse(execOrganizationUnitId)
};
createImportLisResultDtos.Add(createImportLisResultDto);
}
}
}
else
{
throw new Exception("无结果数据");
}
//发送数据
var callResult = await CallAppServiceAsync<List<CreateImportLisResultDto>, object>("api/app/ImportLisResult/ImportResult", createImportLisResultDtos);
_logger.LogResult("银海接收lis检查结果", $"处理成功,Lis申请单号【{createImportLisResultDtos.FirstOrDefault().LisRequestNo}】");
var yinHaiSendInterfaceLogDto = new YinHaiSendInterfaceLogDto
{
LogName = "接收lis检查结果",
ExecDoctorUser = Guid.Parse(execDoctorUser),
InterfaceName = $"{codeName}_{actionName}",
ParaValue = input.message,
RequestNo = createImportLisResultDtos.FirstOrDefault().LisRequestNo,
ReturnParaValue = ""
};
await AddSendLog(yinHaiSendInterfaceLogDto);
res = new CallBussinessResponseDetailDto
{
status_code = "1",
msg = "成功"
};
}
catch (Exception ex)
{
_logger.LogResult("银海接收lis检查结果", ex, $"接收lis检查结果异常");
}
return res;
}
/// <summary>
/// 获取对照数据
/// </summary>
/// <param name="commonTableTypeId"></param>
/// <returns></returns>
private async Task<List<YinHaiItemCommonTableDto>> GetCommonTableByTypeId(string commonTableTypeId)
{
var thirdCode = InterfaceConfig.GetValue("Interface:ThirdCode", "");
using (DbConnection conn = new NpgsqlConnection(AppConnctionStr))
{
string sql;
sql = $@"select display_name,{thirdCode} as thirdCode from common_table where common_table_type_id='{commonTableTypeId}' ";
var yinHaiAsbitemCommonTableList = (await conn.QueryAsync<YinHaiItemCommonTableDto>(sql)).ToList();
return yinHaiAsbitemCommonTableList;
}
}
/// <summary>
/// 添加日志
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
private async Task AddSendLog(YinHaiSendInterfaceLogDto input)
{
using (DbConnection conn = new NpgsqlConnection(AppConnctionStr))
{
string sql;
sql = $@" insert into interface_send_log (id,log_name,request_no,interface_name,para_value,return_para_value,concurrency_stamp,
creation_time,creator_id,last_modification_time,last_modifier_id) values
(@id,@log_name,@request_no,@interface_name,@para_value,@return_para_value,@concurrency_stamp,
@creation_time,@creator_id,@last_modification_time,@last_modifier_id)
";
await conn.ExecuteAsync(sql,
new
{
id = Guid.NewGuid(),
log_name = input.LogName,
request_no = input.RequestNo,
interface_name = input.InterfaceName,
para_value = input.ParaValue,
return_para_value = input.ReturnParaValue,
concurrency_stamp = Guid.NewGuid().ToString("N"),
creation_time = DateTime.Now,
creator_id = input.ExecDoctorUser,
last_modification_time = DateTime.Now,
last_modifier_id = input.ExecDoctorUser
});
}
}
#region 解析xml
private LisResultCallRequestDto GetLisResultCallRequestData(string message)
{
try
{
var doc = new XmlDocument();
doc.LoadXml(message);
var request = new LisResultCallRequestDto();
var root = doc.DocumentElement;
if (root == null) return null;
// 解析 head
var headNode = root.SelectSingleNode("head");
if (headNode != null)
{
request.head = new CallBussinessRequestHeadDto
{
version = GetNodeValue(headNode, "version"),
timestamp = GetNodeValue(headNode, "timestamp"),
sign = GetNodeValue(headNode, "sign"),
request_id = GetNodeValue(headNode, "request_id"),
source_system = GetNodeValue(headNode, "source_system"),
object_system = GetNodeValue(headNode, "object_system"),
action = GetNodeValue(headNode, "action"),
code = GetNodeValue(headNode, "code")
};
}
// 解析 body
var bodyNode = root.SelectSingleNode("body");
if (bodyNode != null)
{
request.body = new LisResultCallRequestBodyDto();
var dataNode = bodyNode.SelectSingleNode("data");
if (dataNode != null)
{
request.body.data = new LisResultCallRequestBodyDataDto();
var requestDetailsNode = dataNode.SelectSingleNode("request_details");
if (requestDetailsNode != null)
{
// 创建 detail 列表
request.body.data.request_details = new LisResultCallRequestBodyDataRequestDetails
{
detail = new List<LisResultCallRequestBodyDataRequestDetail>()
};
// 获取所有 detail 节点
var detailNodes = requestDetailsNode.SelectNodes("detail");
if (detailNodes != null)
{
foreach (XmlNode detailNode in detailNodes)
{
var detail = new LisResultCallRequestBodyDataRequestDetail
{
hos_id = GetNodeValue(detailNode, "hos_id"),
request_no = GetNodeValue(detailNode, "request_no"),
patient_type = GetNodeValue(detailNode, "patient_type"),
report_serial_no = GetNodeValue(detailNode, "report_serial_no"),
org_code = GetNodeValue(detailNode, "org_code"),
system_source = GetNodeValue(detailNode, "system_source"),
org_name = GetNodeValue(detailNode, "org_name"),
patient_id = GetNodeValue(detailNode, "patient_id"),
visit_id = GetNodeValue(detailNode, "visit_id"),
order_id = GetNodeValue(detailNode, "order_id"),
item_serial_no = GetNodeValue(detailNode, "item_serial_no"),
print_no = GetNodeValue(detailNode, "print_no"),
examine_way_name = GetNodeValue(detailNode, "examine_way_name"),
examine_no = GetNodeValue(detailNode, "examine_no"),
examine_code = GetNodeValue(detailNode, "examine_code"),
examine_name = GetNodeValue(detailNode, "examine_name"),
examine_en_name = GetNodeValue(detailNode, "examine_en_name"),
examine_result_type = GetNodeValue(detailNode, "examine_result_type"),
examine_result = GetNodeValue(detailNode, "examine_result"),
computing_unit = GetNodeValue(detailNode, "computing_unit"),
abnormal_tip = GetNodeValue(detailNode, "abnormal_tip"),
reference_range = GetNodeValue(detailNode, "reference_range"),
remark = GetNodeValue(detailNode, "remark"),
batch_no = GetNodeValue(detailNode, "batch_no"),
report_url = GetNodeValue(detailNode, "report_url")
};
request.body.data.request_details.detail.Add(detail);
}
}
}
}
}
return request;
}
catch (Exception ex)
{
//_logger.LogError(ex, "解析 XML 失败");
return null;
}
}
private string GetNodeValue(XmlNode parent, string nodeName)
{
var node = parent.SelectSingleNode(nodeName);
return node?.InnerText ?? "";
}
#endregion
private async Task<TOut> CallAppServiceAsync<TInput, TOut>(string url, TInput data, string method = "post")
{
if (string.IsNullOrWhiteSpace(_appBaseAddress))
{
throw new Exception("_appBaseAddress不能为空");
}
string baseAddress = _appBaseAddress;
await CheckLoginAsync();
using (var httpClientHandler = new HttpClientHandler())
{
using (var httpClient = new HttpClient(httpClientHandler))
{
httpClient.BaseAddress = new Uri(baseAddress);
httpClient.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue("application/json"));//设置accept标头,告诉JSON是可接受的响应类型
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", _accesToken);
IsoDateTimeConverter timeFormat = new IsoDateTimeConverter();
timeFormat.DateTimeFormat = "yyyy-MM-dd HH:mm:ss";
var sendData = JsonConvert.SerializeObject(data, Newtonsoft.Json.Formatting.Indented, timeFormat);
using (HttpContent httpContent = new StringContent(sendData))
{
httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
HttpResponseMessage response = null;
if (method == "post")
{
response = await httpClient.PostAsync(url, httpContent);
}
else
{
response = await httpClient.GetAsync(url);
}
string result;
if (!response.IsSuccessStatusCode)
{
result = response.Content.ReadAsStringAsync().Result;
throw new Exception("http通信错误:" + response.StatusCode + ",结果:" + result);
}
result = await response.Content.ReadAsStringAsync();
var resultDto = JsonConvert.DeserializeObject<WebApiOutDto<TOut>>(result);
if (resultDto.Code == -1)
{
throw new Exception($"调用WebApi失败,返回-1,消息:" + resultDto.Message);
}
return resultDto.Data;
}
}
}
}
#region 获取token
private async Task<WebApiOutDto<LoginOutDataDto>> LoginAsync(string userId, string password)
{
if (string.IsNullOrWhiteSpace(userId))
{
throw new Exception("用户ID不能为空");
}
if (string.IsNullOrWhiteSpace(password))
{
throw new Exception("密码不能为空");
}
using (var httpClientHandler = new HttpClientHandler())
{
using (var httpClient = new HttpClient(httpClientHandler))
{
httpClient.BaseAddress = new Uri(_appBaseAddress);
httpClient.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue("application/json"));//设置accept标头,告诉JSON是可接受的响应类型
var url = "api/identity/users/login";
var loginUser = new LoginInputDto()
{
UserName = userId,
Password = password
};
var sendData = JsonConvert.SerializeObject(loginUser);
using (HttpContent httpContent = new StringContent(sendData))
{
httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
HttpResponseMessage response = await httpClient.PostAsync(url, httpContent);
string result;
if (!response.IsSuccessStatusCode)
{
result = response.Content.ReadAsStringAsync().Result;
throw new Exception("http通信错误:" + response.StatusCode + ",结果:" + result);
}
result = await response.Content.ReadAsStringAsync();
var restultDto = JsonConvert.DeserializeObject<WebApiOutDto<LoginOutDataDto>>(result);
if (restultDto == null)
{
throw new Exception("返回结果是空");
}
if (restultDto.Code != 1)
{
throw new Exception($"登录失败{restultDto.Message}");
}
_accesToken = restultDto.Data.access_token;
return restultDto;
}
}
}
}
private async Task CheckLoginAsync()
{
if (string.IsNullOrWhiteSpace(_accesToken))
{
await LoginAsync();
}
else
{
var handler = new JwtSecurityTokenHandler();
var payload = handler.ReadJwtToken(_accesToken).Payload;
var claims = payload.Claims;
var exp = claims.First(claim => claim.Type == "exp").Value;
if (exp == null)
{
await LoginAsync();
}
else
{
if (long.TryParse(exp, out var expires))
{
var expireTime = DateTimeOffset.FromUnixTimeSeconds(expires).LocalDateTime;
if (expireTime <= DateTime.Now)
{
await LoginAsync();
}
}
else
{
await LoginAsync();
}
}
}
}
private async Task<WebApiOutDto<LoginOutDataDto>> LoginAsync()
{
if (string.IsNullOrWhiteSpace(AppUser))
{
throw new Exception("SelfUser不能为空");
}
if (string.IsNullOrWhiteSpace(AppPassword))
{
throw new Exception("SelfPassword不能为空");
}
var relult = await LoginAsync(AppUser, AppPassword);
return relult;
}
#endregion
}
}