123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using Vinno.IUS.Common.Log;
- using Vinno.vCloud.Common.FIS.Helper;
- using Vinno.vCloud.Common.FIS.Remedicals;
- using Vinno.vCloud.FIS.CrossPlatform.Common.SQLite;
- using WingInterfaceLibrary.LiveConsultation;
- using WingInterfaceLibrary.LiveConsultation.Consultation;
- namespace Vinno.vCloud.Common.FIS.Consultation
- {
- internal class ConsultationManagerV2 : IDisposable
- {
- private readonly ILiveConsultationService _liveConsultationService;
- private readonly object _locker = new object();
- private readonly IList<PatientRelationship> _patientRelationships;
- /// <summary>
- /// 通知切换到本地病人,当vCloudExamInfo的ExamPairInfo的PatientIdInUSMachine为空时需先创建病人及检查,否则直接跳到该病人。
- /// </summary>
- public event EventHandler<vCloudExamInfo> SwitchToLocalPatientEvent;
- public ConsultationManagerV2(ILiveConsultationService liveConsultationService)
- {
- _liveConsultationService = liveConsultationService;
- _patientRelationships = RemedicalSQLiteHelperV2.Instance.GetAll<PatientRelationship>();
- RemedicalSQLiteHelperV2.Instance.PatientRelationshipCreatedOrUpdated += OnPatientRelationshipCreatedOrUpdated;
- }
- private void OnPatientRelationshipCreatedOrUpdated(object sender, PatientRelationship e)
- {
- lock (_locker)
- {
- if (e == null)
- {
- return;
- }
- var existItem = _patientRelationships.FirstOrDefault(x => x.PatientIdInServer == e.PatientIdInServer);
- if (existItem != null)
- {
- existItem.PatientIdInUSMachine = e.PatientIdInUSMachine;
- existItem.PatientIdInServer = e.PatientIdInServer;
- }
- else
- {
- _patientRelationships.Add(e);
- }
- }
- }
- internal vCloudExamInfo FindConsultationDetail(string consultationCode, string token, out string deviceId)
- {
- deviceId = null;
- try
- {
- if (string.IsNullOrWhiteSpace(consultationCode) || string.IsNullOrEmpty(token))
- {
- Logger.WriteLineError($"ConsultationManagerV2 FindConsultationDetail Error:one of args are null,ConsultationCode:{consultationCode},token:{token}");
- return null;
- }
- var findConsultationDetailRequest = new FindConsultationDetailRequest
- {
- ConsultationCode = consultationCode,
- Token = token,
- IsShowOriginal = true,
- };
- var consultationInfo = JsonRpcHelper.FindConsultationDetail(_liveConsultationService, findConsultationDetailRequest);
- deviceId = consultationInfo?.DeviceCode;
- var patientInfo = DTOConverter.ConvertConsultationDetailDTOTovCloudExamInfo(consultationInfo);
- if (patientInfo == null)
- {
- Logger.WriteLineError($"ConsultationManagerV2 FindConsultationDetail Error:Result is null");
- return null;
- }
- else
- {
- if (string.IsNullOrEmpty(patientInfo.ExamPairInfo.PatientIdInServer))//急诊且未补全的病人
- {
- patientInfo.ExamPairInfo.PatientIdInServer = "$ConsultationCode$" + consultationInfo.ConsultationCode;//加前缀是防止与真正的PatientCode重复
- }
- var existPatient = _patientRelationships.FirstOrDefault(x => x.PatientIdInServer == patientInfo.ExamPairInfo.PatientIdInServer);
- if (existPatient != null)
- {
- patientInfo.ExamPairInfo.PatientIdInUSMachine = existPatient.PatientIdInUSMachine;
- }
- return patientInfo;
- }
- }
- catch (Exception ex)
- {
- Logger.WriteLineError($"ConsultationManagerV2 FindConsultationDetail Error:{ex}");
- return null;
- }
- }
- internal ResultInfoDTO SwitchToLocalPatientFromFlutter(SwitchToLocalPatientRequestDTO switchToLocalPatientRequestDTO)
- {
- try
- {
- if (switchToLocalPatientRequestDTO == null)
- {
- return new ResultInfoDTO
- {
- IsSuccess = false,
- FailMessage = "ConsultationV2 SwitchToLocalPatientFromFlutter Fail:Args is null",
- };
- }
- var patientInfo = FindConsultationDetail(switchToLocalPatientRequestDTO.ConsultationCode, switchToLocalPatientRequestDTO.Token, out _);
- if (patientInfo == null)
- {
- return new ResultInfoDTO
- {
- IsSuccess = false,
- FailMessage = "ConsultationV2 SwitchToLocalPatientFromFlutter Fail:patientInfo is null",
- };
- }
- else
- {
- SwitchToLocalPatientEvent?.Invoke(this, patientInfo);
- }
- return new ResultInfoDTO { IsSuccess = true, };
- }
- catch (Exception ex)
- {
- Logger.WriteLineError($"ConsultationManagerV2 SwitchToLocalPatientFromFlutter Error:{ex}");
- return new ResultInfoDTO
- {
- IsSuccess = false,
- FailMessage = $"ConsultationManagerV2 SwitchToLocalPatientFromFlutter Fail:{ex}",
- };
- }
- }
- internal bool UpdatePatientPairInfo(PatientPairInfo patientPairInfo)
- {
- try
- {
- if (patientPairInfo == null)
- {
- return false;
- }
- if (string.IsNullOrEmpty(patientPairInfo.PatientIdInUSMachine))
- {
- Logger.WriteLineError($"patientPairInfo PatientIdInUSMachine is null");
- return false;
- }
- if (string.IsNullOrEmpty(patientPairInfo.PatientIdInServer))
- {
- Logger.WriteLineError($"patientPairInfo PatientIdInServer is null");
- return false;
- }
- lock (_locker)
- {
- var existItem = _patientRelationships.FirstOrDefault(x => x.PatientIdInServer == patientPairInfo.PatientIdInServer);
- if (existItem != null && existItem.PatientIdInUSMachine != patientPairInfo.PatientIdInUSMachine)
- {
- existItem.PatientIdInUSMachine = patientPairInfo.PatientIdInUSMachine;
- RemedicalSQLiteHelperV2.Instance.CreateOrUpdatePatientRelationship(existItem);
- }
- else if (existItem == null)
- {
- var cache = new PatientRelationship
- {
- PatientIdInServer = patientPairInfo.PatientIdInServer,
- PatientIdInUSMachine = patientPairInfo.PatientIdInUSMachine,
- };
- _patientRelationships.Add(cache);
- RemedicalSQLiteHelperV2.Instance.CreateOrUpdatePatientRelationship(cache);
- }
- }
- return true;
- }
- catch (Exception ex)
- {
- Logger.WriteLineError($"ConsultationManagerV2 UpdateExamPairInfo Error:{ex}");
- return false;
- }
- }
- public void Dispose()
- {
- RemedicalSQLiteHelperV2.Instance.PatientRelationshipCreatedOrUpdated -= OnPatientRelationshipCreatedOrUpdated;
- }
- }
- }
|