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.
333 lines
15 KiB
333 lines
15 KiB
using Azure.Core;
|
|
using Dapper;
|
|
using Microsoft.Extensions.Configuration;
|
|
using Newtonsoft.Json;
|
|
using Newtonsoft.Json.Converters;
|
|
using Newtonsoft.Json.Linq;
|
|
using Npgsql;
|
|
using Shentun.Peis.AppointPatientRegisters;
|
|
using Shentun.Peis.AppointRegisterAsbitems;
|
|
using Shentun.Peis.CustomerReports;
|
|
using Shentun.Peis.Enums;
|
|
using Shentun.Peis.PlugIns.ColumnReferences;
|
|
using Shentun.Peis.TransToWebPeiss;
|
|
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;
|
|
|
|
namespace Shentun.Peis.PlugIns.WebAppoints
|
|
{
|
|
public class WebAppointWebPeisPlugIns : WebAppointPlugInsBase
|
|
{
|
|
private string _webPeisUser;
|
|
private string _webPeisPassword;
|
|
private string _webPeisBaseAddress;
|
|
private static string _webPeisToken;
|
|
private string _webPeisGetAppointPatientRegisterListByFilterUrl;
|
|
private string _webPeisGetAppointRegisterAsbitemListByIdUrl;
|
|
private string _webPeisGetByPatientRegisterIdUrl;
|
|
private string _webPeisGetAppointStatisticsReportUrl;
|
|
private Guid _medicalCenterId;
|
|
public WebAppointWebPeisPlugIns(Guid thirdInterfaceId) : base(thirdInterfaceId)
|
|
{
|
|
var configurationBuilder = new ConfigurationBuilder()
|
|
.AddJsonStream(new MemoryStream(Encoding.UTF8.GetBytes(_thirdInterfaceDto.ParmValue)));
|
|
InterfaceConfig = configurationBuilder.Build();
|
|
|
|
var medicalCenterIdStr = InterfaceConfig.GetSection("Interface").GetSection("MedicalCenterId").Value;
|
|
if (!Guid.TryParse(medicalCenterIdStr, out _medicalCenterId))
|
|
{
|
|
throw new Exception("体检中心ID格式不正确");
|
|
}
|
|
_webPeisUser = InterfaceConfig.GetSection("Interface").GetSection("User").Value;
|
|
_webPeisPassword = InterfaceConfig.GetSection("Interface").GetSection("Password").Value;
|
|
_webPeisBaseAddress = InterfaceConfig.GetSection("Interface").GetSection("BaseAddress").Value;
|
|
_webPeisGetAppointPatientRegisterListByFilterUrl = InterfaceConfig.GetSection("Interface").GetSection("GetAppointPatientRegisterListByFilterUrl").Value;
|
|
_webPeisGetAppointRegisterAsbitemListByIdUrl = InterfaceConfig.GetSection("Interface").GetSection("GetAppointRegisterAsbitemListByIdUrl").Value;
|
|
_webPeisGetByPatientRegisterIdUrl = InterfaceConfig.GetSection("Interface").GetSection("GetByPatientRegisterIdUrl").Value;
|
|
_webPeisGetAppointStatisticsReportUrl = InterfaceConfig.GetSection("Interface").GetSection("GetAppointStatisticsReportUrl").Value;
|
|
}
|
|
/// <summary>
|
|
/// 获取预约人员列表
|
|
/// </summary>
|
|
/// <param name="input"></param>
|
|
/// <returns></returns>
|
|
public override async Task<List<AppointPatientRegisterDto>> GetAppointPatientRegisterListByFilterAsync(AppointPatientRegisterInputDto input)
|
|
{
|
|
var medicalCenterId = _medicalCenterId;
|
|
var webAppointPatientRegisterInput = new WebAppointPatientRegisterInput()
|
|
{
|
|
MedicalCenterId = medicalCenterId,
|
|
IdNo = input.IdNo,
|
|
MobilePhone = input.MobilePhone,
|
|
AppointStartDate = input.AppointStartDate,
|
|
AppointStopDate = input.AppointStopDate,
|
|
CompleteFlag = input.CompleteFlag,
|
|
};
|
|
var appointPatientRegisterDtos = await CallWePeisAppServiceAsync<WebAppointPatientRegisterInput, List<AppointPatientRegisterDto>>(_webPeisGetAppointPatientRegisterListByFilterUrl,
|
|
webAppointPatientRegisterInput);
|
|
return appointPatientRegisterDtos;
|
|
|
|
}
|
|
/// <summary>
|
|
/// 获取组合项目列表通过预约ID
|
|
/// </summary>
|
|
/// <param name="input"></param>
|
|
/// <returns></returns>
|
|
public override async Task<List<AppointRegisterAsbitemDto>> GetAppointRegisterAsbitemListByIdAsync(AppointPatientRegisterIdInputDto input)
|
|
{
|
|
var webAppointPatientRegisterIdInput = new WebAppointPatientRegisterIdInput()
|
|
{
|
|
AppointPatientRegisterId = new Guid(input.AppointPatientRegisterId)
|
|
};
|
|
var appointRegisterAsbitemDtos = await CallWePeisAppServiceAsync<WebAppointPatientRegisterIdInput,
|
|
List<AppointRegisterAsbitemDto>>
|
|
(_webPeisGetAppointRegisterAsbitemListByIdUrl, webAppointPatientRegisterIdInput);
|
|
return appointRegisterAsbitemDtos;
|
|
}
|
|
/// <summary>
|
|
/// 获取预约信息人员通过登记ID
|
|
/// </summary>
|
|
/// <param name="input"></param>
|
|
/// <returns></returns>
|
|
public override async Task<AppointPatientRegisterDto> GetByPatientRegisterIdAsync(AppointPatientRegisterIdInputDto input)
|
|
{
|
|
var webAppointPatientRegisterIdInput = new WebPatientRegisterIdInput()
|
|
{
|
|
PatientRegisterId = input.PatientRegisterId
|
|
};
|
|
var appointPatientRegisterDto = await CallWePeisAppServiceAsync<WebPatientRegisterIdInput,
|
|
AppointPatientRegisterDto>
|
|
(_webPeisGetByPatientRegisterIdUrl, webAppointPatientRegisterIdInput);
|
|
return appointPatientRegisterDto;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取需要同步到web的人员ID
|
|
/// </summary>
|
|
/// <param name="input"></param>
|
|
/// <returns></returns>
|
|
public override async Task<List<SyncPatientRegisterIdsDto>> GetSyncPatientRegisterIds(SyncPatientRegisterReportInputDto input)
|
|
{
|
|
using (DbConnection conn = new NpgsqlConnection(AppConnctionStr))
|
|
{
|
|
string sql;
|
|
sql = @"
|
|
SELECT
|
|
id
|
|
from patient_register
|
|
where last_modification_time >= @HandDate and
|
|
complete_flag = @CompleteFlag
|
|
";
|
|
var patientRegisterIds = (await conn.QueryAsync<SyncPatientRegisterIdsDto>(sql,
|
|
new { HandDate = DateTime.Now.Date.AddDays(-input.QueryDays), CompleteFlag = PatientRegisterCompleteFlag.SumCheck })).ToList();
|
|
return patientRegisterIds;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取需要同步到web的预登记人员ID
|
|
/// </summary>
|
|
/// <param name="input"></param>
|
|
/// <returns></returns>
|
|
public override async Task<List<SyncPatientRegisterIdsDto>> GetSyncPreRegistrationPatientRegisterIds(SyncPatientRegisterReportInputDto input)
|
|
{
|
|
using (DbConnection conn = new NpgsqlConnection(AppConnctionStr))
|
|
{
|
|
string sql;
|
|
sql = @"
|
|
SELECT
|
|
id
|
|
from patient_register
|
|
where summary_date >= @HandDate and
|
|
complete_flag = @CompleteFlag
|
|
";
|
|
var patientRegisterIds = (await conn.QueryAsync<SyncPatientRegisterIdsDto>(sql,
|
|
new { HandDate = DateTime.Now.Date.AddDays(-input.QueryDays), CompleteFlag = PatientRegisterCompleteFlag.PreRegistration })).ToList();
|
|
return patientRegisterIds;
|
|
}
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// 获取预约统计报表
|
|
/// </summary>
|
|
/// <param name="input"></param>
|
|
/// <returns></returns>
|
|
/// <exception cref="NotImplementedException"></exception>
|
|
public override async Task<List<GetAppointStatisticsReportDto>> GetAppointStatisticsReportAsync(List<CustomerOrgInputDto> input)
|
|
{
|
|
var appointStatisticsReportListDto = await CallWePeisAppServiceAsync<List<CustomerOrgInputDto>,
|
|
List<GetAppointStatisticsReportDto>>
|
|
(_webPeisGetAppointStatisticsReportUrl, input);
|
|
return appointStatisticsReportListDto;
|
|
}
|
|
|
|
public async virtual Task<WebApiOutDto<LoginOutDataDto>> LoginWebPeisAsync()
|
|
{
|
|
if (string.IsNullOrWhiteSpace(_webPeisUser))
|
|
{
|
|
throw new Exception("WebPeisUser不能为空");
|
|
}
|
|
if (string.IsNullOrWhiteSpace(_webPeisPassword))
|
|
{
|
|
throw new Exception("WebPeisPassword不能为空");
|
|
}
|
|
var relult = await LoginWebPeisAsync(_webPeisUser, _webPeisPassword);
|
|
return relult;
|
|
}
|
|
|
|
public async Task<WebApiOutDto<LoginOutDataDto>> LoginWebPeisAsync(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(_webPeisBaseAddress);
|
|
|
|
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}");
|
|
}
|
|
_webPeisToken = restultDto.Data.access_token;
|
|
return restultDto;
|
|
}
|
|
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
public async Task<TOut> CallWePeisAppServiceAsync<TInput, TOut>(string url, TInput data, string method = "post")
|
|
{
|
|
//if (string.IsNullOrWhiteSpace(_webPeisBaseAddress))
|
|
//{
|
|
// throw new Exception("_webPeisBaseAddress不能为空");
|
|
//}
|
|
string baseAddress = _webPeisBaseAddress;
|
|
await CheckWebPeisLoginAsync();
|
|
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", _webPeisToken);
|
|
IsoDateTimeConverter timeFormat = new IsoDateTimeConverter();
|
|
timeFormat.DateTimeFormat = "yyyy-MM-dd HH:mm:ss";
|
|
var sendData = JsonConvert.SerializeObject(data, 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 != null)
|
|
{
|
|
if (resultDto.Code == -1)
|
|
{
|
|
throw new Exception($"调用WebApi失败,返回-1,消息:" + resultDto.Message);
|
|
}
|
|
|
|
return resultDto.Data;
|
|
}
|
|
return resultDto.Data;
|
|
}
|
|
|
|
}
|
|
}
|
|
}
|
|
|
|
private async Task CheckWebPeisLoginAsync()
|
|
{
|
|
if (string.IsNullOrWhiteSpace(_webPeisToken))
|
|
{
|
|
await LoginWebPeisAsync();
|
|
}
|
|
else
|
|
{
|
|
var handler = new JwtSecurityTokenHandler();
|
|
var payload = handler.ReadJwtToken(_webPeisToken).Payload;
|
|
var claims = payload.Claims;
|
|
var exp = claims.First(claim => claim.Type == "exp").Value;
|
|
if (exp == null)
|
|
{
|
|
await LoginWebPeisAsync();
|
|
}
|
|
else
|
|
{
|
|
if (long.TryParse(exp, out var expires))
|
|
{
|
|
var expireTime = DateTimeOffset.FromUnixTimeSeconds(expires).LocalDateTime;
|
|
if (expireTime <= DateTime.Now)
|
|
{
|
|
await LoginAsync();
|
|
}
|
|
}
|
|
else
|
|
{
|
|
await LoginWebPeisAsync();
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
}
|
|
}
|
|
}
|