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.

458 lines
22 KiB

1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
  1. using Microsoft.Extensions.Configuration;
  2. using Newtonsoft.Json.Converters;
  3. using Newtonsoft.Json;
  4. using Shentun.Peis.Enums;
  5. using Shentun.Peis.Models;
  6. using Shentun.Peis.PatientRegisters;
  7. using Shentun.Peis.PlugIns.Sms;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.IO;
  11. using System.Linq;
  12. using System.Net.Http.Headers;
  13. using System.Net.Http;
  14. using System.Text;
  15. using System.Threading.Tasks;
  16. using Volo.Abp.Application.Services;
  17. using Volo.Abp.Domain.Repositories;
  18. using Microsoft.AspNetCore.Authorization;
  19. using Microsoft.AspNetCore.Mvc;
  20. using NPOI.POIFS.Crypt.Dsig;
  21. using NPOI.SS.Formula.Functions;
  22. namespace Shentun.Peis.ThirdBookingPushs
  23. {
  24. /// <summary>
  25. /// 推送体检信息到人寿
  26. /// </summary>
  27. [Authorize]
  28. [ApiExplorerSettings(GroupName = "Work")]
  29. public class ThirdBookingPushAppService : ApplicationService
  30. {
  31. private readonly IRepository<ThirdInterface, Guid> _thirdInterfaceRepository;
  32. private readonly IRepository<PatientRegister, Guid> _patientRegisterRepository;
  33. private readonly IRepository<ThirdBooking, Guid> _thirdBookingRepository;
  34. private readonly IRepository<CustomerOrgGroup, Guid> _customerOrgGroupRepository;
  35. public ThirdBookingPushAppService(
  36. IRepository<ThirdInterface, Guid> thirdInterfaceRepository,
  37. IRepository<PatientRegister, Guid> patientRegisterRepository,
  38. IRepository<ThirdBooking, Guid> thirdBookingRepository,
  39. IRepository<CustomerOrgGroup, Guid> customerOrgGroupRepository
  40. )
  41. {
  42. _thirdInterfaceRepository = thirdInterfaceRepository;
  43. _patientRegisterRepository = patientRegisterRepository;
  44. _thirdBookingRepository = thirdBookingRepository;
  45. _customerOrgGroupRepository = customerOrgGroupRepository;
  46. }
  47. /// <summary>
  48. /// 推送登记
  49. /// </summary>
  50. /// <returns></returns>
  51. [HttpPost("api/app/ThirdBookingPush/PushRegister")]
  52. public async Task PushRegisterAsync(PatientRegisterIdInputDto input)
  53. {
  54. var thirdBookingInterface = await _thirdInterfaceRepository.FirstOrDefaultAsync(o => o.ThirdInterfaceType ==
  55. ThirdInterfaceTypeFlag.ThirdBooking);
  56. if (thirdBookingInterface != null && thirdBookingInterface.IsActive == 'Y')
  57. {
  58. var parmValue = thirdBookingInterface.ParmValue;
  59. var configurationBuilder = new ConfigurationBuilder()
  60. .AddJsonStream(new MemoryStream(System.Text.Encoding.UTF8.GetBytes(parmValue)));
  61. var interfaceConfig = configurationBuilder.Build();
  62. var baseApiAddress = interfaceConfig.GetSection("Interface").GetSection("BaseApiAddress").Value;
  63. var registerApiAddress = interfaceConfig.GetSection("Interface").GetSection("RegisterApiAddress").Value;
  64. var publicKey = interfaceConfig.GetSection("Interface").GetSection("YuanFuPublicKey").Value;
  65. var privateKey = interfaceConfig.GetSection("Interface").GetSection("MinZhongPrivateKey").Value;
  66. var medicalYear = interfaceConfig.GetSection("Interface").GetSection("MedicalYear").Value;
  67. var isActive = interfaceConfig.GetSection("Interface").GetSection("IsActive").Value;
  68. if (!string.IsNullOrWhiteSpace(isActive)
  69. && isActive == "Y")
  70. {
  71. var patientRegisterEnt = await _patientRegisterRepository.GetAsync(input.PatientRegisterId);
  72. if (patientRegisterEnt != null
  73. && !string.IsNullOrWhiteSpace(patientRegisterEnt.ThirdBookingId))
  74. {
  75. var thirdBookingEnt = await _thirdBookingRepository.FirstOrDefaultAsync(f => f.Id == Guid.Parse(patientRegisterEnt.ThirdBookingId));
  76. string iCode = thirdBookingEnt.ICode;
  77. if (thirdBookingEnt.MedicalStatus == '0')
  78. {
  79. var customerOrgGroupEnt = await _customerOrgGroupRepository.GetAsync(g => g.Id == Guid.Parse(thirdBookingEnt.CustomerOrgGroupId));
  80. var pushRegisterInput = new PushRegisterInputDto
  81. {
  82. BOOKINGDATE = thirdBookingEnt.BookingDate.ToString("yyyy-MM-dd"),
  83. BOOKINGDATEDESC = "",
  84. BOOKINGID = patientRegisterEnt.ThirdBookingId,
  85. BOOKINGTIME = thirdBookingEnt.BookingDate.ToString("HH:mm:ss"),
  86. BOOKINGTYPE = "1",
  87. BOOKINGTYPEELSEDESC = "",
  88. CONFIRMORG = "MINZ",
  89. CONFIRMORGCENTERDESC = "民众",
  90. CONFIRMTIME = patientRegisterEnt.CreationTime.ToString("yyyy-MM-dd HH:mm:ss"),
  91. CONFIRMTYPE = "1",
  92. EXAMPACKAGECODE = thirdBookingEnt.CustomerOrgGroupId,
  93. EXAMPACKAGEDESC = customerOrgGroupEnt.DisplayName,
  94. IDNO = thirdBookingEnt.IdNo,
  95. IDTYPE = thirdBookingEnt.IdType,
  96. MOBILE = thirdBookingEnt.Phone,
  97. NAME = patientRegisterEnt.PatientName,
  98. SOURCE = "BjGJ",
  99. YEAR = medicalYear
  100. };
  101. var inputPara = GetEncryptData(JsonConvert.SerializeObject(pushRegisterInput), iCode, publicKey, privateKey);
  102. var resultPara = await CallBookingAppServiceAsync<EncryDataRequstDto, EncryDataResultDto>(baseApiAddress, registerApiAddress, inputPara, "application/json;charset=utf-8");
  103. if (resultPara.FLAG)
  104. {
  105. var decryptDataPara = GetDecryptData<DecryptBaseDto>(resultPara, publicKey, privateKey);
  106. if (decryptDataPara.STATUS == 0)
  107. {
  108. thirdBookingEnt.MedicalStatus = '1';
  109. await _thirdBookingRepository.UpdateAsync(thirdBookingEnt);
  110. }
  111. }
  112. }
  113. }
  114. }
  115. }
  116. }
  117. /// <summary>
  118. /// 推送完成体检数据
  119. /// </summary>
  120. /// <returns></returns>
  121. [HttpPost("api/app/ThirdBookingPush/PushCompletePhysical")]
  122. public async Task PushCompletePhysicalAsync(PatientRegisterIdInputDto input)
  123. {
  124. var thirdBookingInterface = await _thirdInterfaceRepository.FirstOrDefaultAsync(o => o.ThirdInterfaceType ==
  125. ThirdInterfaceTypeFlag.ThirdBooking);
  126. if (thirdBookingInterface != null && thirdBookingInterface.IsActive == 'Y')
  127. {
  128. var parmValue = thirdBookingInterface.ParmValue;
  129. var configurationBuilder = new ConfigurationBuilder()
  130. .AddJsonStream(new MemoryStream(System.Text.Encoding.UTF8.GetBytes(parmValue)));
  131. var interfaceConfig = configurationBuilder.Build();
  132. var baseApiAddress = interfaceConfig.GetSection("Interface").GetSection("BaseApiAddress").Value;
  133. var completeApiAddress = interfaceConfig.GetSection("Interface").GetSection("CompleteApiAddress").Value;
  134. var publicKey = interfaceConfig.GetSection("Interface").GetSection("YuanFuPublicKey").Value;
  135. var privateKey = interfaceConfig.GetSection("Interface").GetSection("MinZhongPrivateKey").Value;
  136. var isActive = interfaceConfig.GetSection("Interface").GetSection("IsActive").Value;
  137. var medicalYear = interfaceConfig.GetSection("Interface").GetSection("MedicalYear").Value;
  138. if (!string.IsNullOrWhiteSpace(isActive)
  139. && isActive == "Y")
  140. {
  141. var patientRegisterEnt = await _patientRegisterRepository.GetAsync(input.PatientRegisterId);
  142. //已总检才能上传
  143. if (patientRegisterEnt != null
  144. && !string.IsNullOrWhiteSpace(patientRegisterEnt.ThirdBookingId))
  145. {
  146. var thirdBookingEnt = await _thirdBookingRepository.FirstOrDefaultAsync(f => f.Id == Guid.Parse(patientRegisterEnt.ThirdBookingId));
  147. string iCode = thirdBookingEnt.ICode;
  148. if (thirdBookingEnt.MedicalStatus == '1')
  149. {
  150. var customerOrgGroupEnt = await _customerOrgGroupRepository.GetAsync(g => g.Id == Guid.Parse(thirdBookingEnt.CustomerOrgGroupId));
  151. var pushCompletePhysicalInput = new PushCompletePhysicalInputDto
  152. {
  153. BOOKINGDATE = thirdBookingEnt.BookingDate.ToString("yyyy-MM-dd"),
  154. BOOKINGDATEDESC = "",
  155. BOOKINGID = patientRegisterEnt.ThirdBookingId,
  156. BOOKINGTIME = thirdBookingEnt.BookingDate.ToString("HH:mm:ss"),
  157. BOOKINGTYPE = "1",
  158. BOOKINGTYPEELSEDESC = "",
  159. CONFIRMORG = "MINZ",
  160. CONFIRMORGCENTERDESC = "民众",
  161. CONFIRMTIME = patientRegisterEnt.CreationTime.ToString("yyyy-MM-dd HH:mm:ss"),
  162. CONFIRMTYPE = "1",
  163. EXAMPACKAGECODE = thirdBookingEnt.CustomerOrgGroupId,
  164. EXAMPACKAGEDESC = customerOrgGroupEnt.DisplayName,
  165. IDNO = thirdBookingEnt.IdNo,
  166. IDTYPE = thirdBookingEnt.IdType,
  167. MOBILE = thirdBookingEnt.Phone,
  168. NAME = patientRegisterEnt.PatientName,
  169. SOURCE = "BjGJ",
  170. YEAR = medicalYear,
  171. AGE = patientRegisterEnt.Age == null ? (short)0 : patientRegisterEnt.Age.Value,
  172. BIRTHDAY = patientRegisterEnt.BirthDate != null ? patientRegisterEnt.BirthDate.Value.ToString("yyyy-MM-dd") : "",
  173. EXAMBEGINTIME = patientRegisterEnt.MedicalStartDate != null ? patientRegisterEnt.MedicalStartDate.Value.ToString("yyyy-MM-dd HH:mm:ss") : "",
  174. EXAMCOMPLETETIME = patientRegisterEnt.SummaryDate != null ? patientRegisterEnt.SummaryDate.Value.ToString("yyyy-MM-dd HH:mm:ss") : "",
  175. EXAMID = patientRegisterEnt.Id.ToString(),
  176. GENDER = GetSexId(patientRegisterEnt.SexId)
  177. };
  178. var inputPara = GetEncryptData(JsonConvert.SerializeObject(pushCompletePhysicalInput), iCode, publicKey, privateKey);
  179. var resultPara = await CallBookingAppServiceAsync<EncryDataRequstDto, EncryDataResultDto>(baseApiAddress, completeApiAddress, inputPara, "application/json;charset=utf-8");
  180. if (resultPara.FLAG)
  181. {
  182. var decryptDataPara = GetDecryptData<DecryptBaseDto>(resultPara, publicKey, privateKey);
  183. if (decryptDataPara.STATUS == 0)
  184. {
  185. thirdBookingEnt.MedicalStatus = '2';
  186. await _thirdBookingRepository.UpdateAsync(thirdBookingEnt);
  187. }
  188. }
  189. }
  190. }
  191. }
  192. }
  193. }
  194. /// <summary>
  195. /// 推送体检报告
  196. /// </summary>
  197. /// <returns></returns>
  198. [HttpPost("api/app/ThirdBookingPush/PushReportFile")]
  199. public async Task PushReportFileAsync(PushReportFileInputDto input)
  200. {
  201. var thirdBookingInterface = await _thirdInterfaceRepository.FirstOrDefaultAsync(o => o.ThirdInterfaceType ==
  202. ThirdInterfaceTypeFlag.ThirdBooking);
  203. if (thirdBookingInterface != null && thirdBookingInterface.IsActive == 'Y')
  204. {
  205. var parmValue = thirdBookingInterface.ParmValue;
  206. var configurationBuilder = new ConfigurationBuilder()
  207. .AddJsonStream(new MemoryStream(System.Text.Encoding.UTF8.GetBytes(parmValue)));
  208. var interfaceConfig = configurationBuilder.Build();
  209. var baseApiAddress = interfaceConfig.GetSection("Interface").GetSection("BaseApiAddress").Value;
  210. var completeApiAddress = interfaceConfig.GetSection("Interface").GetSection("CompleteApiAddress").Value;
  211. var publicKey = interfaceConfig.GetSection("Interface").GetSection("YuanFuPublicKey").Value;
  212. var privateKey = interfaceConfig.GetSection("Interface").GetSection("MinZhongPrivateKey").Value;
  213. var aesKey = interfaceConfig.GetSection("Interface").GetSection("AesKey").Value;
  214. var isActive = interfaceConfig.GetSection("Interface").GetSection("IsActive").Value;
  215. var medicalYear = interfaceConfig.GetSection("Interface").GetSection("MedicalYear").Value;
  216. if (!string.IsNullOrWhiteSpace(isActive)
  217. && isActive == "Y")
  218. {
  219. var patientRegisterEnt = await _patientRegisterRepository.GetAsync(input.PatientRegisterId);
  220. if (patientRegisterEnt != null
  221. && !string.IsNullOrWhiteSpace(patientRegisterEnt.ThirdBookingId)
  222. && patientRegisterEnt.CompleteFlag == PatientRegisterCompleteFlag.SumCheck)
  223. {
  224. var thirdBookingEnt = await _thirdBookingRepository.FirstOrDefaultAsync(f => f.Id == Guid.Parse(patientRegisterEnt.ThirdBookingId));
  225. string iCode = thirdBookingEnt.ICode;
  226. if (thirdBookingEnt.MedicalStatus == '2')
  227. {
  228. var customerOrgGroupEnt = await _customerOrgGroupRepository.GetAsync(g => g.Id == Guid.Parse(thirdBookingEnt.CustomerOrgGroupId));
  229. #region 上传文件
  230. byte[] filebytes = Convert.FromBase64String(input.ReportBase64);
  231. string fileDataEncrypt = AesHelper.AESEncrypt(filebytes, aesKey);
  232. string fileSign = AesHelper.GetMD5Hash(fileDataEncrypt);
  233. var fileInputParm = new EncryDataRequstDto
  234. {
  235. DATA = fileDataEncrypt,
  236. SIGN = fileSign,
  237. ICODE = thirdBookingEnt.ICode
  238. };
  239. var fileResultEncry = await CallBookingAppServiceAsync<EncryDataRequstDto, EncryDataResultDto>(baseApiAddress, completeApiAddress, fileInputParm, "multipart/form-data");
  240. if (fileResultEncry.FLAG)
  241. {
  242. //验签
  243. if (fileResultEncry.SIGN == AesHelper.GetMD5Hash(fileResultEncry.DATA))
  244. {
  245. //解密
  246. string fileResultStr = AesHelper.AESDecrypt(fileResultEncry.DATA, aesKey); //报文ID
  247. #region 上传报告
  248. var pushMedicalReportInput = new PushMedicalReportInputDto
  249. {
  250. EXAMPACKAGECODE = thirdBookingEnt.CustomerOrgGroupId,
  251. IDNO = thirdBookingEnt.IdNo,
  252. IDTYPE = thirdBookingEnt.IdType,
  253. NAME = patientRegisterEnt.PatientName,
  254. SOURCE = "BjGJ",
  255. YEAR = medicalYear,
  256. BUSINESSID = patientRegisterEnt.Id.ToString(),
  257. JSONBASE64 = "",
  258. ORGCODE = "MINZ",
  259. PDFBASE64 = "",
  260. PDFFILEID = fileResultStr,
  261. PUSHTIME = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")
  262. };
  263. var pushMedicalReportInputEncrypt = GetEncryptData(JsonConvert.SerializeObject(pushMedicalReportInput), iCode, publicKey, privateKey);
  264. var reportResultPara = await CallBookingAppServiceAsync<EncryDataRequstDto, EncryDataResultDto>(baseApiAddress, completeApiAddress, pushMedicalReportInputEncrypt, "application/json;charset=utf-8");
  265. if (reportResultPara.FLAG)
  266. {
  267. var decryptDataPara = GetDecryptData<PushReportDecryptDto>(reportResultPara, publicKey, privateKey);
  268. if (decryptDataPara.FLAG == true)
  269. {
  270. thirdBookingEnt.MedicalStatus = '3';
  271. await _thirdBookingRepository.UpdateAsync(thirdBookingEnt);
  272. }
  273. }
  274. #endregion
  275. }
  276. }
  277. #endregion
  278. }
  279. }
  280. }
  281. }
  282. }
  283. /// <summary>
  284. /// 发起请求
  285. /// </summary>
  286. /// <typeparam name="TInput"></typeparam>
  287. /// <typeparam name="TOut"></typeparam>
  288. /// <param name="baseApiAddress"></param>
  289. /// <param name="url"></param>
  290. /// <param name="data"></param>
  291. /// <param name="contentType"></param>
  292. /// <param name="method"></param>
  293. /// <returns></returns>
  294. private async Task<TOut> CallBookingAppServiceAsync<TInput, TOut>(string baseApiAddress, string url, TInput data, string contentType, string method = "post")
  295. {
  296. using (var httpClientHandler = new HttpClientHandler())
  297. {
  298. using (var httpClient = new HttpClient(httpClientHandler))
  299. {
  300. httpClient.BaseAddress = new Uri(baseApiAddress);
  301. httpClient.DefaultRequestHeaders.Accept.Add(
  302. new MediaTypeWithQualityHeaderValue("application/json"));//设置accept标头,告诉JSON是可接受的响应类型
  303. var sendData = JsonConvert.SerializeObject(data);
  304. using (HttpContent httpContent = new StringContent(sendData))
  305. {
  306. httpContent.Headers.ContentType = new MediaTypeHeaderValue(contentType);
  307. HttpResponseMessage response = null;
  308. if (method == "post")
  309. {
  310. response = await httpClient.PostAsync(url, httpContent);
  311. }
  312. else
  313. {
  314. response = await httpClient.GetAsync(url);
  315. }
  316. string result;
  317. if (!response.IsSuccessStatusCode)
  318. {
  319. result = response.Content.ReadAsStringAsync().Result;
  320. //throw new Exception("http通信错误:" + response.StatusCode + ",结果:" + result);
  321. }
  322. result = await response.Content.ReadAsStringAsync();
  323. var resultDto = JsonConvert.DeserializeObject<TOut>(result);
  324. return resultDto;
  325. }
  326. }
  327. }
  328. }
  329. /// <summary>
  330. /// 生成请求密文数据
  331. /// </summary>
  332. /// <param name="resultJson"></param>
  333. /// <param name="ICODE"></param>
  334. /// <param name="publicKey"></param>
  335. /// <param name="privateKey"></param>
  336. /// <returns></returns>
  337. private static EncryDataRequstDto GetEncryptData(string resultJson, string ICODE, string publicKey, string privateKey)
  338. {
  339. string data = newRsaHelper.RSAEncrypt(resultJson, publicKey);
  340. string sign = newRsaHelper.SignData(data, privateKey);
  341. var result = new EncryDataRequstDto
  342. {
  343. DATA = data,
  344. SIGN = sign,
  345. ICODE = ICODE
  346. };
  347. return result;
  348. }
  349. /// <summary>
  350. /// 返回数据密文解密
  351. /// </summary>
  352. /// <param name="encryDataResultDto">返回的密文对象</param>
  353. /// <param name="publicKey">对方公钥</param>
  354. /// <param name="privateKey">自己私钥</param>
  355. /// <returns></returns>
  356. private static TOut GetDecryptData<TOut>(EncryDataResultDto encryDataResultDto, string publicKey, string privateKey)
  357. {
  358. bool isVerify = newRsaHelper.VerifySignature(encryDataResultDto.DATA, Convert.FromBase64String(encryDataResultDto.SIGN), publicKey);
  359. if (isVerify)
  360. {
  361. string resultStr = newRsaHelper.RSADecrypt(encryDataResultDto.DATA, privateKey);
  362. TOut resultDto = JsonConvert.DeserializeObject<TOut>(resultStr);
  363. return resultDto;
  364. }
  365. else
  366. {
  367. return default(TOut);
  368. }
  369. }
  370. /// <summary>
  371. /// 转换性别
  372. /// </summary>
  373. /// <returns></returns>
  374. private string GetSexId(char SexId)
  375. {
  376. string SexName = "";
  377. if (SexId == 'M')
  378. {
  379. SexName = "0";
  380. }
  381. if (SexId == 'F')
  382. {
  383. SexName = "1";
  384. }
  385. return SexName;
  386. }
  387. }
  388. }