ConsultationManagerV2.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Vinno.IUS.Common.Log;
  5. using Vinno.vCloud.Common.FIS.Helper;
  6. using Vinno.vCloud.Common.FIS.Remedicals;
  7. using Vinno.vCloud.FIS.CrossPlatform.Common.SQLite;
  8. using WingInterfaceLibrary.LiveConsultation;
  9. using WingInterfaceLibrary.LiveConsultation.Consultation;
  10. namespace Vinno.vCloud.Common.FIS.Consultation
  11. {
  12. internal class ConsultationManagerV2 : IDisposable
  13. {
  14. private readonly ILiveConsultationService _liveConsultationService;
  15. private readonly object _locker = new object();
  16. private readonly IList<PatientRelationship> _patientRelationships;
  17. /// <summary>
  18. /// 通知切换到本地病人,当vCloudExamInfo的ExamPairInfo的PatientIdInUSMachine为空时需先创建病人及检查,否则直接跳到该病人。
  19. /// </summary>
  20. public event EventHandler<vCloudExamInfo> SwitchToLocalPatientEvent;
  21. public ConsultationManagerV2(ILiveConsultationService liveConsultationService)
  22. {
  23. _liveConsultationService = liveConsultationService;
  24. _patientRelationships = RemedicalSQLiteHelperV2.Instance.GetAll<PatientRelationship>();
  25. RemedicalSQLiteHelperV2.Instance.PatientRelationshipCreatedOrUpdated += OnPatientRelationshipCreatedOrUpdated;
  26. }
  27. private void OnPatientRelationshipCreatedOrUpdated(object sender, PatientRelationship e)
  28. {
  29. lock (_locker)
  30. {
  31. if (e == null)
  32. {
  33. return;
  34. }
  35. var existItem = _patientRelationships.FirstOrDefault(x => x.PatientIdInServer == e.PatientIdInServer);
  36. if (existItem != null)
  37. {
  38. existItem.PatientIdInUSMachine = e.PatientIdInUSMachine;
  39. existItem.PatientIdInServer = e.PatientIdInServer;
  40. }
  41. else
  42. {
  43. _patientRelationships.Add(e);
  44. }
  45. }
  46. }
  47. internal vCloudExamInfo FindConsultationDetail(string consultationCode, string token, out string deviceId)
  48. {
  49. deviceId = null;
  50. try
  51. {
  52. if (string.IsNullOrWhiteSpace(consultationCode) || string.IsNullOrEmpty(token))
  53. {
  54. Logger.WriteLineError($"ConsultationManagerV2 FindConsultationDetail Error:one of args are null,ConsultationCode:{consultationCode},token:{token}");
  55. return null;
  56. }
  57. var findConsultationDetailRequest = new FindConsultationDetailRequest
  58. {
  59. ConsultationCode = consultationCode,
  60. Token = token,
  61. IsShowOriginal = true,
  62. };
  63. var consultationInfo = JsonRpcHelper.FindConsultationDetail(_liveConsultationService, findConsultationDetailRequest);
  64. deviceId = consultationInfo?.DeviceCode;
  65. var patientInfo = DTOConverter.ConvertConsultationDetailDTOTovCloudExamInfo(consultationInfo);
  66. if (patientInfo == null)
  67. {
  68. Logger.WriteLineError($"ConsultationManagerV2 FindConsultationDetail Error:Result is null");
  69. return null;
  70. }
  71. else
  72. {
  73. if (string.IsNullOrEmpty(patientInfo.ExamPairInfo.PatientIdInServer))//急诊且未补全的病人
  74. {
  75. patientInfo.ExamPairInfo.PatientIdInServer = "$ConsultationCode$" + consultationInfo.ConsultationCode;//加前缀是防止与真正的PatientCode重复
  76. }
  77. var existPatient = _patientRelationships.FirstOrDefault(x => x.PatientIdInServer == patientInfo.ExamPairInfo.PatientIdInServer);
  78. if (existPatient != null)
  79. {
  80. patientInfo.ExamPairInfo.PatientIdInUSMachine = existPatient.PatientIdInUSMachine;
  81. }
  82. return patientInfo;
  83. }
  84. }
  85. catch (Exception ex)
  86. {
  87. Logger.WriteLineError($"ConsultationManagerV2 FindConsultationDetail Error:{ex}");
  88. return null;
  89. }
  90. }
  91. internal ResultInfoDTO SwitchToLocalPatientFromFlutter(SwitchToLocalPatientRequestDTO switchToLocalPatientRequestDTO)
  92. {
  93. try
  94. {
  95. if (switchToLocalPatientRequestDTO == null)
  96. {
  97. return new ResultInfoDTO
  98. {
  99. IsSuccess = false,
  100. FailMessage = "ConsultationV2 SwitchToLocalPatientFromFlutter Fail:Args is null",
  101. };
  102. }
  103. var patientInfo = FindConsultationDetail(switchToLocalPatientRequestDTO.ConsultationCode, switchToLocalPatientRequestDTO.Token, out _);
  104. if (patientInfo == null)
  105. {
  106. return new ResultInfoDTO
  107. {
  108. IsSuccess = false,
  109. FailMessage = "ConsultationV2 SwitchToLocalPatientFromFlutter Fail:patientInfo is null",
  110. };
  111. }
  112. else
  113. {
  114. SwitchToLocalPatientEvent?.Invoke(this, patientInfo);
  115. }
  116. return new ResultInfoDTO { IsSuccess = true, };
  117. }
  118. catch (Exception ex)
  119. {
  120. Logger.WriteLineError($"ConsultationManagerV2 SwitchToLocalPatientFromFlutter Error:{ex}");
  121. return new ResultInfoDTO
  122. {
  123. IsSuccess = false,
  124. FailMessage = $"ConsultationManagerV2 SwitchToLocalPatientFromFlutter Fail:{ex}",
  125. };
  126. }
  127. }
  128. internal bool UpdatePatientPairInfo(PatientPairInfo patientPairInfo)
  129. {
  130. try
  131. {
  132. if (patientPairInfo == null)
  133. {
  134. return false;
  135. }
  136. if (string.IsNullOrEmpty(patientPairInfo.PatientIdInUSMachine))
  137. {
  138. Logger.WriteLineError($"patientPairInfo PatientIdInUSMachine is null");
  139. return false;
  140. }
  141. if (string.IsNullOrEmpty(patientPairInfo.PatientIdInServer))
  142. {
  143. Logger.WriteLineError($"patientPairInfo PatientIdInServer is null");
  144. return false;
  145. }
  146. lock (_locker)
  147. {
  148. var existItem = _patientRelationships.FirstOrDefault(x => x.PatientIdInServer == patientPairInfo.PatientIdInServer);
  149. if (existItem != null && existItem.PatientIdInUSMachine != patientPairInfo.PatientIdInUSMachine)
  150. {
  151. existItem.PatientIdInUSMachine = patientPairInfo.PatientIdInUSMachine;
  152. RemedicalSQLiteHelperV2.Instance.CreateOrUpdatePatientRelationship(existItem);
  153. }
  154. else if (existItem == null)
  155. {
  156. var cache = new PatientRelationship
  157. {
  158. PatientIdInServer = patientPairInfo.PatientIdInServer,
  159. PatientIdInUSMachine = patientPairInfo.PatientIdInUSMachine,
  160. };
  161. _patientRelationships.Add(cache);
  162. RemedicalSQLiteHelperV2.Instance.CreateOrUpdatePatientRelationship(cache);
  163. }
  164. }
  165. return true;
  166. }
  167. catch (Exception ex)
  168. {
  169. Logger.WriteLineError($"ConsultationManagerV2 UpdateExamPairInfo Error:{ex}");
  170. return false;
  171. }
  172. }
  173. public void Dispose()
  174. {
  175. RemedicalSQLiteHelperV2.Instance.PatientRelationshipCreatedOrUpdated -= OnPatientRelationshipCreatedOrUpdated;
  176. }
  177. }
  178. }