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

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
1 year ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
  1. using Azure.Core;
  2. using Dapper;
  3. using Microsoft.Extensions.Configuration;
  4. using Newtonsoft.Json;
  5. using Newtonsoft.Json.Converters;
  6. using Newtonsoft.Json.Linq;
  7. using Npgsql;
  8. using Shentun.Peis.AppointPatientRegisters;
  9. using Shentun.Peis.AppointRegisterAsbitems;
  10. using Shentun.Peis.CustomerReports;
  11. using Shentun.Peis.Enums;
  12. using Shentun.Peis.PlugIns.ColumnReferences;
  13. using Shentun.Peis.TransToWebPeiss;
  14. using System;
  15. using System.Collections.Generic;
  16. using System.Data.Common;
  17. using System.IdentityModel.Tokens.Jwt;
  18. using System.Linq;
  19. using System.Net.Http.Headers;
  20. using System.Text;
  21. using System.Threading.Tasks;
  22. namespace Shentun.Peis.PlugIns.WebAppoints
  23. {
  24. public class WebAppointWebPeisPlugIns : WebAppointPlugInsBase
  25. {
  26. private string _webPeisUser;
  27. private string _webPeisPassword;
  28. private string _webPeisBaseAddress;
  29. private static string _webPeisToken;
  30. private string _webPeisGetAppointPatientRegisterListByFilterUrl;
  31. private string _webPeisGetAppointRegisterAsbitemListByIdUrl;
  32. private string _webPeisGetByPatientRegisterIdUrl;
  33. private string _webPeisGetAppointStatisticsReportUrl;
  34. private Guid _medicalCenterId;
  35. public WebAppointWebPeisPlugIns(Guid thirdInterfaceId) : base(thirdInterfaceId)
  36. {
  37. var configurationBuilder = new ConfigurationBuilder()
  38. .AddJsonStream(new MemoryStream(Encoding.UTF8.GetBytes(_thirdInterfaceDto.ParmValue)));
  39. InterfaceConfig = configurationBuilder.Build();
  40. var medicalCenterIdStr = InterfaceConfig.GetSection("Interface").GetSection("MedicalCenterId").Value;
  41. if (!Guid.TryParse(medicalCenterIdStr, out _medicalCenterId))
  42. {
  43. throw new Exception("体检中心ID格式不正确");
  44. }
  45. _webPeisUser = InterfaceConfig.GetSection("Interface").GetSection("User").Value;
  46. _webPeisPassword = InterfaceConfig.GetSection("Interface").GetSection("Password").Value;
  47. _webPeisBaseAddress = InterfaceConfig.GetSection("Interface").GetSection("BaseAddress").Value;
  48. _webPeisGetAppointPatientRegisterListByFilterUrl = InterfaceConfig.GetSection("Interface").GetSection("GetAppointPatientRegisterListByFilterUrl").Value;
  49. _webPeisGetAppointRegisterAsbitemListByIdUrl = InterfaceConfig.GetSection("Interface").GetSection("GetAppointRegisterAsbitemListByIdUrl").Value;
  50. _webPeisGetByPatientRegisterIdUrl = InterfaceConfig.GetSection("Interface").GetSection("GetByPatientRegisterIdUrl").Value;
  51. _webPeisGetAppointStatisticsReportUrl = InterfaceConfig.GetSection("Interface").GetSection("GetAppointStatisticsReportUrl").Value;
  52. }
  53. /// <summary>
  54. /// 获取预约人员列表
  55. /// </summary>
  56. /// <param name="input"></param>
  57. /// <returns></returns>
  58. public override async Task<List<AppointPatientRegisterDto>> GetAppointPatientRegisterListByFilterAsync(AppointPatientRegisterInputDto input)
  59. {
  60. var medicalCenterId = _medicalCenterId;
  61. var webAppointPatientRegisterInput = new WebAppointPatientRegisterInput()
  62. {
  63. MedicalCenterId = medicalCenterId,
  64. IdNo = input.IdNo,
  65. MobilePhone = input.MobilePhone,
  66. AppointStartDate = input.AppointStartDate,
  67. AppointStopDate = input.AppointStopDate,
  68. CompleteFlag = input.CompleteFlag,
  69. };
  70. var appointPatientRegisterDtos = await CallWePeisAppServiceAsync<WebAppointPatientRegisterInput, List<AppointPatientRegisterDto>>(_webPeisGetAppointPatientRegisterListByFilterUrl,
  71. webAppointPatientRegisterInput);
  72. return appointPatientRegisterDtos;
  73. }
  74. /// <summary>
  75. /// 获取组合项目列表通过预约ID
  76. /// </summary>
  77. /// <param name="input"></param>
  78. /// <returns></returns>
  79. public override async Task<List<AppointRegisterAsbitemDto>> GetAppointRegisterAsbitemListByIdAsync(AppointPatientRegisterIdInputDto input)
  80. {
  81. var webAppointPatientRegisterIdInput = new WebAppointPatientRegisterIdInput()
  82. {
  83. AppointPatientRegisterId = new Guid(input.AppointPatientRegisterId)
  84. };
  85. var appointRegisterAsbitemDtos = await CallWePeisAppServiceAsync<WebAppointPatientRegisterIdInput,
  86. List<AppointRegisterAsbitemDto>>
  87. (_webPeisGetAppointRegisterAsbitemListByIdUrl, webAppointPatientRegisterIdInput);
  88. return appointRegisterAsbitemDtos;
  89. }
  90. /// <summary>
  91. /// 获取预约信息人员通过登记ID
  92. /// </summary>
  93. /// <param name="input"></param>
  94. /// <returns></returns>
  95. public override async Task<AppointPatientRegisterDto> GetByPatientRegisterIdAsync(AppointPatientRegisterIdInputDto input)
  96. {
  97. var webAppointPatientRegisterIdInput = new WebPatientRegisterIdInput()
  98. {
  99. PatientRegisterId = input.PatientRegisterId
  100. };
  101. var appointPatientRegisterDto = await CallWePeisAppServiceAsync<WebPatientRegisterIdInput,
  102. AppointPatientRegisterDto>
  103. (_webPeisGetByPatientRegisterIdUrl, webAppointPatientRegisterIdInput);
  104. return appointPatientRegisterDto;
  105. }
  106. /// <summary>
  107. /// 获取需要同步到web的人员ID
  108. /// </summary>
  109. /// <param name="input"></param>
  110. /// <returns></returns>
  111. public override async Task<List<SyncPatientRegisterIdsDto>> GetSyncPatientRegisterIds(SyncPatientRegisterReportInputDto input)
  112. {
  113. using (DbConnection conn = new NpgsqlConnection(AppConnctionStr))
  114. {
  115. string sql;
  116. sql = @"
  117. SELECT
  118. id
  119. from patient_register
  120. where last_modification_time >= @HandDate and
  121. complete_flag = @CompleteFlag
  122. ";
  123. var patientRegisterIds = (await conn.QueryAsync<SyncPatientRegisterIdsDto>(sql,
  124. new { HandDate = DateTime.Now.Date.AddDays(-input.QueryDays), CompleteFlag = PatientRegisterCompleteFlag.SumCheck })).ToList();
  125. return patientRegisterIds;
  126. }
  127. }
  128. /// <summary>
  129. /// 获取需要同步到web的预登记人员ID
  130. /// </summary>
  131. /// <param name="input"></param>
  132. /// <returns></returns>
  133. public override async Task<List<SyncPatientRegisterIdsDto>> GetSyncPreRegistrationPatientRegisterIds(SyncPatientRegisterReportInputDto input)
  134. {
  135. using (DbConnection conn = new NpgsqlConnection(AppConnctionStr))
  136. {
  137. string sql;
  138. sql = @"
  139. SELECT
  140. id
  141. from patient_register
  142. where summary_date >= @HandDate and
  143. complete_flag = @CompleteFlag
  144. ";
  145. var patientRegisterIds = (await conn.QueryAsync<SyncPatientRegisterIdsDto>(sql,
  146. new { HandDate = DateTime.Now.Date.AddDays(-input.QueryDays), CompleteFlag = PatientRegisterCompleteFlag.PreRegistration })).ToList();
  147. return patientRegisterIds;
  148. }
  149. }
  150. /// <summary>
  151. /// 获取预约统计报表
  152. /// </summary>
  153. /// <param name="input"></param>
  154. /// <returns></returns>
  155. /// <exception cref="NotImplementedException"></exception>
  156. public override async Task<List<GetAppointStatisticsReportDto>> GetAppointStatisticsReportAsync(List<CustomerOrgInputDto> input)
  157. {
  158. var appointStatisticsReportListDto = await CallWePeisAppServiceAsync<List<CustomerOrgInputDto>,
  159. List<GetAppointStatisticsReportDto>>
  160. (_webPeisGetAppointStatisticsReportUrl, input);
  161. return appointStatisticsReportListDto;
  162. }
  163. public async virtual Task<WebApiOutDto<LoginOutDataDto>> LoginWebPeisAsync()
  164. {
  165. if (string.IsNullOrWhiteSpace(_webPeisUser))
  166. {
  167. throw new Exception("WebPeisUser不能为空");
  168. }
  169. if (string.IsNullOrWhiteSpace(_webPeisPassword))
  170. {
  171. throw new Exception("WebPeisPassword不能为空");
  172. }
  173. var relult = await LoginWebPeisAsync(_webPeisUser, _webPeisPassword);
  174. return relult;
  175. }
  176. public async Task<WebApiOutDto<LoginOutDataDto>> LoginWebPeisAsync(string userId, string password)
  177. {
  178. if (string.IsNullOrWhiteSpace(userId))
  179. {
  180. throw new Exception("用户ID不能为空");
  181. }
  182. if (string.IsNullOrWhiteSpace(password))
  183. {
  184. throw new Exception("密码不能为空");
  185. }
  186. using (var httpClientHandler = new HttpClientHandler())
  187. {
  188. using (var httpClient = new HttpClient(httpClientHandler))
  189. {
  190. httpClient.BaseAddress = new Uri(_webPeisBaseAddress);
  191. httpClient.DefaultRequestHeaders.Accept.Add(
  192. new MediaTypeWithQualityHeaderValue("application/json"));//设置accept标头,告诉JSON是可接受的响应类型
  193. var url = "api/identity/users/login";
  194. var loginUser = new LoginInputDto()
  195. {
  196. UserName = userId,
  197. Password = password
  198. };
  199. var sendData = JsonConvert.SerializeObject(loginUser);
  200. using (HttpContent httpContent = new StringContent(sendData))
  201. {
  202. httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
  203. HttpResponseMessage response = await httpClient.PostAsync(url, httpContent);
  204. string result;
  205. if (!response.IsSuccessStatusCode)
  206. {
  207. result = response.Content.ReadAsStringAsync().Result;
  208. throw new Exception("http通信错误:" + response.StatusCode + ",结果:" + result);
  209. }
  210. result = await response.Content.ReadAsStringAsync();
  211. var restultDto = JsonConvert.DeserializeObject<WebApiOutDto<LoginOutDataDto>>(result);
  212. if (restultDto == null)
  213. {
  214. throw new Exception("返回结果是空");
  215. }
  216. if (restultDto.Code != 1)
  217. {
  218. throw new Exception($"登录失败{restultDto.Message}");
  219. }
  220. _webPeisToken = restultDto.Data.access_token;
  221. return restultDto;
  222. }
  223. }
  224. }
  225. }
  226. public async Task<TOut> CallWePeisAppServiceAsync<TInput, TOut>(string url, TInput data, string method = "post")
  227. {
  228. //if (string.IsNullOrWhiteSpace(_webPeisBaseAddress))
  229. //{
  230. // throw new Exception("_webPeisBaseAddress不能为空");
  231. //}
  232. string baseAddress = _webPeisBaseAddress;
  233. await CheckWebPeisLoginAsync();
  234. using (var httpClientHandler = new HttpClientHandler())
  235. {
  236. using (var httpClient = new HttpClient(httpClientHandler))
  237. {
  238. httpClient.BaseAddress = new Uri(baseAddress);
  239. httpClient.DefaultRequestHeaders.Accept.Add(
  240. new MediaTypeWithQualityHeaderValue("application/json"));//设置accept标头,告诉JSON是可接受的响应类型
  241. httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", _webPeisToken);
  242. IsoDateTimeConverter timeFormat = new IsoDateTimeConverter();
  243. timeFormat.DateTimeFormat = "yyyy-MM-dd HH:mm:ss";
  244. var sendData = JsonConvert.SerializeObject(data, Formatting.Indented, timeFormat);
  245. using (HttpContent httpContent = new StringContent(sendData))
  246. {
  247. httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
  248. HttpResponseMessage response = null;
  249. if (method == "post")
  250. {
  251. response = await httpClient.PostAsync(url, httpContent);
  252. }
  253. else
  254. {
  255. response = await httpClient.GetAsync(url);
  256. }
  257. string result;
  258. if (!response.IsSuccessStatusCode)
  259. {
  260. result = response.Content.ReadAsStringAsync().Result;
  261. throw new Exception("http通信错误:" + response.StatusCode + ",结果:" + result);
  262. }
  263. result = await response.Content.ReadAsStringAsync();
  264. var resultDto = JsonConvert.DeserializeObject<WebApiOutDto<TOut>>(result);
  265. if (resultDto != null)
  266. {
  267. if (resultDto.Code == -1)
  268. {
  269. throw new Exception($"调用WebApi失败,返回-1,消息:" + resultDto.Message);
  270. }
  271. return resultDto.Data;
  272. }
  273. return resultDto.Data;
  274. }
  275. }
  276. }
  277. }
  278. private async Task CheckWebPeisLoginAsync()
  279. {
  280. if (string.IsNullOrWhiteSpace(_webPeisToken))
  281. {
  282. await LoginWebPeisAsync();
  283. }
  284. else
  285. {
  286. var handler = new JwtSecurityTokenHandler();
  287. var payload = handler.ReadJwtToken(_webPeisToken).Payload;
  288. var claims = payload.Claims;
  289. var exp = claims.First(claim => claim.Type == "exp").Value;
  290. if (exp == null)
  291. {
  292. await LoginWebPeisAsync();
  293. }
  294. else
  295. {
  296. if (long.TryParse(exp, out var expires))
  297. {
  298. var expireTime = DateTimeOffset.FromUnixTimeSeconds(expires).LocalDateTime;
  299. if (expireTime <= DateTime.Now)
  300. {
  301. await LoginAsync();
  302. }
  303. }
  304. else
  305. {
  306. await LoginWebPeisAsync();
  307. }
  308. }
  309. }
  310. }
  311. }
  312. }